n = int(input())
a = list(map(int,input().split()))
ans = 0
check = [0]*n
flag1 = 0
flag2 = n-1
while check != [1]*n:
    if abs(a.index(max(a))-flag1) > abs(a.index(max(a))-flag2):
        ans += max(a)*abs(a.index(max(a))-flag1)
        a[a.index(max(a))] = 0
        check[flag1]=1
        flag1 += 1
    if abs(a.index(max(a))-flag1) <= abs(a.index(max(a))-flag2):
        ans += max(a)*abs(a.index(max(a))-flag2)
        a[a.index(max(a))] = 0
        check[flag2]=1
        flag2 -= 1
print(ans)