#include <iostream>
#include <vector>
#include <algorithm>
#include <utility>
using namespace std;

int main()
{
    int N;
    cin>>N;
    vector<long long> A(N);
    for (long long &a: A)
        cin>>a;

    vector<pair<long long, int>> V(N);
    for (int i=0; i<N; i++)
        V[i] = make_pair(A[i], i);
    sort(V.begin(), V.end());
    reverse(V.begin(), V.end());

    vector<long long> T(N+1, -1);
    T[0] = 0;
    for (int i=0; i<N; i++)
    {
        vector<long long> P(N+1, -1);
        P.swap(T);
        int x = V[i].second;
        for (int j=0; j<=i; j++)
        {
            T[j+1] = max(T[j+1], P[j]+abs(x-j)*A[x]);
            T[j] = max(T[j], P[j]+abs(x-(N-i+j-1))*A[x]);
        }
    }
    cout<<*max_element(T.begin(), T.end())<<endl;
}
