n = int(input())
li = list(map(int,input().split()))
import copy

def maxmize(members, seats):
    if seats == []:
        return 0
    else:
        saidai = max(members)
        saidai_index = members.index(saidai)
        newmembers = copy.deepcopy(members)
        newmembers[saidai_index] = 0
        #print("members",members)
        #print("seats",seats[:-1])
        #print("saidai",saidai)
        #print("hoge",abs(saidai_index - seats[-1]))
        right = maxmize(newmembers,seats[:-1]) + saidai * abs(saidai_index - seats[-1])
        left = maxmize(newmembers,seats[1:]) + saidai * abs(saidai_index - seats[0])                                                      
        
        #print(left,right)
    return max(left,right)

givenseats = [j for j in range(n)]
maxmize(li, givenseats)