m1ndy5's coding blog

[자료구조] 백준 10773번 제로 with python 본문

알고리즘 with python/자료구조

[자료구조] 백준 10773번 제로 with python

정민됴 2023. 3. 22. 10:26

https://www.acmicpc.net/problem/10773

import sys

k = int(sys.stdin.readline().rstrip())
stack = []
s = 0

for i in range(k):
    n = int(sys.stdin.readline().rstrip())

    if n == 0:
        p = stack.pop()
        s -= p
    else:
        stack.append(n)
        s += n

print(s)

0이 들어오면 pop하고 sum에서도 빼주는 것이 키포인트였다.