#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int n, b, c, balance, nxt;
  vector<int> boxes;
  while (cin >> n) {
    long long int sum = 0;
    for (long long int i = 0; i < n; i++) {
      cin >> b;
      sum += b;
      boxes.push_back(b);
    }
    c = sum / n;
    balance = 0;
    long long int answ = 0;
    for (long long int i = 0; i < n - 1; i++) {
      balance = boxes[i] + balance;
      if (balance != c) {
        answ = answ + abs(balance - c);
        balance = balance - c;
      } else
        balance = 0;
    }
    cout << answ << "\n";
  }
  return 0;
}
