#include <bits/stdc++.h>
using namespace std;
int main() {
  long long a[50000];
  long long b = 0;
  long long box, tbox;
  cin >> box;
  tbox = box;
  long long temp;
  while (tbox--) {
    cin >> temp;
    a[b] = temp;
    b++;
  }
  long long totalmatch = 0;
  long long i, j;
  for (i = 0; i < b; i++) totalmatch = totalmatch + a[i];
  long long eachbox;
  eachbox = totalmatch / box;
  long long moves = 0;
  long long stockmatches = 0;
  for (i = 0; i < b; i++) {
    if (i + 1 == b) break;
    if (a[i] < eachbox) {
      stockmatches = eachbox - a[i];
      a[i] = a[i] + stockmatches;
      a[i + 1] = a[i + 1] - stockmatches;
      moves = moves + stockmatches;
    } else {
      stockmatches = a[i] - eachbox;
      a[i] = a[i] - stockmatches;
      a[i + 1] = a[i + 1] + stockmatches;
      moves = moves + stockmatches;
    }
  }
  cout << moves << endl;
}
