N = int(input()) H = list(map(int, input().split())) stk = [(10 ** 18, 1)] ans = 0 def query(h, cl): global ans while stk: pre_h, pre_cl = stk[-1] if h < pre_h: if pre_cl != cl: stk.append((h, cl)) ans -= h * cl break else: stk.pop() ans += pre_h * pre_cl print(ans) for i, h in enumerate(H): cl = 1 if i % 2 else -1 query(h, cl)