n = int(input())
a = list(map(int, input().split()))
ans = 0
r_list = []
l_list = []
ag = []
for i in range(n):
    ag.append([a[i], i])
ag = sorted(ag)
for i in range(n):
    right = n - 1 - ag[i][1]
    left = ag[i][1]
    if right > left:
        ans += (right - len(r_list)) * ag[i][0]
        r_list.append(0)
    elif right < left:
        ans += (left - len(l_list)) * ag[i][0]
        l_list.append(0)
    elif len(r_list) < len(l_list):
        ans += (right - len(r_list)) * ag[i][0]
        r_list.append(0)
    else:
        ans += (left - len(l_list)) * ag[i][0]
        l_list.append(0)

print(ans)
