#include <bits/stdc++.h>
using LL = long long;
using namespace std;
int main() {
  int n;
  cin >> n;
  int a[n];
  for (int &i : a) cin >> i;
  LL ans = 0;
  int l = 0, r = n - 1;
  for (int j = 0; j <= n / 2; j++) {
    int i = max_element(a, a + n) - a;
    if (i - l > r - i) {
      ans += (a[i] + a[l]) * (i - l);
      a[i] = a[l] = 0;
      ++l;
    } else {
      ans += (a[i] + a[r]) * (r - i);
      a[i] = a[r] = 0;
      --r;
    }
  }
  cout << ans;
}
