#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
  long long n;
  cin >> n;
  vector<long long> oa(n);
  for (auto& v : oa) cin >> v;
  long long mx = 0;
  for (int cut = 0; cut <= n; ++cut) {
    vector<long long> na = oa;
    sort(na.begin(), na.begin() + cut);
    sort(na.begin() + cut, na.end(),
         [](long long a, long long b) { return a > b; });
    long long old = 0;
    for (int i = 0; i < cut; ++i) {
      old -= i * oa[i];
    }
    for (int i = cut; i < n; ++i) {
      old += i * oa[i];
    }
    long long nv = 0;
    for (int i = 0; i < cut; ++i) {
      nv += (i + n - cut) * na[i];
    }
    for (int i = cut; i < n; ++i) {
      nv -= (i - cut) * na[i];
    }
    mx = max(mx, nv + old);
  }
  cout << mx;
}
