N=int(input()) A=list(map(int,input().split())) count = 0 while len(A)>=2: if max(A[0]-A[1],A[-1]-A[-2])==A[0]-A[1]: count += A[0]-A[1] A=A[2:] else: count+=A[-1]-A[-2] A=A[:-2] print(count)