n = int(input())
a = list(map(int, input().split()))

left = 0
right = len(a) - 1
ans = 0

for i in range(n):
    # print(right, left)
    tmp = 0
    num = 0
    l, r = False, False
    for j in range(len(a)):
        if abs(j - right) > abs(j - left):
            if tmp <= abs(j - right) * a[j]:
                tmp = abs(j - right) * a[j]
                num = j    
                l, r = False, True

        elif abs(j - right) < abs(j - left):
            if tmp <= abs(j - left) * a[j]:
                tmp = abs(j - left) * a[j]
                num = j    
                l, r = True, False

        else:
            if tmp <= abs(j - left) * a[j]:
                for k in range(len(a)):
                    tl, tr = False, False
                    te = 0
                    if k != j:
                        if abs(k - right) > abs(j - left):
                            if te < abs(k - right) * a[k]:
                                te = abs(k - right) * a[k]
                                tl, tr = False, True
                        else:
                            if te < abs(k - left) * a[k]:
                                te = abs(k - left) * a[k]
                                tl, tr = True, False
                num = j
                if tl:
                    l, r = False, True
                    tmp += abs(j - right) * a[j]
                else:
                    l, r = True, False
                    tmp += abs(j - left) * a[j]
                
    ans += tmp    
    a[num] = 0
   
    if l:
        left += 1
    if r:
        right -= 1

print(ans)
