import sys 
try:
	sys.stdin = open('input.txt', 'r') 
	sys.stdout = open('output.txt', 'w')
except:
	pass
input = sys.stdin.readline
for tt in range(1):
	n = int(input())
	# a,b = map(int,input().split())
	arr = list(map(int,input().split()))
	i = 0
	j = 1
	right = n-1
	left = 0
	arr = list(enumerate(arr))
	arr.sort(reverse = True, key = lambda x:(x[1],x[0]))
	# print(arr)
	ans = 0 
	while i<n and j<n:
		first = max(arr[i][1] * (abs(arr[i][0] - right)), arr[i][1] * (abs(arr[i][0] - left)))
		second = max(arr[j][1] * (abs(arr[j][0] - right)), arr[j][1] * (abs(arr[j][0] - left)))
		# print(first,second)
		if first > second:
			if abs(arr[i][0] - right) > abs(arr[i][0] - left):
				right -= 1
			else:
				left += 1
			ans += first
			i=j
			j+=1
		else:
			if abs(arr[j][0] - right) > abs(arr[j][0] - left):
				right -= 1
			else:
				left += 1
			j+=1
			ans += second
		# print("i and j", i,j)
		# print("Right Left", right, left)
		# print()
	# print(ans,i,j,right,left)
	if i<n:
		ans += max(arr[i][1] * (abs(arr[i][0] - right)), arr[i][1] * (abs(arr[i][0] - left)))
	if j<n:
		ans += max(arr[j][1] * (abs(arr[j][0] - right)), arr[j][1] * (abs(arr[j][0] - left)))
	# print(i,j,right,left)
	print(ans)