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):
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])

Solution (Python):
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])

Solution (Python):
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))])

Solution (Python):
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])

Solution (Python):
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)

Solution (Python):
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])

Solution (Python):
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)

Solution (Python):
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)

Solution (Python):


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())

Solution (Python):
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)

Solution (Python):
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)

Solution (Python):
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()

Solution (Python):
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)

Solution (Python):

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")

Solution (Python):
import sys
N = int(input())
b = list(map(int, sys.stdin.readline().rstrip().split()))
a = []

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
    

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

Solution (Python):
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)

Solution (Python):
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)

Solution (Python):
import sys

N = int(input())

Bs = [B - 1 for B in map(int, input().split())]

anss = []
for _ in range(N):
    iNow = -1
    for i in reversed(range(len(Bs))):
        if Bs[i] == i:
            iNow = i
            break
    else:
        print(-1)
        sys.exit()

    anss.append(iNow)

    Bs = Bs[:iNow] + Bs[iNow+1:]

print('\n'.join(map(str, [ans+1 for ans in anss[::-1]])))

Solution (Python):

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:
        
        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))

Solution (Python):
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])

Solution (Python):

import sys

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)

Solution (Python):


import sys

N = int(input())

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


ans = []

while B:
    
    flag = True
    for i in reversed(range(len(B))):
        
        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)

Solution (Python):
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()

Solution (Python):
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]))

Solution (Python):
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)

Solution (Python):



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)
    
    ans.append(pos + 1)
    arr = arr[:pos] + arr[pos+1:]
ans.reverse()
for a in ans:
    print(a)

Solution (Python):


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')

Solution (Python):
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)

Solution (Python):



def in_place_solve(n, bs):
    
    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()

Solution (Python):


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

ans = []
while True:
    
    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

Solution (Python):
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
    
if ng:
    print(-1)
else:
    print('\n'.join(map(str, reversed(ans))))

Solution (Python):
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))))

Solution (Python):
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)

Solution (Python):
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')

Solution (Python):
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

Solution (Python):
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]))

Solution (Python):
import sys

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

ok = 1
for i in range(N):
    if B[i] > i+1:
        ok = 0
if not ok:
    print(-1)
    exit(0)

ans = []
S = [0]*N
for i in range(N):
    rest = i
    for j in range(N-1, -1, -1):
        if S[j] == 1:
            rest -= 1
            continue
        if rest+1 == B[j]:
            ans.append("%d\n" % B[j])
            S[j] = 1
            break
sys.stdout.writelines(ans)

Solution (Python):
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)))

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))):
        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")

Solution (Python):
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')

Solution (Python):
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