#include <bits/stdc++.h>
using namespace std;
int main() {
  std::ios::sync_with_stdio(false);
  long long n, total = 0;
  cin >> n;
  vector<long long> a(n);
  for (int i = (0); i < (n); ++i) {
    cin >> a[i];
    total += a[i];
  }
  long long avg = total / n, res = 0;
  for (int i = (0); i < (n - 1); ++i) {
    res += abs(a[i] - avg);
    a[i + 1] += a[i] - avg;
  }
  cout << res;
}
