#include <bits/stdc++.h>
using namespace std;
const long long N = 200005;
signed main() {
  std::ios::sync_with_stdio(false);
  cin.tie(NULL);
  long long n;
  cin >> n;
  long long a[n];
  vector<pair<long long, long long> > vect;
  for (long long i = 0; i < n; i++) {
    cin >> a[i];
    vect.push_back({a[i], i});
  }
  sort(vect.begin(), vect.end());
  reverse(vect.begin(), vect.end());
  long long li = 0;
  long long ri = n - 1;
  long long ans = 0;
  for (pair<long long, long long> p : vect) {
    long long pos = p.second;
    long long v = p.first;
    long long p1 = abs(pos - li) * v;
    long long p2 = abs(ri - pos) * v;
    for (long long i = ri; i > pos; i--) {
      p2 += a[i];
    }
    for (long long i = li; i < pos; i++) {
      p1 += a[i];
    }
    if (p1 >= p2) {
      for (long long i = pos; i > li; i--) {
        a[i] = a[i - 1];
      }
      ans += ((pos - li) * v);
      li++;
    } else {
      for (long long i = pos; i < ri; i++) {
        a[i] = a[i + 1];
      }
      ans += ((ri - pos) * v);
      ri--;
    }
  }
  cout << ans;
  return 0;
}
