N = int(input())
AA = list(map(int,input().split()))
AAwIdx = [[0]*2 for _ in range(N)]
for i in range(N):
    AAwIdx[i][0] = i
    AAwIdx[i][1] = AA[i]
AA_sorted = sorted(AAwIdx, key=lambda x: x[1], reverse=True)
#print(AA_sorted)

answer = [[0]*(N+1) for _ in range(N+1)]

ans_all = 0
Idx_i = 0
Idx_f = N-1
for i in range(0,N):
    for no in range(i+1):
        idxi = i-no
        idxf = Idx_f-no
        answer[i+1-no][no] = max(answer[i+1-no][no],answer[i-no][no] + abs(AA_sorted[i][0]-idxi)*AA_sorted[i][1])
        answer[i-no][no+1] = max(answer[i-no][no+1],answer[i-no][no] + abs(AA_sorted[i][0]-idxf)*AA_sorted[i][1])
        #print('idx:',AA_sorted[i][0], 'val:',AA_sorted[i][1],'idx_i:',idxi, 'idx_f:',idxf)
        #print('idx:',i-no,no,'ans:',answer[i-no][no])
        #print('idx:',i+1-no,no,'ans:',abs(AA_sorted[i][0]-idxi)*AA_sorted[i][1])
        #print('idx:',i-no,no+1,'ans:',abs(AA_sorted[i][0]-idxf)*AA_sorted[i][1])
        ans_all = max(answer[i+1-no][no], ans_all)
        ans_all = max(answer[i-no][no+1], ans_all)
#print(np.array(answer))
#    print(AAs)
#print(AnsIdx)
print(ans_all)