#include <bits/stdc++.h>
using namespace std;
int main(int, char**) {
  int n;
  vector<std::pair<int, int> > a;
  cin >> n;
  int tmp;
  for (int i = 0; i < n; i++) {
    cin >> tmp;
    a.push_back(make_pair(tmp, i));
  }
  sort(a.begin(), a.end(),
       [](const std::pair<int, int>& x, const std::pair<int, int>& y) {
         return x.first > y.first;
       });
  int sum = 0;
  int s = a.size();
  for (int i = 0; i < s / 2; i++) {
    int tmp1 = a[2 * i].first * abs(a[2 * i].second - i) +
               a[2 * i + 1].first * abs(a[2 * i + 1].second - (s - 1 - i));
    int tmp2 = a[2 * i + 1].first * abs(a[2 * i + 1].second - i) +
               a[2 * i].first * abs(a[2 * i].second - (s - 1 - i));
    sum += max(tmp1, tmp2);
  }
  if (s % 2 != 0) sum += a[s - 1].first * abs(a[s - 1].second - s / 2);
  cout << sum << endl;
  return 0;
}
