#include <bits/stdc++.h>
using namespace std;
long long int arr[50010];
int main() {
  int n;
  cin >> n;
  long long int x = 0;
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
    x += arr[i];
  }
  x /= n;
  if (x == 0) {
    cout << "0\n";
    return 0;
  }
  long long int res = 0;
  for (int i = 0; i < n; i++) {
    if (arr[i] > x) {
      arr[i + 1] += (arr[i] - x);
      res += (arr[i] - x);
      arr[i] = x;
    } else if (arr[i] < x) {
      arr[i + 1] -= (x - arr[i]);
      res += (x - arr[i]);
      arr[i] = x;
    }
  }
  cout << res << endl;
}
