먼저 위 문제를 이해해보자.
# 24511
import sys
from collections import deque
n = int(input())
a = list(map(int, sys.stdin.readline().split()))
b = list(map(int, sys.stdin.readline().split()))
m = int(input())
c = list(map(int, sys.stdin.readline().split()))
lst = deque()
for i in range(n):
if a[i] == 0:
lst.append(b[i])
for j in range(m):
lst.appendleft(c[j])
for _ in range(m):
print(lst.pop(), end=' ')
위 문제는 문제 자체를 이해하는 데 오랜 시간이 걸렸다.
stack에서는 새롭게 append한 원소가 그대로 pop이 되고, queue에서는 가장 처음에 append된 원소가 pop된다는 것을 이용하여 코드를 작성하였다.