#P9810. [CCC 2015 S1] Zero That Out

[CCC 2015 S1] Zero That Out

Problem Description

Given kk integers a1,a2,aka_1, a_2 \dots, a_k, maintain a sequence and perform the following operations in order:

  • When ai=0a_i = 0, delete the last number that was added to the sequence.
  • Otherwise, add aia_i to the sequence.

For example, when a={1,3,5,4,0,0,7,0,0,6}a = \{1,3,5,4,0,0,7,0,0,6\}, the operations are as follows.

aia_i Sequence
11 {1}\{1\}
33 {1,3}\{1,3\}
55 {1,3,5}\{1,3,5\}
44 {1,3,5,4}\{1,3,5,4\}
00 {1,3,5}\{1,3,5\}
{1,3}\{1,3\}
77 {1,3,7}\{1,3,7\}
00 {1,3}\{1,3\}
{1}\{1\}
66 {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 kk.

The next kk lines each contain an integer aia_i.

Output Format

Output one line with one integer, representing the sum of all numbers in the final sequence.

4
3
0
4
0
0
10
1
3
5
4
0
0
7
0
0
6
7

Hint

Constraints:

1k1051 \le k \le 10^5, 0ai1000 \le a_i \le 100.

It is guaranteed that when ai=0a_i = 0, the sequence is not empty.

Translated by ChatGPT 5