N = int(input()) Y = list(map(int,input().split())) import sys if N == 1: print(0) exit() A = [Y[i+1] - Y[i] for i in range(N-1)] ans = 0 if A[0] < 0: ans += -A[0] A[0] = 0 for i in range(1,len(A)): if A[i] >= 0: continue if A[i-1] > 0: if A[i] + A[i-1] >= 0: ans += -A[i] A[i] = 0 continue else: ans += A[i-1] A[i] += A[i-1] if i == len(A)-1: ans += -A[i] else: ans += -A[i] A[i+1] += A[i] A[i] = 0 print(ans)