#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define int long long
#define ld long double
#define endl '\n'
#define vi vector<int>
#define vvi vector<vi>
#define pii pair<int, int>
#define vpii vector<pii>
#define gr vector<vpii>
#define str string
#define mask vector<bool>
#define vs vector<str>
#define pq priority_queue
#define ordered_set_of(T) tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>
#define fi first
#define se second
#define sz(x) (int)((x).size())
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define nub(x) x.resize(unique(all(x)) - x.begin())
#define TWO class T1, class T2
#define YES cout << "YES" << endl
#define NO cout << "NO" << endl
#define GAME_OVER return 0
using namespace std;
using namespace __gnu_pbds;
char _$ = ' ';
struct sep {sep(char c) {_$ = c;}};
ostream& operator<<(ostream& o, sep s) {return o;}
template<TWO> ostream& operator<<(ostream& o, pair<T1, T2> x) {return o << x.fi << ' ' << x.se;}
template<TWO> istream& operator>>(istream& i, pair<T1, T2>& x) {return i >> x.fi >> x.se;}
template<class T> ostream& operator<<(ostream& o, vector<T> x) {for (auto it : x) o << it << _$; return o;}
template<class T> istream& operator>>(istream& i, vector<T>& x) {for (auto& it : x) i >> it; return i;}
signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    int n;
    cin >> n;
    vpii a(n);
    for (int i = 0; i < n; i++)
        cin >> a[i].fi, a[i].se = i;
    sort(rall(a));
    vvi dp(n + 1, vi(n + 1));
    int res = 0;
    for (int i = 0; i < n; i++)
        for (int j = 0; j < n; j++)
        {
            int pos = i + j;
            if (pos == n)
            {
                res = max(res, dp[i][j]);
                break;
            }
            dp[i + 1][j] = max(dp[i + 1][j], dp[i][j] + abs(a[pos].se - i) * a[pos].fi);
            dp[i][j + 1] = max(dp[i][j + 1], dp[i][j] + abs(a[pos].se - (n - 1 - j)) * a[pos].fi);
        }
    cout << res << endl;
    GAME_OVER;
}