import math

N = int(input())
List = list(map(int, input().split()))

for i in range (0, N):
	List[i]=List[i]+0.0000001*i

Listsorted = sorted(List, reverse = True)

left = 0
right = 0
total = 0

for i in range (0, N):
	p = List.index(Listsorted[i])
	if p-left > N-1-p-right:
		total+= math.floor(List[p]*(p-left))
		left+=1
		print('left')
		print(total)
	else:
		total+= math.floor(List[p]*(N-1-p-right))
		right+=1
		print('right')
		print(total)

print(total)