if __name__ == '__main__': n=int(input()) A=[int(i) for i in input().split()] ans=0 while 1: if len(A)==1: hom+=A[0] break if len(A)==0: break if A[0]>A[-1]: ans+=A[0]-A[1] del A[0:2] else: ans+=A[-1]-A[-2] del A[-2:] print(ans)