N = int(input()) H = list(map(int, input().split())) bottom = 0 ans = 0 for i in range(N-1, -1, -1): h = H[i] if i%2==0: #水色 ans += max(0, h-bottom) else: pass bottom = max(bottom, h) print(ans) """ 5 7 3 2 4 5 """