N = int(input())
IA = [(i, a) for i, a in enumerate(map(int, input().split()))]

B = []
for i, a in IA:
    for j in range(N):
        B.append((i, j, a))
B.sort(key=lambda a: a[2] * abs(a[0] - a[1]), reverse=True)

confilm = set()
ans = [-1] * N
for i, j, a in B:
    if i in confilm or ans[j] != -1:
        continue
    ans[j] = (i, a)
    confilm.add(i)

res = 0
for j, (i, a) in enumerate(ans):
    res += a * abs(j - i)
print(res)
