n = int(input())
a = list(map(int, input().split()))

l = [[a[i]*max(i, n-1-i), a[i], i] for i in range(n)]
l.sort()

rmax = n-1
rmin = 0

happiness = 0
for i in range(n-1, -1, -1):
    if abs(l[i][2]-rmin) > abs(l[i][2]-rmax):
        happiness += l[i][1] * abs(l[i][2]-rmin)
        rmin += 1
    elif abs(l[i][2]-rmin) < abs(l[i][2]-rmax):
        happiness += l[i][1] * abs(l[i][2]-rmax)
        rmax -= 1
    else:
        if i == 0:
            happiness += l[i][1] * abs(l[i][2]-rmin)
        elif l[i-1][2] < l[i][2]:
            happiness += l[i][1] * abs(l[i][2]-rmin)
            rmin += 1
        else:
            happiness += l[i][1] * abs(l[i][2]-rmax)
            rmax -= 1    

print(happiness)