#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;
#define MAX 1001000
long long a[MAX], c[MAX];
int main()
{
int n;
long long sum, v, x, ans;
while(scanf("%d", &n) != -1)
{
sum = ans = 0;
for(int i=1; i<=n; i++){
scanf("%lld", &a[i]);
sum += a[i];
}
v = sum / n;
c[1] = v-a[1];
for(int i=2; i<=n; i++){
c[i] = v-a[i] + c[i-1];
}
sort(c+1, c+n+1);
x = -c[n/2+1];
for(int i=1; i<=n; i++)
ans += abs(c[i]+x);
printf("%lld\n", ans);
}
return 0;
}