Editorial for CCC '15 S1 - Zero That Out


Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.

Submitting an official solution before solving the problem yourself is a bannable offence.

Author: quantum

This problem is simply implementing a stack which pushes the input onto the stack when non-zero, and pop the top of the stack when zero.

Complexity: \mathcal{O}(N)

Solution — Python 2

a = []
for i in xrange(input()):
    k = input()
    if k:
        a.append(k)
    else:
        a.pop()
print sum(a)

Comments

There are no comments at the moment.