n=int(input()) a=[int(j) for j in input().split()] from collections import deque q=deque(a) ans=0 for i in range(n): if q[0]-q[1]>q[-1]-q[-2]: ans+=q.popleft() ans-=q.popleft() else: ans+=q.pop() ans-=q.pop() print(ans)