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