N = int(input())
A = list(map(int, input().split()))

import heapq

q_left = []
for i in range(N):
    score = i * A[i]
    heapq.heappush(q_left, (-score, i, score))

q_right = []
for i in range(N):
    score = i * A[N - 1 - i]
    heapq.heappush(q_right, (-score, N - 1 - i, score))

ans = 0
used = [False] * N
for i in range(N):
    if i % 2 == 0:
        idx = i // 2
        while len(q_left) > 0:
            _, pos, score = heapq.heappop(q_left)
            if pos >= idx and used[pos] == False:
                ans += (score - A[pos] * idx)
                # print("#", i, pos, ans)
                break
        #
        used[pos] = True
    else:
        idx = N - 1 - i // 2
        while len(q_right) > 0:
            _, pos, score = heapq.heappop(q_right)
            if pos <= idx and used[pos] == False:
                ans += (score - A[pos] * (N - 1 - idx))
                # print("#", i, pos, ans)
                break
        #
        used[pos] = True

print(ans)