#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  long long int a[n];
  for (int i = 0; i < n; i++) cin >> a[i];
  int left = 0, right = n - 1;
  long long int total = 0;
  for (int i = 0; i < (n / 2); i++) {
    int pos = max_element(a + left, a + right) - a;
    if (a[pos] < 0) continue;
    if (left > right) break;
    if (abs(pos - left) >= abs(right - pos)) {
      total += abs(pos - left) * (a[pos] + a[left]);
      a[pos] = -1;
      a[left] = -1;
      while (a[left] < 0) left++;
    } else {
      total += abs(pos - right) * (a[pos] + a[right]);
      a[pos] = -1;
      a[right] = -1;
      while (a[right] < 0) right--;
    }
  }
  cout << total << endl;
  return 0;
}
