#include <bits/stdc++.h>
const long long inf = 1e18 + 44;
const int MAX = 3e5 + 9;
const long long int MOD = 1e9 + 7;
const double eps = 1e-1;
double const PI = 3.1415926535897931;
using namespace std;
vector<pair<int, int> > adj[MAX];
int main() {
  int n, v;
  cin >> n;
  long long arr[n];
  long long s = 0;
  for (int i = 1; i <= n; i++) {
    cin >> arr[i];
    s = s + arr[i];
  }
  long long ans = 0;
  long long prev = 0;
  long long k = s / n;
  for (int i = 1; i <= n; i++) {
    ans = ans + abs(prev);
    prev += arr[i] - k;
  }
  cout << ans;
}
