N = int(input())

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

import numpy as np

B_list = A_list.copy()

A_list = [np.max(A_list[0:i+1]) for i in range(1, len(A_list)) ]

A_list = [B_list[0]] + A_list

A_array = np.array(A_list)
B_array = np.array(B_list)

print(np.sum(A_array - B_array))   