n = int(input())
A = list(map(int, input().split()))

y = [i for i in range(n)]
ans = 0

for i in range(n):
    h = [0, 0]
    x = [0, 0]
    for j in range(n):
        ht = A[j] * abs(j - y[0])
        hl = A[j] * abs(j - y[-1])
        if ht > h[0]:
            h[0] = ht
            x[0] = j
        if hl >= h[1]:
            h[1] = hl
            x[1] = j
    if h[0] >= h[1]:
        ans += h[0]
        A[x[0]] = -1
        y.pop(0)
    else:
        ans += h[1]
        A[x[1]] = -1
        y.pop(-1)

print(ans)
