#abc313c. C - Approximate Equalization 2

C - Approximate Equalization 2

Score : 400400 points

问题描述

你给定一个整数序列 A=(A1,A2,,AN)A=(A_1,A_2,\dots,A_N)。你可以执行以下操作任意次数(包括零次)。

  • 选择整数 iijj,满足 1i,jN1\leq i,j \leq N。将 AiA_i 减一,并将 AjA_j 加一。

找出使 AA 中最小值与最大值之差最多为一时所需的最少操作次数。

以上为通义千问 qwen-max 翻译,仅供参考。

Problem Statement

You are given an integer sequence A=(A1,A2,,AN)A=(A_1,A_2,\dots,A_N). You can perform the following operation any number of times (possibly zero).

  • Choose integers ii and jj with 1i,jN1\leq i,j \leq N. Decrease AiA_i by one and increase AjA_j by one.

Find the minimum number of operations required to make the difference between the minimum and maximum values of AA at most one.

Constraints

  • 1N2×1051\leq N \leq 2\times 10^5
  • 1Ai1091\leq A_i \leq 10^9
  • All input values are integers.

Input

The input is given from Standard Input in the following format:

NN

A1A_1 A2A_2 \dots ANA_N

Output

Print the answer as an integer.

Sample Input 1

4
4 7 3 7

Sample Output 1

3

By the following three operations, the difference between the minimum and maximum values of AA becomes at most one.

  • Choose i=2i=2 and j=3j=3 to make A=(4,6,4,7)A=(4,6,4,7).
  • Choose i=4i=4 and j=1j=1 to make A=(5,6,4,6)A=(5,6,4,6).
  • Choose i=4i=4 and j=3j=3 to make A=(5,6,5,5)A=(5,6,5,5).

You cannot make the difference between maximum and minimum values of AA at most one by less than three operations, so the answer is 33.

Sample Input 2

1
313

Sample Output 2

0

Sample Input 3

10
999999997 999999999 4 3 2 4 999999990 8 999999991 999999993

Sample Output 3

2499999974

update @ 2024/3/10 08:55:11