def main():
    from collections import defaultdict
    from operator import itemgetter

    N = int(input())
    *a, = map(int, input().split())

    d = defaultdict(int)
    for i, x in enumerate(sorted(a), 1):
        d[x] = i

    left = []
    right = []
    for i, x in enumerate(a):
        if N - d[x] < i:
            left.append((x, i))
        else:
            right.append((x, i))

    left.sort(key=itemgetter(0), reverse=True)
    right.sort(key=itemgetter(0))

    ans = 0
    for j, (x, i) in enumerate(left + right):
        ans += x * abs(j - i)
    print(ans)


if __name__ == '__main__':
    main()

# import sys
# input = sys.stdin.readline
# 
# sys.setrecursionlimit(10 ** 7)
# 
# (int(x)-1 for x in input().split())
# rstrip()
