#!/usr/local/bin/python3
N = int(input())
A = list(map(int, input().split()))[::-1]

m = A[0]
n = 0
s = 0
ans = 0
for i in range(N):
    s += A[i]
    n += 1
    if A[i] > m:
        # print(f"max!: {m}, {n}, {s}, {ans}")
        m = A[i]
    else:
        # print(f"ans!: {m}, {n}, {s}, {ans}")
        ans += (n-1)*m - (s-A[i])
        n = 1
        s = m = A[i]
    # print(f"result: {m}, {n}, {s}, {ans}")
ans += (n-1)*m - (s-A[i])

print(ans)
