#include<stdio.h>
#include<iostream>
#include<string.h>
#include<string>
#include<ctype.h>
#include<math.h>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<bitset>
#include<algorithm>
#include<time.h>
using namespace std;
void fre(){freopen("c://test//input.in","r",stdin);freopen("c://test//output.out","w",stdout);}
#define MS(x,y) memset(x,y,sizeof(x))
#define MC(x,y) memcpy(x,y,sizeof(x))
#define MP(x,y) make_pair(x,y)
#define ls o<<1
#define rs o<<1|1
typedef long long LL;
typedef unsigned long long UL;
typedef unsigned int UI;
template <class T1,class T2>inline void gmax(T1 &a,T2 b){if(b>a)a=b;}
template <class T1,class T2>inline void gmin(T1 &a,T2 b){if(b<a)a=b;}
const int N=1e5+10,M=0,Z=1e9+7,ms63=0x3f3f3f3f;
int n;
LL a[N];
int main()
{
while(~scanf("%d",&n))
{
LL sum=0;
for(int i=1;i<=n;++i)scanf("%lld",&a[i]),sum+=a[i];
LL ave=sum/n;
int num=sum%n;
sort(a+1,a+n+1);
LL val=0;
for(int i=n;i>n-num;--i)val+=abs(a[i]-(ave+1));
for(int i=1;i<=n-num;++i)val+=abs(a[i]-ave);
printf("%lld\n",val/2);
}
return 0;
}
/*
【题意】
给你n(1e5)个数a[],我们可以把任一个数拿出1,给与另外任意一个数。
这样我们到最后,有一个最大的数ma和一个最小的数mb,我们希望使得ma-mb尽可能小。
并且,在实现这个目标的前提下,使得操作的步数尽可能少。
【类型】
贪心 排序
【分析】
我们从讨论比较少的地方入手。
很显然,ma-mb的值,只可能是两种——0或者1。
0:这个时候,显然,操作步数就是∑(a[i]-ave)/2
1:这个时候,我们最终肯定有一些数(n-sum%n个)变成x,一些数(sum%n个)变成x+1.
于是,我们选择让最小的那n-sum%n个数变成x,让最大的那sum%n个数变成x+1就好啦。
计数还是总的步长差/2
【时间复杂度&&优化】
O(n)
*/