Problem:


Score : 400 points

Problem Statement

Snuke has an empty sequence a.

He will perform N operations on this sequence.

In the i-th operation, he chooses an integer j satisfying 1 \leq j \leq i, and insert j at position j in a (the beginning is position 1).

You are given a sequence b of length N. Determine if it is possible that a is equal to b after N operations. If it is, show one possible sequence of operations that achieves it.

Constraints

All values in input are integers.

1 \leq N \leq 100

1 \leq b_i \leq N

Input

Input is given from Standard Input in the following format:

N
b_1 \dots b_N

Output

If there is no sequence of N operations after which a would be equal to b, print -1.
If there is, print N lines. In the i-th line, the integer chosen in the i-th operation should be printed. If there are multiple solutions, any of them is accepted.

Sample Input 1

3
1 2 1

Sample Output 1

1
1
2

In this sequence of operations, the sequence a changes as follows:

After the first operation: (1)

After the second operation: (1,1)

After the third operation: (1,2,1)

Sample Input 2

2
2 2

Sample Output 2

-1

2 cannot be inserted at the beginning of the sequence, so this is impossible.

Sample Input 3

9
1 1 1 2 2 1 2 3 2

Sample Output 3

1
2
2
3
1
2
2
1
1



Sample input:
3
1 2 1


Sample output:
1
1
2

Submission status: Accepted

Solution code:
n = int(input())
bn = list(map(int,input().split()))
ans = []

while bn:
    for i in range(len(bn))[::-1]:
        if i+1 == bn[i]:
            ans.append(bn[i])
            bn.pop(i)
            break
    else:
        break
if bn:
    print(-1)
else:
    [print(i) for i in ans[::-1]]

Submission status: Accepted

Solution code:
n = int(input())
bn = list(map(int,input().split()))

ans = []

while bn:
  lst = []
  for i in range(len(bn)):
    if i+1 == bn[i]:
      lst.append(i)
  if not lst:
    print(-1)
    exit(0)
  ans.append(bn.pop(lst[-1]))
[print(i) for i in ans[::-1]]

Submission status: Accepted

Solution code:
n = int(input())
bn = list(map(int,input().split()))

ans = []

while len(bn)>0:
  lst = []
  for i in range(len(bn)):
    if i+1 == bn[i]:
      lst.append(i)
  if len(lst) == 0:
    print(-1)
    exit(0)
  ans.append(bn.pop(lst[-1]))
for i in ans[::-1]:
  print(i)

Submission status: Accepted

Solution code:
n = int(input())
b = [int(i) for i in input().split()]
a = []
for i in range(n):
	f = 1
	for j in range(len(b)-1, -1, -1):
		if b[j] == j+1:
			b = b[:j]+b[j+1:]
			a.append(j+1)
			f=0
			break
	if f == 1:
		print(-1)
		exit()
a.reverse()
for i in a:
	print(i)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
ans = []
flg = 0
for i in range(N) :
    for j in range(len(A)-1,-1,-1) :
        k = j+1
        if k == A[j] :
            ans.append(k)
            A = A[:j]+A[j+1:]
            break
    else :
        print(-1)
        flg = 1
        break
if not flg :
    print(*ans[::-1],sep="\n")

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
#AGC32 A

import sys
import math
import bisect
from heapq import heappush, heappop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
mod = 10**9 + 7

ans = []
n = int(input())
b = list(map(int,input().split()))
for i in range(n):
    for j in range(len(b))[::-1]:
        if j+1 == b[j]:
            ans.append(b[j])
            b.remove(b[j])
            break
    else:
        print(-1)
        quit()
ans = ans[::-1]
for i in range(n):
    print(ans[i])

Submission status: Accepted

Solution code:
#!usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S(): return list(sys.stdin.readline())[:-1]
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

#A
def A():
    n = I()
    b = LI()
    ans = []
    for j in range(n):
        for i in range(len(b))[::-1]:
            if b[i] == i+1:
                x = b.pop(i)
                ans.append(x)
                break
    if b:
        print(-1)
    else:
        print(*ans[::-1],sep = "\n")
    return

#B
def B():
    n = I()

    return

#C
def C():
    n = I()

    return

#D
def D():
    n = I()

    return

#E
def E():
    n = I()

    return

#F
def F():
    n = I()

    return

#Solve
if __name__ == "__main__":
    A()

Submission status: Accepted

Solution code:
#後ろから見る
N = int(input())
*B, = map(int,input().split())
ans = []
while B:
    a = max([0] + [j for i,j in enumerate(B,1) if i==j])
    if a==0:break
    B.pop(a-1)
    ans.append(a)
if B:print(-1)
else:print(*ans[::-1],sep="\n")

Submission status: Accepted

Solution code:
import math
import fractions
import copy
import numpy as np
def j(q):
    if q==1: print("Yes")
    elif q == 0:print("No")
    exit(0)
rem = pow(10,9)+7
"""
def ct(x,y):
    if (x>y):print("+")
    elif (x<y): print("-")
    else: print("?")
"""

def ip():
    return int(input())
def iprow():
    return [int(i) for i in input().split()]
def ips():
    return (int(i) for i in input().split())
def printrow(a):
    for i in a:
        print(i)

"""
#decomment when needed
def combinations(n,r):
    if n<r:return 0
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))
def permutations(n,r):
    if n<r:return 0
    return math.factorial(n) // math.factorial(n - r)
def lcm(x, y):
    return (x * y) // fractions.gcd(x, y)
"""
n = ip()
a = iprow()
ans = []
for i in range(n):
    for p in range(len(a)-1,-1,-1):
        if p+1 == a[p]:
            ans.insert(0,p+1)
            a.pop(p)
            break
if len(a):
    print(-1)
else:
    for i in ans:
        print(i)

Submission status: Accepted

Solution code:
import sys
stdin = sys.stdin

sys.setrecursionlimit(10 ** 7)

def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x) - 1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())

n = ni()
b = list(li())

num_ofst = [[bi, (i+1) - bi] for i, bi in enumerate(b)]
ans = []

ok = True
while ok and num_ofst:
    cur = -1
    cur_idx = -1
    for i in range(len(num_ofst)-1, -1, -1):
        if num_ofst[i][1] == 0:
            cur = num_ofst[i][0]
            cur_idx = i
            break

        else:
            num_ofst[i][1] -= 1

    if cur != -1:
        num_ofst = num_ofst[:i] + num_ofst[i+1:]
        ans.append(cur)
    else:
        ok = False

if ok:
    print("\n".join(map(str, ans[::-1])))
else:
    print(-1)

Submission status: Accepted

Solution code:
n = int(input())
b = list(map(int,input().split()))
ans = []

while 1:
    lenth = len(b)
    if len(b) ==0:
        break
    for i in range(lenth):
        if b[lenth-1-i]==lenth-i:
            ans.append(b.pop(lenth-1-i))
            break
        else:
            pass
    if lenth==len(b):
        print(-1)
        exit()

for i in range(n):
    print(ans[n-1-i])

Submission status: Accepted

Solution code:
N=int(input())
b=list(map(int,input().split()))
ans=[]
for k in range(N):
    for i in range(len(b)):
        if b[len(b)-1-i]==len(b)-i:
            x=b.pop(len(b)-1-i)
            ans.insert(0,x)
            break
if len(b)==0:
    for p in range(N):
        print(ans[p])
else:
    print(-1)

Submission status: Accepted

Solution code:
N = int(input())
b = list(map(int, input().split()))
ans = []
while len(b) > 0:
    ok = True
    for i in range(len(b)-1, -1, -1):
        if b[i] == i+1:
            ans.append(b.pop(i))
            break
        else:
            if i == 0:
                ok = False
    if not ok:
        break
if len(ans) != N:
    print(-1)
else:
    for i in range(N-1, -1, -1):
        print(ans[i])

Submission status: Accepted

Solution code:
import sys

input = sys.stdin.readline


def main():
    n = int(input())
    b = [int(i) for i in input().split()]
    l = []
    b_len = len(b)

    for _ in range(b_len):
        for i in range(len(b)-1, -1, -1):
            if b[i] == i+1:
                l.append(b.pop(i))
                break
        else:
            print(-1)
            sys.exit()

    l.reverse()
    for i in l:
        print(i)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
from sys import stdin, exit
from collections import deque

N = int(stdin.readline().rstrip())
b = [int(x) for x in stdin.readline().rstrip().split()]
ans = deque([])

while N > 0:
    index = N - 1
    while index >= 0:
        if b[index] == index + 1:
            ans.appendleft(b[index])
            b1 = b[:index]
            b2 = b[index + 1:]
            b = b1 + b2
            N -= 1
            break
        index -= 1
        if index == -1:
            print(-1)
            exit()

print(*ans)

Submission status: Accepted

Solution code:
import numpy as np
import sys

N = int(input())
b = list(map(int, input().split()))
ans = [0] * N

for i in range(N, 0, -1):
    if i in b[:i-1]:
        print(-1)
        sys.exit()
    for j in range(i, 0, -1):
        if j == b[j - 1]:
            ans[i - 1] = j
            del(b[j - 1])
            break

for i in ans:
    print(i)

Submission status: Accepted

Solution code:
import sys
from collections import deque, defaultdict
import copy
input=sys.stdin.readline
sys.setrecursionlimit(10 ** 9)


N = int(input())
b = list(map(int, input().split()))

tejun = []
for iteration in range(N):
    judge = 0
    for i in range(len(b)-1, -1, -1):
        if b[i] == i+1:
            judge = 1
            tejun.append(copy.deepcopy(b[i]))
            del b[i]
            break
    if judge == 0:
        break

if judge == 0:
    print(-1)
else:
    for i in range(len(tejun)-1, -1, -1):
        print(tejun[i])

Submission status: Accepted

Solution code:
n = int(input())
b = list(map(int, input().split()))

impossible = False
for i in range(n):
    if b[i] > i+1:
        impossible = True
        break

if impossible:
    print("-1")
else:
    pass
    ans = []
    while b:
        # print(b)
        for i in reversed(range(len(b))):
            if b[i] == i+1:
                ans.append(b.pop(i))
                break
    for i in reversed(range(n)):
        print(ans[i])

Submission status: Accepted

Solution code:
n=int(input())
X=[int(i) for i in input().split()]
ans=[]


while True:
    lenX=len(X)
    for i in range(lenX):
        if X[lenX-i-1] == lenX-i:
            del X[lenX-i-1]
            ans.append(lenX-i)
            break
    else:
        break

ans.reverse()

if len(ans)==n:
    for a in ans:
        print(a)
else:
    print(-1)

Submission status: Accepted

Solution code:
import numpy as np

def cal(output_lst, b_lst):
    for i in range(N):
        t = -1
        for j in range(N-i):
            if b_lst[j] == j+1:
                t = j
        if t == -1:
            return False
        output_lst.append(b_lst.pop(t))
    return True


N = int(input())

output_lst = []


b_lst = list(map(int, input().split()))

judge = cal(output_lst, b_lst)
output_lst.reverse()

if judge:
    for low in output_lst:
        print(low)
else:
    print(-1)
            

print()

Submission status: Accepted

Solution code:
N = int(input())
b = list(map(int, input().split()))

ans = []
while not b == []:
  lenb = len(b)
  for i, v in enumerate(reversed(b)):
#    print(i, v, lenb, b)
    if (lenb - i) == v:
      del b[lenb - i - 1]
      ans.append(lenb - i)
      break
  else:
    print(-1)
    exit()

for a in reversed(ans):
  print(a)

Submission status: Accepted

Solution code:
N = int(input())
b = list(map(int, input().split()))

res = []
for _ in range(N):
    flag = False
    for i in range(len(b)-1, -1, -1):
        if b[i] == i+1:
            res.append(b.pop(i))
            flag = True
            break
    if flag == False:
        print(-1)
        exit()

res.reverse()
for i in range(N):
    print(res[i])

Submission status: Accepted

Solution code:
N = int(input())
b_list = list(map(int,input().split()))

b_list = [0] + b_list

result = []
def func():
    global b_list
    global result
#     print(b_list)
    for i in range(len(b_list)-1,-1,-1):
        if i==0:
#             print("karanisuru")
            b_list = []
            break
        if b_list[i] == i:
#             print("icchi")
            result.append(i)
            del b_list[i]
            break

    if len(b_list) > 1:
        func()

func()

if len(b_list) == 0:
    print(-1)
else:
    for i in result[::-1]:
        print(i)

Submission status: Accepted

Solution code:
N = int(input())
b = list(map(int, input().split()))

ans = []
while b:
    
    next = -1
    for i, n in enumerate(b):
        if i + 1 == n:
            next = i
    if next == -1:
        ans = []
        break
    else:
        ans.append(b.pop(next))

if ans:
    ans = ans[::-1]
    for s in ans:
        print(s)
else:
    print(-1)

Submission status: Accepted

Solution code:
n=int(input())
hoge=list(map(int,input().split()))
ans=[]
for i in range(n):
  for j in range(len(hoge)):
    if(hoge[-j-1]==len(hoge)-j):
      ans.append(hoge[-j-1])
      hoge.pop(-j-1)
      break
ans.reverse()
if(len(hoge)==0):
  for i in ans:
    print(i)
else:
  print(-1)

Submission status: Accepted

Solution code:
n=int(input())
b=list(map(int,input().split()))
a=[]
f=0
for _ in range(n):
  for i in range(len(b)-1,-1,-1):
    if b[i]==i+1:
      a.append(b.pop(i))
      break
    if i==0:
      f=1
if f==0:
  for i in range(n-1,-1,-1):
    print(a[i])
else:
  print(-1)

Submission status: Accepted

Solution code:
N = int(input())
*B, = map(int, input().split())
now = max(B)
ops = []
while N > 0:
    temp = (-1, -1)
    for i, b in enumerate(B):
        if b <= N and b == i + 1:
            temp = max(temp, (b, i))
    if temp != (-1, -1):
        _, i = temp
        ops.append(B.pop(i))
    N -= 1
if B: print(-1)
else: print(*list(reversed(ops)), sep='\n')

Submission status: Accepted

Solution code:
N = int(input())
b = [int(i) for i in input().split()]

answer_list = []
for i in range(N-1, -1, -1):
  answer = False
  for index, j in list(reversed(list(enumerate(b)))):
    if index == j-1:
      answer = True
      answer_list.append(j)
      b.pop(index)
      break
  if not answer:
      print(-1)
      exit()

for i in range(len(answer_list)-1, -1, -1):
  print(answer_list[i])

Submission status: Accepted

Solution code:
N = int(input())
b = list(map(int, input().split()))
b = [x for x in reversed(b)]
ans = []
for _ in range(N):
    for i, bb in enumerate(b):
        #print(len(b) - i, bb)
        if (len(b) - i) == bb:
            b.pop(i)
            ans.insert(0, bb)
            break
        elif (len(b) - i) < bb:
            print(-1)
            exit()
for a in ans:
    print(a)

Submission status: Accepted

Solution code:
N = int(input())
B = [int(i) for i in input().split()]
S = []
i = 0
while i < N:
  for j in range(len(B))[::-1]:
    if B[j] == j+1:
      S.append(j+1)
      B = B[:j] + B[j+1:]
      break
  i += 1
if B:
  print(-1)
else:
  for k in range(N)[::-1]:
    print(S[k])

Submission status: Accepted

Solution code:
n=int(input())
l=list(map(int,input().split()))
ans=[]
for i in range(n)[::-1]:
  for j in range(i+1)[::-1]:
    if l[j]==j+1:
      a=l.pop(j)
      ans.append(a)
      break
    if j==0 and l[j]!=1:
      break
if l!=[]:
  print(-1)
else:
  for i in range(n)[::-1]:
    print(ans[i])

Submission status: Accepted

Solution code:
from sys import stdin
N = int(stdin.readline().rstrip())
data = [int(x) for x in stdin.readline().rstrip().split()]

ans = []
for _ in range(N):
    for i in range(len(data) - 1, -1, -1):
        #print(data,i)
        if data[i] == i+1:
            #print(data[i-1],i,data)
            ans.append(data.pop(-(len(data)-i)))
            break
if data:
    print(-1)
else:
    for j in reversed(ans):
        print(j)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline

n=int(input())
b=[int(i) for i in input().split()]

anslist=[]
while len(b) > 0:
    val=110
    for i in range(len(b)-1,-1,-1):
        if i+1==b[i]:
            val=i+1
            break
    anslist.append(val)
    b.pop(i)

if anslist.count(110)>0:
    print(-1)
else:
    for i in (reversed(anslist)):
        print(i)

Submission status: Accepted

Solution code:
from fractions import gcd

def inverse(a):
    return pow(a, mod - 2, mod)


def usearch(x, a):
    lft = 0
    rgt = len(a) + 1
    while rgt - lft > 1:
        mid = (rgt + lft) // 2
        if a[mid] <= x:
            lft = mid
        else:
            rgt = mid
    return lft



def main():
    n=int(input())
    b = list(map(int, input().split()))
    ans = []
    for i in range(n):
        for j in range(len(b)-1, -1, -1):
            if b[j] == j+1:
                ans.append(j+1)
                b = b[:j] + b[j+1:]
                break
        if len(b) + i + 1 != n:
            print(-1)
            return
    ans = ans[::-1]
    for i in range(n):
        print(ans[i])

main()

Submission status: Accepted

Solution code:
n = int(input())
b = [int(i) for i in input().split()]
flag = True
for i in range(n):
    if b[i] > i+1:
        flag = False
        break

if not flag:
    print(-1)
else:
    ans = []
    while len(b) > 0:
        for m in range(max(b), 0, -1):
            if b[m-1] == m:
                ans.append(b[m-1])
                del b[m-1]
                break
    ans = ans[::-1]
    print(*ans, sep="\n")

Submission status: Accepted

Solution code:
n = int(input())
b = list(map(int, input().split()))
ans = []
while n > 0:
    k = n
    for i in range(n):
        if b[i] == i + 1:
            k = i
    if k == n:
        print(-1)
        exit()
    ans.append(b.pop(k))
    n -= 1
for i in ans[::-1]:
    print(i)

Submission status: Accepted

Solution code:
n = int(input())
arr = list(map(int, input().split()))
ans = []
flag = False
for _ in range(n):
  for i in range(len(arr)-1,-1,-1):
    if arr[i] == i+1:
      ans.append(arr[i])
      arr = arr[:i]+arr[i+1:]
      break
  else:
    flag = True
    break
if flag:
  print(-1)
else:
  ans = ans[::-1]
  for i in range(n):
    print(ans[i])

Submission status: Accepted

Solution code:
n=int(input())
arr=list(map(int,input().split()))
ans=[]
flag=False
for _ in range(n):
  for i in range(len(arr)-1,-1,-1):
    if arr[i]==i+1:
      ans.append(arr[i])
      arr=arr[:i]+arr[i+1:]
      break
  else:
    flag=True
    break
if flag==True:
  print(-1)
else:
  ans=ans[::-1]
  for i in range(n):
    print(ans[i])

Submission status: Accepted

Solution code:
from sys import exit, stdin
N = int(stdin.readline().rstrip())
B = [int(_) for _ in stdin.readline().rstrip().split()]
def solve():
    ans = []
    for _ in range(N):
        pivot = -1
        for j in range(len(B)-1, -1, -1):
            if B[j] == j+1:
                pivot = j
                break
        if pivot == -1:
            print(-1)
            exit()
        ans.append(pivot+1)
        B.pop(0+pivot)
    ans.reverse() 
    for v in ans:
        print(v)
solve()

Submission status: Accepted

Solution code:
n = int(input())
b = list(map(int, input().split()))

ans = []
for _ in range(200):
    if len(b) == 0:
        for i in reversed(ans):
            print(i)
        break
    for i, v in enumerate(reversed(b)):
        if len(b)-i == v:
            ans.append(len(b)-i)
            del b[len(b)-i-1]
            break
    else:
        print(-1)
        break

Submission status: Accepted

Solution code:
n=int(input())
b=list(map(int,input().split()))
q=[]
f=True
while(b and f):
    ma=len(b)
    for i in range(ma):
        tar=ma-i
        if b[ma-i-1]==tar:
            q.append(b.pop(ma-i-1))
            break
    else:
        f=False
        break
if f:
    for i in range(n):
        x=q.pop()
        print(x)
else:
    print(-1)

Submission status: Accepted

Solution code:
t = int(input())
a = []
a = list(map(int, input().split()))
answer = []

pos = True
while len(a) > 0:
    for i in range(len(a)-1, -1 ,-1):
        if a[i] == i+1:
            temp = a.pop(i)
            answer.append(temp)
            break
    else:
        pos = False
        break

if pos == True:
    for i in answer[::-1]:
        print(i)
else:
    print(-1)

Submission status: Accepted

Solution code:
size = int(input())
sequence = list(map(int, input().split()))

answer = []
while len(sequence):
  if not any(sequence[i] == i + 1 for i in range(len(sequence))):
    print(-1)
    exit()
  for i in reversed(range(len(sequence))):
    if sequence[i] == i + 1:
      answer.append(sequence[i])
      sequence.pop(i)
      break

for ans in reversed(answer):
  print(ans)

Submission status: Accepted

Solution code:
n = int(input())
b = list(map(int, input().split()))
ans = []

while b:
    for i in range(len(b)-1, -1, -1):
        if b[i] == i+1:
            ans += [b.pop(i)]
            break

    else:
        print(-1)
        exit()

for i in ans[::-1]:
    print(i)

Submission status: Accepted

Solution code:
N = int(input())
n = N
B = list(map(int,input().split()))
B = B[::-1]
A = []
for i in range(N):
    for m,b in enumerate(B):
        if (N-m == b):
            A.append(b)
            B.pop(m)
            N -= 1
            break
if len(A) == n:
    for a in A[::-1]:
        print(a)
else:
    print(-1)

Submission status: Accepted

Solution code:
n=int(input())
b=list(map(int,input().split()))
a=[]
while True:
    for i in range(len(b),0,-1):
        if b[i-1]==i:
            del(b[i-1])
            a+=[i]
            break
    else:
        print(-1)
        exit()
    if len(b)==0:
        break
for i in range(1,n+1):
    print(a[n-i])

Submission status: Accepted

Solution code:
N=int(input())
B=list(map(int,input().split()))
T=True
L=[]
while(T):
    if len(B)==0:
        break
    t=False
    for i in range(len(B)-1,-1,-1):
        if i+1==B[i]:
            L.append(B.pop(i))
            t=True
            break
    if not t:
        T=False
if T:
    for i in L[::-1]:
        print(i)
else:
    print(-1)

Submission status: Accepted

Solution code:
n = int(input())
B = list(map(int, input().split()))
ANS = [-1 for _ in range(n)]

def findlast(str):
    for i in range(len(str))[::-1]:
        if str[i] == i+1:
            return i
    else:
        return -1

ok = True
for i in range(n)[::-1]:
    j = findlast(B)
    if j > -1:
        ANS[i] = B[j]
        B = B[:j] + B[j+1:]
    else:
        ok = False
if ok:
    for i in ANS:
        print(i)
else:
    print(-1)

Submission status: Accepted

Solution code:
import copy
N = int(input())
N2 = copy.copy(N)
bs = list(map(int, input().split()))
ans = []
flag = True
while bs:
    if not flag:
        break
    else:
        for i in range(N):
            if bs[-(i+1)] == N-i:
                if i != 0:
                    bs = bs[:-(i+1)]+bs[-i:]
                else:
                    bs = bs[:-(i+1)]
                ans.append(N-i)
                N -= 1
                break
        if bs:
            if bs[0] != 1:
                flag = False

if flag:
    for j in range(1, N2+1):
        print(ans[-j])
else:
    print(-1)

Submission status: Accepted

Solution code:
n = int(input())
bs = list(map(int, input().split()))

ans = []  
 
def check(bs, pre):
  tmp = pre[:]
  if len(bs) == 0:
    ans.append(tmp)
    return
  cand = []
  for i, b in enumerate(bs):
    if i + 1 == b:
      cand.append(i)
  if len(cand) == 0:
    return
  i = cand[-1]
  check(bs[:i] + bs[i+1:], [i + 1] + tmp)

check(bs, [])

if len(ans):
  for ai in ans[0]:
    print(ai)
else:
  print(-1)

Submission status: Accepted

Solution code:
N = int(input())
b = list(map(int,input().split()))
ans = []
t = 0
while len(b) > 0:
    f = 1
    for k in range(len(b),-1,-1):
        if b[k-1]==k:
            f = 0
            b.pop(k-1)
            ans.append(k)
            break
    if f:
        print(-1)
        exit(0)
print(*ans[::-1], sep = "\n")

Submission status: Accepted

Solution code:
N = int(input())
B = list(map(int, input().split()))

seq = []
while B:
	for n in range(len(B) - 1, -1, -1):
		if (n + 1) == B[n]:
			seq += [B.pop(n)]
			break
	else:
		print(-1)
		break
else:
	for t in seq[::-1]:
		print(t)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
N = int(input())
b_list = list(map(int, input().split()))
out_list = []

for i in range(N):
    ng_checker = 1
    for j in range(N - i):
        if b_list[(N - i) - (j + 1)] == (N - i) - j:
            out_list.insert(0, b_list.pop((N - i) - (j + 1)))
            ng_checker = 0
            break
    if ng_checker == 1:
        print(-1)
        break
if ng_checker != 1:
    for i in out_list:
        print(i)

Submission status: Accepted

Solution code:
n=int(input())
b=list(map(int,input().split()))
s=[]


while b:
    flag=0
    buf=0
    for j in range(len(b)):
        if(b[j]==j+1):
            if(buf<j):
                buf=j
            flag=1         
    if (flag==0):
        print(-1)
        break
    s.append(b.pop(buf))
    #print(b)
if (flag==1):
    for i in range(n):
        print(s[n-i-1],end='\n')

Submission status: Accepted

Solution code:
import copy

N = int(input())
B = [int(i) for i in input().split()]


def dfs(arr, indexes):
    if len(arr) == 0:
        return indexes

    for idx, elm in enumerate(arr):
        if idx+1 < elm:
            return False

    for i in reversed(range(len(arr))):
        if i+1 == arr[i]:
            _indexes = copy.copy(indexes)
            _indexes.append(i)
            res = dfs(arr[:i] + arr[i+1:], _indexes)
            if res:
                return res
    return False


res = dfs(B, [])
if res:
    for i in reversed(res):
        print(i+1)
else:
    print(-1)

Submission status: Accepted

Solution code:
from itertools import*
from math import*
from collections import*
from heapq import*
from bisect import bisect_left,bisect_right
from copy import deepcopy
inf = 10**18
mod = 10**9+7
from functools import reduce
import sys
sys.setrecursionlimit(10**7)

#最後の状態からみれば最後に挿入した数字iはi-1番目に存在することがわかる。
n = int(input())
s = list(input().split(" "))
ans =deque()
while len(s)>0:
    bool = 1
    for i in range(len(s))[::-1]:
        if int(s[i]) == i+1:
            si = s.pop(i)
            ans.append(si)
            bool = 0
            break
    if bool:
        print(-1)
        exit()
while ans:
    print(ans.pop())

Submission status: Accepted

Solution code:
n=int(input())
b=list(map(int,input().split()))
num=[]
cnt=0
while cnt<n:
    flag=0
    i=len(b)-1
    while 0<=i:
        if b[i]==i+1:
            cnt+=1
            num.append(b[i])
            del b[i]
            flag=1
            break
        i-=1
    if flag==0:
        break
if cnt==n:
    for k in range(cnt):
        print(num[n-1-k])
else:
    print(-1)

Submission status: Accepted

Solution code:
import sys
n = int(input())
k =[]
x = list(map(int, input().split()))
x = x[::-1]
for i in range(n):
    for j,p in enumerate(x):
        if p == n-j:
            k.append(p)
            x.pop(j)
            n-=1

            break
    else:
        print(-1)
        sys.exit()
for i in k[::-1]:
    print(i)

Submission status: Accepted

Solution code:
N = int(input())
b = [int(x) for x in input().split()]
li=[]
def dfs(B,li):
    cand = []
    if(B == []):
        return True
    if(max(B)>len(B)):
        return False
    for i in range(1,len(B)+1):
        if(i in B):
            if(B.index(i)+1<i):
                return False
            elif(B.index(i)+1==i):
                cand.append(i)

    for i in cand:
        if(dfs(B[:i-1]+B[i:],li)):
            li.append(i)
            return True

    return False

if(dfs(b,li)):
    for i in li:
        print(i)
else:
    print(-1)

Submission status: Accepted

Solution code:
N = int(input())
blist = list(map(int,input().split()))

isExist = False
olist = []
while (True):
  if len(blist) == 0:
    isExist = True
    break
    
  changed = False
  for i in reversed(range(len(blist))):
    if blist[i] == i+1:
      olist.insert(0,blist.pop(i))
      changed = True
      break
      
  if not changed:
    break
    
if isExist:
  for index in olist:
    print(index)
else:
  print(-1)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
 
ops = []
while len(a):
  if not any(a[i] == i + 1 for i in range(len(a))):
    print(-1)
    exit()
  for i in reversed(range(len(a))):
    if a[i] == i + 1:
      ops.append(a[i])
      a.pop(i)
      break
 
for op in reversed(ops):
  print(op)

Submission status: Accepted

Solution code:
# input data
n = int(input())
b = list(map(int, input().split()))

# solve
ans = []

# necessary condition
def cond(_b):
	hist = [0]*(len(_b)+1)
	
	for i in range(0, len(_b)):
		if _b[i] > i + 1:
			return False
	
	return True

# check if _b can be constructed.
# returns True/False
def dfs(_b, _ans):
	
	if not cond(_b):
		return False
	
	for i in range(0, len(_b)):
		
		if i + 1 == _b[i]:
			_b2 = _b[:]
			_ans2 = _ans[:]
			_ans2.append(_b2[i])
			_b2.pop(i)
			
			if dfs(_b2, _ans2) == True:
				return True
	
	if len(_b) == 0:
		## print answer
		_ans.reverse()
		for i in _ans:
			print(i)
		return True
		
	else:
		return False


if not dfs(b,[]):
	print(-1)

Submission status: Accepted

Solution code:
N = int(input())
b = list(map(int, input().split()))

remove_list = []
while b != []:
    #print(b)
    can_remove = False
    remove_index = 0
    for i, e in reversed(list(enumerate(b))):
        #print(i, e)
        if i + 1 == e:
            remove_list.append(e)
            can_remove = True
            remove_index = i
            break
        
    if not can_remove:
        break
    else:
        del b[remove_index]

if b != []:
    print(-1)
else:
    for e in reversed(remove_list):
        print(e)

Submission status: Accepted

Solution code:
N = int(input())
b = list(map(int, input().split()))
ans = []
for i in range(N):
  for j in range(len(b) - 1, -1, -1):
    if j + 1 == b[j]:
      ans.append(b.pop(j))
      break
if b:
  print(-1)
else:
  for a in reversed(ans):
    print(a)

Submission status: Accepted

Solution code:
a = int(input())
ar = list(map(int,input().split(" ")))
br = []
while True:
    b = len(ar)
    s = "N"
    for i in range(b-1,-1,-1):
        if ar[i] == i+1:
            s = "Y"
            br.append(ar[i])
            del ar[i]
            break
    if s == "N":
        print(-1)
        exit()
    if len(ar) == 0:
        break
for i in range(a-1,-1,-1):
    print(br[i])

Submission status: Accepted

Solution code:
n = int(input())
b = list(map(int, input().split()))
ans = []
for i in range(n, 0, -1):
    for j in range(i, 0, -1):
        if b[j-1] == j:
            ans.append(j)
            b.pop(j-1)
            break
    else:
        print(-1)
        break
else:
    for i in ans[::-1]:
        print(i)