#include <bits/stdc++.h>
using namespace std;
const long long INF = 1 << 28;
long long gcd(long long m, long long n) {
  long long r;
  while (n != 0) {
    r = m % n;
    m = n;
    n = r;
  }
  return m;
}
long long expmod(long long a, long long b) {
  long long x = 1, y = a;
  while (b > 0) {
    if (b & 1) x = (x * y) % 1000000007;
    y = (y * y) % 1000000007;
    b >>= 1;
  }
  return (x) % 1000000007;
}
long long inv(long long n) { return expmod(n, 1000000007 - 2); }
int main() {
  long long n;
  cin >> n;
  long long a[n];
  long long sum = 0;
  for (long long i = 0; i < n; i++) {
    cin >> a[i];
    sum += a[i];
  }
  long long req = sum / n;
  long long ans = 0;
  for (long long i = 0; i < n - 1; i++) {
    if (a[i] < req) {
      ans += (req - a[i]);
      a[i + 1] -= (req - a[i]);
    }
    if (a[i] > req) {
      ans += (a[i] - req);
      a[i + 1] += (a[i] - req);
    }
  }
  cout << ans << endl;
  return 0;
}
