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]:
        Fumidai += max(B_list) - A_high_list[n]
        B_list.append(A_high_list[n])
    elif n == 0:
        B_list.append(A_high_list[0])
        continue  
    else:
        B_list.append(A_high_list[n])
        continue

print(Fumidai)