num = int(input()) array = [] for _ in range(num): money = int(input()) if money == 0: array.pop() else: array.append(money) print(sum(array))