문제풀이

[PY] 10773 : 제로

pwerty 2025. 3. 24. 13:45

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

장황한 이야기에 비해 알아야 할 것이 별로 없다.
0이라고 나오는 순간 방금 숫자에 대한 내용을 지우면 되니, 0은 곧 pop을 하라는 신호와 같다.

import sys

def empty(idx):
    if myStack[idx] == 0:
        return True
    else:
        return False

def pop(idx):
    if not empty(idx):
        output = myStack[idx]
        myStack[idx] = 0
        return output
    else:
        return -1
    
def push(item, idx):
    myStack[idx] = item
    return 




commandList = int(sys.stdin.readline())
myStack = [0] * 100000
curStackIdx = -1

for i in range(commandList):
    command = int(sys.stdin.readline().strip())

    if command == 0:
        # 여긴 Push N 전용
        popResult = pop(curStackIdx)
        if popResult != -1:
            curStackIdx -= 1
        
    else:
        curStackIdx += 1
        push(command, curStackIdx)
        

total = 0
for i in range(100000):
    total += myStack[i]

print(total)