def input_one_number():
    return int(input())

def input_multiple_number():
    return map(int, input().split())

def input_multiple_number_as_list():
    return list(map(int, input().split()))

N = input_one_number()
A = input_multiple_number_as_list()

#Aのindexつきのやつ
A2 = [[i,A[i]] for i in range(len(A))]

#元気さん順
A2.sort(key=lambda x:x[1],reverse=True)


dp = [[0]*(N+1) for i in range(N+1)]

def wkwk(a,x,y):
	return a*abs(x-y)


for i in range(1,N+1):
	dp[i][0] = dp[i-1][0]+wkwk(A2[i-1][1],A2[i-1][0],i-1)
	dp[0][i] = dp[0][i-1]+wkwk(A2[i-1][1],A2[i-1][0],N-i)

ans = 0
for first in range(1,N+1):
	for last in range(1,N+1-first):
		yoji_index = first+last-1
		dp[first][last] = max(dp[first-1][last]+wkwk(A2[yoji_index][1],A2[yoji_index][0],first-1),
								dp[first][last-1]+wkwk(A2[yoji_index][1],A2[yoji_index][0],N-last))
		ans = max(ans,dp[first][last])


print(ans)
