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

total_joy = 0
afterA = [0] * n
for i in range(n):
    M = max(A)
    idx = A.index(M)

    if idx <= (n-1) / 2:
        j = n-1
        while(afterA[j] != 0):
            j -= 1
        
        total_joy += M * (j - idx)
        afterA[j] = 1

    else:
        j = 0
        while(afterA[j] != 0):
            j += 1
            
        total_joy += M * (idx - j)
        afterA[j] = 1

    A[idx] = 0

print(total_joy)