class segtree:
    def __init__(self, size, identity, combine):
        self.size = size
        self.identity = identity
        self.combine = combine
        self.tree = [identity for i in range(2 * (size + 1))]
    def update(self, i, x):
        i += self.size; self.tree[i] = x; i >>= 1
        while i:
            self.tree[i] = self.combine(self.tree[i << 1], self.tree[(i << 1) | 1])
            i >>= 1
    def query(self, a, b, inclusive=True):
        ans_l, ans_r = self.identity, self.identity
        a += self.size; b += self.size + inclusive
        while a < b:
            if a & 1: 
                ans_l = self.combine(ans_l, self.tree[a]); a += 1
            if b & 1: 
                b -= 1; ans_r = self.combine(self.tree[b], ans_r)
            a >>= 1; b >>= 1
        return self.combine(ans_l, ans_r)
    def point_query(self, i):
        return self.query(i, i)

n = int(input())
a = [(int(x), i) for i, x in enumerate(input().split())]
gain_by_swapping = segtree(n, (0, 0), max)

def gain_by_moving(val, source_index, dest_index):
    return val * abs(source_index - dest_index)

def gain_by_swapping_with_pred(i):
    old = gain_by_moving(*a[i - 1], i - 1) + gain_by_moving(*a[i], i)
    new = gain_by_moving(*a[i - 1], i) + gain_by_moving(*a[i], i - 1)
    return new - old

for i in range(n):
    gain_by_swapping.update(i, (gain_by_swapping_with_pred(i), i))

while True:
    gain, i = gain_by_swapping.query(1, n)
    if gain <= 0:
        break
    a[i], a[i - 1] = a[i - 1], a[i]
    if i - 1 >= 1:
        gain_by_swapping.update(i - 1, (gain_by_swapping_with_pred(i - 1), i - 1))
    gain_by_swapping.update(i, (gain_by_swapping_with_pred(i), i))
    if i + 1 < n:
        gain_by_swapping.update(i + 1, (gain_by_swapping_with_pred(i + 1), i + 1))

print(sum(gain_by_moving(*a[i], i) for i in range(n)))