N = int(input())
A = [int(i) for i in input().split()]
B = []
a = 0
b = N
ans = 0
import bisect as bi
while(a < b):
    B = A[a:b]
    x = bi.bisect_left(B,max(B))
    for i in range(x+1,b):
        ans += max(B) - B[i]
    b = x

print(ans)