import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop;

immutable long MOD = 10^^9 + 7;

void main() {
    auto N = readln.chomp.to!int;
    auto A = readln.split.map!(to!long).array;
    auto B = N.iota.map!(i => tuple(i, A[i])).array;
    B.sort!"a[1] > b[1]";

    auto dp = new long[][](N+1, N+1);

    foreach (i; 0..N) foreach (j; 0..i+1) {
        int k = i - j;
        auto idx = B[i][0];
        long val = B[i][1];
        dp[i+1][j+1] = max(dp[i+1][j+1], dp[i][j] + abs(idx-j)*val);
        dp[i+1][j] = max(dp[i+1][j], dp[i][j] + abs(N-1-k-idx)*val);
    }

    dp[N].reduce!max.writeln;
}