#設定
import sys
input = sys.stdin.buffer.readline

#ライブラリインポート
from collections import defaultdict

con = 10 ** 9 + 7
#入力受け取り
def getlist():
	return list(map(int, input().split()))

#処理内容
def main():
	N = int(input())
	A = getlist()
	S = [[A[i], i] for i in range(N)]
	for k in range(N):
		for i in range(N - 1):
			A1 = S[i][0]
			A2 = S[i + 1][0]
			x = S[i][1]
			y = S[i + 1][1]
			if A1 * abs(x - i) + A2 * abs(y - i - 1) < A1 * abs(x - i - 1) + A2 * abs(y - i):
				S[i + 1][0] = A1
				S[i][0] = A2
				S[i + 1][1] = x
				S[i][1] = y

	# print(S)

	ans = 0
	for i in range(N):
		a = S[i][0]
		x = S[i][1]
		ans += a * abs(x - i)

	print(ans)


if __name__ == '__main__':
	main()