#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 2087;
ll dp[N][N];
pair<int, int> a[N];
int main()
{
    ll n;
    cin >> n;
    for (int i = 0; i < n; ++i) {
        cin >> a[i].first;
        a[i].second = i + 1;
    }
    sort(a, a + n);
    for (int w = 1; w <= n; ++w) {
        int x, v;
        tie(v, x) = a[w - 1];
        for (ll l = 1, r = l + w - 1; r <= n; ++l, ++r) 
            dp[l][r] = max(dp[l + 1][r] + v * abs(l - x), dp[l][r - 1] + v * abs(r - x));
    }
    cout << dp[1][n] << endl;
}
