#mod = 10**9 + 7
import heapq
############ ---- USER DEFINED INPUT FUNCTIONS ---- ############
def inp():
    return(int(input().rstrip()))
def inlt():
    return(list(map(int,input().rstrip().split())))
def insr():
    s = input().rstrip()
    return(s[:len(s) - 1])
def invr():
    return(map(int,input().rstrip().split()))
################################################################
n = inp()
nums = inlt()
nums = [[nums[i],i] for i in range(n)]
ans = 0
left = 0
right = n - 1
while left < right:
    the_max = 0
    ri = left
    for i in range(left,right + 1):
        val,index = nums[i]
        if abs(right - index)*val > the_max:
            the_max = abs(right - index)*val
            ri = i
    the_m = 0
    for i in range(right,left - 1,-1):
        val,index = nums[i]
        if abs(index - left)*val > the_m:
            the_m = abs(index - left)*val
            li = i
    if the_max > the_m:
        nums[ri][0] = nums[right][0]
        nums[ri][1] = nums[right][1]
        right -= 1
        ans += the_max
    else:
        nums[li][0] = nums[left][0]
        nums[li][1] = nums[left][1]
        left += 1
        ans += the_m
    #print(ans)
val,i= nums[left]
print(ans + abs(left - i)*val)  
#print(n - s if s <= n else -1)
#print('No')
#print()

