#include <bits/stdc++.h>
template <class T>
bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T>
bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
using namespace std;
int main() {
  long long N, ans = 0ll, cnt = 0ll;
  cin >> N;
  long long A[N];
  for (int i = (0); i < (N); ++i) cin >> A[i];
  for (long long i = 0ll; i <= N; ++i) {
    cnt = 0ll;
    vector<pair<long long, long long>> P, Q;
    for (long long j = 0; j < i; ++j) P.push_back(make_pair(A[j], j));
    for (long long j = i; j < N; ++j) Q.push_back(make_pair(A[j], j));
    sort((P).begin(), (P).end());
    sort((Q).begin(), (Q).end());
    reverse((Q).begin(), (Q).end());
    for (long long j = 0ll; j < N - i; ++j) {
      cnt += Q[j].first * abs(Q[j].second - j);
    }
    for (long long j = 0ll; j < i; ++j) {
      cnt += P[j].first * abs(N - i + j - P[j].second);
    }
    sort((Q).begin(), (Q).end());
    if (P[0].first == Q[0].first) {
      long long T = P[0].first, cnta = 0, cntb = 0, kk;
      for (long long j = 0; j < P.size(); ++j) {
        if (P[j].first != T) {
          cnta = j;
          break;
        }
      }
      for (long long j = 0; j < Q.size(); ++j) {
        if (Q[j].first != T) {
          cntb = j;
          break;
        }
      }
      if (cnta < cntb)
        kk = cnta;
      else
        kk = cntb;
      cnt += 2 * kk * kk;
    }
    if (cnt > ans) ans = cnt;
  }
  cout << ans << '\n';
  return 0;
}
