n = gets.to_i
a = gets.split.map(&:to_i)

def calc(a, k, n, memo, f)
    return if k.nil?

    pos = k
    max = 0
    n.times do |i|
        x = a[k] * (i - k).abs
        if memo[i][0] < x && max < x
            pos = i
            max = x
        end
    end

    j = memo[pos][1]
    memo[pos][0] = max
    memo[pos][1] = k
    calc(a, j, n, memo, false)
    calc(a, k + 1, n, memo, true) if f && k != n - 1
end

memo = Array.new(n){[0, nil]}
calc(a, 0, n, memo, true)
puts memo.inject(0) {|r, v| r + v[0]}