n = int(input()) H = list(map(int, input().split())) now = 0 inf = float("INF") stuck = [(inf, 1)] for i in range(n): h = H[i] while stuck[-1][0] <= h: x, t = stuck.pop() if t: now += x else: now -= x if i % 2: if not stuck[-1][1]: now -= h stuck.append((h, 1)) else: if stuck[-1][1]: now += h stuck.append((h, 0)) print(now)