#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC optimization("unroll-loops")
using namespace std;
const long double Pi = 3.141592653;
const long long mod = 1e9 + 7;
long long INF = 1000000000000000000;
bool cmp(pair<int, int> a, pair<int, int> b) {
  return (a.first > b.first) || (a.first == b.first && a.second > b.second);
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int t = 1;
  while (t--) {
    int n;
    cin >> n;
    long long sum = 0, a[n];
    for (int i = 0; i < n; i++) cin >> a[i], sum += a[i];
    long long k = sum / n;
    long long ans = 0;
    for (int i = 0; i < n; i++) {
      ans += abs(a[i] - k);
      if (i + 1 < n) a[i + 1] = a[i + 1] + (a[i] - k);
    }
    cout << ans;
  }
}
