#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int t = 1;
  ;
  while (t--) {
    int n;
    cin >> n;
    int* arr = new int[n];
    int* visit = new int[n];
    for (int i = 0; i < n; i++) {
      visit[i] = 0;
    }
    for (int i = 0; i < n; i++) {
      cin >> arr[i];
    }
    int sum = 0;
    for (int i = 0; i < n; i++) {
      int max = INT_MIN;
      int maxindex;
      if (visit[i] == 1) continue;
      for (int j = 0; j < n; j++) {
        if (visit[j] == 0 && max < arr[j]) {
          max = arr[j];
          maxindex = j;
        }
      }
      int farindex;
      int mm = INT_MIN;
      for (int j = 0; j < n; j++) {
        if (abs(j - maxindex) > mm && visit[j] == 0) {
          mm = abs(maxindex - j);
          farindex = j;
        }
      }
      visit[maxindex] = 1;
      visit[farindex] = 1;
      sum += arr[maxindex] * (abs(maxindex - farindex)) +
             arr[farindex] * (abs(maxindex - farindex));
    }
    cout << sum;
    cout << '\n';
  }
  return 0;
}
