https://www.acmicpc.net/problem/10773
from sys import stdin
input = stdin.readline
data_list = []
for _ in range(int(input())):
num = int(input())
if num == 0:
data_list.pop()
else:
data_list.append(num)
# 출력
print(sum(data_list))