#include <bits/stdc++.h>
using namespace std;
int main() {
  std::ios::sync_with_stdio(false);
  long long n;
  cin >> n;
  long long m[60000];
  long long sum = 0;
  for (int i = 0; i < n; i++) {
    cin >> m[i];
    sum += m[i];
  }
  long long avg = sum / n;
  long long carry = 0;
  long long cost = 0;
  for (int i = 0; i < n; i++) {
    cost += abs(carry);
    carry += m[i] - avg;
  }
  cout << cost;
  return 0;
}
