def score(v,mn,mx):
    ind,val = v
    r = mx -ind
    l = ind -mn
    
    return (l!=r,max(l,r)*val,val)        

def resolve():
    N = int(input())
    A = list(map(int,input().split()))
    X = list(enumerate(A))
    mn = 0
    mx = N-1
    #print(sorted(X,key= lambda x:score(x,mn,mx)))
    ans = 0
    while X:
        X.sort(key= lambda x:score(x,mn,mx))
        ind,val = X[-1]
        r = mx -ind
        l = ind -mn

        if r > l:
            ans += r * val
            mx -=1            
        elif r < l:
            ans += l * val
            mn +=1

        del X[-1]      
        #print(ans,mn,mx,X)  
    print(ans)

resolve()
