#include <bits/stdc++.h>
#define rep(i, n) for(int i=0; i<(n); i++)
#define all(x) (x).begin(), (x).end()
#define bit(x) (1L << (x))
using ll = long long;
using namespace std;

template<typename T>
vector<T> make_v(size_t a,T b){return vector<T>(a,b);}
 
template<typename... Ts>
auto make_v(size_t a,Ts... ts){
        return vector<decltype(make_v(ts...))>(a,make_v(ts...));
}

int main() {
        int n;
        cin >> n;

        vector<int> a(n);
        rep (i, n) cin >> a[i];

        vector<int> o(n);
        iota(o.begin(), o.end(), 0);
        sort(o.begin(), o.end(), [&](int i, int j) { return a[i] > a[j]; });

        auto dp = make_v(n + 1, n + 1, 0LL);
        rep (s, n) rep (i, s + 1) {
                int j = s - i;
                dp[i + 1][j] = max(dp[i + 1][j], dp[i][j] + a[o[s]] * abs(o[s] - i));
                dp[i][j + 1] = max(dp[i][j + 1], dp[i][j] + a[o[s]] * abs(o[s] - (n - 1 - j)));
        } 

        ll ans = 0LL;
        rep (i, n) ans = max(ans, dp[i][n - i]);

        cout << ans << endl;

        return 0;
}
