import sys
input = sys.stdin.readline
N = int(input())
now = list(map(int, input().split()))
stack = []
next = 1
while now or stack:
if now and now[0] == next:
now.pop(0)
next += 1
elif stack and stack[-1] == next:
stack.pop()
next += 1
elif next in now:
for _ in range(now.index(next)):
stack.append(now.pop(0))
else:
break
if stack:
print("Sad")
else:
print("Nice")
import sys
input = sys.stdin.readline
N = int(input())
now = list(map(int, input().split()))
stack = []
next = 1
for i in now:
if i == next: # 해당 번호표이면 간식 받는 곳으로 이동
next += 1
else: # 해당 번호표가 아니라면 스택으로 이동
stack.append(i)
# 스택의 맨 위에 해당 번호표가 존재하면 계속 간식 받는 곳으로 이동
while stack and stack[-1] == next:
stack.pop()
next += 1
if stack:
print("Sad")
else:
print("Nice")
손으로 막 적어가며 굉장히 오래 걸린 문제이다.
이동시키는 방식을 정리하는 게 어려워서 다른 사람 코드도 좀 보았지만 온전히 내 힘으로 풀긴 했다.
구글링한 코드는 내 스스로가 떠올리지는 못할 것 같다....
역대 가장 어려웠던 문제였다..

아래가 내가 푼 코드이다.