N=int(input()) A=list(map(int,input().split())) count = 0 for i in range(N): 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)