N = int(input())
A = list(map(int, input().split()))
X = 1 if len(set(A)) != N else 0
A = sorted([(a, i) for i, a in enumerate(A)], key=lambda x: (x[0], x[1]), reverse=True)

ans = 0
left, right = 0, N - 1
for a, i in A:
    left_v = (i - left)
    right_v = (right - i)
    #print(a, i, left, right, left_v, right_v)
    if left_v >= right_v:
        ans += a * left_v
        left += 1

    else:
        ans += a * right_v
        right -= 1

print(ans + X)
