n = int(input().strip())
a = list(map(int, input().split()))
dic = {}
for i in range(len(a)):
    if dic.get(a[i]):
        dic[a[i]].append(i)
    else:
        dic[a[i]] = [i]
dic = sorted(dic.items(), reverse=True)
fo = n - 1
ba = 0
ans = 0
for elem in dic:
    print(elem)
    val = elem[0]
    arr = elem[1]
    for idx in arr:
        if abs(fo - idx) < abs(ba - idx):
            ans += val * abs(ba - idx)
            ba += 1
        else:
            ans += val * abs(fo - idx)
            fo -= 1
print(ans)
