#include <bits/stdc++.h>
#define F first
#define S second
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
LL solve(int L, int R, auto &data, auto &dp, int idx) {
    if (~dp[L][R]) return dp[L][R];
    if (idx == data.size()) return 0;
    dp[L][R] = max(
        solve(L + 1, R, data, dp, idx + 1) + abs(data[idx].S - L) * data[idx].F,
        solve(L, R - 1, data, dp, idx + 1) + abs(data[idx].S - R + 1) * data[idx].F
    );
    return dp[L][R];
}
int main() {
    ios_base::sync_with_stdio(false); cin.tie(0);
    int n; cin >> n;
    vector<pii> data;
    for (int i = 0 ; i < n ; i++) {
        int v; cin >> v;
        data.push_back({v, i});
    }
    sort(data.begin(), data.end());
    reverse(data.begin(), data.end());
    vector<vector<int> > dp(n + 1, vector<int>(n + 1, -1));
    cout << solve(0, n, data, dp, 0) << '\n';
}
