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

dp = [0 for i in range(n)]
dp[0] = A[0]
cnt = 0
for i in range(1, len(A)):
	# if(A[i] > dp[i-1]+A[i-1] 
	if(A[i] <= A[i-1]):
		dp[i] = dp[i-1]
		cnt += dp[i] - A[i]
	else:
		dp[i] = A[i]
print(cnt)
