#include <bits/stdc++.h>
using namespace std;
inline void io(int &res) {
  register char c;
  while (c = getchar_unlocked()) {
    if (c != ' ' && c != '\n') break;
  }
  res = c - '0';
  while (c = getchar_unlocked()) {
    if (c == ' ' || c == '\n' || c == EOF) break;
    res = (res << 1) + (res << 3) + (c - '0');
  }
}
int main() {
  int n;
  cin >> n;
  long long res = 0;
  long long arr[50005];
  long long k = 0;
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
    k += arr[i];
  }
  k /= n;
  long long cur = 0;
  for (int i = 0; i < n; i++) {
    arr[i] += cur;
    res += abs(arr[i] - k);
    cur = arr[i] - k;
  }
  cout << res << endl;
  return 0;
}
