from collections import deque def main(): N = int(input()) H = tuple(map(int, input().split())) dq = deque() ans = 0 for i, h in enumerate(H): while len(dq) > 0 and dq[0][1] <= h: ans -= dq[0][1] - dq[0][0] dq.popleft() if i%2 == 0: # cyan if len(dq) > 0 and dq[0][0] < h: ans += dq[0][0] dq[0][0] = 0 else: ans += h dq.appendleft([0, h]) else: # green if len(dq) > 0 and dq[0][0] < h: ans -= h - dq[0][0] dq[0][0] = h print(ans) main()