n = int(input())
A = [int(_) for _ in input().split()]

d = {}
for i in range(n):
    d[i+1] = (A[i], i)
L = [i+1 for i in range(n)]
# print(L)
flag = 0
ans = 0
while flag == 0:
    flag = 1
    # for i in range(n-1):
    #     for j in range(i, n):
    for i in range(n-1, -1, -1):
        for j in range(i, -1, -1):
            # print(i,j)
            v_i = d[L[i]][0]
            idx_i = d[L[i]][1]
            v_j = d[L[j]][0]
            idx_j = d[L[j]][1]
            res = (abs(j - idx_i) * v_i + abs(i - idx_j) * v_j) - (abs(i - idx_i) * v_i + abs(j - idx_j) * v_j)
            if res > 0:
                t = L[i]
                L[i] = L[j]
                L[j] = t
                ans += res
                flag = 0
                # print('i', i)
                # print('res', res)
                # print(L)
print(ans)