Problem:


Score : 400 points

Problem Statement

Snuke has an empty sequence a.

He will perform N operations on this sequence.

In the i-th operation, he chooses an integer j satisfying 1 \leq j \leq i, and insert j at position j in a (the beginning is position 1).

You are given a sequence b of length N. Determine if it is possible that a is equal to b after N operations. If it is, show one possible sequence of operations that achieves it.

Constraints

All values in input are integers.

1 \leq N \leq 100

1 \leq b_i \leq N

Input

Input is given from Standard Input in the following format:

N
b_1 \dots b_N

Output

If there is no sequence of N operations after which a would be equal to b, print -1.
If there is, print N lines. In the i-th line, the integer chosen in the i-th operation should be printed. If there are multiple solutions, any of them is accepted.

Sample Input 1

3
1 2 1

Sample Output 1

1
1
2

In this sequence of operations, the sequence a changes as follows:

After the first operation: (1)

After the second operation: (1,1)

After the third operation: (1,2,1)

Sample Input 2

2
2 2

Sample Output 2

-1

2 cannot be inserted at the beginning of the sequence, so this is impossible.

Sample Input 3

9
1 1 1 2 2 1 2 3 2

Sample Output 3

1
2
2
3
1
2
2
1
1



Sample input:
3
1 2 1


Sample output:
1
1
2

Submission status: Accepted

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

ans=[-1]*N
for i in reversed(range(N)):
  for j in reversed(range(1,i+2)):
    br=0
    if b[j-1]==j:
      ans[i]=j
      b.pop(j-1)
      br=1
      break
  if br==0:
    print(-1)
    exit()
      
for i in ans:
  print(i)

Submission status: Accepted

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

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

        else:
            ans = [-1]
            break

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


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
class Node:
    def __init__(self, prev, val, next):
        self.prev = prev
        self.val = val
        self.next = next


NIL = 0
START = -1
END = -2


class List:
    def __init__(self, li):
        li.append(END)
        self.li = [Node(NIL, START, NIL)]
        for e in li:
            node = Node(self.li[-1], e, NIL)
            self.li[-1].next = node
            self.li.append(node)

        self.l = len(self.li) - 1
        self.ans = []

    def step(self):
        i = self.l
        u = self.li[-1]
        while u.val != START:
            if u.val == i:
                u.prev.next = u.next
                u.next.prev = u.prev
                self.ans.append(u.val)
                self.l -= 1
                return True

            else:
                u = u.prev
                i -= 1

        self.ans = [-1]
        return False

    def solve(self):
        for _ in range(self.l - 1):
            if not self.step():
                break

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


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

    l = List(b)
    l.solve()


if __name__ == "__main__":
    main()

Submission status: Accepted

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

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

Submission status: Accepted

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

N = int(input().split()[0])
b_list = list(map(int, input().split()))
w_list = b_list[:]

# 引いていく方法で考える
is_ok = True
ans_list = []

# 深さ優先探索か？
# 候補が複数ある場合が考えられる
while w_list:
    kouho_list = [i for i, w in enumerate(w_list) if w == i + 1]
    if not kouho_list:
        is_ok = False
        break

    # 右側から処理すると順序が変わらないので、
    # 右側から抜いていく
    kouho_list = list(reversed(kouho_list))
    ans_list.append(w_list.pop(kouho_list[0]))

if is_ok:
    ans_list = [str(x) for x in reversed(ans_list)]
    ans = "\n".join(ans_list)
else:
    ans = -1
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import Counter,defaultdict,deque
from heapq import heapify,heappop,heappush
import sys,bisect,math,itertools,string,queue
sys.setrecursionlimit(10**8)
mod = 10**9+7
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpl_str(): return list(sys.stdin.readline().split())
def inpln(n): return list(int(sys.stdin.readline()) for i in range(n))

n = inp()
a = inpl()
res = []
for i in range(n):
    for j in range(len(a))[::-1]:
        if a[j] == j+1:
            res.append(a[j])
            del a[j]
            break
    else:
        print(-1)
        quit()
for i in range(n)[::-1]:
    print(res[i])

Submission status: Accepted

Solution code:
from collections import Counter,defaultdict,deque
from heapq import heapify,heappop,heappush
import sys,bisect,math,itertools,string,queue
sys.setrecursionlimit(10**8)
mod = 10**9+7
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpl_str(): return list(sys.stdin.readline().split())
def inpln(n): return list(int(sys.stdin.readline()) for i in range(n))

n = inp()
a = inpl()
res = []
for i in range(n):
    for j in range(len(a))[::-1]:
        # print(j)
        if a[j] > j+1:
            print(-1)
            quit()
        elif a[j] == j+1:
            res.append(a[j])
            del a[j]
            break
    # print(a)
for i in range(n)[::-1]:
    print(res[i])

Submission status: Accepted

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

    ans = list()

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

            if j is 0:
                print(-1)
                exit()
    for i in reversed(range(N)):
        print(ans[i])

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#import math
import copy
#import sys
#import bisect
#input = sys.stdin.readline
mod=10**9+7
def factorial(x): #階乗計算
    n=1
    for i in range(x):
        n=(n*(i+1))%mod
    return n

def gcd(a, b):
    if b == 0:
        return a
    else:
        return gcd(b, a%b)
def lcm(a, b):
    return((a*b)//gcd(a,b))
def div(x): #約数配列
    div=[1]
    check=2
    while(x!=1 and check <= int(x**0.5)+2):
        if(x%check==0):
            div.append(check)
            while(x%check==0):
                x=x//check
        check+=1
    if x != 1:
      div.append(x)
    return div
def div2(x): #素因数分解配列
    div2=[]
    check=2
    while(x!=1 and check <= int(x**0.5)+2):
        while x%check==0:
            div2.append(check)
            x/=check
        check+=1
    if x != 1:
      div2.append(x)
    return div2

def main():
    # x,y = map(int,input().split())           x=1,y=2
    # a = input().split()                      a=['1','2','3',...,'n']
    # a = list(map(int,input().split()))       a=[1,2,3,4,5,...,n]
    # li = input().split('T')	              FFFTFTTFF => li=['FFF', 'F', '', 'FF']
    # 複数行の「1 2 3 4」型の入力を一配列に
    # x = sorted([list(map(int, input().split())) for _ in range(n)])
    # ソート x.sort(key=lambda y:y[1])
    # [chr(i) for i in range(97, 97+26)] 英語小文字リスト
    # [chr(i) for i in range(65, 65+26)] 英語大文字リスト
    n = int(input())
    b = list(map(int,input().split()))
    key=0
    for i in range(n):
        if b[i]<=i+1:
            True
        else:
            key=1
            break
    if key==1:
        print(-1)
    else:
        ans=[]
        while b!=[]:
            for i in range(len(b)):
                if b[len(b)-1-i]==len(b)-i:
                    ans.append(b.pop(len(b)-1-i))
                    break
        ans.reverse()
        for i in range(len(ans)):
            print(ans[i])

main()

Submission status: Accepted

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

        if j==0:
            print(-1)
            exit()
for i in reversed(range(n)):
    print(ans[i])

Submission status: Accepted

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

for i in range(N, 0, -1):
    for j in range(len(b) - 1, 0, -1):
        if j == b[j]:
            answer.append(j)
            b.remove(j)
            break
    else:
        print(-1)
        exit()
else:
    print('\n'.join(map(str, reversed(answer))))

Submission status: Accepted

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

while len(b) > 0:
    for i, v in zip(b[::-1], range(len(b), 0, -1)):
        if i == v:
            removed.append(b.pop(i - 1))
            break
    else:
        removed = [-1]
        break

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import copy

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


root=[]
string = copy.deepcopy(a)
while len(string)!=0:
    temp=[]
    for i,s in enumerate(string):
        if i+1 == s:
            temp.append(i+1)
    if len(temp)==0:
        break
    
    if temp[0]==1 and len(temp)==1:
        string.pop(0)
        root.append(1)
    else:
        if 1 in temp:
            temp.remove(1)
        root.append(temp[-1])
        string.pop(temp[-1]-1)
        
if len(string)!=0:
    print(-1)
else:
    for i in root[::-1]:
        print(i)

Submission status: Accepted

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

print(*reversed(A),sep="\n")

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    b = list(map(int,input().split()))
    f = []
    res = []
    while b:
        for i in range(len(b)):
            if i+1 == b[i]:
                f.append(i)
        if f:
            tg = max(f)
        else:
            print(-1)
            return False
        res.append(tg+1)
        b.pop(tg)
        f.clear()
    
    res.reverse()
    for r in res:
        print(r)

main()

Submission status: Accepted

Solution code:
N = int(input())
array = input().split()
flag = True
lst = [0]
for i in range(1, N+1):
        x = int(array[i-1])
        if len(lst) < x:
            flag = False
            break
        lst.insert(x, x)

if flag:
    for i in lst[1:]:
        print(i)
else:
    print(-1)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
ni = lambda: int(input())
nm = lambda: map(int, input().split())
nl = lambda: list(map(int, input().split()))

n = ni()
b = nl()

ans = []

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

print(' '.join([str(_) for _ in reversed(ans)]))

Submission status: Accepted

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

def a():
    A = []
    while len(B) > 0:
        lb = len(B)
        for i in range(len(B)-1, -1, -1):
            if i == B[i]-1:
                A.append(B[i])
                t = B.pop(i)
                break
        if len(B) == lb:
            return -1
    return A

aa = a()
if aa != -1:
    for i in aa[::-1]:
        print(i)
else:
    print(-1)

# def b():
#     while True:
#         if True:
#             break
#         return 1
#     return 0

# print(b())

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!
N=int(input())
l=list(map(int,input().split()))
pre=[]
ans=[]

while l:
    if pre==l:
        print(-1)
        exit()
    pre.clear()
    pre+=l
    for i in range(len(l))[::-1]:
        if l[i]==i+1:
            ans.append(l.pop(i))
            break

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

Submission status: Accepted

Solution code:
N = int(raw_input())
A = map(int, raw_input().split())

T = []

for i in A:
    if len(T) >= i - 1:
        T.insert(i - 1, i)
    else:
        print -1
        quit()

for i in T:
    print i

Submission status: Accepted

Solution code:
n = int(input())

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

len = n
for j in range(n-1,-1,-1):
    len -= 1
    for i in range(min(j,len),-1,-1):

        if b[i]== i+1:
            ans.append(i)
            b.pop(i)
            break
                
ans.reverse()
if not b:
    for i in range(n):
        print(ans[i] + 1)
else:
    print(-1)

Submission status: Accepted

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

ans = []
for _ in range(n):
    #print(b)
    check = 0
    for i in range(len(b)-1, -1, -1):
        if b[i] == i+1:
            del b[i]
            ans.append(i+1)
            check = 1
            break
    if check == 0:
        print(-1)
        exit()

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

Submission status: Accepted

Solution code:
import sys

sys.setrecursionlimit(10**7)

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

used = [False] * n
res = []


def f(index):
    for j in range(n):
        for i in reversed(range(index, n)):
            if not used[i] and ls[i] == sum(used[:i]) + 1:
                used[i] = True
                res.append(ls[i])
                f(i)
        if sum(used[index:]) == n - index:
            break


f(0)
if len(res) == n:
    print(*res, sep='\n')
else:
    print(-1)

Submission status: Accepted

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

ans = []
while(len(B) > 0):
  for i in range(len(B)):
    if B[i] > i+1:
      print(-1)
      exit()
    if B[i] == i+1:
      a = i+1
  ans.append(a)
  if a == 1:
    B = B[a:]
  elif a == len(B):
    B = B[:a-1]
  else:
    B = B[:a-1]+B[a:]
for i in range(len(ans)):
  print(ans[-i-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(b.pop(i))
            break
    else:
        ans = [-1]
        break

ans.reverse()
print(*ans, sep="\n")

Submission status: Accepted

Solution code:
N = int(input())
B = list(map(int, input().split()))
res = []
while 0 < len(B):
    index = None
    for i, b in enumerate(B, 1):
        if i == b:
            index = i
    if index is None:
        print(-1)
        exit()
    else:
        res.append(B[index-1])
    del B[index-1]
for x in res[::-1]:
    print(x)

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
N = int(input())
bs = list(map(int, input().split()))
js = []
for i in range(N):
  k = -1
  for j in reversed(range(N-i)):
    if bs[j] == j+1:
      k = j
      break
  if k < 0:
    break
  js.append(k)
  bs.pop(j)
if not len(js) == N:
  print(-1)
  exit()
for j in reversed(js):
  print(j+1)

Submission status: Accepted

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

if len(b) == 0:
    for i in reversed(range(N)):
        print(ans[i])
else:
    print(-1)

Submission status: Accepted

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

def solve(A):
  res = []
  while len(A) > 0:
    k = -1
    i = 0
    while i < len(A):
      if A[i] == (i+1):
        k = i
      i += 1
    if k == -1:
      return -1
    res.append(k+1)
    A = A[:k] + A[k+1:]
  return res[::-1]

res = solve(A)
if res == -1:
  print(res)
else:
  print("\n".join(map(str,res)))

Submission status: Accepted

Solution code:
n=int(input())
b=[int(i) for i in input().split()]
c=[]
for _ in range(n):
  for i in range(len(b)-1,-1,-1):
    if i+1==b[i]:
      del b[i]
      c.append(i+1)
      break
  else:
    print(-1)
    exit()
for i in c[::-1]:
  print(i)

Submission status: Accepted

Solution code:
n = int(input())
B = list(map(int, input().split()))
A = []
C = []
flag = True
j = 0
if max(B) > n:
  flag = False
if flag:
  while j < n:
    A = []
    for i in range(n-j):
      if B[i] == i+1:
        A.append(i+1)
    if A ==[]:
      flag = False
      break
    else:
      C.append(B[max(A)-1])
      del B[max(A)-1]
      j += 1
if flag:
  for k in range(1, n+1):
    print(C[-k])
else:
  print(-1)

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    b = list(map(int, input().split()))
    ansl = []
    for i in range(n):
        f = False
        for j in range(n - i - 1, -1, -1):
            if b[j] == j+1:
                ansl.append(b.pop(j))

                f = True
                break

        if not f:
            print(-1)
            exit()
    for i in range(n - 1, -1, -1):
        print(ansl[i])


if __name__ == '__main__':
    main()

Submission status: Accepted

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

v = []
while b:
    x = -1
    for i in range(len(b)):
        if b[i] == i + 1:
            x = i

    if x == -1:
        print(-1)
        exit()

    v.append(b[x])
    del b[x]

for e in reversed(v):
    print(e)

Submission status: Accepted

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

ans = []
while b:
    for i in range(len(b)):
        if b[len(b)-1-i] == len(b)-i:
            ans.append(b.pop(len(b)-1-i))
            break
    else:
        print(-1)
        exit()

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

Submission status: Accepted

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


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

    # KeyInsight
    # 数列aに数字を追加して数列bが作れるか?
    # ⇒数列bのk番目の値kを取り除くことができるか? と言い換える
    # https://www.youtube.com/watch?v=wOacBGq5OWU

    while b:
        index = -1
        candidate = -1

        # 一致するkが複数ある場合は，最も右側の値を選択
        for i, bi in enumerate(b, 1):
            if bi == i:
                index = i
                candidate = bi

        if index == -1:
            print(-1)
            exit()

        b.pop(index - 1)
        ans.append(candidate)

    # 最後の操作から行っているため，反転させる
    print('\n'.join(map(str, ans[::-1])))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N, *B = map(int, open(0).read().split())

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

print(*reversed(A), sep='\n')

Submission status: Accepted

Solution code:
N=int(input())
B=list(map(int,input().split()))
ans=[]
while B:
    tmp = -1
    for i,b in enumerate(B):
        if i+1 < b:
            print(-1)
            exit()
        elif i+1 == b:
            tmp = i
    ans.append(B[tmp])
    del B[tmp]
for a in reversed(ans):
    print(a)

Submission status: Accepted

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,queue,copy

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7
dd=[(-1,0),(0,1),(1,0),(0,-1)]
ddn=[(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return input()

def main():
  n=I()
  l=LI()

  ans=[]
  while True:
    if len(l)==0:
      break
    i=len(l)-1
    f=True
    for _ in range(len(l)):
      if l[i]==i+1:
        ans.append(l[i])
        l=l[:i]+l[i+1:]
        f=False
        break
      i-=1
    if f:
      print(-1)
      exit()
  ans.reverse()
  for x in ans:
    print(x)

main()
# print(main())

Submission status: Accepted

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

Submission status: Accepted

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


def check():
    ans=[]
    for i in range(n-1,-1, -1):
        for j in range(i, -1, -1):
            if j+1==b[j]:
                ans.append(b[j])
                del b[j]
                break
    if len(ans)==n:
        return ans
    else:
        return -1

ans=check()

if type(ans)==list:
    for i in ans[::-1]:
        print(i)
else:
    print(ans)

Submission status: Accepted

Solution code:
# 2019/09/03

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

ans=[]
while b:
    lb=len(b)
    if lb==1:break
    idx=0
    for i in range(lb):
        if i==b[i]:
            idx=i
    if idx:
        ans.append(idx)
        b.remove(idx)
    else:
        print(-1)
        exit()

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

Submission status: Accepted

Solution code:
n=int(input())
b=[int(j) for j in input().split()]
ans=[]
while b:
    tmp=10**18
    for i in range(len(b))[::-1]:
        if b[i]==i+1:
            tmp=i
            break
    if tmp==10**18:
        print(-1)
        exit()
    else:
        ans.append(b[tmp])
        del b[tmp]

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

Submission status: Accepted

Solution code:
N, *B = map(int, open(0).read().split())
A = []
for b in B:
    if b - 1 > len(A):
        print(-1)
        quit()
    A.insert(b - 1, b)
print(*A, sep='\n')

Submission status: Accepted

Solution code:
N, *B = map(int, open(0).read().split())
A = []
for _ in range(N):
    for j in reversed(range(len(B))):
        if B[j] == j + 1:
            A.append(B.pop(j))
            break
    else:
        print(-1)
        quit()
print(*reversed(A), sep="\n")

Submission status: Accepted

Solution code:
N, *B = map(int, open(0).read().split())
A = []
for _ in range(N):
    for j in reversed(range(len(B))):
        if B[j] == j + 1:
            del B[j]
            A.append(j + 1)
            break
    else:
        print(-1)
        quit()
print(*reversed(A), sep="\n")

Submission status: Accepted

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

Submission status: Accepted

Solution code:
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:
            ans.append(b.pop(j - 1))
            break
    else:
        break
    continue

if len(ans) < N:
    print(-1)
else:
    for i in range(N):
        print(ans[N - 1 - i])

Submission status: Accepted

Solution code:
import sys
import heapq
from operator import itemgetter
from collections import deque, defaultdict
from bisect import bisect_left, bisect_right
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)
MOD = 10**9 + 7

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

    ans = []

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

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

sol()

Submission status: Accepted

Solution code:
import bisect
import heapq
import itertools
import math
import operator
import os
import re
import string
import sys
from collections import Counter, deque, defaultdict
from copy import deepcopy
from decimal import Decimal
from fractions import gcd
from functools import lru_cache, reduce
from operator import itemgetter, mul, add, xor

import numpy as np

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

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

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

op = []
ok = True
while B and ok:
    for j in reversed(range(len(B))):
        if B[j] == j + 1:
            op.append(j + 1)
            B.pop(j)
            break
        if B[j] > j + 1:
            ok = False
            break

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

Submission status: Accepted

Solution code:
import sys


def option_index(blist):
  option_num = -1 
  option_i = None
  for i in range(len(blist)):
    if i + 1 == blist[i]:
      if option_num < blist[i]:
        option_i = i 
        option_num = blist[i]
  return option_i

N = int(sys.stdin.readline())
blist = list(map(int, sys.stdin.readline().split()))
ans = []
while len(blist)>0:
  result = option_index(blist)
  if result is not None:
    ans = [blist[result]] + ans
    blist.pop(result)
  else:
    print(-1)
    exit()

print("\n".join(map(str,ans)))

Submission status: Accepted

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

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

ans = []
while len(B) > 0:
    tmp = []
    for i,b in enumerate(B):
        if i+1 == b:
            tmp.append(b)
    if tmp == []:
        print(-1)
        exit()
    else:
        x = max(tmp)
        ans.append(x)
        B.remove(x)
ans.reverse()
for i in ans:
    print(i)

Submission status: Accepted

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

ans = list()
for k in reversed(range(N)):
    hit = False
    for l in reversed(range(1, k + 2)):
        if B[l - 1] == l:
            B = B[:l - 1] + B[l:]
            hit = True
            ans.append(l)
            break
    if not hit:
        print(-1)
        exit(0)

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

Submission status: Accepted

Solution code:
n,*B=map(int,open(0).read().split())
a=[]
while n:
	f=1
	for i,b in enumerate(B[::-1]):
		if b==n-i:
			n-=1
			a+=[B.pop(b-1)]
			f=0
			break
	if f:
		print(-1)
		exit()
print(*a[::-1],sep="\n")

Submission status: Accepted

Solution code:
#%%
N = int(input())
A = list(map(int, input().split()))
l = []

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

l = l[::-1]

if len(A) == 0:
    for i in range(len(l)):
        print(l[i])
else:
    print(-1)



#%%

Submission status: Accepted

Solution code:
n = int(input())
s = list(input().split())
seq = []


for i in range(n):
    m = -1
    for j in range(len(s)):
        if int(s[j]) == j+1:
            m = j
    if m != -1:
        s = s[:m]+s[m+1:]
        seq.append(m+1)

# print(s)
# print(seq)

if len(s) == 0:
    for i in range(1,n+1):
        print(seq[-i])
else:
    print("-1")

Submission status: Accepted

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

#成り立たせることができるかどうか
for i in range(1, N + 1):
    if b[i - 1] > i:
        print (-1)
        exit()


check_lst = [False] * N #i番目の数を使ったかどうか

ans_lst = [] #答え

#後ろから順にチャックし、使うことができるものから順に使う
index = N - 1
while len(ans_lst) != N: #全て使ったとき終了
    if check_lst[index] == False and b[index] == check_lst[:index].count(True) + 1: #使われていない　かつ　その数字より前にその数字の数 + 1個の使われた数字
        ans_lst.append(b[index])
        check_lst[index] = True
        index = N - 1
    else:
        index -= 1

for i in ans_lst:
    print (i)

Submission status: Accepted

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

#成り立たせることができるかどうか
for i in range(1, N + 1):
    if b[i - 1] > i:
        print (-1)
        exit()


check_lst = [False] * N #i番目の数を使ったかどうか

ans_lst = [] #答えの逆順
index = N - 1
while len(ans_lst) != N:
    if check_lst[index] == False and b[index] <= check_lst[:index].count(True) + 1:
        ans_lst.append(b[index])
        check_lst[index] = True
        index = N - 1
    else:
        index -= 1

for i in ans_lst:
    print (i)

Submission status: Accepted

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

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

def F(arr):
    # (最後に挿入する数、挿入前のarray) を返す
    L = len(arr)
    x = None
    for i,y in enumerate(arr[::-1]):
        if L - i == y:
            x = y
            break
    if x is None:
        return None,None
    return x, arr[:x-1] + arr[x:]

answer = []
while B:
    x, B = F(B)
    if x is None:
        answer = [-1]
        break
    answer.append(x)
answer = answer[::-1]

print('\n'.join(map(str,answer)))

Submission status: Accepted

Solution code:
N = int(input())
b = list(map(int,input().split()))
tmp = b[:]
ans = []
i = N-1
while i >= 0:
    if tmp[i] == 1:
        tmp[i] = float('inf')
        ans.append(b[i])
        while i < N:
            tmp[i] -= 1
            i += 1
    i -= 1
if len(ans) == N:
    for i in ans:
        print(i)
else:
    print(-1)

Submission status: Accepted

Solution code:
# AGC032A - Limited Insertion
def main():
    # last manipulation <-> rightmost number which corresponds with its index
    n = int(input())
    B = list(map(int, input().rstrip().split()))
    ans = []
    while len(B) > 0:
        for i in range(len(B) - 1, -1, -1):
            if B[i] == i + 1:
                ans += [B.pop(i)]
                break
        else:  # invalid sequence
            print(-1)
            return
    print(*ans[::-1], sep="\n")


if __name__ == "__main__":
    main()

Submission status: Accepted

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

while bn:
    for i in range(len(bn))[::-1]:
        if i+1 == bn[i]:
            ans.append(bn[i])
            del bn[i]
            break
    else:
        break
if bn:
    print(-1)
else:
    [print(i) for i in ans[::-1]]