N = int(input())
A_high_list = list(map(int,input().split()))
Fumidai = 0
s = 0
B_list = []

for n in range(N):
    if n > 0 and A_high_list[n-1] > A_high_list[n]:
        B_list.append(A_high_list[n-1])
        s = max(B_list) - A_high_list[n]
        Fumidai += s
    else:
        continue

print(Fumidai)