#include <bits/stdc++.h>
int n;
long long int a[50055];
int main() {
  scanf("%d", &n);
  long long int s = 0;
  for (int i = 0; i < n; i++) {
    scanf("%I64d", &a[i]);
    s += a[i];
  }
  s /= n;
  long long int ans = 0;
  for (int i = 0; i < n; i++) {
    if (a[i] < s) {
      long long int det = s - a[i];
      a[i + 1] -= det;
      ans += det;
    }
    if (a[i] > s) {
      long long int det = a[i] - s;
      a[i + 1] += det;
      ans += det;
    }
  }
  printf("%I64d\n", ans);
  return 0;
}
