N = int(input())

A_list = list(map(int, input().split()))

import numpy as np

A_array = np.array(A_list)

A_max_list = list()
for i in range(len(A_array)):
    max_value = np.max(A_array[:i+1])
    A_max_list.append(max_value)
    
A_max_array = np.array(A_max_list)   

print(np.sum(A_max_array - A_array))
