N = int(input())
A = sorted([(int(a), i) for i, a in enumerate(input().split())], key = lambda x: x[0])[::-1]
X = [0] + [-1<<100] * (N + 1)
for k, (a, i) in enumerate(A):
    nX = [0] * (N + 2)
    for j in range(k + 1):
        nX[j] = max(nX[j], X[j] + abs(N - (k - j) - 1 - i) * a)
        nX[j+1] = max(nX[j+1], X[j] + abs(j - i) * a)
    X = nX
print(max(X))