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:
import bisect
import copy
import heapq
import math
import sys
from collections import *
from functools import lru_cache
from itertools import accumulate, combinations, permutations, product
def input():
    return sys.stdin.readline()[:-1]
def ruiseki(lst):
    return [0]+list(accumulate(lst))
sys.setrecursionlimit(5000000)
mod=pow(10,9)+7
al=[chr(ord('a') + i) for i in range(26)]
direction=[[1,0],[0,1],[-1,0],[0,-1]]

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

anslst=[]
while b:
    for i in range(len(b))[::-1]:
        if b[i]==i+1:
            anslst.append(b[i])
            b.pop(i)
            break
    else:
        print(-1)
        quit()
# print(anslst)
for i in range(n):
    print(anslst[-1-i])

Submission status: Accepted

Solution code:
n = int(input())
b = list(map(int,input().split()))
rev = []
for i in range(n): #i番目の操作のものを見つける
    cand = []
    for j in range(len(b)):
        if b[j] != j+1:
            if cand:
                continue
            break
        else:
            cand.append(j+1)
    if not cand:
        print(-1)
        exit()
    rev.append(cand[-1])
    b.pop(cand[-1]-1)
ans = reversed(rev)
for k in ans:
    print(k)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

l=[]
flg=0

for i in range(n-1,-1,-1):
  #print(i,"kocchi",b)
  flg=0
  for j in range(i,-1,-1):
    #print(j,"acchi")
    if j+1==b[j]:
      flg=1
      l.append(b.pop(j))
      break
  if flg==0:
    break

if flg==0:
  print(-1)
else:
  for i in range(n-1,-1,-1):
    print(l[i])

Submission status: Accepted

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

def main():
    N = int(input())
    B = list(map(int,input().split()))
    ret = []
    while len(B)>0:
        for j in range(len(B)-1,-1,-1):
            if B[j]==j+1:
                ret.append(j+1)
                B.pop(j)
                break
            if j==0 and B[j]!=(j+1):
                print(-1)
                exit()
    print('\n'.join(map(str,ret[::-1])))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys

N = int(input())
B = list(map(int, sys.stdin.readline().rsplit()))

res = []
for _ in range(N):
    tmp = -1
    for i, b in enumerate(B):
        if i + 1 == b:
            tmp = i
    if tmp == -1:
        print(-1)
        exit()
    res.append(B.pop(tmp))

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

Submission status: Accepted

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

ansl = []
while len(b) != 0:
	tmp = 0
	for i in range(len(b)):
		targ = i+1-b[i]
		if targ == 0:
			tmp = i
	ansl.append(str(b[tmp]))
	b.pop(tmp)

ansl.reverse()
print("\n".join(ansl))

Submission status: Accepted

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

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

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

ans.reverse()

for ans_i in ans:
    print(ans_i)

Submission status: Accepted

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

ans = []

for i in range(N):
    pop = -1
    for j, v in enumerate(b):
        if j+1 == v:
            pop = j
    if pop == -1:
        print(-1)
        exit()
    ans.append(b.pop(pop))
for i in ans[::-1]:
    print(i)

Submission status: Accepted

Solution code:
#import sys
#import numpy as np
import math
#import itertools
#from fractions import Fraction
#import itertools
from collections import deque
#import heapq
#from fractions  import gcd
#input=sys.stdin.readline
#import bisect
n=int(input())
b=list(map(int,input().split()))
ans=[]
while True:
    f=False
    for i in range(len(b)-1,-1,-1):
        if b[i]!=i+1:
            continue
        else:
            ans.append(b[i])
            b.pop(i)
            f=True
            break
    if not b:
        break
    if not f:
        print(-1)
        exit()
for i in range(n):
    print(ans[n-1-i])

Submission status: Accepted

Solution code:
class node():
    def __init__(self, data, child=None):
        self.child = child
        self.data = data

class head():
    def __init__(self, data):
        self.child = None
        self.length = 0
        for i in data:
            self.add(i)

    def add(self, i):
        self.child = node(i, child=self.child)
        self.length += 1
    
    def remove(self):
        idx = self.length
        root = self
        child = self.child
        while child is not None:
            if child.data == idx:
                root.child = child.child
                self.length -= 1
                return child.data
            idx -= 1
            root = child
            child = root.child

        return False

n = int(input())
data = list(map(int, input().split()))
linked = head(data)
ans = []
while True:
    step = linked.remove()
    if step is False:
        break
    ans.append(step)

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

Submission status: Accepted

Solution code:
from fractions import gcd

def mi():
    return map(int, input().split())

def main():
    N = int(input())
    b = list(mi())
    a = []
    for i in range(N):
        bl = False
        for j, v in enumerate(b):
            if v == j+1:
                t = j
                bl = True
        if bl:
            a.append(b.pop(t))
        else:
            print(-1)
            return
    
    for v in reversed(a):
        print(v)



if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n=int(input())
L=list(map(int,input().split()))
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)
        k=i-1
        while k>i-t:
            if x[k]>t-i+k:
                t=x[k]
                i=k
            k -= 1
        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: Accepted

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

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

ans.reverse()

for ans_i in ans:
    print(ans_i)

Submission status: Accepted

Solution code:
#from statistics import median
#import collections
#aa = collections.Counter(a) # list to list || .most_common(2)で最大の2個とりだせるお a[0][0]
#from fractions import gcd
#from itertools import combinations # (string,3) 3回
#from collections import deque
from collections import deque,defaultdict
#import bisect
#
#    d = m - k[i] - k[j]
#    if kk[bisect.bisect_right(kk,d) - 1] == d:
#
#
#
# pythonで無理なときは、pypyでやると正解するかも！！
#
#

import sys
sys.setrecursionlimit(10000000)
mod = 10**9 + 7
#mod = 9982443453
def readInts():
  return list(map(int,input().split()))
def I():
  return int(input())
n = I()
b = readInts()
ans = []
for i in range(n):
    pivot = -1
    for j in range(len(b)-1,-1,-1):
        if b[j] == j+1:
            pivot = j
            break
    if pivot == -1:
        print(-1)
        exit()
    ans.append(pivot+1)
    b.pop(pivot)
print(*ans[::-1],sep='\n')

Submission status: Accepted

Solution code:
import sys

sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline
f_inf = float('inf')
mod = 10 ** 9 + 7


def resolve():
    n = int(input())
    B = list(map(int, input().split()))

    res = []
    for i in range(n):
        L = len(B)
        for j in reversed(range(L)):
            if j + 1 == B[j]:
                b = B.pop(j)
                res.append(b)
                break
        else:
            print(-1)
            break
    else:
        print(*res[::-1], sep="\n")


if __name__ == '__main__':
    resolve()

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:
import sys
sys.setrecursionlimit(1000000)
#def input():
#    return sys.stdin.readline()[:-1]

'''
n=int(input())
for i in range(n):
    a[i]=int(input())
    a[i],b[i]=map(int,input().split())
a=[int(x) for x in input().split()]
n,m=map(int,input.split())
from operator import itemgetter
a = [(1, "c", 1), (1, "b", 3), (2, "a", 0), (1, "a", 2)]
print(sorted(a))  # 0 番目の要素でソート、先頭の要素が同じなら 1 番目以降の要素も見る
print(sorted(a, key=itemgetter(0)))  # 0 番目の要素だけでソート
print(sorted(a, key=itemgetter(0, 2)))  # 0 番目と 2 番目の要素でソート
print(sorted(a, key=lambda x: x[0] * x[2]))  # 0 番目の要素 * 2 番目の要素でソート
print(sorted(a, reverse=True))  # 降順にソート
a.sort()  # 破壊的にソート、sorted() よりも高速
try: # エラーキャッチ list index out of range
    for i in range():
        k=b[i]
except IndexError as e:
    print(i)
'''

test_data1 = '''\
3
1 2 1
'''

test_data2 = '''\
2 3 0
4 5 6
7 8 9
-1 2 -5
'''

test_data3 = '''\
9
1 1 1 2 2 1 2 3 2
'''
td_num=3

def GetTestData(index):
    if index==1:
        return test_data1
    if index==2:
        return test_data2
    if index==3:
        return test_data3

if False:
    with open("../test.txt", mode='w') as f:
        f.write(GetTestData(td_num))
    with open("../test.txt") as f:
        # Start Input code ---------------------------------------
        n=int(f.readline())
        b=list(map(int,f.readline().split()))
        # End Input code ---------------------------------------
else:
    for i in range(1):
        # Start Input code ---------------------------------------
        n=int(input())
        b=list(map(int,input().split()))
        # End Input code ---------------------------------------
#print('n,m,x,y=',n,m,x,y)
ans=[]
#print(b)
cnt=0
while cnt<100 and len(b)>0:
    m=len(b)
    cnt+=1
    flg=False
    for i in range(m):
        #print('i,i+1,m-1-i,b[m-1-i]',i,i+1,m-1-i,b[m-1-i],b)
        if b[m-1-i]==m-i:
            ans.append(b.pop(m-1-i))
            
            flg=True
            break
    if flg==False:
        break
if len(ans)==n:
    for i in range(n):
        print(ans[-i-1])
else:
    print(-1)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**6)
from math import floor,ceil,sqrt,factorial,log
from heapq import heappop, heappush, heappushpop
from collections import Counter,defaultdict,deque
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
from copy import deepcopy
from operator import itemgetter
from fractions import gcd
mod = 10 ** 9 + 7
inf = float('inf')
ninf = -float('inf')
 
#整数input
def ii(): return int(sys.stdin.readline().rstrip()) #int(input())
def mii(): return map(int,sys.stdin.readline().rstrip().split())
def limii(): return list(mii()) #list(map(int,input().split()))
def lin(n:int): return [ii() for _ in range(n)]
def llint(n: int): return [limii() for _ in range(n)]
#文字列input
def ss(): return sys.stdin.readline().rstrip() #input()
def mss(): return sys.stdin.readline().rstrip().split()
def limss(): return list(mss()) #list(input().split())
def lst(n:int): return [ss() for _ in range(n)]
def llstr(n: int): return [limss() for _ in range(n)]

#本当に貪欲法か？ DP法では？？
#本当に貪欲法か？ DP法では？？
#本当に貪欲法か？ DP法では？？

n=ii()
arr=limii()
ans=[]
while len(arr)>0:
    chk=False
    maxtmp=0
    for i,j in enumerate(arr):
        #print(i,j,i+1,arr[i])
        if i+1==j:
            maxtmp=j-1
            chk=True
    p=arr.pop(maxtmp)
    ans.append(p)
    if chk==False:
        print(-1)
        exit()
    #print(ans)

k=reversed(ans)
for i,j in enumerate(k):
    print(j)

Submission status: Accepted

Solution code:


def myAnswer(N:int,B:list) -> None:
   ans = []
   for n,b in enumerate(B):
      if(n + 1 < b):
         print(-1)
         return
   
   while len(B) > 0:
      tmp = 0
      for n,b in enumerate(B):
         if(n + 1 == b):
            tmp = n
      ans.append(B[tmp])
      B.pop(tmp)
   for a in ans[::-1]:
      print(a)
   
def modelAnswer():
   return
def main():
   N = int(input())
   B = list(map(int,input().split()))
   myAnswer(N,B)
if __name__ == '__main__':
   main()

Submission status: Accepted

Solution code:
N=int(input())
B=list(map(int, input().split()))
#a 空
for i in range(N):
    if i+1<B[i]:
        print(-1)
        exit()

ans=[]

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

Submission status: Accepted

Solution code:
def m():
    N = int(input())
    B = list(map(int, input().split()))
    ans = []
    while B:
        f = True
        for i in reversed(range(len(B))):
            if B[i] == i+1:
                B.pop(i)
                ans.append(i+1)
                f = False
                break
        if f: return print('-1')
    ans.reverse()
    for i in ans:
        print(i)
    return


m()

Submission status: Accepted

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

#############
# Libraries #
#############

import sys
input = sys.stdin.readline

import math
#from math import gcd
import bisect
from collections import defaultdict
from collections import deque
from collections import Counter
from functools import lru_cache


#############
# Constants #
#############

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

#############
# Functions #
#############

######INPUT######
def I(): return int(input().strip())
def S(): return input().strip()
def IL(): return list(map(int,input().split()))
def SL(): return list(map(str,input().split()))
def ILs(n): return list(int(input()) for _ in range(n))
def SLs(n): return list(input().strip() for _ in range(n))
def ILL(n): return [list(map(int, input().split())) for _ in range(n)]
def SLL(n): return [list(map(str, input().split())) for _ in range(n)]

######OUTPUT######
def P(arg): print(arg); return
def Y(): print("Yes"); return
def N(): print("No"); return
def E(): exit()
def PE(arg): print(arg); exit()
def YE(): print("Yes"); exit()
def NE(): print("No"); exit()

#####Shorten#####
def DD(arg): return defaultdict(arg)


#####Inverse#####
def inv(n): return pow(n, MOD-2, MOD)

######Combination######
kaijo_memo = []
def kaijo(n):
  if(len(kaijo_memo) > n):
    return kaijo_memo[n]
  if(len(kaijo_memo) == 0):
    kaijo_memo.append(1)
  while(len(kaijo_memo) <= n):
    kaijo_memo.append(kaijo_memo[-1] * len(kaijo_memo) % MOD)
  return kaijo_memo[n]

gyaku_kaijo_memo = []
def gyaku_kaijo(n):
  if(len(gyaku_kaijo_memo) > n):
    return gyaku_kaijo_memo[n]
  if(len(gyaku_kaijo_memo) == 0):
    gyaku_kaijo_memo.append(1)
  while(len(gyaku_kaijo_memo) <= n):
    gyaku_kaijo_memo.append(gyaku_kaijo_memo[-1] * pow(len(gyaku_kaijo_memo),MOD-2,MOD) % MOD)
  return gyaku_kaijo_memo[n]

def nCr(n,r):
  if(n == r):
    return 1
  if(n < r or r < 0):
    return 0
  ret = 1
  ret = ret * kaijo(n) % MOD
  ret = ret * gyaku_kaijo(r) % MOD
  ret = ret * gyaku_kaijo(n-r) % MOD
  return ret

######Factorization######
def factorization(n):
  arr = []
  temp = n
  for i in range(2, int(-(-n**0.5//1))+1):
    if temp%i==0:
      cnt=0
      while temp%i==0: 
        cnt+=1 
        temp //= i
      arr.append([i, cnt])
  if temp!=1:
    arr.append([temp, 1])
  if arr==[]:
    arr.append([n, 1])
  return arr

#####MakeDivisors######
def make_divisors(n):
  divisors = []
  for i in range(1, int(n**0.5)+1):
    if n % i == 0:
      divisors.append(i)
      if i != n // i: 
        divisors.append(n//i)
  return divisors

#####MakePrimes######
def make_primes(N):
  max = int(math.sqrt(N))
  seachList = [i for i in range(2,N+1)]
  primeNum = []
  while seachList[0] <= max:
    primeNum.append(seachList[0])
    tmp = seachList[0]
    seachList = [i for i in seachList if i % tmp != 0]
  primeNum.extend(seachList)
  return primeNum

#####GCD#####
def gcd(a, b):
    while b:
        a, b = b, a % b
    return a

#####LCM#####
def lcm(a, b):
    return a * b // gcd (a, b)

#####BitCount#####
def count_bit(n):
  count = 0
  while n:
    n &= n -1
    count += 1
  return count

#####ChangeBase#####
def base_10_to_n(X, n):
  if X//n:
    return base_10_to_n(X//n, n)+[X%n]
  return [X%n]

def base_n_to_10(X, n):
  return sum(int(str(X)[-i-1])*n**i for i in range(len(str(X))))

#####IntLog#####
def int_log(n, a):
  count = 0
  while n>=a:
    n //= a
    count += 1
  return count

#############
# Main Code #
#############
N = I()
A = IL()
ans = []
flag = 1
while flag:
  flag = 0
  for i in range(N)[::-1]:
    if i+1 == A[i]:
      ans.append(A[i])
      A = A[:i]+A[i+1:]
      N -= 1
      flag = 1
      break
if A:
  print(-1)
else:
  for a in ans[::-1]:
    print(a)

Submission status: Accepted

Solution code:
import sys

sys.setrecursionlimit(10 ** 8)

input = sys.stdin.readline


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

    ans = []
    while len(ans) < N:
        cnt = 0
        cnti = -1
        for i in range(N):
            if B[i] == 0:
                cnt += 1
            else:
                if B[i] == i - cnt + 1:
                    cnti = i
        if cnti == -1:
            print(-1)
            return
        else:
            ans.append(B[cnti])
            B[cnti] = 0

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


if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

for num in lst[::-1]:
    print(num)

Submission status: Accepted

Solution code:
from collections import deque

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

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

if not b:
    print('\n'.join(str(i) for i in result))
else:
    print(-1)

Submission status: Accepted

Solution code:
from collections import deque

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

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

if not b:
    print('\n'.join(str(i) for i in result))
else:
    print(-1)

Submission status: Accepted

Solution code:
#from collections import deque,defaultdict
printn = lambda x: print(x,end='')
inn = lambda : int(input())
inl   = lambda: list(map(int, input().split()))
inm   = lambda:      map(int, input().split())
ins = lambda : input().strip()
DBG = True # and False
BIG = 10**18
R = 10**9 + 7

def ddprint(x):
  if DBG:
    print(x)

n = inn()
b = inl()
ans = []
for i in range(n):
    #ddprint(i)
    #ddprint(b)
    #ddprint(ans)
    for j in range(n-i-1,-1,-1):
        if b[j]==j+1:
            ans.append(j+1)
            for k in range(j,n-i-1):
                b[k] = b[k+1]
            break
    if len(ans)<i+1:
        print(-1)
        exit()
ans.reverse()
for x in ans:
    print(x)

Submission status: Accepted

Solution code:
N = int(input())
B = list(map(lambda x: int(x) - 1, input().split()))
for i in range(N):
    if B[i] > i:
        print(-1)
        break
else:
    ans = []
    for _ in range(N):
        for i in range(len(B) - 1, -1, -1):
            if B[i] == i:
                ans.append(i)
                B = B[:i] + B[i + 1 :]
                break
    print("\n".join(map(lambda x: str(x + 1), ans[::-1])))

Submission status: Accepted

Solution code:
def main():
    n, *b = map(int, open(0).read().split())

    m = []
    for k in range(1, n + 1):
        for i in range(n - k, -1, -1):
            if b[i] == i + 1:
                b.pop(i)
                m.append(i + 1)
                break
        else:
            print(-1)
            exit()

    print('\n'.join(map(str, m[::-1])))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def main():
    n, *b = map(int, open(0).read().split())

    m = []
    for k in range(1, n+1):
        for i in range(n - k, -1, -1):
            if b[i] == i + 1:
                b.pop(i)
                m.append(i+1)
                break

    if len(b) > 0:
        print(-1)
    else:
        print('\n'.join(map(str, m[::-1])))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
data = []
for i in range(n):
    for j in range(len(a)):
        if a[len(a) - j - 1] == len(a) - j:
            data.append(a[len(a) - j - 1])
            del a[len(a) - j - 1]
            break
if len(data) == n:
    data.reverse()
    for x in data:
        print(x)
else:
    print(-1)

Submission status: Accepted

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

solve()

Submission status: Accepted

Solution code:
from queue import Queue

def solve():
    N = int(input())
    b = list(map(int, input().split()))
    idx = [i+1 for i in reversed(range(N))]
    
    l = []
    for i in range(N):
        for bi, j in zip(reversed(b), idx):
            if j == bi:
                l.append(str(j))
                break
        del idx[0], b[j-1]
    
    return '\n'.join(reversed(l)) if len(l) == N else -1

print(solve())

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
n, *b = map(int, open(0).read().split())
l = []
while True:
    F = 1
    for i, j in enumerate(b[::-1]):
        if len(b) - i == j:
            del b[len(b) - i - 1]
            l += j,
            F = 0
            break
    if F:
        break
if b:
    exit(print(-1))
for i in l[::-1]:
    print(i)

Submission status: Accepted

Solution code:
from functools import reduce
from fractions import gcd
from collections import defaultdict
import math
import bisect
import itertools
import sys
sys.setrecursionlimit(10**7)
input = sys.stdin.readline
INF = float("inf")


# 処理内容
def main():
    N = int(input())
    B = list(map(int, input().split()))

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


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N=int(input())
b=list(map(int,input().split()))
ans=[]
turn=N
while(b):
    last=-1
    for i in range(len(b)):
        if i+1==b[i]:
            last=i
    if last==-1:
        print(-1)
        exit()
    ans.append(last+1)
    del b[last]
    
print(*ans[::-1],sep='\n')

Submission status: Accepted

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

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

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

Submission status: Accepted

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

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

a.reverse()
print('\n'.join(map(str, a)))

Submission status: Accepted

Solution code:
n=int(input())
b=list(map(int,input().split()))
ba=[]#右からの距離を足したやつ
for i in range(n):
    if len(ba)<b[i]-1:
        print(-1)
        break
    else:
        ba.insert(b[i]-1,b[i])
else:
    for i in range(n):
        print(ba[i])

Submission status: Accepted

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

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 reversed(range(len(B))):
        if j == B[j] - 1:
            ans[i] = B[j]
            if len(B) > 0:
                B.pop(j)
            break

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

Submission status: Accepted

Solution code:
n = int(input())
b = list(map(int, input().split()))
for i in range(n):
    if b[i] > i + 1:
        print(-1)
        exit()
L = []
i = 1
while i <= n:
    for j in range(1, len(b)+1):
        if b[j-1] == j:
            m = j
    L.append(m)
    b.pop(m-1)
    i += 1
for l in reversed(L):
    print(l)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def f():
    res = []
    for i in range(n):
        pivot = -1
        for j in range(len(LIST)-1,-1,-1):
            if LIST[j] == j:
                pivot = j
                break
        if pivot == -1:
            print(-1)
            exit()
        res.append(pivot+1)
        LIST.pop(pivot)
    res = res[::-1]
    #print(res)
    for v in res:
        print(v)



n = int(input())
LIST = list(map(lambda x:int(x)-1,input().split()))
f()

Submission status: Accepted

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

Submission status: Accepted

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

ans = []

for k in range(N):
    flag = True
    for i, b in enumerate(reversed(B)):
        if N - k - i == b:
            ans.append(B.pop(b-1))
            flag = False
            break
    if flag:
        print(-1)
        exit()

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

Submission status: Accepted

Solution code:
import sys
import numpy as np

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

ans = []
for i in range(n, 0, -1):
    cp = np.arange(1, i + 1)
    diff = b - cp
    if np.where(diff == 0)[0].shape[0] == 0:
        print(-1)
        sys.exit()
    else:
        rem = (np.where(diff == 0)[0]).max()
        ans.append(b[rem])
        b = np.delete(b, rem, axis=0)

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

Submission status: Accepted

Solution code:
import sys

# import re
import math
import collections
# import decimal
import bisect
# import itertools
import fractions
# import functools
import copy
import heapq
import decimal
# import statistics
import queue

sys.setrecursionlimit(10000001)
INF = 10 ** 16
MOD = 10 ** 9 + 7

ni = lambda: int(sys.stdin.readline())
ns = lambda: map(int, sys.stdin.readline().split())
na = lambda: list(map(int, sys.stdin.readline().split()))


# ===CODE===


def main():
    n = ni()
    a = na()

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

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


if __name__ == '__main__':
    main()

Submission status: Accepted

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

c = b[:]
ans = []
jdg = True

while c:
    temp = []
    for i in range(len(c)):
        if i + 1 - c[i] == 0:
            temp.append([c[i], i])
    if not temp:
        jdg = False
        break
    l = max(temp)
    c = c[:l[1]] + c[l[1]+1:]
    ans.append(l[0])

if jdg:
    ans.reverse()
    print(*ans, sep='\n')
else: print(-1)

Submission status: Accepted

Solution code:
import sys
def input(): return sys.stdin.readline().rstrip()
def I(): return int(input())
def Is(): return (int(x) for x in input().split())
def LI(): return list(Is())

# ----------------------------------------------------------- #

n = I()
B = LI()

ANS = []
for _ in range(n):
    for i in reversed(range(len(B))):
        if i+1 == B[i]:
            ANS.append(B.pop(i))
            break
    else:
        print(-1)
        exit()

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