import heapq
import math
import random
import sys
import numpy as np


sys.setrecursionlimit(10**6+1)
write = sys.stdout.write
input = sys.stdin.buffer.readline

MOD = 10**9 + 7


if __name__ == "__main__":
    n = int(input())
    arr = sorted(enumerate(map(int, input().split())),
                 key=lambda x: x[1], reverse=True)
    dp = np.zeros((n+1, n+1), dtype=np.int)

    for i in range(n):
        for j in range(n-i):
            idx, a = arr[i+j]
            dp[i+1, j] = max(dp[i+1, j], dp[i, j] + a*(idx-i))
            dp[i, j+1] = max(dp[i, j+1], dp[i, j] + a*(n-1-j-idx))

    print(max(dp[i, n-i] for i in range(n+1)))
