#include <bits/stdc++.h>
using namespace std;
const int maxx = 5 * 1000 * 100 + 20;
long long a[maxx], n, sum = 0, ans = 0;
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    sum += a[i];
  }
  sum /= n;
  for (int i = 0; i < n; i++) {
    ans += abs(sum - a[i]);
    a[i + 1] += a[i] - sum;
  }
  cout << ans;
  return 0;
}
