P9810 [CCC 2015 S1] Zero That Out
Description
Given $k$ integers $a_1, a_2 \dots, a_k$, maintain a sequence and perform the following operations in order:
- When $a_i = 0$, delete the last number that was added to the sequence.
- Otherwise, add $a_i$ to the sequence.
For example, when $a = \{1,3,5,4,0,0,7,0,0,6\}$, the operations are as follows.
|$a_i$|Sequence|
|:--:|:--:|
|$1$|$\{1\}$|
|$3$|$\{1,3\}$|
|$5$|$\{1,3,5\}$|
|$4$|$\{1,3,5,4\}$|
|$0$|$\{1,3,5\}$|
|$0$|$\{1,3\}$|
|$7$|$\{1,3,7\}$|
|$0$|$\{1,3\}$|
|$0$|$\{1\}$|
|$6$|$\{1,6\}$|
You need to compute the sum of all numbers in the final sequence.
Input Format
The first line contains an integer $k$.
The next $k$ lines each contain an integer $a_i$.
Output Format
Output one line with one integer, representing the sum of all numbers in the final sequence.
Explanation/Hint
**Constraints:**
$1 \le k \le 10^5$, $0 \le a_i \le 100$.
It is guaranteed that when $a_i = 0$, the sequence is not empty.
Translated by ChatGPT 5