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

Solution (Python):
import sys
N = int(input())
b = list(map(int, input().split()))

ans = []
while len(b) > 0:
  
  poped = False
  for i in range(len(b)-1, -1, -1):
    
    if i+1 == b[i]:
      ans.append(b.pop(i))
      poped = True
      break
  if not poped:
    print(-1)
    sys.exit()

ans.reverse()
for a in ans:
  print(a)

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

ans = []
while True:
    num = 0
    for i in range(len(b)):
        if b[i] == i + 1:
            num = i + 1
    else:
        if num == 0:
            print("-1")
            quit()
        else:
            ans.append(b[num - 1])
            del b[num - 1]
    if b == []:
        break


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

Solution (Python):
import sys

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

check = 0
for i in range(n):
    if b[i] > i + 1:
        check = 1

if check == 1:
    print(-1)
    sys.exit()

ans = [] 

for i in range(n):
    ans.insert(i + 1 - b[i], b[i])

ans.reverse()

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

Solution (Python):
N = int(input())
b = [int(i) for i in input().split()]
a = [0 for i in range(N)]
number = []
for j in range(N):
    for i in range(len(b)-1,-1,-1):
        if(i+1==b[i] and a[i]==0):
            del a[i]
            number.append(b[i])
            del b[i]
            break
if(not a):
    for i in range(N):
        print(number.pop())
if(a):
    print(-1)

Solution (Python):
















a = []

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



N = 100




for i in range(num_len):
    
    
    
    
    
    

    if b[i]-1 <= len(a):
        
        a.insert(b[i]-1, b[i])
        
        flag = True
    else:
        flag = False
        break


if flag:
    for i in a:
        print(i)
else:
    print(-1)

Solution (Python):
_,s=open(0);a=['']
for i in s.split():a.insert(int(i)-1,i)
print(*a[-1]and[-1]or a)

Solution (Python):
n = int(input())
b = list(map(int,input().split()))
op = []
while b:
    cur = 0
    for i in range(len(b)):
        if b[i] == i+1:
             cur = max(cur,i+1)
        else:
            continue
    if cur == 0:
        print(-1)
        exit()
    else:
        op.append(cur)
        b = b[0:cur-1]+b[cur:]
for i in range(n):
    print(op.pop())

Solution (Python):
N = int(input())
barr = list(map(int,input().split()))
cntb = []
for i in range(N):
    cntb.append([barr[i],i])
task = []
ok = True
for n in range(N):
    for i in range(N)[::-1]:
        if cntb[i][1]+1 == cntb[i][0]:
            task.append(cntb[i][0])
            cntb[i][0] = -1
            break
        elif cntb[i][0] > 0 and cntb[i][1] + 1 < cntb[i][0]:
            ok = False
        else:
            cntb[i][1] -= 1

if ok:
    for i in range(N)[::-1]:
        print(task[i])
else:
    print(-1)

Solution (Python):
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
    else:
        print(-1)
        exit()

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

Solution (Python):
N = int(input())
b = list(map(int, input().split()))
a = []
for i in range(N):
    k = N - i
    for j in range(k):
        l = k - j
        if b[l-1] == l:
            a.append(l)
            del b[l-1]
            break
    if len(b) == k:
        break
if len(b) == 0:
    for i in reversed(range(N)):
        print(a[i])
else:
    print(-1)

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

ans = []

while len(b) > 0:
    check = -1
    for i in range(len(b)):
        if b[i] == i+1:
            check=i
    if check==-1:
        print(-1)
        exit()
    ans.append(check+1)
    b = b[:check] + b[check+1:]
for i in range(n):
    print(ans[n-i-1])

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

ans=[]

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

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

Solution (Python):
N = int(input())
B = list(map(int,input().split()))
 
ans = []
while B:
    for i in range(len(B)-1, -1, -1):
        b = B[i]
        if i+1 == b:
            ans.append(i+1)
            break
    else:
        print(-1)
        exit()
    del B[i]
    
for idx in range(0, N):
	tmp = ans.pop(-1)
	print(tmp)

Solution (Python):
N = int(input())
arr = list(map(int, input().split()))
 
ans = []
 
for idx1 in range(1, N+1):
	for idx2 in range(len(arr), 0, -1):
		if arr[idx2-1] == idx2:
			tmp = arr.pop(idx2-1)
			ans.append(tmp)
			break
		elif idx2 == 1:
			print(-1)
			exit()

for idx in range(0, N):
	tmp = ans.pop(-1)
	print(tmp)

Solution (Python):
n = int(input())
nums = list(map(int,input().split()))

operation = []
for i in range(n):
    for j in range(len(nums)):
        if nums[len(nums)-j-1] == len(nums)-j:
            operation.append(nums[len(nums)-j-1])
            del nums[-j-1]
            break

if len(nums) == 0:
    for elem in reversed(operation):
        print(elem)
else:
    print(-1)

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

l = []
for i in range(N-1, -1, -1):
    for j in range(len(B)-1, -1, -1):
        if B[j] == j+1:
            l.append(j+1)
            B.pop(j)
            break

if len(B) == 0:
    for l_i in l[::-1]:
        print(l_i)
else:
    print(-1)

Solution (Python):
n = int(input())
b = list(map(int, input().split()))
ans = []
flag = True
for i in range(n):
  flag = True
  m = len(b)
  for j in range(m):
    if b[m-1-j] == m-j:
      ans.append(m-j)
      del b[m-1-j]
      flag = False
      break
  if flag:
    print(-1)
    break
if not flag:
  for i in range(n):
    print(ans[n-1-i])

Solution (Python):
n = int(input())
b = list(map(int, input().split()))
a = []
count = 0

for _ in range(n):
    for i in reversed(range(len(b))):
        if b[i] == i+1:
            a.append(b.pop(i))
            count += 1
            break

if count != n:
    print(-1)
else:
    for ai in a[::-1]:
        print(ai)

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

ans = []
while B:
    for i in range(len(B)-1, -1, -1):
        b = B[i]
        if i == b-1:
            ans.append(i+1)
            break
    else:
        print(-1)
        exit()
    del B[i]
print(*list(reversed(ans)),sep='\n')

Solution (Python):
import sys

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






steps = []
for i in range(N):
    cands = []
    for j in range(N - i):
        if (j + 1) == b[j]:
            cands.append(j)
    if len(cands) == 0:
        print(-1)
        sys.exit()
    else:
        j = cands[-1]
        steps.append(j + 1)
        b = b[:j] + b[j+1:]
for s in steps[::-1]:
    print(s)

Solution (Python):


n = int(input())
a = list (map(int, input().split()))

a.insert(0,-1)
result = []
for i in range (n, 0, -1) :
    for j in range (i, -1, -1) :

        if a[j] == j :
            result.append(j)

            a = a[:j] + a[j+1:]
            break
        if j == 0 :
            result = [-1]
            break

result.reverse()
for ans in result :
    print (ans)

Solution (Python):
input()
a=[0]
for i in input().split():
  a.insert(int(i)-1,i)
print(*[-1]if a.pop()else a,sep='\n')

Solution (Python):
N = int(input())
prob = list(map(int, input().split()))
ans = []
for i in reversed(range(N)):
  boo = False
  for j in reversed(range(i + 1)):
    if prob[j] == j + 1:
      boo = True
      ans.append(j + 1)
      del prob[j]
      break
  if not boo:
    break
if len(ans) == N:
  for x in reversed(ans):
    print(x)
else:
  print(-1)

Solution (Python):
N = int(input())
b = list(map(int,input().split(" ")))
used = [0] * N
n = N
ans = []
for i in range(N):
    index = n
    flg = True
    for j in range(N-1,-1,-1):
        if used[j] == 0:
            if b[j] == index:
                used[j] = 1
                ans.append(b[j])
                flg = False
                break
            else:
                index -= 1
    if flg:
        print("-1")
        exit(0)
    n -= 1


print(*ans[::-1])

Solution (Python):
import sys
N=int(input())
L=[int(i)-1 for i in input().split()]
M=[]
while True:
    flag=0
    if len(L)==0:
        for m in M[::-1]:
            print(m+1)
        sys.exit()
    for i in range(len(L)-1,-1,-1):
        if L[i]==i:
            s = L.pop(i)
            M.append(s)
            flag=1
            break
    if flag==0 and len(L)!=0:
        print(-1)
        sys.exit()

Solution (Python):
input()
nums  = list(map(int, input().split()))

def low(lis, n):
    return [
        l for l in lis if l <= n
    ]

def rev_idx(lis, n):
    return [
        i for i, e in enumerate(lis[::-1]) if e == n
    ]


try:
    sort = sorted(list(set(nums)))[1:]
    order = low(nums, 1)
    order[0]
    for i, n in enumerate(sort):
        lows = low(nums, n)
        for index in rev_idx(lows, n):
            if (n + index - 1) > len(order):
                raise Exception
            order.insert(n + index - 1, n)
    for o in order:
        print(o)
except:
    print(-1)

Solution (Python):
import numpy as np

def main():
    n = int(input())
    a = []
    b = list(map(int, input().rstrip().split(" ")))

    for i in range(n):
        ind = -1
        for j in range(len(b)):
            if b[j] == j + 1:
                if j + 1 == len(b):
                    ind = j
                elif b[j+1] != j + 2:
                    ind = j
        if ind >= 0:
            a.append(b.pop(ind))

    a.reverse()
    if len(a) == n:
        for v in a:
            print(v)
    else:
        print(-1)



if __name__ == '__main__':
   main()

Solution (Python):
from sys import stdin, stdout
from decimal import Decimal
from random import randint
PEPS = 1e-10
INF = float('inf')


def sqr(a):
    return a * a


def dist(a, b = (0, 0)):
    return sqrt(sqr(a[0] - b[0]) + sqr(a[1] - b[1]))


def s_dist(a, b = (0, 0)):
    return (sqr(a[0] - b[0]) + sqr(a[1] - b[1]))


def dist3(a, b = (0, 0, 0)):
    return sqrt(sqr(a[0] - b[0]) + sqr(a[1] - b[1]) + sqr(a[2] - b[2]))


def s_dist3(a, b = (0, 0, 0)):
    return (sqr(a[0] - b[0]) + sqr(a[1] - b[1]) + sqr(a[2] - b[2]))


def trans(p, al):
    return (p[0] * cos(al) + p[1] * sin(al), - p[0] * sin(al) + p[1] * cos(al))


def get_angle(a, b, c):
    cos_al = (sqr(a) + sqr(b) - sqr(c)) / (2 * a * b)
    return acos(cos_al)


def write_float(ans):
    stdout.write(str('%.13f' % ans) + '\n')


def get_hight(pt1, pt2, pt3):
    a = (pt2[0] - pt1[0], pt2[1] - pt1[1])
    b = (pt3[0] - pt1[0], pt3[1] - pt1[1])
    return abs(a[0] * b[1] - a[1] * b[0]) / dist(pt1, pt2)


def get_square(pt1, pt2, pt3):
    a = (pt2[0] - pt1[0], pt2[1] - pt1[1])
    b = (pt3[0] - pt2[0], pt3[1] - pt2[1])
    return (a[0] * b[-1] - a[1] * b[0]) / 2


def sign(v):
    if v > 0:
        return 1
    elif v < 0:
        return -1
    else:
        return 0


def inn(pt, triangle):
    signs = set()
    signs.add(sign(get_square(triangle[0], triangle[1], pt)))
    signs.add(sign(get_square(triangle[1], triangle[2], pt)))
    signs.add(sign(get_square(triangle[2], triangle[0], pt)))
    return len(signs) == 1


def inter_in(a, b):
    return dist((a[0], a[1]), (b[0], b[1])) < a[-1] + b[-1]


def inter_in3(a, b):
    return dist3(a[:-1], b[:-1]) < a[-1] + b[-1]


def shift(what, wth):
    return (what[0] - wth[0], what[1] - wth[1])


n = int(stdin.readline())
vl = list(map(int, stdin.readline().split()))
ans = []

while len(vl):
    for i in range(len(vl) - 1, -1, -1):
        if vl[i] == i + 1:
            ans.append(i + 1)
            vl = vl[:i] + vl[i + 1:]
            break
    else:
        stdout.write('-1\n')
        break


if len(ans) == n:
    ans = ans[::-1]
    stdout.write('\n'.join(list(map(str, ans))))

Solution (Python):
N = int(input())
b = [int(num) for num in input().split()]
queue = [[b,[]]]

def bfs(queue):
    c,prev = queue.pop(0)
    cnt = len(c)-1
    while cnt >= 0:
        if c[cnt] == cnt+1:
            del c[cnt]
            prev = [cnt+1] + prev
            queue.append([c,prev])
            break
        cnt -= 1
    return queue

ans = -1
while queue != []:
    if queue[-1][0] == []:
        ans = queue[-1]
        break
    queue = bfs(queue)
        
if ans == -1:
    print(ans)
else:
    for num in ans[1]:
        print(num)

Solution (Python):
N=int(input())
b=[int(i) for i in input().split()]
a=[]
try:
    while len(b)>0:
        a2=max([j for i,j in enumerate(b) if b[i]==i+1])
        a.insert(0,a2)
        x=b.pop(a2-1)
    for n in a:
        print(n)
except:
    print(-1)

Solution (Python):
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
mod = 10**9+7
dd = [(-1,0),(0,1),(1,0),(0,-1)]
ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)


def main():
    n = I()
    a = LI()

    r = []
    while a:
        l = len(a)
        nf = False
        for i in range(l-1,-1,-1):
            if a[i] == i+1:
                r.append(i+1)
                a = a[:i] + a[i+1:]
                nf = True
                break
        if not nf:
            return -1

    return '\n'.join(map(str, r[::-1]))


print(main())

Solution (Python):
N=int(input())
b=[int(i) for i in input().split()]
ans=[]
M=N
while(M>0):
    j=-1
    for i in range(M)[::-1]:
        if b[i]==i+1:
            j=i
            break
    if j==-1:
        print(-1)
        exit()
    a=[]
    for i in range(M):
        if j!=i:
            a.append(b[i])
    ans.append(b[j])
    b=[i for i in a]
    M=len(b)
for line in ans[::-1]:
    print(line)

Solution (Python):
import sys
sys.setrecursionlimit(20000000)
input = sys.stdin.readline
n = int(input())
a = list(map(int,input().split()))
for i in range(n):
    if i+1 < a[i]:
        print(-1)
        exit()
 
def dfs(x,ans,so):
        if x == n:
                if ans == a:
                        so = so[::-1]
                        for i in so:
                                print(i)
                        exit()
                return 0
        for i in range(len(ans)-1,-1,-1):
            if ans[i] == i+1:
                ans.pop(i)
                so.append(i+1)
                dfs(x+1,ans,so)
                ans.insert(i,i+1)
                so.pop()
        return -1
print(dfs(0,a,[]))

Solution (Python):
n=int(input())
m=list(map(int,input().split()))
ans=[]
for i in range(n):
  for j in range(n-1-i,-1,-1):
    if not m[j]==j+1:
      continue
    
    ans.append(j+1)
    m=m[:j]+m[j+1:]
    break
if not len(m)==0:
  print(-1)
else:
  for i in range(n):
    print(ans[n-1-i])

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

Solution (Python):
import sys

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

s=s+[10**6]
ans=[0]*N


for i in range (N):
    m=-1
    for j in range (N-i):
        if s[j]==j+1:
            m=j
    if m<0:
        print(m)
        sys.exit()
    
    ans[N-i-1]=m+1
    
    for j in range (m,N):
        s[j]=s[j+1]
        
    
for i in range(N): print(ans[i])

Solution (Python):
def main():
    N = int(input())
    B = list(map(int, input().split()))
    ans = [0] * N
    for i in range(N):
        idx = 0
        for j in range(N-i):
            if B[j] == j+1:
                idx = j+1
        if idx == 0:
            print(-1)
            return
        ans[i] = idx
        B = B[:idx-1] + B[idx:]
    for a in ans[::-1]:
        print(a)


if __name__ == "__main__":
    main()

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

ans=[]
while b:
    for i in reversed(range(len(b))):
        if i==b[i]-1:
            b.remove(b[i])
            ans.append(i)
            break
        if i==0:
            print(-1)
            exit()
ans.reverse()
for i in ans:
    print(i+1)

Solution (Python):
N = int(input())
b = list(map(int,input().split()))
answer = []

def check(series):
    L = len(series)
    if L == 0:
        return(True)
    matching = [False for _ in range(L)]
    maxMatchIndex = -1
    for i in range(L):
        num = series[i]
        match = num == i+1
        if match:
            matching[i] = True
            maxMatchIndex = i
    if maxMatchIndex == -1:
        return(False)
    answer.append(maxMatchIndex + 1)
    del series[maxMatchIndex]
    return(check(series))

res = check(b)
if res:
    answer.reverse()
    for ans in answer:
        print(ans)
else:
    print(-1)

Solution (Python):
import sys
def input():
	return sys.stdin.readline()[:-1]

n = int(input())
a = list(map(int, input().split()))
ans = []
for i in range(n):
	for j in range(n-i-1, -1, -1):
		if a[j] == j+1:
			ans.append(j+1)
			a.pop(j)
			break
	else:
		print(-1)
		sys.exit()
print(*ans[::-1], sep="\n")

Solution (Python):
n = int(input())
b = [int(i) for i in input().split()]
a = []
while b:
    p = 0
    for i in range(len(b)-1,-1,-1):
        if b[i]==i+1:
            p = b.pop(i)
            break
    if p: a.append(p)
    else: break
if p:
    for i in a[::-1]: print(i)
else: print(-1)

Solution (Python):
import sys

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

max_b = max(b)
ans = []
flag = 0

while len(b) != 0:
    for i in range(min(max_b-1,len(b)-1),-1,-1):
        if b[i] == i+1:
            flag = 1
            b.pop(i)
            ans.append(i+1)
            break
    if flag == 0:
        print(-1)
        sys.exit()
    flag = 0

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

Solution (Python):


from collections import deque
import itertools as it
import sys
import math

sys.setrecursionlimit(1000000)

INF = 10 ** 18
MOD = 10 ** 9 + 7

N = input()
b = map(int, raw_input().split())
ans = []
for _ in range(N):
    pos = -1
    for i in range(len(b)):
        if b[i] == i + 1:
            pos = i
    if pos < 0:
        print -1
        exit()
    ans += [b[pos]]
    b = b[:pos] + b[pos + 1:]
for p in ans[::-1]:
    print p

Solution (Python):
N = int(input())

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

ans = []
for k in range(N):
    flag = 1
    for i in range(len(B)):
        j = len(B)-i-1
        if j+1 == B[j]:
            ans.insert(0,B[j])
            B.pop(j)
            flag = 0
            break
    if flag == 1:
        ans = -1
        break

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

Solution (Python):
n=int(input())
b=[int(i) for i in input().split()]
ans=[]
result=True
while len(b):
    for i,j in enumerate(b[::-1]):
        if len(b)-i==j:
            ans.append(j)
            b.pop(len(b)-(i+1))
            break
    else:
        result=False
        break
if result:
    for i in ans[::-1]:
        print(i)
else:
    print(-1)

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

ans = [0]

for b in B:
    ans.insert(b-1, b)

if sum(ans[:N]) == sum(B):
    for i in range(N):
        print(ans[i])
else:
    print(-1)

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

L = []
flag = 1
while len(B):
    for i,b in enumerate(B[::-1]):
        if len(B)-i == b:
            L.append(b)
            B.pop(len(B)-i-1)
            break
    else:
        flag = 0
        break

if flag:
    for s in L[::-1]:
        print(s)
else:
    print(-1)

Solution (Python):
from sys import stdin

def main():
    N = int(input())
    B = list(map(int, input().split()))

    ans = []
    for n in range(N, 0, -1):
        for i in range(N-1, -1, -1):
            if not B[i]:
                continue
            if B[i] == n:
                ans.append(n)
                B[i] = 0
                break
            n -= 1
        else:
            print(-1)
            return
    print(*ans[::-1], sep='\n')

input = lambda: stdin.readline()
main()

Solution (Python):



import sys
import math



def solve(b):
  ret = []
  while len(b) > 0:
    dct = [] 
    for i in range(len(b)):
      if b[i] == i+1:
        dct.append(i)
    if len(dct) == 0:
      return [-1]
    else:
      ret.append(dct[-1]+1)
      b.pop(dct[-1])
  return ret[::-1]


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

for i in solve(B):
  print(i)

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


ans = []
while B:
    itr = list(enumerate(B, start=1))[::-1]
    for i, b in itr:
        if i == b:
            del B[i - 1]
            ans.append(b)
            break
    else:
        print(-1)
        exit()

print(*ans[::-1], sep='\n')

Solution (Python):
n = int(input())
b = list(map(int,input().split()))[::-1]
a = []
while b:
    for i,v in enumerate(b):
        if v == len(b)-i:
            a.append(v)
            b.pop(i)
            break
    else:
        print(-1)
        exit()

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

Solution (Python):
N = int(input())
b = map(int,input().split())
b_ = [int(s) for s in b]
ans = []
for i in range(N):
    for j in range(len(b_),0,-1):
        if j == b_[j-1]:
            del b_[j-1]
            ans.append(j)
            break
if len(ans) == N:
    for i in reversed(ans):
        print(i)
else:
    print(-1)

Solution (Python):
n = int(input())
a = list(map(int,input().split()))
acts = []
while True:
    for i in range(len(a)):
        idx = len(a)-i-1
        if a[idx] == idx+1:
            a.pop(idx)
            acts.append(idx+1)
            break
    else:
        print('-1')
        break
    if len(a) == 0:
        acts.reverse()
        print('\n'.join(map(str,acts)))
        break

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

for i in range(N):
	if b[i] > i+1:
		print(-1)
		exit()

a = []
for i in range(N):
	for j in range(len(b)-1, -1, -1):
		if b[j] == j+1:
			a += [b.pop(j)]
			break
a.reverse()
			
if len(b) != 0:
	print(-1)
else:
	for ai in a:
		print(ai)

Solution (Python):
n = int(input())
bb = list(map(int, input().split()))
ans = [0]*n
for k in range(n,0,-1):
  for i,num in enumerate(bb[::-1]):
    if num+i == k:
      ans[k-1] = bb.pop(-i-1)
      break
  else:
    print(-1)
    exit()
for i in ans:
  print(i)

Solution (Python):
def main():
    N = int(input())
    B = [int(_) for _ in input().split()]

    
    outs = []

    for i in range(N):
        for k in range(N-1-i,-1,-1):
            if B[k] == k+1:
                del B[k]
                outs.insert(0,k+1)
                break
        else:
            print(-1)
            return

    for out in outs:
        print(out)

if __name__ == '__main__':
    main()

Solution (Python):
def a_limited_insertion(N, B):
    """
    空の数列Aに対し、
    ・i回目の操作では i<=j<=iを満たす整数jを選び、Aの先頭からj番目にjを挿入する
    という操作をN回行う。その後A, Bが一致することがあるか判定し、あるなら
    それを達成する操作手順を1つ示せ。
    """

    
    
    
    
    
    
    
    
    ans = []
    while B:
        for k in range(len(B) - 1, -1, -1):
            if B[k] == k + 1:
                ans.append(k + 1)
                B.pop(k)
                break
        else:
            return -1
    
    return ' '.join(map(str, reversed(ans)))

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

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


ans=[]
for i in range(N):
    for j in range(len(b)):
        if b[len(b)-1-j]==len(b)-j:
            ans.append(len(b)-j)
            del b[len(b)-1-j]
            
            break
if b!=[]:
    print(-1)
else:
    ans=ans[::-1]
    for i in range(N):
        print(ans[i])

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

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

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

Solution (Python):
N = int(input())
b_list = list(map(int, input().split()))
rev_b = reversed(b_list)

ans = []

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

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

b.reverse()
ans=[]
disable=False

for i in range(N):
    for j in range(len(b)):
        if b[j]==len(b)-j:
            ans.append(b.pop(j))
            break
        if j==len(b)-1:
            disable=True
            ans=[-1]
            break
    if disable:
        break

ans.reverse()
for a in ans:
    print(a)

Solution (Python):
n = int(input())

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

ans = []

for i in list(reversed(range(n))):
    for j in list(reversed(range(i+1))):
        if (b[j] == j+1):
            ans.append(j+1)
            del b[j]
            break
        if j == 0:
            print("-1")
            exit()

ans = list(reversed(ans))

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

Solution (Python):
N = int(input())
B = list(map(int, input().split()))
A_rev = []
import sys
while(len(B)):
    max = -1
    for i in range(len(B)):
        if B[i] == i+1:
            max = i
    if max == -1:
        print(-1)
        sys.exit()
    A_rev.append(B[max])
    B.pop(max)
for a in A_rev[::-1]:
    print(a)

Solution (Python):
import sys
import re

N = input()
b = [int(item) for item in re.split(r"\s", input())]

out_list = []

while len(b) > 0:
    for i in range(len(b)):
        index = len(b) - 1 - i
        if b[index] == index + 1:
            out_list.append(b.pop(index))
            break
    else:
        
        print(-1)
        sys.exit()

else:
    for item in out_list[::-1]:
        print(item)

Solution (Python):


import sys

INF = float('inf')
MOD = 10 ** 9 + 7

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x) - 1 for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def II(): return int(sys.stdin.readline())
def SI(): return input()


def main():
    n = II()
    B = LI()

    res = []
    T = B[:]
    for _ in range(n):
        for i in range(len(T))[::-1]:
            if i + 1 == T[i]:
                res.append(T[i])
                T = T[:i] + T[i+1:]
                break
        else:
            print(-1)
            return 0

    print(*[i for i in reversed(res)], sep='\n')
    return 1


main()

Solution (Python):
n = int(input())
b = [int(_) for _ in input().split(' ')]

answer = []
p = True
for i in range(n, 0, -1):
    for j in range(i, 0, -1):
        if b[j - 1] == j:
            answer.append(j)
            b.pop(j - 1)
            break
        elif b[j - 1] > j:
            p = False

if p:
    answer.reverse()
    for i in answer:
        print(i)
else:
    print(-1)

Solution (Python):
import sys
input = sys.stdin.readline

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

from queue import Queue
q = Queue()
ans = Queue()

import numpy as np
n = np.array(n)


def bfs(b):
    q.put(b)
    ans.put([])

    while q.empty() == False:
        p = q.get()
        a = ans.get()

        if p == []:
            break

        if p[0] != 1:
            continue

        m = len(p)

        for i in range(m-1, -1, -1):
            if p[i] == i+1:
                q.put(p[:i]+p[i+1:])
                ans.put(a+[i+1])
                break


    if p == []:
        a = a[::-1]
        for aa in a:
            print(aa)
    else:
        print(-1)


bfs(b)

Solution (Python):

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



def f(c):
    j = max([j for j, x in enumerate(c, 1) if j == x], default=0)
    return (
        f(c[:(j-1)] + c[j:]) + [j] if j and len(c) >= 2 else
        [j] if j and len(c) == 1 else
        []
    )


res = f(b)


ans = '\n'.join(map(str, res)) if len(res) == N else -1
print(ans)

Solution (Python):
n = int(input())
b = list(map(int,input().split()))
l = []
while not b==[]:
    k=0
    for i in range(len(b)-1,-1,-1):
        if i+1==b[i]:
            l.append(b[i])
            del b[i]
            k=1
            break
    if not k:
        print(-1)
        exit()
l.reverse()
for l_ in l:
    print(l_)

Solution (Python):
n = int(input())

l = input().split()

res = []

for i in range(n):
    
    
    for j in range(len(l)-1,-1,-1):
        if(l[j] == str(j+1)):
            res.append(l[j])
            del(l[j])
            break
    


            

if(l != []):
    print(-1)
else:
    print("\n".join(res[::-1]))

Solution (Python):
import math, string, itertools, fractions, heapq, collections, re,  array, bisect, sys, random, time, copy, functools
sys.setrecursionlimit(10**7)
inf = 10 ** 20
eps = 1.0 / 10**10
mod = 10**9+7
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]
ddn = [(-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1)]
def LI(): return [int(x) for x in input().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(input())
def pf(s): return print(s, flush=True)


N = I()
b = LI()

result = []
while True:
    
    for i in range(1, len(b)+1):
        
        if b[-i] == len(b)-i+1:
            
            result.append(b[-i])
            b.pop(-i)
            break
    else:
        if not b:
            break
        print(-1)
        exit()
for i in result[::-1]:
    print(i)

Solution (Python):
N = int(input())
B = [int(x) - 1 for x in input().split()]

ans = []
while B:
    popped = -1
    for i in reversed(range(len(B))):
        if B[i] == i:
            ans.append(i + 1)
            popped = B.pop(i)
            break
    if popped == -1:
        print(-1)
        quit()
print(*ans[::-1], sep="\n")