#!/usr/bin/python3
# -*- coding: utf-8 -*-
n = int(input())
a_list = list(map(int, input().split()))
start = 0
end = n - 1
ans = 0
flag = 0
i = 0
b_iti = -1
b_score = -1
while(i < n):
    i += 1
    mae_max_score = -1
    usi_max_score = -1
    mae_iti = -1
    usi_iti = -1
    for j in range(0, n):
        if mae_max_score < a_list[j] * abs(j - start):
            mae_max_score = a_list[j] * abs(j - start)
            mae_iti = j
        if usi_max_score < a_list[j] * abs(end - j):
            usi_max_score = a_list[j] * abs(end - j)
            usi_iti = j
    if flag == 0:
        if mae_max_score > usi_max_score:
            a_list[mae_iti] = 0
            ans += mae_max_score
            start += 1
        elif mae_max_score < usi_max_score:
            a_list[usi_iti] = 0
            ans += usi_max_score
            end -= 1
        else:
            if mae_iti != usi_iti:
                a_list[mae_iti] = 0
                ans += mae_max_score
                start += 1
                a_list[usi_iti] = 0
                ans += usi_max_score
                end -= 1
                i += 1
            elif start == end:
                a_list[mae_iti] = 0
                ans += mae_max_score
            else:
                a_list[mae_iti] = 0
                b_score = mae_max_score
                flag = 1
    else:
        flag = 0
        if mae_max_score > usi_max_score:
            a_list[mae_iti] = 0
            ans += mae_max_score
            start += 1
            ans += b_score
            end -= 1
        elif mae_max_score <= usi_max_score:
            a_list[usi_iti] = 0
            ans += usi_max_score
            end -= 1
            ans += b_score
            start += 1

print(ans)
