#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
long long a[1000005],c[1000005];
int main()
{
freopen("Wealth.in","r",stdin);
freopen("Wealth.out","w",stdout);
int n;
while(scanf("%d",&n) == 1)
{
long long tot = 0;
for(int i = 1;i <= n;++i)
{
scanf("%lld",&a[i]);
tot+=a[i];
}
long long M = tot/n;
c[0] = 0;
for(int i = 1;i < n;++i)
c[i] = c[i-1] + a[i] - M;
sort(c,c+n);
long long x = c[n/2],ans = 0;
for(int i = 0;i <= n-1;++i)
ans = ans + abs(x-c[i]);
printf("%lld\n",ans);
}
return 0;
}