n = int(input())
a = [int(c) for c in input().split()]
li = 0
ri = n-1
out = 0
tmp = [0] * n
tmp2 = [0]
diff = [0] * n
tmp[0] = a[0]
tmp2[0] = a[0]
for i in range(1, n):
    item = a[i]
    best_ind = 0
    best_val = -1
    for j in range(i+1):
        val = item * (i - j) + tmp[j]
        if val > best_val:
            best_val = val
            best_ind = j
    prev = diff[best_ind]
    diff[best_ind] = (best_ind - i)
    tmp2.insert(best_ind, item)
    out += best_val
    for ii in range(best_ind+1, i+1):
        prev2 = diff[ii]
        diff[ii] = prev+1
        prev = prev2
    vv = 0
    for ii in range(i, -1, -1):
        sign = 1 if diff[ii]>=0 else -1
        vv += (tmp2[ii] * sign)
        tmp[ii] = vv
print(out)