import numpy as np

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

m = np.zeros((n, n))
for i in range(n):
    for j in range(n):
        m[i][j] = np.abs(i - j) * a[i]

row = np.sum(m, axis=0)
col = np.sum(m, axis=1)

_m = 2 * m - row[:, None] - col[None, :]

res = 0

for i in range(n):
    x, y = np.unravel_index(np.argmax(_m), _m.shape) 
    res += m[x][y]
    m[x, :] = 0
    m[:, y] = 0
    row = np.sum(m, axis=0)
    col = np.sum(m, axis=1)
    _m = 2 * m - row[:, None] - col[None, :]

print(int(res))
