n = int(input())
a = [int(i) for i in input().split()]
b = sorted(a, reverse=True)
left, right = 0, 0
sum = 0
for i in range(n):
    index = a.index(b[i])
    a[index] = 0
    if index < n / 2:
        sum += b[i]*(((n - 1) - right) - index)
        right += 1
    else:
        sum += b[i]*(index - left)
        left += 1
print(sum)