#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ii = pair<int, int>;
constexpr int MOD = 7 + 1000000000;
constexpr int MAXN = 5 + 2000;
ii arr[MAXN];
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  int n;
  cin >> n;
  for (int i = (int)0; i < (int)n; ++i) {
    cin >> arr[i].first;
    arr[i].second = i;
  }
  int l = 0, r = n - 1;
  ll ans = 0;
  while (l <= r) {
    ll op1 = -1, op2 = -1;
    int b1 = -1, b2 = -1;
    for (int i = l; i <= r; ++i) {
      if (abs(arr[i].second - l) * arr[i].first > op1) {
        op1 = abs(arr[i].second - l) * arr[i].first;
        b1 = i;
      }
      if (abs(arr[i].second - r) * arr[i].first > op2) {
        op2 = abs(arr[i].second - r) * arr[i].first;
        b2 = i;
      }
    }
    if (op1 >= op2) {
      ans += op1;
      swap(arr[l], arr[b1]);
      l++;
    } else {
      ans += op2;
      swap(arr[r], arr[b2]);
      r--;
    }
  }
  cout << ans << '\n';
  return 0;
}
