#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:36777216")
using namespace std;
int n;
long long sum, ans, a[50005];
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) scanf("%I64d", &a[i]), sum += a[i];
  long long k = sum / n;
  for (int i = 1; i <= n; i++) {
    if (a[i] != k) {
      if (a[i] > k) {
        long long d = a[i] - k;
        a[i] -= d, a[i + 1] += d, ans += d;
      } else if (a[i] < k) {
        long long d = k - a[i];
        a[i] += d, a[i + 1] -= d, ans += d;
      }
    }
  }
  cout << ans << endl;
}
