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())
b = list(map(int, input().split()))

procedure = [-1]*n
tmp_n = n
for m in range(n-1,-1,-1):
	for i in range(tmp_n-1, -1, -1):
		if b[i] == i+1:
			procedure[m] = i+1
			del b[i]
			tmp_n -= 1
			break
	else:
		print(-1)
		exit()

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

Submission status: Accepted

Solution code:
def limited_insertion(N: int, B: list)->list:
    # print(N, B)
    if N == 1:
        return [1] if B[0] == 1 else []
    # 後ろからみていって、インデックス(1-indexed)と
    # その位置の配列(B)の値が等しい最初のペアを探す。
    # なければ失敗
    for i in range(N, 0, -1):
        if i == B[i-1]:
            # 見つけたら、それを除いた配列に関して
            # 再帰的に同じことを行う。
            # print('>>>pair', i, B[i-1])
            rec = limited_insertion(N-1, B[:i-1] + B[i:])
            if len(rec) > 0:
                return rec + [i]
            else:
                return []

    return []


if __name__ == "__main__":
    N = int(input())
    B = [int(s) for s in input().split()]
    A = limited_insertion(N, B)
    if len(A) == 0:
        print(-1)
    else:
        for a in A:
            print(a)

Submission status: Accepted

Solution code:
N = int(input())
b = list(map(int,input().split()))
B = len(b)
ans = []
can_t = 0
for i in range(B):
	if can_t == 0:
		judge = 0
		j = 0
		while judge == 0:
			if len(b)- 1 - j < 0:
				can_t = 1
				judge = 1
			elif b[len(b)-j-1] == len(b)-j:
				judge = 1
				ans.append(len(b)-j)
				del b[len(b)-1-j]
			else:
				j += 1

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

Submission status: Accepted

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

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


if targ == -1:
    print(-1)
else:
    for i in range(len(ans)):
        print(ans[-1*(1+i)])

Submission status: Accepted

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


def solve(b, a):
  #print(f'solve called with, b = {b}, a = {a}')
  if len(b) == 0:
    return a

  ## check if legal
  for bb, ii in zip(b, range(1, len(b)+1)):
    if bb > ii:
      return None

  for bb, ii in zip(b, range(1, len(b)+1)):
    if bb == ii:
      #print(f'found {bb} in {b}[{ii-1}]')
      a_cp = a[:]
      a_cp.append(bb)
      b_cp = b[:]
      b_cp.pop(ii-1)
      ans = solve(b_cp, a_cp)
      if ans is not None:
        return ans

answer = solve(b, [])
if answer == None:
  print(-1)
else:
  for aa in reversed(answer):
    print(aa)

Submission status: Accepted

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

b_cur = b
sol = []
while b_cur:
    for i in reversed(range(len(b_cur))):
        if b_cur[i] == i+1:
            sol.append(b_cur[i])
            b_cur = b_cur[:i] + b_cur[i+1:]
            break
    else:
        sol = False
        break
if sol:    
    for i in reversed(sol):
        print(i)
else:
    print(-1)

Submission status: Accepted

Solution code:
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
import math
import collections

N,= map(int, sys.stdin.readline().rstrip().split(" "))
bs = map(int, sys.stdin.readline().rstrip().split(" "))

countdict = collections.defaultdict(list)
for i, b in enumerate(bs):
    countdict[b].append(i+1)


def find_one():
    for i in range(101, 0, -1):
        if i in countdict:
            for j in range(len(countdict[i])):
                if countdict[i][j] == i:
                    del countdict[i][j]
                    return i

def change_pos(pos):
    for counts in countdict.values():
        for i in range(len(counts)):
            if counts[i] > pos:
                counts[i] -= 1

results = []
while True:
    res = find_one()
    if res is None:
        break
    results.append(res)
    change_pos(res)

rest = sum(len(counts) for counts in countdict.values())
if rest > 0:
    print(-1)
else:
    for i in reversed(results):
        print(i)

exit(0)

Submission status: Accepted

Solution code:
from math import *



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

l=[]

prev=n
new=0
while(prev!=new):
	prev=len(a)

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

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

Submission status: Accepted

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

while len(a) < n and judge != 1:
    c = 0
    for i in range(len(b)):
        if b[i] == i+1:
            c = b[i]
    if c == 0:
        judge = 1
        break
    else:
        a.append(c)
        b.remove(c)

a = a[::-1]
if judge == 1:
    print(-1)
else:
    for i in range(n):
        print(a[i])

Submission status: Accepted

Solution code:
#!/usr/bin/env python
# coding: utf-8


def main():
    n = int(input())
    lb = list(map(int, input().split()))
    ret = []
    for i in range(n):
        for j in range(n-i, 0, -1):
            if lb[j-1] == j:
                ret.append(lb.pop(j-1))
                break
        else:
            print("-1")
            return
    print("\n".join(map(str, ret[::-1])))



if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
ri = lambda: int(input())
rl = lambda: list(map(int,input().split()))
rr = lambda N: [ri() for _ in range(N)]
YN = lambda b: print('YES') if b else print('NO')
yn = lambda b: print('Yes') if b else print('No')
OE = lambda x: print('Odd') if x%2 else print('Even')
INF = 10**18

N=ri()
b=rl()

def ANS():
    ans = []
    for i in range(N):
        rm_i = -1
        for j in range(N-i):
            if b[j] == (j+1):
                rm_i = j
            if j==(N-i-1) and rm_i==-1:
                return -1
        ans.append(b.pop(rm_i))
    return ans
    
res = ANS()
if res==-1:
    print('-1')
else:
    for r in res[::-1]:
        print(r)

Submission status: Accepted

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

flag = True
for i in range(N):
    if i+1 < b[i]:
        flag = False
if not flag:
    print(-1)
else:
    while True:
        if len(b) ==  0:
            break
        m = 0
        for j in range(len(b)):
            if b[j] == j+1:
                m = max(m,j)
        a.append(m+1)
        b.pop(m)

    for i in reversed(range(N)):
        print(a[i])

Submission status: Accepted

Solution code:
from collections import deque
import itertools
import numpy as np
import sys
MAX_INT = int(10e10)
MIN_INT = -MAX_INT
mod = 1000000007
def IL(): return list(map(int,input().split()))
def SL(): return input().split()
def I(): return int(sys.stdin.readline())
def S(): return input()

N = I()
l = IL()

ans = []
while l:
    for i in range(N-1,-1,-1):
        if l[i] == i+1:
            num = l.pop(i)
            ans.append(num)
            N -= 1
            break
    else:
        print(-1)
        exit()

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

Submission status: Accepted

Solution code:
n = input()
l = map(int, raw_input().split())
ans = []
for i in range(n):
	for j in range(len(l)-1, -1, -1):
		if l[j] == j + 1:
			ans.append(j+1)
			l.pop(j)
			break
if len(ans) != n:
	print -1
else:
	print '\n'.join([str(x) for x in list(reversed(ans))])

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import sys
stdin = sys.stdin
 
sys.setrecursionlimit(10**5) 
 
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())

ans = []
exist = True

res = n

while res > 0:
    cur = -1
    for i in range(len(b)):
        if b[i] == i+1:
            cur = i
            
    if cur == -1:
        exist = False
        break
    else:
        b = b[:cur] + b[cur+1:]
        ans.append(cur+1)
        
    res -= 1

if not exist:
    print(-1)
else:
    for ansi in ans[::-1]:
        print(ansi)

Submission status: Accepted

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

while len(b) > 0:
    f = False
    for i in reversed(range(0, len(b))):
        if b[i] == i + 1:
            r.append(b[i])
            del b[i]
            f = True
            break
    if not f:
        print(-1)
        r = []
        break

for i in reversed(range(0, len(r))):
    print(r[i])

Submission status: Accepted

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

Submission status: Accepted

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

for i in range(n, -1, -1):
    for j in range(i - 1, -1, -1):
        if b[j] == j + 1:
            ans.append(j + 1)
            b = b[:j] + b[j + 1:]
            break
    if i < len(b):
        break

if len(ans) == n:
    print("\n".join(map(str, reversed(ans))))
else:
    print(-1)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-

def solve():
    N = int(input())
    B = list(map(int, input().split()))
    res = []
    while B:
        for i in range(len(B)-1,-1,-1):
            if B[i] == i+1:
                res.append(B[i])
                del B[i]
                break
        else:
            if B:
                res = '-1'
                break
    else:
        res = '\n'.join(map(str, res[::-1]))
    return str(res)
if __name__ == '__main__':
    print(solve())

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
A = [int(_) for _ in input().split()]


def dfs(A, X):
    M = len(A)
    if not A:
        print(*X, sep='\n')
        exit()
    for i in range(M):
        if A[i] > i + 1:
            return
    for i in range(M):
        if A[i] == i + 1:
            dfs(A[:i] + A[i + 1:], [i + 1] + X)


dfs(A, [])
print(-1)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**7)
INF = 10 ** 18
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 LF(): return [float(x) 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()
    for i, b in enumerate(B, 1):
        if b > i:
            print(-1)
            return -1
    ans = [0] * N
    nofill = list(range(N))
    for b in B[::-1]:
        ans[nofill[b - 1]] = b
        del nofill[b - 1]
    for a in ans:
        print(a)

    return 0

main()

Submission status: Accepted

Solution code:

N = int(input())
B = list(map(int, input().split()))
ans = []
while len(B)!=0:
    for i in range(len(B)-1, -1, -1):
        if B[i] == i+1:
            ans.append(i+1)
            del B[i]
            break
    else:
        print(-1)
        exit()
print(*ans[::-1], sep="\n")

Submission status: Accepted

Solution code:
import sys
N = int(input())
b = list(map(int, sys.stdin.readline().rstrip().split()))
a = []
# print(b)
cnt = 0
while(cnt < 110):
    for i in range(len(b)-1, -1, -1):
        if b[i] == i+1:
            a.append(i+1)
            b.pop(i)
            break
    cnt += 1
    # print(a)

if len(a) != N:
    print(-1)
else:
    for i in range(N-1, -1, -1):
        print(a[i])

Submission status: Accepted

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

Submission status: Accepted

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

for i in range(N):
	rem = None
	for j in range(N-i):
		if B[j] == j+1:
			rem = j
	if rem == None:
		print("-1")
		exit()

	B.pop(rem)
	ans_rev.append(rem+1)

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

Submission status: Accepted

Solution code:

def read_tokens():
    return input().strip().split(' ')

def read_ints():
    return [int(token) for token in read_tokens()]

def solve(b, n):
    ret = []
    while len(b) > 0:
        # print(b)
        c = None
        for i in reversed(range(len(b))):
            if b[i] > i + 1:
                return [-1]
            if b[i] == i + 1:
                c = b[:i] + b[i+1:]
                ret.append(b[i])
                break
        if c is None:
            return [-1]
        b = c
    return list(reversed(ret))

n, = read_ints()
b = read_ints()

ans = solve(b, n)
print('\n'.join(str(x) for x in ans))

Submission status: Accepted

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

Submission status: Accepted

Solution code:

import sys
#sys.stdin=open("data.txt")
input=sys.stdin.readline
mii=lambda:map(int,input().split())

n=int(input())
a=list(mii())

bad=0
want=[]
for i in range(n):
    if a[i]>i+1:
        bad=1
        break
    want=want[:a[i]-1]+[a[i]]+want[a[i]-1:]

if bad:
    print(-1)
else:
    for i in want:
        print(i)

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!
import sys

N = int(input())

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

# print(B)
ans = []

while B:
    # print(ans)
    flag = True
    for i in reversed(range(len(B))):
        # print(i)
        if B[i] == i+1:
            flag = False
            ans.append(B[i])
            B.pop(i)
            break
            
    if flag:
        print(-1)
        sys.exit()
        
for a in reversed(ans):
    print(a)

Submission status: Accepted

Solution code:
def main():
    n = input()
    bb = map(int, raw_input().split())
    if any(b > i for i, b in enumerate(bb, 1)):
        print -1
        return
    ans = []
    while bb:
        for i in range(len(bb) - 1, -1, -1):
            if bb[i] == i + 1:
                ans.append(i + 1)
                bb.pop(i)
                break
    for a in ans[::-1]:
        print a


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
N = int(input())
B = [int(x) - 1 for x in input().split()]
A = ['0'] * N
for i, b in enumerate(B):
  if b > i:
    print(-1)
    break
  else:
    A = A[:b] + [str(b + 1)] + A[b:]
else:
  print('\n'.join(A[:N]))

Submission status: Accepted

Solution code:
import sys

N = int(input())
b = [int(s) for s in input().split()]

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

for v in reversed(revans):
    print(v)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import array
from bisect import *
from collections import *
import fractions
import heapq
from itertools import *
import math
import random
import re
import string
import sys

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

ans = []
while len(arr) > 0:
    pos = -1
    for i in range(len(arr)):
        if arr[i] == i + 1:
            pos = i
    if pos == -1:
        print(-1)
        sys.exit(0)
    # print("arr, pos = ", arr, pos )
    ans.append(pos + 1)
    arr = arr[:pos] + arr[pos+1:]
ans.reverse()
for a in ans:
    print(a)

Submission status: Accepted

Solution code:
#後ろにある数字を消していく

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

ans = []

while len(B) > 0:
    for i in range(len(B) - 1, -1, -1):
        if B[i] == i + 1:
            ans.append(i + 1)
            del B[i]
            break
    else:
        print(-1)
        exit()

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#!/usr/bin/env pypy3


def in_place_solve(n, bs):
    # sentinel
    bs.insert(0, 0)
    rev_ops = []
    for i in range(1, n + 1)[::-1]:
        for j in range(1, i + 1)[::-1]:
            if bs[j] == j:
                rev_ops.append(j)
                del bs[j]
                break
        else:
            return False, None
    return True, rev_ops[::-1]


def main():
    n = int(input())
    bs = [int(z) for z in input().split()]
    is_possible, ops = in_place_solve(n, bs)
    if not is_possible:
        print(-1)
    else:
        print(*ops, sep="\n")


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
# N = 9
# b = [1, 1, 1, 2, 2, 1, 2, 3, 2]
N = int(input())
b = list(map(int, input().split()))

ans = []
while True:
    #print(b)
    if b==[]:
        print(*ans[::-1], sep="\n")
        break
    for i in reversed(range(len(b))):
        if b[i]==i+1:
            b.remove(b[i])
            ans.append(i+1)
            break
    else:
        print(-1)
        break

Submission status: Accepted

Solution code:
n = int(input())
b = list(map(int, input().split()))
ans = []
ng = False
while len(b) > 0 and not ng:
    ng = True
    for i in range(len(b) - 1, -1, -1):
        if b[i] == i + 1:
            ans.append(i + 1)
            del b[i]
            ng = False
            break
    # print(b, ng)
if ng:
    print(-1)
else:
    print('\n'.join(map(str, reversed(ans))))

Submission status: Accepted

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

b_tmp = list(b)
reverse_ans = []
a = []
while b_tmp:
    for i in reversed(range(len(b_tmp))):
        if b_tmp[i] == i+1:
            reverse_ans.append(i+1)
            b_tmp.pop(i)
            break
    else:
        print(-1)
        exit()

print("\n".join(map(str,reversed(reverse_ans))))

Submission status: Accepted

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

ans = []

for i in range(N,0,-1):
    for j in range(i,0,-1):
        if B[j] == j:
            ans = [j] + ans
            B.pop(j)
            break
    else:
        print(-1)
        break
else:
    for a in ans:
        print(a)

Submission status: Accepted

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

is_incomp = False
ans = []
while True:
  now = None
  for i, b in enumerate(blist):
    if i + 1 == b:
      now = (i+1, b)
  if now is None:
    is_incomp = True
    break
  else:
    ans.append(now[0])
    del blist[now[0]-1]
    if not blist:
      break

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

Submission status: Accepted

Solution code:
n = int(raw_input())
b = map(int, raw_input().split())
ans = []
for i in xrange(n):
    p = 0
    for j, x in enumerate(b):
        if j + 1 == x:
            p = x
    if not p:
        print -1
        quit()
    ans.append(p)
    b.pop(p - 1)
for x in reversed(ans):
    print x

Submission status: Accepted

Solution code:
from sys import exit
N = int(input())
B = list(map(int, input().split()))
ctr = 0
ans = []
for _ in range(N):
    ctr = 0
    for i, b in enumerate(B, 1):
        if i == b:
            ctr = i
    ans.append(ctr)
    if ctr == 0:
        print(-1)
        exit()
    B = B[:ctr-1] + B[ctr:]
print('\n'.join([str(s) for s in ans][::-1]))

Submission status: Accepted

Solution code:
n = int(input())
bbb = list(map(int, input().split()))
for i, b in enumerate(bbb, start=1):
    if i < b:
        print(-1)
        exit()

ans = []
for _ in range(n):
    for i, b in reversed(list(enumerate(bbb, start=1))):
        if i == b:
            ans.append(i)
            bbb.pop(i - 1)
            break
ans.reverse()
print('\n'.join(map(str, ans)))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
ii = lambda: int(input())
mi = lambda: map(int, input().split())
li = lambda: list(mi())

n = ii()
a = li()
ans = []

while 1:
    rem = 0
    for i in range(len(a), 0, -1):
        if a[i - 1] == i:
            ans.append(i)
            del a[i - 1]
            rem = 1
            break
    if not rem: break

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

Submission status: Accepted

Solution code:
N = input()
b = [int(t)-1 for t in raw_input().split()]
ans = []
for i in range(N):
  for j in range(len(b)-1, -1, -1):
    if b[j]==j:
      ans += [j]
      del b[j]
      break
  else:
    print -1
    exit(0)

for a in ans[::-1]:
  print a+1

Submission status: Runtime Error

Solution code:
n = int(input())
b = list(map(int, input().split()))
l = [""]
for i in b:
    l.insert(i - 1, i)
for i in range(n):
    if l[i] > i + 1:
        exit(print(-1))
print(*l, sep="\n")

Submission status: Runtime Error

Solution code:
N=int(input())
B=["*"]+list(map(int,input().split()))

X=[]
for t in range(N):
    p=-1
    for k in range(1,(N-t)+1):
        if B[k]==k:
            p=k

    if p>=1:
        X.append(p)
        del B[p]
    else:
        print(-1)
        sys.exit()

print("\n".join(map(str,X[::-1])))

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees, log2, gcd
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left, insort, insort_left
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7 
#mod = 998244353
from decimal import *
#import numpy as np
#decimal.getcontext().prec = 10

#階乗#
lim = 2*10**5   #必要そうな階乗の限界を入力
fact = [1] * (lim+1)
for n in range(1, lim+1):
	fact[n] = n * fact[n-1] % mod

#階乗の逆元#
fact_inv = [1]*(lim+1)
fact_inv[lim] = pow(fact[lim], mod-2, mod)
for n in range(lim, 0, -1):
	fact_inv[n-1] = n*fact_inv[n]%mod

def C(n, r):
	if n < r or n < 0 or r < 0:
		return 0
	else:
		return (fact[n]*fact_inv[r]%mod)*fact_inv[n-r]%mod

N, K = MAP()

if N == K:
	for i in range(1, N+1):
		print(i if i == 1 else 0)
	exit()

for i in range(1, K+1):
	blue = C(K-1, i-1) #青いボールをi個の塊に分ける
	red = (C(N-K-1, i-2) + 2*C(N-K-1, i-1) + C(N-K-1, i))%mod #赤いボールをi-1、i、i+1個の塊に分ける(i個の場合は2パターン)
	print((blue*red)%mod)

Submission status: Runtime Error

Solution code:
n=int(input())
L=list(map(int,input().split()))
ans=[]
for i in range(n):
    if L[i]>i+1:
        print(-1)
        exit()
def mal(x):
    if len(x)==0:
        return []
    else:
        t=max(x)
        i=x.index(t)
        after=x[:i-t+1]
        before=x[i-t+1:]
        before.remove(t)
        return mal(before)+[t]+mal(after)
ans=mal(L)

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

Submission status: Runtime Error

Solution code:
n = int(input())
b = list(map(int,input().split()))
import sys
sys.setrecursionlimit(10**7)

def solve(p,q):
    list = p[:]
    memo = q[:]
    if len(list)==0:
        for m in range(n):
            print(memo[n-m-1])
        sys.exit()
    else:
        z = 0
        for i in range(len(list)):
            if list[i]==i+1:
                z +=1
                memo.append(list.pop(i))
                solve(list,memo)
        else:
            if z==0:
                return
c = []
solve(b,c)
print(-1)

Submission status: Runtime Error

Solution code:
def solve():
    N = input()
    a = ['']
    for i in input().split():
        a.inser(int(i)-1, i)
    return *a[-1]and[-1]or a

print(solve())

Submission status: Runtime Error

Solution code:
def solve()
    N = input()
    a = ['']
    for i in input().split():
        a.inser(int(i)-1, i)
    return *a[-1]and[-1]or a

print(solve())

Submission status: Runtime Error

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

for i in range(n):
    nn = len(b)
    for j in range(nn-1,-1,-1):
        if b[j] == j+1:
            b = b[:j]+b[j+1:]
            p.append(c[j])
            break
            
if b == []:
    for i in range(n-1,-1,-1):
        print(p[i])
else:
    print(-1)

Submission status: Runtime Error

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

if B[0] != 1:
	print(-1)
	exit()

cnt = 1
for i in range(1, n):
	if B[i] == 1:
		cnt = 1
	
	if B[i] - 1 <= cnt:
		cnt += 1
	else:
		print(-1)
		exit()

res = []
end = n
for i in reversed(range(n)):
	if B[i] == 1:
		A = B[i + 1 : end]
		res.append(1)
		L = []
		for j in range(len(A)):
			if j + 2 != A[j]:
				L.append(A[j])
				A.pop(j)
			
		L.sort()
		for l in L:
			res.append(l)
		
		for a in A:
			res.append(a)
			
		end = i

for i in res:
	print(i)

Submission status: Runtime Error

Solution code:
n=int(input())
b = list(map(int, input().split()))
ans=[]
dl = -1
for num in range(n):
    for i in range(len(b)):
        if b[i] == i+1:
            dl=i
    if dl==-1:
        print(-1)
        exit()
    b.pop(dl)
    ans.append(dl+1)
ans = list(reversed(ans))
for i in ans:
    print(i)

Submission status: Runtime Error

Solution code:
N,*B = map(int, open('0').read().split())
ans = [0]*N
for i in range(N):
  for j in range(len(B)-1,-1,-1):
    if B[j]==j+1:
      ans[N-1-i] = j+1
      B = B[:j]+B[j+1:]
      break
print('\n'.join(map(str,ans)))

Submission status: Runtime Error

Solution code:
n = int(input())
b = [int(i) for i in input().split()]
ans = []
idx = n
if 1 not in b:
    print(-1)
    exit()
for i in reversed(range(n)):
    if b[i] != 1: continue
    else:
        b_ = b[i:idx]
        idx = i
        ma = max(b_)
        for j, bi in enumerate(b_):
            if bi == j + 1:
                for _ in range(b_.count(bi)):
                    ans.append(bi)
            elif bi < j + 1:
                break
            else:
                print(-1)
                exit()
for i in range(n): print(ans[i])

Submission status: Runtime Error

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

B = []
Btmp = [Bin[0]]
for i in range(1, N):
  if Bin[i] == 1:
    B.append(Btmp)
    Btmp = []
  Btmp.append(Bin[i])
else:
  B.append(Btmp)
#print(B)
#print(list(reversed(B)))

def solve(arr):
  ret = []
  while len(arr) > 0:
    before = len(arr)
    for i in reversed(range(len(arr))):
      if arr[i] == i + 1:
        ret.append(arr.pop(i))
        break
    if len(arr) == before:
      return None
  return reversed(ret)

A = []
for Bsub in reversed(B):
  a = solve(Bsub)
  A.extend(a)
#print(A)
for a in A:
  print(a)

Submission status: Runtime Error

Solution code:
N = int(input())
blist = list(map(int, input().split()))
ans = []
for i in range(N):
    for index in range(N-1, -1, -1):
        if blist[index] == index + 1:
            ans.append(blist.pop(index))
            break
    else:
        print(-1)
        break
else:
    for num in ans:
        print(num)

Submission status: Runtime Error

Solution code:
inp=input().split()
for k in range(N):
  x.append(int(inp[k]))
def rec(x,y):
  if len(x)==1:
    for k in range(len(y)):
      print(y[k])
    return y
  OK=False
  for i in range(len(x)-1,0,-1):
    if x[i]==i:
      del x[i]
      y.insert(0,i)
      rec(x,y)
      OK=True
      break
  if OK==False:
    print(-1)
    return -1
rec(x,[])

Submission status: Runtime Error

Solution code:
N=int(input())
r=[0]
inp=input().split()
for k in range(N):
  r.append(int(inp[k]))
def rec(x,y,i,N):
    if len(x)==1:
        for k in range(len(y)):
            print(y[k])
        return(y)
    if i==N+1:
        if len(y)==0:
            print(-1)
            return -1
        else:
            m=y[0]
            del y[0]
            x.insert(m,m)
            rec(x,y,m+1,N)     
    if x[i]==i:
        x.pop(i)
        y.insert(0,i)
        rec(x,y,1,N)
        
    else:
        if x[i] < i:
            rec(x,y,i+1,N)
        else:
            if len(y)>0:
                m=y[0]
                del y[0]
                x.insert(m,m)
                rec(x,y,m+1,N)  
            else:
                print(-1)
                return -1
                
                
            
        
    
rec(r,[],1,N)