#include <bits/stdc++.h>
using namespace std;
long long int n, a[50005], vis[50005];
int main() {
  scanf("%d", &n);
  long long sum = 0, ave, ans = 0;
  for (int i = 0; i < n; i++) {
    scanf("%I64d", &a[i]);
    sum += a[i];
  }
  ave = sum / n;
  for (int i = 0; i < n; i++) {
    long long t = a[i] - ave;
    ans += abs(t);
    a[i + 1] += t;
  }
  printf("%I64d\n", ans);
  return 0;
}
