N = int(input())
A = [int(i) for i in input().split()]
B = [True]*N
head = 0
tail = N - 1
ans = 0
for i in range(N):
    max_ = -1
    flag_h = True
    for j in range(N):
        if B[j]:
            h = abs(j - head)
            t = abs(j - tail)
            if max(h,t) * A[j] > max_:
                max_ = max(h, t) * A[j]
                flag_h = True if h > t else False
                tmp_ind = j
    ans += max_
    B[tmp_ind] = False
    if flag_h:
        head += 1
    else:
        tail -= 1
print(ans)