#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, ai, f, b;
  int ans = 0;
  cin >> n;
  deque<int> x;
  vector<pair<int, int>> a(n);
  for (int i = 0; i < n; i++) {
    cin >> ai;
    a.at(i) = make_pair(ai, i);
    x.push_back(i);
  }
  sort(a.begin(), a.end());
  for (int i = 0; i < n; i++) {
    f = abs(a.at(n - i - 1).second - x.front());
    b = abs(a.at(n - i - 1).second - x.back());
    if (f == b) {
      int f2, b2;
      f2 = abs(a.at(n - i).second - x.front());
      b2 = abs(a.at(n - i).second - x.back());
      if (f2 > b2) {
        ans += a.at(n - i - 1).first * b;
        x.pop_back();
      } else {
        ans += a.at(n - i - 1).first * f;
        x.pop_front();
      }
    } else if (f > b) {
      ans += a.at(n - i - 1).first * f;
      x.pop_front();
    } else {
      ans += a.at(n - i - 1).first * b;
      x.pop_back();
    }
  }
  cout << ans << endl;
}
