n = int(input())
a_list = list(map(int, input().split()))

h_list = [0]*(2*10**5+10)

for i in range(1,n):
    if a_list[i] < a_list[i-1]:
        h_list[i] = a_list[i-1] + h_list[i-1] - a_list[i] 
    elif a_list[i] >= a_list[i-1]:
        h_list[i] = 0
        
print(sum(h_list))