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