#include <bits/stdc++.h>
using namespace std;
const int maxn = 50100;
unsigned long long arr[maxn];
int main() {
  ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  unsigned long long s = 0, res = 0;
  long long n;
  cin >> n;
  for (int i = 0; i < n; i++) cin >> arr[i], s += arr[i];
  s = s / n;
  for (int i = 0; i + 1 < n; i++) {
    long long tmp = arr[i] - s;
    res += abs(tmp);
    arr[i + 1] += tmp;
  }
  cout << res << endl;
  return 0;
}
