#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
int n, temp, eval1;
P temp1, temp2;
ll ans, maximum;
int main() {
  cin >> n;
  vector<P> a;
  for (int i = 0; i < (n); ++i) {
    cin >> temp;
    a.push_back(make_pair(temp, i));
  }
  sort(a.rbegin(), a.rend());
  while (a.size() < 2) {
    temp1 = a.at(0);
    a.erase(a.begin());
    maximum = 0;
    for (P i : a) {
      eval1 = abs(temp1.second - i.second) * (temp2.first + i.first);
      if (maximum <= eval1) {
        maximum = eval1;
        temp2 = i;
      }
    }
    ans += maximum;
    a.erase(find(a.begin(), a.end(), temp2));
  }
  cout << ans << endl;
  return 0;
}
