import sys
 
stdin = sys.stdin
sys.setrecursionlimit(10**6)
ns = lambda: stdin.readline().rstrip()
ni = lambda: int(stdin.readline().rstrip())
nm = lambda: map(int, stdin.readline().split())
nl = lambda: list(map(int, stdin.readline().split()))
 
n = ni()
a = nl()
lis = [(e, i) for i, e in enumerate(a)]
lis.sort()

dp = [[-1]*n for _ in range(n)]
 
def solve(l, r):
    if l > r:
        return 0
    idx = r - l
    e, i = lis[idx]
    return max(abs(i-l)*e + solve(l+1, r), abs(r-i)*e + solve(l, r-1))
print(solve(0, n-1))