#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n, i, s, a[50008], k;
  while (cin >> n) {
    memset(a, 0, sizeof(a));
    s = 0;
    for (i = 0; i < n; i++) {
      cin >> a[i];
      s += a[i];
    }
    s = s / n;
    k = 0;
    for (i = 0; i < n - 1; i++) {
      k += abs(a[i] - s);
      a[i + 1] = a[i + 1] + a[i] - s;
    }
    cout << k << endl;
  }
  return 0;
}
