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: Time Limit Exceeded

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

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

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):
        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: Time Limit Exceeded

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, aa in enumerate(arr, 1):
        # 1-indexed
        if aa == i:
            # print(aa, '除去')
            # 最後に入れた数は、何番目と数の大きさが一致する
            rec(arr[:i - 1] + arr[i:], l - 1, lst + [i])
    return


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

rec(b, N, [])

print(-1)

Submission status: Time Limit Exceeded

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]

    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: Time Limit Exceeded

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)):
		if L[i]==i+1:
			tL=L[:]
			del tL[i]
			f,dp=DP(tL)
			if f==1:
				return 1,[i+1]+dp
	return -1,[]
f,dp=DP(B)
if f==1:
	for d in dp[::-1]:
		print(d)
else:
	print(-1)

Submission status: Time Limit Exceeded

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

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

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

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

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
b = list(map(int, input().split()))
ans = []
def dfs(x):
    if x == []:
        return True
    ret = False
    for i, a in enumerate(x):
        if i + 1 == a:
            ret = dfs(x[:i] + x[i+1:])
            if ret:
                ans.append(a)
                break
    return ret
if dfs(b):
    for a in ans:
        print(a)
else:
    print(-1)

Submission status: Accepted

Solution code:
import sys

input = sys.stdin.readline

n = int(input())

b_list = list(map(int, input().split()))
result_list = []
while len(b_list) > 0:
    flag = False
    for i,b in enumerate(reversed(b_list)):
        if len(b_list)-i == b:
            result_list.append(b)
            b_list.pop(len(b_list)-i -1)
            flag = True
            break
    if not flag:
        print(-1)
        exit(0)

for b in reversed(result_list):
    print(b)

Submission status: Accepted

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

ans = []
for i in range(n):
  ind = -1
  for j in range(len(A)):
    if A[j] == j + 1:
      ind = j
  if ind == -1:
    break
  else:
    A.pop(ind)
    ans.append(ind+1)

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

Submission status: Accepted

Solution code:
mod = 1000000007
eps = 10**-9


def main():
    import sys
    input = sys.stdin.readline

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

    ans = []
    for _ in range(N):
        j = -1
        for i in range(len(B)):
            if B[i] == i+1:
                j = i
        if j == -1:
            print(-1)
            exit()
        ans.append(j+1)
        B.pop(j)
    for a in ans[::-1]:
        print(a)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys

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

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

ans = []

while A != []:
  tmp = 0
  for p in range(len(A)):
    if A[p] == p+1:
      tmp = p
  ans.insert(0,A.pop(tmp))

  
for x in ans:
  print(x)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input())
b=list(map(int,input().split()))
ans=[0]*n
t=0
while t<n:
  res=t
  for i in range(n-t):
    if b[n-t-1-i]==n-t-i:
      ans[t]=b[n-t-1-i]
      b.remove(b[n-t-1-i])
      t+=1
      break
  if t==res:
    print(-1)
    exit()
for i in range(n):
  print(ans[n-1-i])

Submission status: Accepted

Solution code:
import math
from typing import List, Any


def read_int() -> int:
    return int(input().strip())


def read_ints() -> List[int]:
    return list(map(int, input().strip().split(' ')))


def solve() -> Any:
    N = read_int()
    B = read_ints()
    answer = [0]*N
    for j in range(N-1, -1, -1):
        last_valid = -1
        for i in range(len(B)):
            if B[i] == i+1:
                last_valid = i
        if last_valid == -1:
            print(-1)
            return
        answer[j] = B.pop(last_valid)
    for a in answer:
        print(a)


if __name__ == '__main__':
    solve()

Submission status: Accepted

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

ans = []
while len(b):
    memo = -1
    for key,val in enumerate(b):
        if key+1==val:
            memo = key
    if memo != -1:
        ans.append(b.pop(memo))
    else:
        break

if len(b):
    print(-1)
else:
    ans.reverse()
    for a in ans:
        print(a)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import deque
n = int(input())
b = list(map(int,input().split()))
ans = deque()
for _ in range(n):
    for i in range(len(b))[::-1]:
        if b[i] == i + 1:
            ans.appendleft(b[i])
            b.pop(i)
            break
        if i == 0:
            print(-1)
            exit(0)
for i in ans:
    print(i)

Submission status: Accepted

Solution code:

def resolve():
    N = int(input())
    B = list(map(int, input().split()))
    ans = []
    while B:
        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)
            return 
    print(*ans[::-1], sep='\n')



if __name__ == "__main__":
    resolve()

Submission status: Accepted

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

while (cnt < 100000) & (new_B != []):
    B = new_B
    length = len(B)
    new_B = []
    for i in range(1, length + 1):
        if B[-i] < (len(B) - i + 1):
            new_B = [B[-i]] + new_B
        elif B[-i] == (len(B) - i + 1):
            ans.append(B[-i])
            new_B = B[:-i] + new_B
            break
        else:
            print(-1)
            exit()
    cnt += 1


if cnt == 100000:
    print(-1)
else:
    for i in ans[::-1]:
        print(i)

Submission status: Accepted

Solution code:
N = int(input())
B = list(map(int, input().split()))
ans = []
max_b = max(B)
for i in range(N):
    for b in range(max_b, 0, -1):
        if len(B) > b-1 and B[b-1] == b:
            ans += [B.pop(b-1)]
            break

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

Submission status: Accepted

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

while B:
    # print(f'{B=}')
    for idx in range(len(B) - 1, -1, -1):
        if B[idx] == idx + 1:
            v = B.pop(idx)
            V.append(v)
            break
    else:
        print(-1)
        exit()

for v in V[::-1]:
    print(v)

Submission status: Accepted

Solution code:
N=int(input())
B=list(map(int,input().split()))
answers=[]
while B:
    length=len(B)
    for n in range(len(B)-1,-1,-1):
        if B[n]==n+1:
            answers.append(B[n])
            del B[n]
            break
    if length==len(B):
        break
if len(answers)!=N:
    print(-1)
else:
    for answer in list(reversed(answers)):
        print(answer)

Submission status: Accepted

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

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

if len(ans) == n:
    ans.reverse()
    for item in ans:
        print(item)
else:
    print(-1)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
b = []; c = 0
while len(a) > 0:
    for j in range(len(a)):
        if a[-j-1] == len(a)-j: b.append(a.pop(-j-1)); break
    else: c = 1; break
else:
    b.reverse()
    for i in range(n):
        if b[i] > i+1: c = 1
print(-1) if c == 1 else print(*b, sep="\n")

Submission status: Accepted

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

Submission status: Accepted

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

ans = []


while b:
    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())
b=list(map(int, input().split()))
a=[]
for i in b:
    if len(a)<i-1:
        print(-1)
        exit()
    a.insert(i-1,i)
for i in a:
    print(i)

Submission status: Accepted

Solution code:
N = int(input())
b = list(map(int, input().split()))
a = []
for i in b:
    if len(a) < i - 1:
        print(-1)
        exit()
    a.insert(i-1,i)
for i in a:
    print(i)

Submission status: Accepted

Solution code:
from collections import defaultdict
from collections import deque
from collections import Counter
import math

def readInt():
	return int(input())
def readInts():
	return list(map(int, input().split()))
def readChar():
	return input()
def readChars():
	return input().split()

n = readInt()
b = readInts()

d = deque()

def renzoku(arr):
	i = -1
	boo = 1
	for i in range(1,len(arr)+1):
		#print(arr[len(arr)-i],len(arr)-i+1)
		if arr[len(arr)-i] == len(arr)-i+1:
			boo = 0
			break
	if boo:
		print(-1)
		exit()
	#print("return :",len(arr)-i)
	return len(arr)-i


for i in range(len(b)):
	r = renzoku(b)
	#print(b)
	#print(b[r])
	d.append(r)
	del b[r]

for i in reversed(d):
	print(i+1)

Submission status: Accepted

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

Submission status: Accepted

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

for i in b:
    if len(ans) >= i-1:
        ans.insert(i-1, i)
    else:
        print(-1)
        sys.exit()
for i in ans:
    print(i, sep='\n')

Submission status: Accepted

Solution code:
n = int(input())
b = list(map(int, input().split()))
fl = [False] * n
q = []
while len(b) != 0:
    ptr = -1
    for i in range(len(b)):
        if b[i] == i+1:
            ptr = i

    if ptr == -1:
        break

    _b = []
    for i in range(len(b)):
        if i == ptr:
            q.append(b[i])
        else:
            _b.append(b[i])

    b = _b.copy()

if ptr == -1:
    print(ptr)
else:
    for qq in reversed(q):
        print(qq)

Submission status: Accepted

Solution code:
N=int(input())
*A,=map(int,input().split())
ans=[]
for a in A:
    if len(ans)<a-1:
        ans=[-1]
        break
    else:
        ans.insert(a-1,a)

for x in ans:
    print(x)

Submission status: Accepted

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

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=[]

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

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

Submission status: Accepted

Solution code:
import sys

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

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

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

Submission status: Accepted

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

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

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

Submission status: Accepted

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

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

        remove_flag = False

for i in result:
    print(i)

Submission status: Accepted

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

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

Submission status: Accepted

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

ans = []
while len(ans) < N:
    j = -1
    for i,b in enumerate(B):
        if b==i+1:
            j = i
    if j==-1:
        print(-1)
        exit()
    ans.append(j+1)
    del B[j]
print(*ans[::-1], sep='\n')

Submission status: Accepted

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

import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(2147483647)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

n = I()
b = LI()
flag = [True] * n
ans = []
for i in range(n):
    cnt = 0
    idx = -1
    for j in range(n):
        if flag[j]:
            cnt += 1
            if b[j] == cnt:
                idx = j
    if idx == -1:
        print(-1)
        quit() 
    flag[idx] = False
    ans.append(b[idx])
for i in ans[::-1]:
    print(i)

Submission status: Accepted

Solution code:
n = int(input())
b = list(map(int, input().split()))
a = []
while b:
    m = len(b)
    ok = False
    for i in range(m)[::-1]:
        # print(m, i)
        if b[i] == i+1:
            a.append(b.pop(b[i]-1))
            ok = True
            break
    
    if not ok:
        print(-1)
        exit()

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

Submission status: Accepted

Solution code:
N = int(input())
B = list(map(int,input().split()))
B.insert(0,0)
A = []
while True:
    flag = 0
    for i in range(len(B)-1,0,-1):
        if B[i]==i:
            A.append(B.pop(i))
            flag = 1
            break
    if flag==0 and len(B)>1:break
    if len(B)==1:break
if len(B)>1:
    print(-1)
else:
    for _ in range(N):
        print(A.pop())

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):
    c = np.arange(1, n - i + 1)
    diff = b - c
    if not 0 in diff:
        print(-1)
        sys.exit()
    rem = np.where(diff == 0)[0][-1]
    ans.append(rem + 1)
    b = np.delete(b, rem)

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

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
N = int(input())
b = list(map(int, input().split()))
ans = []
for i in range(N):
    # それぞれの数字の余裕度を計算し、余裕度0で一番後ろのやつをとる
    d = -1
    for j in range(N-i):
        if (j+1)-b[j] == 0:
            d = j
    if d == -1:
        print(-1)
        exit()
    else:
        ans.append(b[d])
        del b[d]
[print(a) for a in ans[::-1]]

Submission status: Accepted

Solution code:
n = int(input())

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

Flag = True

memo = []

while Flag:
    Flag = False
    index = -1
    for i, v in enumerate(b):
        if i == v-1:
            Flag = True
            index = i
    if index != -1:
        m = b.pop(index)
        memo.append(m)
if len(b):
    print(-1)
else:
    memo.reverse()
    for aa in memo:
        print(aa)

Submission status: Accepted

Solution code:

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

curr_bl = bl[:]

ans = []
for i in range(n):
    rem = n - i
    deleted_j = -1
    for j in range(rem-1,-1,-1):
        if curr_bl[j] == j+1:
            deleted_j = j
            ans.append(j+1)
            break
    if deleted_j == -1:
        print(-1)
        exit()
    else:
        new_bl = []
        for j in range(rem):
            if j != deleted_j:
                new_bl.append(curr_bl[j])
        curr_bl = new_bl[:]


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

Submission status: Accepted

Solution code:
N = int(input())
b = [0] + list(map(int, input().split()))
#print(b[N])
flag = [1] * (N + 1) #抜き取った番号を記録する配列
ans = []
i = N
_i = 0
while True:
    #print(i - _i)
    if b[i - _i] > i - _i:
        print(-1)
        exit()
        break
    elif b[i - _i] == i - _i:
        ans.append(i - _i)
        del b[i - _i]
        i -= 1
        _i = 0
    else:
        _i += 1
    if _i >= i:
        #print(list(reversed(ans)))
        break
for a in reversed(ans):
    print(a)

Submission status: Accepted

Solution code:
n=input()
b=list(map(int,input().split()))
a=[]
while b:
  c=0
  for i,j in enumerate(b,1):
    if i==j:c=i
  if c==0:
    print(-1)
    exit()
  a+=[b.pop(c-1)]
print(*a[::-1],sep='\n')

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    B = list(map(int, input().split()))
    ans = list()
    while len(B) > 0:
        D = [(i + 1) - b for i, b in enumerate(B)]
        last_zero_index = -1
        for i, d in enumerate(D):
            if d < 0:
                print(-1)
                return
            if d == 0:
                last_zero_index = i
        if last_zero_index == -1:
            print(-1)
            return
        ans.append(B[last_zero_index])
        B = B[:last_zero_index] + B[(last_zero_index+1):]
    ans.reverse()
    for a in ans:
        print(a)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

anslist = []

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

anslist.reverse()
for i in anslist:
    print(i)

Submission status: Accepted

Solution code:
N = int(input())
B = list(map(int,input().split()))
ans = []
while len(ans) < N:
    for i in range(len(B) - 1, -1, -1):
        num = i + 1
        if num == B[i]:
            B.pop(i)
            ans.append(num)
            break
        if i == 0:
            print(-1)
            exit()
ans = ans[::-1]
print(*ans, sep='\n')

Submission status: Accepted

Solution code:
n=int(input())
b=[int(i) for i in input().split()]
ans=[]
while len(b)>0:
  f=0
  for i in range(len(b),0,-1):
    if b[i-1]==i:
      ans.append(i)
      del b[i-1]
      f=1
      break
  if f==0:
    print(-1)
    exit()
print(*ans[::-1],sep='\n')

Submission status: Accepted

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

ans = [0 for _ in range(n)]

for i in range(n):
    p = False
    for j in range(len(a)-1, -1, -1):
        if a[j] == j+1:
            ans[n-1-i] = a.pop(j)
            p = True
            break

    if not p:
        print(-1)
        exit()

for ai in ans:
    print(ai)

Submission status: Accepted

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

ans=[]
for i in range(N):
    for j in range(N-1-i,-1,-1):
        if b[j]==j+1:
            ans.append(b.pop(j))
            break
        elif j==0:
            print('-1')
            sys.exit()
        else:
            continue

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

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    B = list(map(int, input().split()))
    ans = []
    while B:
        for i in range(len(B)-1,-1,-1):
            if B[i] == i + 1:
                ans.append(B[i])
                B.pop(i)
                break
        else:
            print(-1)
            exit()
    for i in range(N-1,-1,-1):
        print(ans[i])





if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import sys


def input():
    return sys.stdin.readline().strip()
# sys.setrecursionlimit(100000)


def main():
    n = int(input().strip())
    B = [int(i) for i in input().strip().split()]
    ans = []
    ptr = len(B)
    while ptr > 0:
        if B[ptr - 1] == ptr:
            ans.append(B.pop(ptr - 1))
            ptr = len(B)
        else:
            ptr -= 1
    if len(ans) == n:
        for i in ans[::-1]:
            print(i)
    else:
        print(-1)

    return


if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
L = list(map(int,input().split()))
li = []
a = 0
while a <= 101:
    for i in range(len(L)-1,-1,-1):
        if i == L[i]-1:
            li.append(L[i])
            L.pop(i)
            break
    a +=1
    
if len(L) > 0:
    print(-1)
else:        
    li = li[::-1]
    for i in range(len(li)):
        print(li[i])

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
N = int(input())
ans = []
b = list(map(int,input().split()))
while b:
    i = len(b)-1
    while b[i] != i+1 and i >= 1:
        i -= 1
    if b[i] == i+1:
        ans.append(b.pop(i))
    else:
        break
if b:
    print(-1)
else:
    for i in ans[::-1]:
        print(i)

Submission status: Accepted

Solution code:
import sys

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

Submission status: Accepted

Solution code:
import sys

input = sys.stdin.readline


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

    is_ok = True
    for i, b in enumerate(B, 1):
        if i < b:
            is_ok = False
    if not is_ok:
        print(-1)
        exit()

    ans = []
    for _ in range(N):
        for i in reversed(range(len(B))):
            if B[i] == i + 1:
                break
        ans.append(B[i])
        del B[i]

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


if __name__ == "__main__":
    main()

Submission status: Accepted

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

ans = []
for i in range(N):
  flag = True
  for j in range(N-i):
    n = N-i-j
    if B[n-1] == n:
      ans.append(n)
      B.pop(n-1)
      flag = False
      break
  if flag:
    break
    
if flag:
  print(-1)
else:
  ans.reverse()
  for i in range(N):
    print(ans[i])

Submission status: Accepted

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

# from numba import njit

# input = stdin.readline

# @njit

def solve(n,b):
  a = []
  for i in range(n-1,-1,-1):
    flag = False
    for j in range(i,-1,-1):
      if b[j] == j+1:
        a.append(j+1)
        b.pop(j)
        flag = True
        break
    if not flag:
      return [-1]
  return reversed(a)



def main():
  N = int(input())
  b = list(map(int,input().split()))
  print(*solve(N,b),sep="\n")
  return

if __name__ == '__main__':
  main()

Submission status: Accepted

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

def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))

def main():
    mod=10**9+7
    N=I()
    b=LI()
    ans=[]
    for _ in range(N):
        for i in range(len(b)-1,-1,-1):
            if b[i]==i+1:
                ans.append(b[i])
                b.pop(i)
                break
            
    if len(ans)==N:
        for i in range(N):
            print(ans[-1-i])
    else:
        print(-1)
        

main()

Submission status: Accepted

Solution code:
import bisect

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

As = []
while Bs:
  f = 0
  for i in range(len(Bs)-1,-1,-1):
    if Bs[i] == i + 1:
      f = 1
      As.append(Bs.pop(i))
      break
  if f == 0:
    print('-1')
    exit()
    
for a in As[::-1]:
  print(a)

Submission status: Accepted

Solution code:
def check_val(ind):
    if ind > len(b): return False 
    if b[ind-1] == ind:
        ans.append(ind)
        del b[ind-1]
        if ind not in b: set_b.remove(ind)
        return True
    else:
        return False


N,*b = map(int,open(0).read().split())
if b[0] != 1: 
    print(-1)
    exit()

set_b = sorted(set(b),reverse=True)
ans = []

while len(b) != 0:
    flg = False
    for i in set_b:
        flg = check_val(i)
        if flg: break
    
    if flg == False:
        print(-1)
        exit() 
else: #消せるものがなくなった
    print(*ans[::-1],sep="\n")

Submission status: Accepted

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

ans = []
while B:
    flag = False
    idx = -1
    for i in range(len(B)):
        if B[i] == i+1:
            flag = True
            idx = i
    else:
        if flag:
            ans.append(B[idx])
            B.pop(idx)
        else:
            print(-1)
            exit(0)

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

Submission status: Accepted

Solution code:
n= int(input())
b= list(map(int,input().split()))
getnum = []
while len(b)>0:
    tempmax=-1
    for i,c in enumerate(b):
        if i+1==c:tempmax=i
    if tempmax==-1:
        print(-1)
        break
    else:
        getnum.append(b[tempmax])
        del b[tempmax]
else:
    for t in getnum[::-1]:print(t)

Submission status: Accepted

Solution code:
from math import ceil,floor,factorial,gcd,sqrt,log2,cos,sin,tan,acos,asin,atan,degrees,radians,pi,inf,comb
from itertools import accumulate,groupby,permutations,combinations,product,combinations_with_replacement
from collections import deque,defaultdict,Counter
from bisect import bisect_left,bisect_right
from operator import itemgetter
from heapq import heapify,heappop,heappush
from queue import Queue,LifoQueue,PriorityQueue
from copy import deepcopy
from time import time
from functools import reduce
import string
import sys
sys.setrecursionlimit(10 ** 7)
def input() : return sys.stdin.readline().strip()
def INT()   : return int(input())
def MAP()   : return map(int,input().split())
def LIST()  : return list(MAP())

n = INT()
b = LIST()
ans = deque()

while b:
    m = len(b)
    for i in range(m-1, -1, -1):
        if b[i] == i + 1:
            ans.appendleft(b.pop(i))
            break
    else:
        print(-1)
        exit()
for x in ans:
    print(x)