#include <bits/stdc++.h>
using namespace std;
void sync() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
}
int main() {
  sync();
  long long int n;
  cin >> n;
  long long int a[n];
  for (long long int i = 0; i < n; i++) cin >> a[i];
  map<long long int, vector<long long int>> indexes;
  map<long long int, long long int> cnt, cnt1;
  for (long long int i = 0; i < n; i++)
    indexes[a[i]].push_back(i), cnt[a[i]] = 0, cnt1[a[i]]++;
  long long int ans = 0;
  sort(a, a + n, greater<long long int>());
  vector<bool> ck(n, false);
  for (long long int i = 0; i < n; i++) {
    if (cnt[a[i]] >= cnt1[a[i]]) continue;
    long long int k, mx = -1;
    for (long long int j = i + 1; j < n; j++) {
      if (cnt[a[j]] >= cnt1[a[j]]) continue;
      if (abs(indexes[a[i]][cnt[a[i]]] - indexes[a[j]][cnt[a[j]]]) *
              (a[i] + a[j]) >
          mx) {
        mx = abs(indexes[a[i]][cnt[a[i]]] - indexes[a[j]][cnt[a[j]]]) *
             (a[i] + a[j]);
        k = j;
      }
    }
    ans += mx;
    cnt[a[i]]++;
    cnt[a[k]]++;
  }
  cout << ans << endl;
}
