#!/usr/bin/env python3
import sys


def solve(N: int, A: "List[int]"):
    A_ = []
    indices = []
    tmp = sorted(enumerate(A), reverse=True, key=lambda x:x[1])
    for row in tmp:
        k, v = row
        indices.append(k)
        A_.append(v)
    A = A_
    
    dp = [[0 for i in range(N+1)] for j in range(N+1)]
    for i in range(N):
        for j in range(0, N-i):
            a = A[i+j]
            idx = indices[i+j]
            dp[i+1][j] = max(dp[i+1][j], dp[i][j] + a * abs(idx - i))
            dp[i][j+1] = max(dp[i][j+1], dp[i][j] + a * abs(N - 1 - j - idx))
    result = -1
    for i in range(N):
        tmp = dp[i][N-i]
        if tmp > result:
            result = tmp
    print(result)
    return


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    A = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    solve(N, A)

if __name__ == '__main__':
    main()
