#include <bits/stdc++.h>
using namespace std;
struct aaa {
  aaa() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(20);
  };
} aaa;
template <class T>
ostream &operator<<(ostream &o, const vector<T> &v) {
  o << "{";
  for (int i = 0; i < (int)v.size(); i++) o << (i > 0 ? ", " : "") << v[i];
  o << "}";
  return o;
}
using int64 = long long;
using P = pair<int64, int64>;
int main() {
  int64 n;
  cin >> n;
  vector<P> ai;
  for (int64 i = 0; i < n; i++) {
    int64 a;
    cin >> a;
    ai.emplace_back(a, i);
  }
  sort(ai.rbegin(), ai.rend());
  int64 ans = 0;
  {
    int64 pre = 1;
    int64 back = n - 1;
    int64 cnt = ai[0].first * abs(0 - ai[0].second);
    for (int64 i = 1; i < n; i++) {
      auto [a, index] = ai[i];
      if (abs(pre - index) > abs(back - index)) {
        cnt += a * abs(pre - index);
        pre++;
      } else {
        cnt += a * abs(back - index);
        back--;
      }
    }
    ans = max(ans, cnt);
  }
  {
    int64 pre = 0;
    int64 back = n - 2;
    int64 cnt = ai[0].first * abs((n - 1) - ai[0].second);
    for (int64 i = 1; i < n; i++) {
      auto [a, index] = ai[i];
      if (abs(pre - index) > abs(back - index)) {
        cnt += a * abs(pre - index);
        pre++;
      } else {
        cnt += a * abs(back - index);
        back--;
      }
    }
    ans = max(ans, cnt);
  }
  cout << ans << endl;
}
