from collections import deque N = int(input()) H = list(map(int, input().split())) color = deque([[0, 10**9+1]]) cnt = 0 for i, h in enumerate(H): is_green = bool(i%2) left = 0 idx = 0 while color[0][1] <= h: c = color.popleft() if idx % 2 == 0: cnt += (c[1] - c[0]) * (-1 if is_green else 1) idx += 1 c = color.popleft() if idx % 2 == 0: cnt += (h - c[0]) * (-1 if is_green else 1) else: h = c[1] if color: c = color.popleft() for a in [[h, c[1]], [0, h]]: if a[0] != a[1]: color.appendleft(a) print(cnt)