from operator import itemgetter
import sys
sys.setrecursionlimit(10**5)

def f(x, po, h, t, ple):
    if po=='h':
        ple+=B[x][1]*abs(B[x][0]-h)
        h+=1
    else:
        ple+=B[x][1]*abs(B[x][0]-t)
        t-=1
    if x==N-1:
        return ple
    
    if abs(B[x+1][0]-h)==abs(B[x+1][0]-t):
        return max(f(x+1, 'h', h, t, ple), f(x+1, 't', h, t, ple))
    elif abs(B[x+1][0]-h)>abs(B[x+1][0]-t):
        return f(x+1, 'h', h, t, ple)
    else:
        return f(x+1, 't', h, t, ple)

N=int(input())
A=list(map(int, input().split()))
B=[[i, A[i]] for i in range(N)]
B=sorted(B, key=itemgetter(1), reverse=True)
h=0
t=N-1

if abs(h-B[0][0])==abs(t-B[0][0]):
    ans=max(f(0, 'h', h, t, 0), f(0, 't', h, t, 0))
elif abs(h-B[0][0])>abs(t-B[0][0]):
    ans=f(0, 'h', h, t, 0)
else:
    ans=f(0, 't', h, t, 0)
print(ans)