import heapq
import math
import random
import sys
from bisect import bisect_left, bisect_right
from collections import Counter, defaultdict, deque, namedtuple, UserDict
from functools import cmp_to_key, lru_cache, reduce
from itertools import (chain, combinations, combinations_with_replacement,
                       permutations, product)

sys.setrecursionlimit(10**6+1)
write = sys.stdout.write
# input = sys.stdin.buffer.readline

MOD = 10**9 + 7


if __name__ == "__main__":
    n = int(input())
    nums = [int(c) for c in input().split()]
    nodes = [(-num, i) for i, num in enumerate(nums)]
    left, right = 0, n-1
    heapq.heapify(nodes)
    ans = 0
    while nodes:
        cur, i = heapq.heappop(nodes)
        if i >= (left + right)//2:
            ans += abs((i-left) * cur)
            left += 1
        else:
            ans += abs((i-right) * cur)
            right -= 1
        print(i, cur, left, right)
    print(ans)





