include <bits/stdc++.h>
using namespace std;
int main(void){
    long long N;
    cin >> N;
    vector<pair<long long, int>> A(N+1);
    A.at(0).first = 0;
    for (long long i = 1; i < N+1; i++){
        A.at(i).second= i;
        cin >> A.at(i).first;
    }
    sort(A.begin() + 1, A.end(), greater<pair<long long, int>>() );
    vector<vector<long long>> dp(N+1, vector<long long> (N+1));
    dp.at(0).at(0) = 0;
    for (int xy = 1; xy < N+1; xy++){
        for (int x = 1; x < xy; x++){
            dp.at(x).at(xy-x) = max( dp.at(x-1).at(xy-x) + A.at(xy).first * abs( A.at(xy).second - x ) , 
                                  dp.at(x).at(xy-x-1) + A.at(xy).first * abs( A.at(xy).second - N + xy - x - 1 ));
        }
        dp.at(0).at(xy) = dp.at(0).at(xy-1) + A.at(xy).first * abs( A.at(xy).second - N + xy - 1 );
        dp.at(xy).at(0) = dp.at(xy-1).at(0) + A.at(xy).first * abs( A.at(xy).second - xy );
    }
    long long ans = 0;
    for (int i = 0; i <= N; i++){
        ans = max(ans, dp.at(i).at(N-i));
    }
    printf("%lld\n", ans);
}