import math  # noqa
import bisect  # noqa
import queue  # noqa


if __name__ == '__main__':
    N = int(input())
    A = list(map(int, input().split()))

    MAX = N - 1
    MIN = 0
    used = set()

    ans = 0
    while len(used) < N:
        best = 0
        best_idx = -1
        for i, a in enumerate(A):
            diff = max(abs(i - MIN), abs(MAX - i))
            if (best < diff * a) and (i not in used):
                best = diff * a
                best_idx = i
        ans += best
        used.add(best_idx)
        if abs(best_idx - MIN) < abs(MAX - best_idx):
            MAX -= 1
        else:
            MIN += 1

    print(ans)
