from collections import deque, Counter, defaultdict
from itertools import chain, combinations
import json
# import numpy as np
import bisect
import sys
import math
import bisect
from functools import lru_cache

sys.setrecursionlimit(10 ** 8)

M = 10 ** 9 + 7
INF = 10 ** 17


def main():
    N = int(input())
    A = [int(a) for a in input().split()]

    res = [(i, a) for i, a in enumerate(A)]

    for i in range(N):
        current_index, current = res[i]
        old_current = current * abs(current_index - i)
        maxj = None
        max_d = 0
        for j in range(N):
            original_index, target = res[j]
            old_target = target * abs(original_index - j)
            new_current_score = current * abs(current_index - j)
            new_other_score = target * abs(original_index - i)

            new_score = new_current_score + new_other_score
            old_score = old_current + old_target
            d = new_score - old_score
            if d > max_d:
                max_d = d
                maxj = j

        if max_d > 0:
            res[i], res[maxj] = res[maxj], res[i]

    score = sum([current * abs(current_index - i) for i, (current_index, current) in enumerate(res)])
    print(score)

    # @lru_cache(None)
    # def f(i, j):
    #     if i < 0 or j < 0:
    #         return -INF
    #     if i == 0:
    #         return -INF
    #     if abs(j - i // 2 - 1) >= 2:
    #         return -INF
    #     if j == 0:
    #         return 0
    #     if j == 1:
    #         return max(A[:i])
    #     return max(A[i - 1] + f(i - 2, j - 1), f(i - 1, j))
    #
    # print(f(N, math.floor(N / 2)))


if __name__ == "__main__":
    main()
