#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n;
  cin >> n;
  vector<long long> a(n);
  priority_queue<vector<long long>, vector<vector<long long>>,
                 less<vector<long long>>>
      q;
  for (long long i = 0; i < (long long)(n); i++) {
    cin >> a.at(i);
    q.push({a.at(i), i});
  }
  long long ans = 0;
  long long minpos = 0;
  long long maxpos = n - 1;
  while (q.size() > 0) {
    long long x = q.top().at(0);
    long long y = q.top().at(1);
    long long lcnter = 0;
    long long rcnter = 0;
    for (long long i = 0; i < (long long)(n); i++) {
      if (i < y && a.at(i) <= x) {
        lcnter += a.at(i);
      } else if (i > y && a.at(i) <= x) {
        rcnter += a.at(i);
      }
    }
    lcnter += x * (y - minpos);
    rcnter += x * (maxpos - y);
    if (lcnter >= rcnter) {
      ans += x * (y - minpos);
      minpos++;
    } else {
      ans += x * (maxpos - y);
      maxpos--;
    }
    a[y] = 10000000000;
    q.pop();
  }
  cout << ans << endl;
}
