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()))
c = 0
t = []
for i in range(n):
        if i + 1 < b[i]:
                print(-1)
                c = 1
                break

if c == 0:
        while b != []:
                s = []
                for  i in range(len(b)):
                        if i + 1 == b[i]:
                                s.append(b[i])
                t.insert(0,max(s))
                b.pop(max(s)-1)
        for i in range(len(t)):
                print(t[i])

Submission status: Accepted

Solution code:
import sys,heapq
from collections import deque,defaultdict
printn = lambda x: sys.stdout.write(x)
inn = lambda : int(input())
inl   = lambda: list(map(int, input().split()))
inm   = lambda:      map(int, input().split())
DBG =  True   and False
def ddprint(x):
  if DBG:
    print(x)

n = inn()
b = inl()
seq = [0] * n
for i in range(n-1,-1,-1):
    ok = False
    for j in range(i,-1,-1):
        ddprint("i {} j {} bj {}".format(i,j,b[j]))
        if b[j] == j+1:
            seq[i] = j
            ddprint("j {} to seq i {}".format(j,i))
            b.pop(j)
            ok = True
            break
    ddprint("ok {}".format(ok))
    ddprint(b)
    if not ok:
        print(-1)
        exit()

for i in range(n):
    print(seq[i]+1)

Submission status: Accepted

Solution code:
N = int(input())
tmp = input().split()
b = []
ans = []

for i in range(N):
  b.append(tmp[i])
  
for index, item in enumerate(b):
  if int(item) > index+1:
    print('-1')
    exit()
  dist = index + 1 - int(item)
  ans.insert(index-dist, int(item))

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

Submission status: Accepted

Solution code:
n = int(input())
a = list()
b = [int(i) for i in input().split()]
flag = True
find = False
while b != []:
    for i in reversed(range(0,len(b))):
        if b[i] == (i+1):
            del b[i]
            a.append(i+1)
            find = True
            break
    if find == True:
        find = False
        continue
    else:
        flag = False
        break
if flag == False:
    print(-1)
else:
    for i in reversed(range(0,n)):
        print(a[i])

Submission status: Accepted

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

generate_seq = []


def is_ok(b):
    len_b = len(b)
    if len_b == 1 and b[0] == 1:
        generate_seq.append(1)
        return True

    for idx, val in enumerate(b):
        if idx + 1 < val:
            return False

    processing = []
    for idx in range(len_b):
        if b[idx] != idx + 1:
            processing.append(False)
        else:
            b_tmp = b[:]
            del b_tmp[idx]
            tmp = is_ok(b_tmp)
            if tmp:
                generate_seq.append(idx + 1)
                return True
    return False


result = is_ok(b_list)
if result:
    print('\n'.join(list(map(str, generate_seq))))
else:
    print(-1)

Submission status: Accepted

Solution code:
# encoding: utf-8
import sys
from collections import deque
input = sys.stdin.readline

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

for i, bi in enumerate(b):
    if i + 1 < bi:
        q = deque([])
        break
else:
    q = deque([(b, [])])
    
while len(q) > 0:
    b_tmp, seq = q.pop()
    if len(b_tmp) == 0: break
    for i, bi in enumerate(b_tmp):
        if i + 1 < bi: break
        elif i + 1 == bi:
            if i == 0: q.append((b_tmp[i + 1:], seq + [i + 1]))
            elif i == len(b_tmp) - 1: q.append((b_tmp[:i], seq + [i + 1]))
            else: q.append((b_tmp[:i] + b_tmp[i + 1:], seq + [i + 1]))
else:
    seq = [-1]

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

Submission status: Accepted

Solution code:
import sys

sys.setrecursionlimit(10 ** 9)


def rec(arr, l, lst):
    # lstは操作の逆順
    # print(arr, l, lst[::-1])
    if l == 0:
        for p in lst[::-1]:
            print(p)
        exit()
        return

    for i in sorted(range(1, l + 1), reverse=True):
        # 1-indexed
        if arr[i-1] == i:
            # print(aa, '除去')
            # 最後に入れた数は、何番目と数の大きさが一致する
            rec(arr[:i - 1] + arr[i:], l - 1, lst + [i])
        elif arr[i-1] > i:
            # 何番目よりも数が大きい
            return
    return


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

rec(b, N, [])

print(-1)

Submission status: Accepted

Solution code:
def solve(N, Bs):
    ans = []
    while len(Bs):
        failed = True
        for i, b in enumerate(Bs[::-1]):
            i = len(Bs) - i
            if i == b:
                ans.append(Bs.pop(i - 1))
                failed = False
                break
        if failed:
            return "-1"
    return "\n".join(str(a) for a in ans[::-1])

if __name__ == "__main__":
    N = int(input())
    bs = list(map(int, input().split(" ")))
    print(solve(N, bs))

Submission status: Accepted

Solution code:
#a
from collections import deque
n = int(input())
b = list(map(int, input().split()))

is_valid = True
for x in range(len(b)):
    if b[x] > x + 1:
        print(-1)
        is_valid =False
        break

if is_valid:
    generate = deque()
    while b:
        for x in reversed(range(len(b))):
            if (x + 1) == b[x]:
                i = b.pop(x)
#                 print(i)
                generate.appendleft(i)
#                 print(f'b: {b}')
                break

    for x in range(len(generate)):
        print(generate[x])

Submission status: Accepted

Solution code:
N = int(input())
blist = list(map(int,input().split()))
answer = []
current = []
flag = True
while flag:
    if len(blist) == 0:
        break
    if blist.index(max(blist)) + 1 < max(blist):
        flag = False
    else:
        current = [x for i, x in enumerate(blist) if x == i+1]
        current
        answer.append(blist.pop(max(current)-1))
if flag:
    for ans in answer[::-1]:
        print(ans)
else:
    print(-1)        

#まずbの最大値のインデックスが自身の値を超えていないか見る
#次にインデックスの一致する要素を取得，そのうちの最大のものを削除
#これを繰り返す

Submission status: Accepted

Solution code:
n=int(input())
lst = list(map(int, input().split()))
ans=[]
if not(1 in lst):
    print(-1)
    quit()
while (1 in lst):
  l=len(lst)
  for i in range(l):
    if(lst[-i-1]==l-i):
      ans=[l-i]+ans
      del(lst[-i-1])
      break
  else:
    print(-1)
    quit()
if(len(ans)!=n):
    print(-1)
    quit()
for i in ans:
  print(i)

Submission status: Accepted

Solution code:
n = int(input())
ans = []
lis = list(map(int,input().split()))
num = 0
for i in range(n):
    li = []
    key = 0
    for j in range(n-i):
        if lis[n-i-j-1] != n-i-j or key != 0:
            li.insert(0,lis[n-i-j-1])
        else:
            ans.append(n-i-j)
            key = 1
    lis = li
if lis == []:
    for i in range(n):
        print(ans[-(1+i)])
else:
    print("-1")

Submission status: Accepted

Solution code:
N = int(input())
B = list(map(int, input().split()))
def check_pos(L):
    num_L = len(L)
    ans = True
    for idx_L in range(num_L):
        if L[idx_L] > idx_L + 1:
            ans = False
            break
    return ans

if check_pos(B) == False:
    print(-1)
else:
    ans = []
    num_L = len(B)
    for i in range(num_L):
        idx = 0
        for j in range(len(B)):
            if B[j] == j + 1:
                idx = j
        ans.append(idx + 1)
        B.pop(idx)
    ans.reverse()
    for x in ans:
        print(x)

Submission status: Accepted

Solution code:
import sys
N = int(input())
b_arr = list(map(int, input().split()))

a_arr = []


while b_arr != []:
    tmp = -1
    for i in b_arr:
        if i-1 > len(b_arr)-1:
            print(-1)
            sys.exit()

        if b_arr[i-1] == i:
            tmp = max(i-1,tmp)

    a_arr.append(b_arr.pop(tmp))

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

Submission status: Accepted

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

A = []
for _ in range(N):
    for i in range(len(B) - 1, -1, -1):
        if i + 1 == B[i]:
            p = B.pop(i)
            A.append(p)
            break
        
A.reverse()
if B != []:
    print(-1)
else:
    for a in A:
        print(a)

Submission status: Accepted

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

while True:
    if not Bs:
        break
        
    b_len = len(Bs)
    for B in Bs[::-1]:
        if Bs[b_len -1] == b_len:
            ans.append(Bs[B-1])
            del Bs[B-1]
            break
        else:
            b_len -= 1
    else:
        flg = False
        print("-1")
        break

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

Submission status: Accepted

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

def calc(X):
    if len(X) == 0:
        return [0, []]
    Y = [x for x in X]
    m = -1
    for i in range(len(Y)):
        if i+1 == Y[i]:
            m = i
    if m < 0:
        return [-1]
    
    del Y[m]
    r = calc(Y)
    if r[0] < 0:
        return [-1]
    
    return [0, r[1] + [m+1]]
    
r = calc(A)
if r[0] < 0:
    print(-1)
else:
    print(*r[1])

Submission status: Accepted

Solution code:
num = int(input())
Seq = list(map(int, input().split()));
revSeq = reversed(Seq)
answer = []

able = True;

for i in range(num):
    if i+1<Seq[i]:
        able = False;
        break;

if able==False:
    print(-1);
else:
    while len(Seq)>0:
        for i in reversed(range(num)):
            if i+1==Seq[i]:
                answer.append(Seq[i]);
                del Seq[i];
                num-=1;
                break;

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

#A
def A():
    n = I()
    b = LI()
    ans = []
    d = max(b)
    i = 0
    while b:
        j = len(b)-1
        while j >= 0:
            if b[j] > j+1:
                print(-1)
                quit()
            elif b[j] == j+1:
                ans.append(b[j])
                b.pop(j)
                break
            j -= 1
    for i in ans[::-1]:
        print(i)
#B
def B():
    n = I()
    if n%2:
        ans = []
        for i in range(1,n+1):
            for j in range(1,n+1):
                if j != n-i and j != i:
                    ans.append((min(i,j),max(i,j)))
        ans = list(set(ans))
        print(len(ans))
        ans.sort()
        for i in ans:
            print(*i)
    else:
        ans = []
        for i in range(1,n+1):
            for j in range(1,n+1):
                if j != n+1-i and j != i:
                    ans.append((min(i,j),max(i,j)))
        ans = list(set(ans))
        print(len(ans))
        ans.sort()
        for i in ans:
            print(*i)
#C
def C():
    return

#D
def D():
    return

#E
def E():
    return

#F
def F():
    return

#G
def G():
    return

#H
def H():
    return

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

Submission status: Accepted

Solution code:
import sys

input = sys.stdin.readline
n = int(input())
b_list = list(map(int, input().split()))
ans_list = []
for i in range(n):
    flag = 0
    tmp = 0
    for j in range(n-i):
        if j == b_list[j]-1:
            tmp = j
            flag = 1
    if flag == 0:
        ans_list.append(-1)
        break
    else:
        num = b_list.pop(tmp)
        ans_list.append(num)
if ans_list[-1] == -1:
    print(-1)
else:
    for a in ans_list[::-1]:
        print(a)

Submission status: Accepted

Solution code:
input = [input() for i in range(2)]

N = int(input[0])
b = input[1].split(" ")
b = list(map(int,b))
output = []

# print(N)
# print(b)

for i in range(N,0,-1):
    # print("i = " + str(i))
    for j in range(i-1,-1,-1):
        # print("j = " + str(j))
        if j + 1 == b[j]:
            # print("pop " + str(b[j]))
            output.append(b.pop(j))
            # print(b)
            break

if b == []:
    for i in range(len(output)-1,-1,-1):
        print(output[i])
else:
    print(-1)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
N = int(input())
b = list(map(int, input().split()))

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

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

Submission status: Accepted

Solution code:
N=int(input())
B=list(map(int,input().split(' ')))
able=True
ans=[]
if [1 if B[i]<=(i+1) else 0 for i in range(N)].count(0)!=0:
	able=False
	print('-1')
if able:
	while True:
		if len(B)==0:
			break
		else:
			goodposition=0
			for i in range(len(B)):
				if B[i]==(i+1):
					goodposition=i
			ans.append(str(B.pop(goodposition)))
	for a in ans[::-1]:
		print(a)

Submission status: Accepted

Solution code:
from sys import exit
# A,B,C = [int(n) for n in input().split()]
N = int(input())
b = [int(n) for n in input().split()]
test = [i for i in range(1,N+1)]
ans = []
i = N
while(i != 0):#for i in range(N,0,-1)
    # print(i)
    if b[i-1] == i:
        ans.append(b[i-1])
        del b[i-1]
        i = len(b)
    else:
        i-=1
    if len(b) == 0:
        for l in range(len(ans)-1,-1,-1):
            print(ans[l])
        exit()

print(-1)

# S = str(input())
# L = len(S)
# T = str(input())
exit()

Submission status: Accepted

Solution code:
import numpy as np

# import sys
# sys.setrecursionlimit(200000)

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

res = []

while len(b) > 0:
    seq = np.arange(1, len(b) + 1)
    same = np.where(np.array(b) == seq)[0]
    if len(same) == 0:
        print(-1)
        exit()
    else:
        maximum = same[-1]
    res.append(str(b.pop(maximum)))

print("\n".join(res[::-1]))

Submission status: Accepted

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

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

b.reverse()
L = []
for i in range(1, N + 1):
  cnt = 0
  for j in range(0, N):
    if i == 1:
      if b[j] == i:
        L.append(i)
    else:
      if b[j] == i:
        L.insert(i + cnt - 1, i)
        cnt += 1
      elif b[j] < i:
        cnt += 1

for i in L:
  print(i)

Submission status: Accepted

Solution code:
#
import sys
import math
import numpy as np
import itertools


# ひとつ入力
n = int(input())
# リストに入れる
b = [int(i) for i in input().split()] 

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

a =[]
for i in range(n):
    a.append(i+1-b[i])
answer=[]
for i in range(n-1,-1,-1):
    for j in range(i,-1,-1):
        if a[j] == 0:
            answer.insert(0,j+1)
            a.pop(j)
            break
        else:
            a[j] -= 1

#print(answer)
for i in range(n):
    print(answer[i])

Submission status: Accepted

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


for i in range(n):
    flag_1=1
    for j in range(n-i-1, -1, -1):
        if b[j]==j+1:
            result.append(b.pop(j))
            flag_1=0
            break

    if flag_1:
        print(-1)
        flag_2=0
        break

if flag_2:
    for i in range(n-1, -1, -1):
        print(result[i])

Submission status: Accepted

Solution code:
input()
l=list(map(int,input().split(" ")))
N=len(l)
r=[]
for i,x in enumerate(l):
  if x-1 > i:
    r=None
    break
  r.insert(x-1,x)

if r==None:
  print(-1)
else:
  for z in r:
    print(z)

Submission status: Accepted

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

result = []

for j in range(0, N):
    b_length = len(b)
    for i in range(0, b_length):
        if b[b_length - 1 - i] == b_length - i:
            result.append(str(b[b_length - 1 - i]))
            b.pop(b_length - 1 - i)
            break

if len(result) < N:
    print(-1)
else:
    result.reverse()
    for r in result:
        print(r)

Submission status: Accepted

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

Submission status: Accepted

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

def pos(b):
    n = len(b)
    res = False
    for i in range(1,n+1):
        if b[-i] == n+1-i:
            b.pop(-i)
            return True,n+1-i,b
    return False,0,b 

ans = []
for i in range(N):
    possible,j,b = pos(b)
    if not possible:
        print(-1)
        break
    ans.append(j)
if possible:
    for i in range(N-1,-1,-1):
        print(ans[i])

Submission status: Accepted

Solution code:
n=int(input())
b=list(map(int,input().split()))
a=[0]*(n+1)
a[0]=0
for i in b:a.insert(int(i)-1,i)
if sum(a[:n])==sum(b):
    for i in range(n):
        print(a[i])
else:print(-1)

Submission status: Accepted

Solution code:
N = int(input())
bList = [int(x) for x in input().split()]
numset = list(set(bList))
numset.sort(reverse=True)

result = []
for i in range(N, 0, -1):
    okflg = False
    for num in numset:
        kouho = [i+1 for i, x in enumerate(bList) if x == num]
        if num in kouho:
            del bList[num-1]
            result.append(num)
            okflg = True
            break
    if not okflg:
        print('-1')
        quit()

for x in result[::-1]:
    print(x)

Submission status: Accepted

Solution code:
#!venv/bin/python

import math
import numpy as np
import random
import copy
import statistics 
import bisect
import heapq
from collections import defaultdict 

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

can_solve = True
ans = []
for _i in range(N):
    #print(B)
    for i in range(len(B)-1,-1,-1):
        b = B[i]
        if i + 1 == b:
            B.pop(i)
            ans.insert(0, b)
            break
    else:
        can_solve = False
        break
if can_solve:
    print("\n".join(map(str,ans)))
else:
    print(-1)

Submission status: Accepted

Solution code:
def list_input():
    return list(map(int,input().split()))
def map_input():
    return map(int,input().split())
def map_string():
    return input().split()
    
def solve(b):
	res = [0]*(len(b))
	m = max(b)
	#n-i-2+1 for 1s
	#n-i-2+1 for 2s
	#n-i-2+3 for 3s
	#n-i-2+m for ms
	c = []
	for i in range(len(b)):
		if b[i] == m:
			ind = len(b)-i-2+m
			if(ind < 0 or ind >= len(b)):
				print(-1)
				exit()
			res[ind] = m
		else:
			c.append(b[i])		
	if(c):
		bitch = solve(c)
		j = 0
		for i in bitch:
			while res[j] != 0:
				j += 1
			res[j] = i
	return res			


n = int(input())    
a = list_input()
ans = solve(a)      
for i in ans:
	print(i)

Submission status: Accepted

Solution code:
def solve():
    n=int(input())
    b=[int(i) for i in input().split()]
    ans=[]
    for i in range(n):
        flag=False
        for i in range(len(b),0,-1):
            if b[i-1]==i:
                ans.append(i)
                del(b[i-1])
                flag=True
                break
        if flag==False:
            print(-1)
            return
    for i in ans[::-1]:
        print(i)

solve()

Submission status: Accepted

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

a_list = []
while(1):
    flag_i = -1
    for i in range(n):
        if i+1 == b_list[i]:
            a = b_list[i]
            flag_i = i
    if flag_i == -1:
        break
    a_list.append(a)
    b_list.pop(flag_i)
    n -= 1

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

Submission status: Accepted

Solution code:
N=int(input())
B=list(map(int,input().split()))
def DP(L):
	if L==[]:
		return 1,[]
	ret=[]
	for i in range(len(L))[::-1]:
		if L[i]==i+1:
			tL=L[:]
			del tL[i]
			f,dp=DP(tL)
			if f==1:
				return 1,[i+1]+dp
			else:
				break
	return -1,[]
f,dp=DP(B)
if f==1:
	for d in dp[::-1]:
		print(d)
else:
	print(-1)

Submission status: Accepted

Solution code:
n=int(input())
A=list(map(int,input().split()))
for i in range(n):
  if A[i]>i+1:
    print(-1)
    exit()
    
ANS=[]
for _ in range(n):
  for i in range(1,n+1):
    if A[-i]==n-i+1:
      ANS.append(A[-i])
      del A[-i]
      n-=1
      break
    

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

Submission status: Accepted

Solution code:
n=int(input())
b=list(map(int,input().split()))
a=[]
for i in range(n):
  if b[i]-1>len(a):
    print(-1)
    break;
  else:
    a.insert(b[i]-1,b[i])
if len(a)==n:
  for i in range(n):
    print(a[i])

Submission status: Accepted

Solution code:
#import collections as c
#ip = lambda : map(int, input().split())

#################
N = int(input())
b = []
for n,i in enumerate(input().split()):
    b.append(n+1 - int(i))
r = []

for i in range(N):
    for j in range(len(b)):
        if b[-1-j] == 0:
            r.insert(0,len(b)-j)
            del b[-1-j]
            break
        else:
            b[-1-j] -= 1

        if j==len(b)-1:
            print(-1)
            exit()

for i in r:
    print(i)

Submission status: Accepted

Solution code:
from collections import deque

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

qa = deque([]) 

while len(b) >= 1:
    t = False
    for i in range(len(b), 0, -1):
        if b[i-1] == i:
            b.pop(i-1)
            qa.appendleft(i)
            t = True
            break
    if not(t):
        print(-1)
        break

if t:
    for i in range(len(qa)):
        print(qa[i])

Submission status: Accepted

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

ans = []
for i in range(N):
    f = False
    for j in range(N - i - 1, -1, -1):
        if b[j] == j + 1:
            ans.append(b.pop(j))
            f = True
            break
    if not f:
        print(-1)
        break
else:
    for a in reversed(ans):
        print(a)

Submission status: Accepted

Solution code:
""" AtCoder """

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

a = []

for i in b:
    if i > len(a) + 1:
        print(-1)
        exit()
    a.insert(i - 1, i)

#a.reverse()

for i in a:
    print(i)

Submission status: Accepted

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

answer = [0]*N

def p(b):
  global answer
  
  if sum([1 if i+1==bi else 0 for i,bi in enumerate(b)]) >= 1  and sum([1 if i+1<bi else 0 for i,bi in enumerate(b)]) == 0 :
    x = max([i+1 if i+1==bi else 0 for i,bi in enumerate(b)])
    answer[len(b)-1] = x
    del b[x-1]
    if b:
      p(b)
    return 
  else:
    answer = False
    return
  
p(b)

if answer:
  for _ in answer:
    print(_)
else:
  print('-1')

Submission status: Accepted

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

a = []
possible = True
while(len(b) > 0 and possible):
    possible = False
    for i in range(len(b)-1, -1, -1):
        if b[i] == i + 1:
            a = [b.pop(i)] + a
            possible = True            
            break

if possible:
    for a_ in a:
        print(a_)
else:
    print(-1)

Submission status: Accepted

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

anslist = []
for i in range(len(b)):
    if i - b[i] + 1 >= 0:
        anslist = anslist[0:i - b[i] + 1] + [b[i]] + anslist[i - b[i] + 1::]

        #print(anslist, i,  i - b[i] + 1, b[i])
    else:
        anslist = False
        break
#print(anslist)
if not anslist:
    print(-1)
else:
    for i in anslist[::-1]:
        print(i)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N=int(input())
num_list=list(map(int,input().split()))
length=len(num_list)
result=[]
for trial in range(0,length):
    for i in range(0,len(num_list)):
        #後ろから判定を行なっていく
        if(num_list[len(num_list)-1-i]==(len(num_list)-i)):
            del num_list[len(num_list)-1-i]
            #一個消しちゃったから+1
            result=[len(num_list)-i+1]+result
            break
if(len(result)==length):
    for l in range(0,len(result)):
        print(result[l])
else:
    print(-1)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys


def rec(N, b):
    if N == 0:
        return []
    buf = -1
    for i in range(N):
        if b[i] > i+1:
            return [-1]
        if b[i] == i+1:
            buf = i
    if buf == -1:
        # ここはありえないはずではある
        print("ありえない")
        return [-1]
    a = b.pop(buf)
    return rec(N-1, b) + [a]


def solve(N, b):
    for x in rec(N, b):
        print(x)
    return

# Generated by 1.1.3 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)


def main():
    N = int(input())
    b = [int(x) for x in input().split()]
    solve(N, b)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

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

def dfs(B,LEN,ANS):
    #print(B,LEN,ANS)
    if LEN==0:
        for a in ANS[::-1]:
            print(a)
        sys.exit()
    for i in range(LEN-1,-1,-1):
        if B[i]==i+1:
            C=B[:i]+B[i+1:]
            dfs(C,LEN-1,ANS+[i+1])

dfs(B,N,[])
print(-1)

Submission status: Accepted

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

Submission status: Accepted

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

import sys
from itertools import chain
from functools import reduce
from collections import OrderedDict, Counter, deque
import operator


def IA(): return [int(x) for x in input().split()]


def IM(N): return [IA() for _ in range(N)]


N = int(input())
B = IA()

sys.setrecursionlimit(500)


def f(b, n):
    if n == 0:
        return []
    for i, x in enumerate(b, 1):
        if i < x or n < x:
            return None
    for i, x in enumerate(b, 1):
        if i == x:
            ret = f(tuple(b[:i-1] + b[i:]), n-1)
            if ret is not None:
                return ret + [i]
    return None


ans = f(B, N)
if ans:
    print(*ans, sep="\n")
else:
    print(-1)

Submission status: Accepted

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

if sum([(x > idx + 1) for (idx, x) in enumerate(b)]) >= 1:
  print(-1)
else:
  list_ = []
  while len(b) > 0:
    for idx, c in enumerate(b):
      if idx+1 == c:
        del_idx = idx
    list_.append(b.pop(del_idx))

  for c in list_[::-1]:
      print(c)

Submission status: Accepted

Solution code:
#AGC031-A
def IL(): return list(map(int,input().split()))
def SL(): return input().split()
def I(): return int(input())
def S(): return list(input())
n=I()
B=IL()
ans=[]
while True:
    flag=False
    for i in range(len(B)):
        if len(B)-i==B[-1-i]:
            ans=[B[-i-1]]+ans
            flag=True
            B.remove(B[-i-1])
            break
    if not flag:
        break
if len(ans)!=n:
    print(-1)
else:
    for i in range(n):
        print(ans[i])

Submission status: Accepted

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

def main():
    n = N
    S = []
    while B:
        tmp = -1
        for i in range(n):
            if B[i] == i+1:
                tmp = B[i]
                j = i
        if tmp == -1:
            return -1
        else:
            S.append(B.pop(j))
            n -= 1
    return S[::-1]

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

Submission status: Accepted

Solution code:
saiaku=0

n=int(input())
b_list=input().split(' ')

for bl in range(len(b_list)):
    b_list[bl]=int(b_list[bl])
#print(b_list)

junretu=[]
nagasa=len(b_list)

sakasa=[]
#print(b_list[1:])
for kaisu in range(nagasa):
    maxn=0
    swit=0
    for i in range(nagasa):
        if b_list[i]==i+1:
            maxn=i #i>=0
            swit=1
            #print(maxn)
    sakasa.append(maxn+1)
    b_list=b_list[:maxn]+b_list[maxn+1:]
    nagasa=len(b_list)
    if swit==0:
        saiaku=1
    #print(b_list)
#print(sakasa)
if saiaku==0:
    shin=[]
    for sks in range(len(sakasa)):
        print(sakasa[len(sakasa)-sks-1])
    """
    for sks in range(len(sakasa)):
        shin.append(sakasa[len(sakasa)-sks-1])
    print(shin)
    """
else:
    print(-1)