#include <bits/stdc++.h>
using namespace std;
int main() {
  long matches;
  cin >> matches;
  long long box[matches];
  int i;
  long long sum = 0;
  for (i = 0; i < matches; ++i) {
    cin >> box[i];
    sum += box[i];
  }
  long long move = 0;
  long long average = sum / matches;
  for (i = 0; i < matches - 1; ++i) {
    if (box[i] == average) continue;
    if (box[i] < average) {
      box[i + 1] = box[i + 1] - (average - box[i]);
      move += (average - box[i]);
    }
    if (box[i] > average) {
      box[i + 1] = box[i + 1] + (box[i] - average);
      move += box[i] - average;
    }
  }
  cout << move << endl;
  return 0;
}
