N = int(input())
A = [int(INPUT) for INPUT in input().strip().split(' ')]

happiness = 0

exist_index = list(range(N))
exist_visited = [0] * N
empty_index = list(range(N))
for k in range(N):
    largest_gain = -1
    # print(exist_index, exist_visited, empty_index)
    for i in range(N):
        if exist_visited[i] == 1:
            continue

        place = 0 if abs(exist_index[i] - empty_index[0]) > abs(exist_index[i] - empty_index[-1]) else -1

        gain = max(A[exist_index[i]] * abs(exist_index[i] - empty_index[0]),
                   A[exist_index[i]] * abs(exist_index[i] - empty_index[-1]))
        # print(place, gain)
        if gain > largest_gain:
            largest_gain = gain
            best_child = exist_index[i]
            best_place = place
    # print("--")
    # print(largest_gain, best_child)
    # print("--")
    happiness += largest_gain
    exist_visited[best_child] = 1
    if best_place == 0:
        empty_index = empty_index[1:]
    else:
        empty_index = empty_index[:-1]
    # empty_index = empty_index[0:place] + empty_index[place + 1:]

print(happiness)