import numpy as np
n = int(input())
l = list(map(int, input().split()))
ans = 0
pos = [0] * n
for i in range(n):
    pos[i] = list(map(lambda x: abs(x - i), list(range(n))))
pos = np.array(pos)
pos_2 = np.zeros_like(pos)
for i in range(n):
    for j in range(n):
        pos_2[j, :] = pos[j, :] * l[j]
    ans += np.max(pos_2)
    index = pos_2.argmax()
    x = index // n
    y = index % n
    l[x] = 0
    pos[:, y] = 0
print(ans)
