n = int(input())
a = list(map(int,input().split()))
b = sorted(a)
c = [True for i in range(n)]
#print(a)
#print(b)
m = 0
M = n-1
ans = 0
for i in range(n-1,-1,-1):
    now = b[i]
    for j in range(n):
        if now == a[j] and c[j]:
            idx = j
            c[j] = False
            break
    if M-idx > idx-m:
        ans += a[idx]*(M-idx)
        M -= 1
    else:
        ans += a[idx]*(idx-m)
        m += 1

print(ans)

