from heapq import heappush,heappop,heapify
from collections import deque,defaultdict,Counter
from itertools import permutations,combinations,groupby
import sys,bisect,string,math,time,functools,random
def Golf():*a,=map(int,open(0))
def I():return int(input())
def S_():return input()
def IS():return input().split()
def LS():return [i for i in input().split()]
def LI():return [int(i) for i in input().split()]
def LI_():return [int(i)-1 for i in input().split()]
def NI(n):return [int(input()) for i in range(n)]
def NI_(n):return [int(input())-1 for i in range(n)]
def StoLI():return [ord(i)-97 for i in input()]
def ItoS(n):return chr(n+97)
def LtoS(ls):return ''.join([chr(i+97) for i in ls])
def GI(V,E,Directed=False,index=0):
    org_inp=[];g=[[] for i in range(n)]
    for i in range(E):
        inp=LI();org_inp.append(inp)
        if index==0:inp[0]-=1;inp[1]-=1
        if len(inp)==2:
            a,b=inp;g[a].append(b)
            if not Directed:g[b].append(a)
        elif len(inp)==3:
            a,b,c=inp;aa=(inp[0],inp[2]);bb=(inp[1],inp[2]);g[a].append(bb)
            if not Directed:g[b].append(aa)
    return g,org_inp
def GGI(h,w,search=None,replacement_of_found='.',mp_def={'#':1,'.':0}):
#h,w,g,sg=GGI(h,w,search=['S','G'],replacement_of_found='.',mp_def={'#':1,'.':0}) # sample usage
    mp=[1]*(w+2);found={}
    for i in range(h):
        s=input()
        for char in search:
            if char in s:
                found[char]=((i+1)*(w+2)+s.index(char)+1)
                mp_def[char]=mp_def[replacement_of_found]
        mp+=[1]+[mp_def[j] for j in s]+[1]
    mp+=[1]*(w+2)
    return h+2,w+2,mp,found
def TI(n):return GI(n,n-1)
def bit_combination(k,n=2):
    rt=[]
    for tb in range(n**k):
        s=[tb//(n**bt)%n for bt in range(k)];rt+=[s]
    return rt
def show(*inp,end='\n'):
    if show_flg:print(*inp,end=end)

YN=['YES','NO'];Yn=['Yes','No']
mo=10**9+7
inf=float('inf')
l_alp=string.ascii_lowercase
#sys.setrecursionlimit(10**7)
input=lambda: sys.stdin.readline().rstrip()

class Comb:
    def __init__(self,n,mo=10**9+7):
        self.fac=[0]*(n+1)
        self.inv=[1]*(n+1)
        self.fac[0]=1
        self.fact(n)
        for i in range(1,n+1):
            self.fac[i]=i*self.fac[i-1]%mo
            self.inv[n]*=i
            self.inv[n]%=mo
        self.inv[n]=pow(self.inv[n],mo-2,mo)
        for i in range(1,n):
            self.inv[n-i]=self.inv[n-i+1]*(n-i+1)%mo
        return
    
    def fact(self,n):
        return self.fac[n]
        
    def invf(self,n):
        return self.inv[n]

    def comb(self,x,y):
        if y<0 or y>x:
            return 0
        return self.fac[x]*self.inv[x-y]*self.inv[y]%mo

show_flg=False
#show_flg=True

ans=0


n=I()
A=LI()
a=list(enumerate(A))
a.sort(key=lambda x:x[1])
show('a',a)

b=[[]for i in range(n)]
for i in range(n):
    for j in range(n):
        b[i].append(abs(i-j)*A[i])

for j in b:
    show('b',j)

l,r=0,n-1
rng=list(range(n))
skp=set()
for i in range(n):
    mx_l,mx_r=-1,-1
    for j in range(n):
        if j in skp:continue
        if b[j][l]>mx_l:
            mx_l=b[j][l]
            l_can=set()
            l_can.add(j)
        elif b[j][l]==mx_l:
            l_can.add(j)

        if b[j][r]>mx_r:
            mx_r=b[j][r]
            r_can=set()
            r_can.add(j)
        elif b[j][r]==mx_r:
            r_can.add(j)
    show('mx_l,mx_r',mx_l,mx_r)
    if mx_r>mx_l:
        ans+=mx_r
        r-=1
        mn_l=1<<63
        for x in r_can:
            if b[x][l]<mn_l:
                mn_l=b[x][l]
                r_skp=x
        skp.add(r_skp)
        show('r')
    elif mx_l>mx_r:
        ans+=mx_l
        l+=1
        mn_r=1<<63
        for x in l_can:
            if b[x][r]<mn_r:
                mn_r=b[x][r]
                l_skp=x
        skp.add(l_skp)
        show('l')
    elif mx_l==mx_r:
        ans+=mx_l
        l+=1
        mn_r=1<<63
        for x in l_can:
            if b[x][r]<mn_r:
                mn_r=b[x][r]
                l_skp=x
        skp.add(l_skp)
        show('=')
    show(skp,(l,r),ans)

print(ans)