# coding: utf-8
import math

num = int(input())
total = 0

tall = list(map(int, input().split()))
for i in range(num):
    if i != num-1:
    	num1 = tall[i]
    	num2 = tall[i+1]
    if num1 > num2:
        total += num2 - num1

print(abs(total))