#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std;
#define Ed for(int i=h[x];~i;i=ne[i])
#define Ls(i,l,r) for(int i=l;i<r;++i)
#define Rs(i,l,r) for(int i=l;i>r;--i)
#define Le(i,l,r) for(int i=l;i<=r;++i)
#define Re(i,l,r) for(int i=l;i>=r;--i)
#define L(i,l) for(int i=0;i<l;++i)
#define E(i,l) for(int i=1;i<=l;++i)
#define W(t) while(t--)
#define Wh while
typedef long long ll;
const int N=1000010;
int n,a[N];
ll c[N],b;
int main(){
#ifndef ONLINE_JUDGE
freopen("1.in","r",stdin);
#endif
scanf("%d",&n);
E(i, n)scanf("%d",a+i),b+=a[i];
b/=n;
E(i, n)c[i]=c[i-1]+a[i-1]-b;
sort(c+1,c+1+n);
ll ans=0;
E(i, n)ans+=abs(c[i]-c[(n>>1)+1]);
printf("%lld",ans);
return 0;
}