#include <bits/stdc++.h>
using namespace std;
using ll = int64_t;
const int MOD = 1000000007;
int n;
int main() {
  cin >> n;
  vector<pair<ll, int>> infants(n);
  for (int i = (int)(0); i < (int)(n); i++) {
    cin >> infants[i].first;
    infants[i].second = i;
  }
  sort(infants.begin(), infants.end(),
       [](pair<ll, int> x, pair<ll, int> y) -> bool {
         if (x.first == y.first) {
           ll xo = min(x.second, n - 1 - x.second);
           ll yo = min(y.second, n - 1 - y.second);
           return xo < yo;
         } else {
           return x.first > y.first;
         }
       });
  ll ans = 0L;
  int l = 0;
  int r = n - 1;
  for (auto &&i : infants) {
    if (r - i.second > i.second - l) {
      ans += i.first * (r - i.second);
      r--;
    } else {
      ans += i.first * (i.second - l);
      l++;
    }
  }
  cout << ans << endl;
}
