#include <bits/stdc++.h>
using namespace std;
bool cmp(pair<long long, int> p1, pair<long long, int> p2) {
  if (p1.first > p2.first)
    return true;
  else if (p1.first == p2.first)
    return p1.second < p2.second;
  else
    return false;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int n;
  cin >> n;
  long long a[n];
  vector<pair<long long, int>> v;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    v.push_back(make_pair(a[i], i));
  }
  vector<bool> vis(n, false);
  sort(v.begin(), v.end(), cmp);
  long long sum = 0;
  for (int k = 0; k < n - 1; k++) {
    int x = v[k].second;
    int i = 0, j = n - 1;
    while (true) {
      if (vis[i] == false && vis[j] == false) break;
      if (vis[i] == true) i++;
      if (vis[j] == true) j--;
    }
    int y = max(abs(x - i), abs(x - j));
    if (abs(x - i) > abs(x - j))
      vis[i] = true;
    else
      vis[j] = true;
    sum = sum + v[k].first * y;
  }
  for (int i = 0; i < n; i++) {
  }
  cout << sum;
  return 0;
}
