n = int(input())
l = list(map(int, input().split()))
r = []
for i, x in enumerate(l):
    r.append((x, i))
r.sort(reverse=True)
s = {(0, n-1): 0}
result = 0

for i, x in r:
    # print("HERE: ", i, x)
    new = {}
    for (left, right), value in s.items():
        # print(left, right, value)
        if left==right:
            result = max(result, value + abs(left-x)*i)
            continue
        if (left+1, right) in new:
            new[(left+1, right)] = max(new[(left+1, right)], value + abs(left-x)*i)
        else:
            new[(left+1, right)] = value + abs(left-x)*i
            
        if (left, right-1) in new:
            new[(left, right-1)] = max(new[(left, right-1)], value + abs(right-x)*i)
        else:
            new[(left, right-1)] = value + abs(right-x)*i
            
    s = new
    
print(result)