#include<iostream>
#include<cstdio>
const int maxlong=10005;
using namespace std;
int data[maxlong];
int ans=0,n;
int main()
{
freopen("move.in","r",stdin);
freopen("move.out","w",stdout);
scanf("%d",&n);
int sum=0,aver;
for (int i=1;i<=n;i++)
{
scanf("%d",&data[i]);
sum+=data[i];
}
aver=sum/n;
for (int i=1;i<=n-2;i++)
{
if (data[i]-aver>0)
{
data[i+1]+=data[i]-aver;
ans+=data[i]-aver;
}
if (data[i]-aver<0)
{
data[i+1]-=aver-data[i];
ans+=aver-data[i];
}
}
printf("%d",ans);
return 0;
}