n=int(input()) h=list(map(int,input().split())) stack=[] green=0 blue=0 for i in range(n): while len(stack)>0 and stack[-1][-1]0: if stack[-1][-1]>h[i] and stack[-1][0] ==(i+1)%2: pass else: stack.append(((i+1)%2,h[i])) if (i+1)%2==1: blue+=h[i] else: green+=h[i] else: stack.append(((i+1)%2,h[i])) if (i+1)%2==1: blue+=h[i] else: green+=h[i] print(blue-green)