#include <bits/stdc++.h>

using namespace std;
using ll = long long int;
constexpr ll M = 1e9 + 7;
inline ll sum_1(const ll n) { return ((n * (n + 1)) / 2) % M; }
inline ll sum_2(const ll n) { return ((n * (n + 1) * (2 * n + 1)) / 6) % M; }
inline ll sum_3(const ll n) { return (((n * (n + 1)) / 2) % M * ((n * (n + 1)) / 2) % M) % M; }
inline ll sum_1(const ll i, const ll j) { return (sum_1(j) - sum_1(i - 1) + M) % M; }
inline ll sum_2(const ll i, const ll j) { return (sum_2(j) - sum_2(i - 1) + M) % M; }
inline ll sum_3(const ll i, const ll j) { return (sum_3(j) - sum_3(i - 1) + M) % M; }

void solve() {
    int n;
    cin >> n;

    vector<pair<ll,ll>> A(n);
    for (int i = 0; i < A.size(); ++i) {
        cin >> A[i].first; 
        A[i].second = i;
    }
    sort(A.begin(), A.end(), greater());

    /**
     * dp[L][R] = max(dp[L-1][R] + A[i]*|idx-L|, dp[L][R-1] + A[i]*|idx-R|)
     * 0 <= L+R <= n
     */
    ll dp[n+5][n+5];
    memset(dp, 0, sizeof(dp));

    for (int i = 0; i < n+5; ++i) {
        dp[i][0] = dp[0][i] = INT_MIN;
    }

    for (int i = 1; i <= n+1; ++i) {
        for (int j = 1; j <= n+1; ++j) {
            int curr_ele = (i-1)+(j-1)-1;
            if (curr_ele < 0 || curr_ele >= n) {
                continue;
            }
            dp[i][j] = max(
                dp[i-1][j] + A[curr_ele].first * abs(A[curr_ele].second - (i-2)),
                dp[i][j-1] + A[curr_ele].first * abs(n - (j-1) - A[curr_ele].second)
            );
        }
    }

    ll ans = 0;
    for (int i = 0; i <= n+2; ++i) {
        for (int j = 0; j <= n+2; ++j) {
            ans = max(ans, dp[i][j]);
        }
    }
    cout << ans << endl;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    #ifndef ONLINE_JUDGE
        freopen("in.in", "r", stdin);
        freopen("out.out", "w", stdout);
    #endif

    solve();

    return 0;
}
