#include <bits/stdc++.h>
using namespace std;
inline long long read() {
  long long x = 0;
  bool f = false;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-') f = true;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    x = (x << 3) + (x << 1) + (c ^ '0');
    c = getchar();
  }
  return f ? -x : x;
}
inline void write(long long x) {
  if (x < 0) putchar('-'), x = -x;
  if (x > 9) write(x / 10);
  putchar(x % 10 + '0');
}
const long long N = 5e4 + 10;
long long n, a[N], sum, ans;
signed main() {
  n = read();
  for (register long long i = 1; i <= n; ++i) a[i] = read(), sum += a[i];
  sum /= n;
  for (register long long i = 1; i <= n; ++i)
    ans += abs(sum - a[i]), a[i + 1] += a[i] - sum;
  cout << ans;
  return 0;
}
