N = int(input())
A = map(int, input().split())

B = [[i, a] for i, a in enumerate(A)]

C = sorted(B, key=lambda b: b[1], reverse=True)

l = 0
r = N - 1

ans = 0
queue = [[C, l, r, 0]]
while queue:
    D, l, r, total = queue.pop()

    total2 = total + abs(D[0][0] - l) * D[0][1]
    if len(D) == 1:
        ans = max(ans, total2)
        continue
    else:
        queue.append([D[1:], l+1, r, total2])

    total2 = total + abs(D[0][0] - r) * D[0][1]
    if len(D) == 1:
        ans = max(ans, total2)
        continue
    else:
        queue.append([D[1:], l, r-1, total2])

print(ans)
