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(lambda s: int(s) - 1, input().split()))

ops = []

for i in range(N, 0, -1):
    for j in range(i - 1, -1, -1):
        if b[j] == j:
            ops.append(j)
            b = b[:j] + b[j + 1:]
            break
    else:
        ops = [-2]
        break

for i in range(len(ops) - 1, -1, -1):
    print(ops[i] + 1) # 1-based indexing

Submission status: Accepted

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

for i in range(n):
    for j in range(n-i)[::-1]:
        k = j+1
        if b[j] == k:
            del b[j]
            ans.append(k)
            break
    else:
        ans = [-1]
        break

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys, math, itertools, collections, bisect
input = lambda: sys.stdin.buffer.readline().rstrip().decode('utf-8')
inf = float('inf') ;mod = 10**9+7
mans = inf ;ans = 0 ;count = 0 ;pro = 1

n=int(input())
A=list(map(int,input().split()))
used=[0]*n
ans=[]
for _ in range(n):
    for i in range(len(A)-1,-1,-1):
        if A[i]==i+1:
            ans.append(i+1); count += 1
            # print(A,i+1)
            del A[i]
            break
ans.reverse()
# print(ans)
print(*ans if count == n else [-1],sep="\n")

Submission status: Accepted

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

ans = []
while len(b) > 0:
    flag = False
    for i in range(len(b)-1, -1, -1):
        if b[i] == i+1:
            ans.append(b[i])
            b.pop(i)
            flag = True
            break
    if not flag:
        print(-1)
        exit()

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

Submission status: Accepted

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

def main():
    N = int(input())
    a = list(map(int,input().split()))
    ans = []
    for i in range(N):
        l = len(a)
        for i in reversed(range(l)):
            if a[i] == i+1:
                p = a.pop(i)
                ans.append(p)
                break
                
    if a != []:
        print(-1)
    else:
        for i in range(N):
            print(ans[-1-i])

if __name__ == "__main__":
    main()

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
# これかなり難しくないですか
# 解説見た...

n = int(input())
b_lis = list(map(int, input().split()))
ans = []
while b_lis:
    tmp = -1
    for i, v in enumerate(b_lis):
        if v == i + 1:
            tmp = i
    if tmp >= 0:
        ans.append(tmp + 1)
        b_lis = b_lis[:tmp] + b_lis[tmp + 1 :]
    else:
        print(tmp)
        exit()

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

Submission status: Accepted

Solution code:
from sys import stdin
from itertools import groupby

n = int(stdin.readline().rstrip())
li = list(map(int,stdin.readline().rstrip().split()))
lin = []

while li:
    now = 10**10
    for i,j in enumerate(li,1):
        if i == j:
            now = i
    if now == 10**10:
        print(-1)
        exit()
    lin.append(now)
    li.pop(now-1)

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

Submission status: Accepted

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

ans = []
while len(b) > 0:
    ind = -1
    for i in range(len(b)):
        if b[i] == i + 1:
            ind = i
    if ind == -1:
        break
    else:
        ans.append(b.pop(ind))
    
if ind == -1:
    print(-1)
else:
    print(*ans[::-1], sep='\n')

Submission status: Accepted

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

while len(b) > 0:
    m = -1
    for i in range(len(b)):
        if b[i] == i + 1:
            m = i
    if m == -1:
        break
    else:
        s.append(b.pop(m))

if m == -1:
    print(-1)
else:
    print(*reversed(s), sep='\n')

Submission status: Accepted

Solution code:
n = int(input())
lst = list(map(int,input().split()))
stock = []
flg = True
for i in range(n):
    tmp = -100
    for j in range(len(lst)):
        if lst[j] == j+1:
            tmp = max(tmp, j)
    if tmp == -100:
        flg =  False
        break
    else:
        stock.append(lst[tmp])
        lst = lst[:tmp]+lst[tmp+1:]
if not flg:
    print(-1)
else:
    for x in stock[::-1]:
        print(x)

Submission status: Accepted

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

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

Submission status: Accepted

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

c = sorted(b)

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

ans = []

for i in range(n):
    s = b[i]
    ans.insert(s-1, s)

print(*ans)

Submission status: Accepted

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

res = []

while len(B) > 0:
    p = -1
    for i, b in enumerate(B[::-1]):
        j = len(B) - i
        if b == j:
            p = j
            break
    if p == -1:
        print(-1)
        exit()
    else:
        res.append(p)
        B.pop(p - 1)

for r in res[::-1]:
    print(r)

Submission status: Accepted

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

def solve(l,ans):
	if l == []:
		return ans
	for i in range(len(l)-1,-1,-1):
		if l[i] == i+1:
			ans.append(i+1)
			return solve(l[:i]+l[i+1:], ans)

	return 0


A = solve(a,[])
if A == 0:
	print(-1)
else:
	for a in A[::-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.append(B.pop(j))
            break
    else:
        print(-1)
        break
else:
    for a in ans[::-1]:
        print(a)

Submission status: Accepted

Solution code:
def solve(n, b):
    a = []
    while b:
        m = len(b)
        tj = -1
        for j in range(m, 0, -1):
            if b[j-1] == j:
                tj = j
                break
        if tj == -1:
            return -1
        else:
            a.append(tj)
            b = b[:tj-1] + b[tj:]
    return "\n".join(map(str, a[::-1]))

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

Submission status: Accepted

Solution code:
import math
import bisect
import collections
import itertools
import heapq
import sys
sys.setrecursionlimit(10**9)
def gcd(a,b):return math.gcd #最大公約数
def lcm(a,b):return (a*b) // math.gcd(a,b) #最小公倍数
def iin(): return int(input()) #整数読み込み
def isn(): return input().split() #文字列読み込み
def imn(): return map(int, input().split()) #整数map取得
def iln(): return list(map(int, input().split())) #整数リスト取得
def iln_s(): return sorted(iln()) # 昇順の整数リスト取得
def iln_r(): return sorted(iln(), reverse=True) # 降順の整数リスト取得
def join(l, s=''): return s.join(l) #リストを文字列に変換
def perm(l, n): return itertools.permutations(l, n) # 順列取得
def comb(l, n): return itertools.combinations(l, n) # 組み合わせ取得
def 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
def is_prime(n):
    if n == 1: return False
    for i in range(2, int(n**0.5) + 1):
        if n % i == 0: return False 
    return True
def primes(n):
    is_prime = [True] * (n+1)
    is_prime[0] = False
    is_prime[1] = False
    for i in range(2, int(n**0.5)+1):
        if not is_prime[i]: continue
        for j in range(i * 2, n + 1, i): is_prime[j] = False
    return [i for i in range(n+1) if is_prime[i]]
def sieve_of_e(n):
    is_prime = [True] * (n+1)
    is_prime[0] = False
    is_prime[1] = False
    for i in range(2, int(n**0.5)+1):
        if not is_prime[i]: continue
        for j in range(i * 2, n+1, i): is_prime[j] = False
    return is_prime

N = iin()
b = iln()
ans = []
for _ in range(N):
    if len(b) == 0: break
    matched = -1
    for j in range(0, len(b)):
        if b[j] == j+1: matched = j
    if matched != -1:
        ans.append(b.pop(matched))
    else:
        ans.clear()
        break
if len(ans) > 0:
    for _ in range(len(ans)):
        print(ans.pop())
else:
    print(-1)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input())
b=list(map(int,input().split()))
i=0
c=[]
while len(b)>0 and i<len(b):
  if b[len(b)-i-1]==len(b)-i:
    c.append(len(b)-i)
    b.remove(b[len(b)-i-1])
    i=0
  else:
    i+=1
if len(b)==0:
  for i in range(n):
    print(c[n-i-1])
else:
  print(-1)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
b = list(map(int, input().split()))
i = n - 1
ans = []
while i > -1 and n != 0:
    if b[i] == i + 1:
        ans.append(b.pop(i))
        n -= 1
        i = n - 1
    else:
        i -= 1
if n == 0:
    ans.reverse()
    print(*ans, sep='\n')
else:
    print(-1)

Submission status: Accepted

Solution code:
import sys

stdin = sys.stdin
sys.setrecursionlimit(10 ** 7)

ri = lambda: int(rs())
rl = lambda: list(map(int, stdin.readline().split()))  # applies to only numbers
rs = lambda: stdin.readline().rstrip()  # ignores trailing space

N = ri()
B = rl()
l = []
for i in range(N):
    if i + 1 < B[i]:
        print(-1)
        exit()

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

l = l[::-1]
print(*l, sep='\n')
# 47

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

N,*A = map(int,read().split())

"""
・任意の i に対して A[i] <= i が必要
・このときできる。次のようにすればよい
・A[i] = i となる 最大の i を取り除く（i=1が満たすので存在）。帰納法がまわってできる。
"""

bl = all(x <= i for i,x in enumerate(A,1))
if not bl:
    print(-1)
    exit()

answer = []
while A:
    I = [i for i,x in enumerate(A) if i + 1 == x]
    i = I[-1]
    answer.append(i + 1)
    A = A[:i] + A[i+1:]

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

Submission status: Accepted

Solution code:
res = []
def f(l):
    n = len(l)
    for i in range(n)[::-1]:
            if l[i] == i:
                res.append(i+1)
                l.pop(i)
                break
    return l

N = int(input())
a = [int(_)-1 for _ in input().split()]

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

Submission status: Accepted

Solution code:
n = int(input())
L = list(map(int, input().split()))
rep = []
for i in range(n):
  for j in range(n-1-i, -1, -1):
    if L[j] == j+1:
      rep.append(L.pop(j))
      break
  else:
    print(-1)
    exit()
print(*rep[::-1], sep="\n")

Submission status: Accepted

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

def f(b):
  i=len(b)-1
  while i>=0 and b[i]!=i+1:
    i-=1
  if i>=0:
    del b[i]
    return i, b
  else:
    return -1, []

j, b_=f(b)
while j>=0:
  a=[j]+a
  j, b_=f(b_)
  
if len(a)==N:
  for i in range(N):
    print(a[i]+1)
else:
  print(-1)

Submission status: Accepted

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

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

    answer = []
    while b:
        for i in range(len(b)-1,-1,-1):
            if b[i] == i+1:
                answer.append(b[i])
                b.pop(i)
                break
        else:
            print(-1)
            return
    for i in range(N-1,-1,-1):
        print(answer[i])
    return 

if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
N = int(input())
b = list(map(int, input().split(' ')))

buf = []
for _ in range(N):
    is_end = True
    for i in range(len(b)-1, -1, -1):
        if i+1 == b[i]:
            buf.append(b[i])
            b.pop(i)
            is_end = False
            break

    # 終了条件
    if is_end:
        print(-1)
        exit()

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

Submission status: Accepted

Solution code:
from collections import deque
import itertools

N=int(input())
b=[-1]+list(map(int,input().split()))
ans=deque([])
for i in range(N):
    i=len(b)-1
    while i>0:
        if b[i]==i:
            ans.appendleft(str(i))
            b=list(itertools.chain(b[:i],b[i+1:]))
            break
        i-=1
    else:
        break
if len(b)==1:
    print("\n".join(ans))
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()
def order(a):
    if a == [1]:
        return [1]
    else:
        x = order(a[:-1])
        y = a[-1]
        x.insert(y-1,y)
        return x
print(*order(a),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 reversed(range(len(b))):
        if b[j] == j+1:
            b.pop(j)
            ans.append(j+1)
            break
ans.reverse()
if len(b) == 0:
    for a in ans:
        print(a)
else:
    print(-1)

Submission status: Accepted

Solution code:
N = int(input())
b = list(map(int, input().split()))
ans = []
while len(b):
    tmp = len(b)
    # len(b) - 1 - pointa
    for i in reversed(range(len(b))):
        if b[i] == i+1:
            ans.append(b[i])
            b.pop(i)
            break
    if len(b) == tmp:
        print(-1)
        exit()
ans.reverse()
for a in ans:
    print(a)

Submission status: Accepted

Solution code:
from collections import deque
import sys

N = int(input())

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

q = deque([])

q.append([b,[]])

dic = {}

while len(q) > 0:

    now = q.pop()

    nb = now[0]
    nc = now[1]

    for i in range(len(nb)):

        i = len(nb) - i - 1

        if (i+1) == nb[i]:

            nex = []
            nec = []
            for j in range(len(nb)):
                if j != i:
                    nex.append(nb[j])
            for j in range(len(nc)):
                nec.append(nc[j])
            
            nec.append(nb[i])
            

            s = " ".join(map(str,nex))
            if s in dic:
                continue
            dic[s] = 1

            if len(nex) == 0:
                nec.reverse()
                print (" ".join(map(str,nec)))
                sys.exit()

            q.append([nex,nec])
            break

print (-1)

Submission status: Accepted

Solution code:
n = int(input())
lst_b = list(map(int,input().split()))
lst_a = []
i = 0
while True:
    if len(lst_b) == 0:
        break
    else:
        for i in range(len(lst_b)-1,-1,-1):
            if lst_b[i] == i+1:
                lst_a.append(lst_b.pop(i))
                break
            elif i == 0:
                print(-1)
                exit()

lst_a.reverse()

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

Submission status: Accepted

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

Submission status: Accepted

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

l=[]
f=True
for i in range(1,N+1):
    if not f:
        break
    for j in range(N+1-i,0,-1):
        if b[j]==j:
            l.append(b.pop(j))
            break
        if j==1:
            f=False

if f:
    for k in l[::-1]:
        print(k)
else:
    print(-1)

Submission status: Accepted

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

a=[]
while b:
    f=True
    if len(b)==1:
        if b[0]!=1:
            print(-1)
            exit()
        else:
            a.append(b.pop())
        break
    for i in range(len(b)-1,-1,-1):
        if b[i]==i+1:
            temp=b.pop(i)
            a.append(temp)
            f=False
            break
    if f:
        print(-1)
        exit()

for i in range(N):
    print(a[-1*(i+1)])

Submission status: Accepted

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

ans = []
for i in range(n):
    tmp = -1
    for j, k in enumerate(b):
        if j + 1 == k:
            tmp = j
    if tmp == -1:
        print(-1)
        exit()
    else:
        ans.append(b.pop(tmp))
print(*ans[::-1], sep="\n")

Submission status: Accepted

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


    ok = True
    r = []
    while len(B) != 0:
        match = []
        for i, bi in enumerate(B):
            n = i+1
            if n == bi:
                match.append(i)

        if len(match) == 0:
            ok = False
            break

        t = match[-1]
        r.append(t)
        B.pop(t)
        match = []

    if ok:
        for ri in r[::-1]:
            print(ri + 1)
    else:
        print(-1)



if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
N = int(input())
B = list(map(int, input().split()))
M = 0
for i in range(N):
  if B[i] > i+1:
    M = -1
if M != -1:
  L = []
  for j in range(N):
    s = 0
    for k in range(len(B)):
      if B[k] == k+1:
        s = k
    L.insert(0, s+1)
    del B[s]
  for m in range(N):
    print(L[m])
else:
  print(M)

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))[::-1]:
        if b[j] == j+1:
            ans.append(b.pop(j))
            break
        if j == 0:
            print(-1)
            sys.exit()
    
for i in range(N)[::-1]:
    print(ans[i])

Submission status: Accepted

Solution code:
N = int(input())
B = list(map(int, input().split()))
A = []
i = len(B) - 1
flag = True

while B != []:
    if i < 0:
        flag = False
        break

    if i + 1 == B[i]:
        A.append(B.pop(i))
        i = len(B) - 1
    else:
        i -= 1


if flag:
    print(*A[::-1], sep="\n")
else:
    print(-1)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
N = int(input())
a = list(map(int, input().split()))
res = []
for x in a:
  if len(res) + 1 < x:
    print(-1)
    exit(0)
  res.insert(x - 1, x)
for r in res: print(r)

Submission status: Accepted

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

Submission status: Accepted

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

ans = [None] * N

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

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

Submission status: Accepted

Solution code:
def examA():
    N = I()
    B = LI()
    ans = [0]*N
    flag = False
    for i in range(N-1,-1,-1):
        for j in range(i,-1,-1):
            if j+1==B[j]:
                ans[i] = B[j]
                del B[j]
                break
        if ans[i]==0:
            print("-1")
            flag = True
            break
    if not flag:
        for v in ans:
            print(v)

from string import digits
import sys,copy,bisect,itertools,math
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict,deque
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def SI(): return sys.stdin.readline().strip()
mod = 10**9 + 7
inf = float('inf')

examA()

Submission status: Accepted

Solution code:
import sys

readline = sys.stdin.buffer.readline


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

    A = []
    while B:
        for i in range(len(B)-1, -1, -1):
            if B[i] == i+1:
                A.append(B[i])
                del B[i]
                break

        else:
            print(-1)
            sys.exit()

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


main()

Submission status: Accepted

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

l = [n]
ans = []
for i in range(n):
    x = 0
    f = 0
    for j in range(i+1):
        if i+1-j in b[x:l[j]]:
            ans.append(i+1-j)
            l.append(b.index(i+1-j))
            b[l[-1]] = 0
            l.sort()
            f = 1
            break
        x = l[j]
    if f == 0:
        print(-1)
        exit()

for i in ans:
    print(i)

Submission status: Accepted

Solution code:
n=int(input())
l=list(map(int,input().split()))
b=True
ans=[]
for i in range(n):
  index=-1
  for j in range(n-i):
    if l[j]>j+1:
      b=False
      index=-1
      break
    if l[j]==j+1:
      index=j
  if index==-1:
    b=False
    break
  ans.append(l[index])
  del l[index]
if not b:
  print(-1)
else:
  for i in range(1,n+1):
    print(ans[-i])

Submission status: Accepted

Solution code:
n = int(input())
b = list(map(int,input().split()))
ans = []
flag = 0
if b[0] != 1:
  flag = 1
i = n
key = 0
if flag == 0:
  while(i != 1):
    key = 0
    for j in range(0,i):
      if b[j] == j+1:
        key = j+1
      elif j == i-1 and key == 0:
        flag = 1
        break
    ans.append(key)
    b.pop(key-1)
    i -= 1
ans.reverse()
if b[0] != 1:
  flag = 1
ans.insert(0,b[0])
if flag == 0:
  for k in range(n):
    print(ans[k])
else:
  print(-1)

Submission status: Accepted

Solution code:
# 2019-11-24 20:41:45(JST)
import sys
from collections import deque 

def main():
    n, *b = map(int, sys.stdin.read().split())
    b = [None] + b

    for i in range(1, n+1):
        if b[i] > i:
            print(-1)
            exit()
    
    res = deque()
    while len(b) > 1:
        for i in range(len(b)-1, 0, -1):
            if b[i] == i:
                res.appendleft(b.pop(i))
                break
         
    print('\n'.join(map(str, res)))

if __name__ == '__main__':
    main()

Submission status: Accepted

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

sousa = []

for i in range(N,0,-1):
  for j in range(i,0,-1):
    if B[j-1] == j:
      sousa.append(j)
      del B[j-1]
      break
      
  if j <= 0:
    break
    
if len(B) > 0:
  print(-1)
  exit(0)
#print(sousa)

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

Submission status: Accepted

Solution code:
N = int(input())
B = list(map(int, input().split()))
ans_list = []
for i in range(N):
    for k in range(len(B)-1, -1, -1):
        if B[k] == k+1:
            ans_list.append(B[k])
            del B[k]
            break
        if k == 0:
            print(-1)
            exit()
for k in range(len(ans_list)):
    print(ans_list[- 1 - k])

Submission status: Accepted

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

Submission status: Accepted

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

def solve() :
    ret = []
    while len(b) != 0 :
        M = -1
        for i in range(len(b)) :
            if i + 1 == b[i] :
                M = i
        
        if M == -1 :
            print(-1)
            return 
        
        ret.append(b.pop(M))
    
    for r in ret[::-1] :
        print(r)
        
solve()

Submission status: Accepted

Solution code:

import sys

input = sys.stdin.readline

# input = sys.stdin.readline
sys.setrecursionlimit(2147483647)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7


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

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

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


if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input())
import copy
b=list(map(int,input().split()))
c=copy.copy(b)
ans=[]
for _ in range(n):
	rem=-1
	cnt=0
	for j in range(n):
		if b[j]==j+1-cnt:
			rem=j
		if b[j]==0:
			cnt+=1
	if rem==-1:
		print(-1)
		exit()
	ans.append(rem)
	b[rem]=0
for i in range(n):
	p=ans[-i-1]
	print(c[p])

Submission status: Accepted

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

for i in range(N):
    if((S[i] - i)>1):
        print("-1")
        exit()  

while(len(S)>0):
    c=10**10
    for i in range (len(S)):
        if(S[i]==i+1):
            c=i
    if(c==(10**10)):
        print("-1")
        exit()  
    LIST.append(S.pop(c))

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

Submission status: Accepted

Solution code:
# A
import copy
N = int(input())
b = list(map(int,input().split()))
flag = True
ans = copy.copy(b)
for i in range(N):
    check = i + 1 - b[i]
    if check < 0:
        flag = False
        break
    elif check > 0:
        ans.insert(b[i]-1,ans.pop(i))

if flag:
    for i in ans:
        print(i)
else:
    print('-1')

Submission status: Accepted

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

    ans = []

    i = N-1
    while i >= 0:
        if b[i] == i + 1:
            ans.append(b[i])
            del b[i]
            i = len(b) - 1
        else:
            i -= 1

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

if __name__ == '__main__':
    solve()

Submission status: Accepted

Solution code:

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

for i in range(N):
    tmp = -1
    for j in range(N - i):
        if b[j] == j + 1:
            tmp = j
            
    if tmp != -1:
        a.append(b.pop(tmp))

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

Submission status: Accepted

Solution code:
import math
import heapq
import sys
from collections import Counter

#N,K=map(int, input[].split())  #複数数値入力　「A B」みたいなスペース空いた入力のとき
#x=list(map(int, input[].split()))  #リスト入力 「a1 a2 a3 ...」みたいな配列のような入力のとき

N=int(input())
b=list(map(str,input().split()))  #リスト入力 「a1 a2 a3 ...」みたいな配列のような入力のとき

a=[]

for i in range(1,N+1):
    for k in reversed(range(len(b))):
        #rint('{}回目 要素番号:{} 値:{} {}{}'.format(i,k+1,b[k],b,a))
        if k+1==int(b[k]):
            a.insert(0,b.pop(k))
            break
if len(a)==N:
    for i in a:
        print(int(i))
else:
    print('-1')

Submission status: Accepted

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

if len(b)!=0:
    print(-1)
    exit()

ans.reverse()

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

Submission status: Accepted

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

ans = []
i = len(b)-1
while(1):
    if i == -1:
        print("-1")
        exit()
    if b[i] == i + 1:
        #print("check")
        ans.append(b.pop(i))
        if len(b) == 0:
            break
        i = len(b)-1
    else:
        i -= 1



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

Submission status: Accepted

Solution code:
n = int(input())
num = [ int(v) for v in input().split() ]

def solve(inlist):
    if inlist == []:
        return []
    else:
        p = max([ i for i, v in enumerate(inlist,1) if i == v ])
        new_list = [ v for i, v in enumerate(inlist,1) if i != p ]
        return solve(new_list) + [p]

if all([ v <= i for i, v in enumerate(num,1) ]):
    for i in solve(num):
        print(i)
else:
    print(-1)

Submission status: Accepted

Solution code:
n = int(input())
num = [ int(v) for v in input().split() ]

def solve(inlist):
    if inlist == []:
        return []
    else:
        p = 0
        for i, v in enumerate(inlist):
            if v-1 == i:
                p = i
        new_list = [ v for i, v in enumerate(inlist) if i != p ]
        return solve(new_list) + [p+1]

if all([ v <= i for i, v in enumerate(num,1) ]):
    for i in solve(num):
        print(i)
else:
    print(-1)

solve(num)