#include <bits/stdc++.h>
using namespace std;
int n;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> n;
  vector<int> a(n + 1);
  vector<int> viz(n + 1, 0);
  vector<int> frec(n + 1);
  int sum = 0;
  int med;
  int aux = 0;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    sum += a[i];
    frec[a[i]] += 1;
    if (frec[a[i]] > 1) aux = 1;
    viz[i] = 0;
  }
  med = sum / n;
  int res = 0;
  if (!aux) {
    for (int i = 1; i <= n; i++) {
      int max = a[1];
      int poz = 1;
      for (int j = 1; j <= n; j++) {
        if (a[j] >= max) {
          max = a[j];
          poz = j;
        }
      }
      int maxdist = 0;
      int poz1;
      for (int j = 1; j <= n; j++) {
        if (abs(poz - j) >= maxdist && !viz[j]) {
          maxdist = abs(poz - j);
          poz1 = j;
        }
      }
      viz[poz1] = 1;
      res += a[poz] * maxdist;
      a[poz] = -1;
    }
  } else {
    for (int i = 1; i <= n; i++) {
      int max = 1e9;
      int poz = 0;
      for (int j = 1; j <= n; j++) {
        if (abs(med - a[j]) <= max) {
          max = med - a[j];
          poz = j;
        }
      }
      int maxdist = 0;
      int poz1;
      for (int j = 1; j <= n; j++) {
        if (abs(poz - j) >= maxdist && !viz[j]) {
          maxdist = abs(poz - j);
          poz1 = j;
        }
      }
      viz[poz1] = 1;
      res += a[poz] * maxdist;
      a[poz] = -1;
    }
  }
  cout << res;
  return 0;
}
