#include <cstdio>
#include <algorithm>
using namespace std;
long long a[1000010],c[1000010];
long long sum,M;
int n;
int main()
{
while(scanf("%d",&n)==1)
{
sum=0;
for(int i=1;i<=n;i++)
{
scanf("%lld",&a[i]);
sum += a[i];
}
M = sum/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 x1 = c[n/2] , ans=0;
for(int i=0;i<n;i++) ans += abs( x1-c[i] );
printf("%lld\n",ans);
}
return 0;
}