Problem:


Score : 400 points

Problem Statement

Snuke has an empty sequence a.

He will perform N operations on this sequence.

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

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

Constraints

All values in input are integers.

1 \leq N \leq 100

1 \leq b_i \leq N

Input

Input is given from Standard Input in the following format:

N
b_1 \dots b_N

Output

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

Sample Input 1

3
1 2 1

Sample Output 1

1
1
2

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

After the first operation: (1)

After the second operation: (1,1)

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

Sample Input 2

2
2 2

Sample Output 2

-1

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

Sample Input 3

9
1 1 1 2 2 1 2 3 2

Sample Output 3

1
2
2
3
1
2
2
1
1



Sample input:
3
1 2 1


Sample output:
1
1
2

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10 ** 5 + 10)


def input(): return sys.stdin.readline().strip()


def resolve():
    def main():

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

        while len(B) > 0:
            flag = False

            for i in reversed(range(len(B))):
                if B[i] == i+1:
                    ans.append(B[i])
                    B.pop(i)
                    flag = True
                    break
            if not flag:
                return None

        return ans


    ans=main()
    if ans:
        for i in reversed(ans):
            print(i)
    else:
        print(-1)

resolve()

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, -1, -1):
        if b[j] == j+1:
            x = b.pop(j)
            a.append(x)
            break

if len(a) != n:
    print(-1)
    exit()

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

Submission status: Accepted

Solution code:
import sys

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60


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


def main():
    ans = []
    while B:
        for i in range(len(B), 0, -1):
            if i == B[i - 1]:
                ans.append(B.pop(i - 1))
                break
        else:
            print(-1)
            return
    print(*reversed(ans), sep='\n')


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60


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


def main():
    ans = []
    while B:
        found = False
        for i in range(len(B), 0, -1):
            if i == B[i - 1]:
                ans.append(i)
                del B[i - 1]
                found = True
                break
        if not found:
            print(-1)
            return
    print(*reversed(ans), sep='\n')


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from sys import exit

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

cnt = [0] * n
done = [False] * n
ans = [0] * n
for i in range(n):
  for j in range(n-1, -1, -1):
    if done[j]:
      continue
    if b[j] == cnt[j] + 1:
      ans[i] = b[j]
      for k in range(j+1, n):
        cnt[k] += 1
      done[j] = True
      break
        
for i in range(n):
  if not done[i]:
    print(-1)
    exit()
    
for i in range(n):
  print(ans[i])

Submission status: Accepted

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

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

Submission status: Accepted

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

ans_li = []
for i in range(n):
    for j in range(len(B)-1,0,-1):
        if j==B[j]:
            ans_li.append(B.pop(j))
            break
    else:
        print(-1);exit()
print('\n'.join(map(str,ans_li[::-1])))

Submission status: Accepted

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

def f(c):
    for i in reversed(range(len(c))):
        if c[i] == i+1:
            return (c[i], c[:i] + c[i+1:])
    return (-1, c)

ans = []
for i in range(n):
    (a, b) = f(b)
    if a == -1:
        print(-1)
        exit()
    ans.append(a)
    #print(ans, b)

print('\n'.join(map(str, reversed(ans))))

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**6)

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

def dfs(s, ans):
    if len(s) == 0:
        print('\n'.join(map(str, reversed(ans))))
        exit()
        return
    for i in range(len(s)):
        #if s[i] == i+1:
        #if s[i] == i+1 and max(s[:i] + [0]) >= i:
        if s[i] == i+1:
            s1 = s[:i] + s[i+1:]
            allok = True
            for j in range(len(s1)):
                if s1[j] > j+1:
                    allok = False
                    break
            #print(s[:i], s[i+1:],i)
            if allok:
                ans.append(s[i])
                dfs(s1, ans)
    if len(ans) > 0:
        ans.pop()

dfs(b, [])

print('-1')

Submission status: Accepted

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


result=[]

for i in range(n):
    posi=-1
    for index,ss in enumerate(s):
        if ss==index+1:
            posi=index
    if posi==-1:
        print(-1)
        exit()
    else:
        result.append(s[posi])
        s.pop(posi)

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

Submission status: Accepted

Solution code:
import sys
import math
from collections import defaultdict
from bisect import bisect_left, bisect_right

sys.setrecursionlimit(10**7)
def input():
    return sys.stdin.readline()[:-1]

mod = 10**9 + 7

def I(): return int(input())
def LI(): return list(map(int, input().split()))
def LIR(row,col):
    if row <= 0:
        return [[] for _ in range(col)]
    elif col == 1:
        return [I() for _ in range(row)]
    else:
        read_all = [LI() for _ in range(row)]
        return map(list, zip(*read_all))

#################

N = I()
b = LI()

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# input()
# int(input())
# map(int, input().split())
# list(map(int, input().split()))
import math
import sys
import bisect
import heapq  # 優先度付きキュー(最小値取り出し)
from collections import deque

inf = 10 ** 15
mod = 10 ** 9 + 7

n = int(input())
b = list(map(int, input().split()))
ans = []
while b:
    ind = -1
    for i,c in enumerate(b):
        if i+1 == c:
            ind = i
    if ind == -1:
        print(-1)
        sys.exit()
    ans.append(b.pop(ind))
ans.reverse()
for tmp in ans:
    print(tmp)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
n,*b=map(int,open(0).read().split())
b=[0]+b
ans=[]
for i in range(n):
  for i in range(len(b)-1,0,-1):
    if i==b[i]:
      ans.append(b.pop(i))
      break
  else:
    print(-1)
    exit()
for i in ans[::-1]:
  print(i)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys

stdin = sys.stdin
inf = 1 << 60
mod = 1000000007

sys.setrecursionlimit(10 ** 7)

ni      = lambda: int(ns())
nin     = lambda y: [ni() for _ in range(y)]
na      = lambda: list(map(int, stdin.readline().split()))
nan     = lambda y: [na() for _ in range(y)]
nf      = lambda: float(ns())
nfn     = lambda y: [nf() for _ in range(y)]
nfa     = lambda: list(map(float, stdin.readline().split()))
nfan    = lambda y: [nfa() for _ in range(y)]
ns      = lambda: stdin.readline().rstrip()
nsn     = lambda y: [ns() for _ in range(y)]
ncl     = lambda y: [list(ns()) for _ in range(y)]
nas     = lambda: stdin.readline().split()

from collections import deque

n = ni()
a = na()

ok = True
ans = deque()

s = sum(a)
cur_n = n
while s:
    pos = 0
    flag = False
    i = n - 1
    j = cur_n - 1
    while i >= 0:
        if a[i] == 0:
            i -= 1
            continue
        if a[i] == j + 1:
            flag = True
            pos = i
            break
        j -= 1
        i -= 1

    if flag:
        ans.appendleft(a[pos])
        s -= a[pos]
        a[pos] = 0
        cur_n -= 1
    else:
        print(-1)
        exit()

print(*ans)

Submission status: Accepted

Solution code:
# A - Limited Insertion

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

for i in range(N):
    j = N-i
    while j>0 and b[j-1]!=j:
        j -= 1
    if j==0:
        judge = 0
        break
    else:
        b.pop(j-1)
        a.append(j)

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

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)

n, *b = map(int, read().split())
ans = [0] * n
idx = 0
for i in range(n):
    for j, bb in enumerate(b):
        if j + 1 == bb:
            ans[i] = bb
            idx = j
    if ans[i] == 0:
        print(-1)
        exit()
    b.pop(idx)
print('\n'.join(map(str, ans[::-1])))

Submission status: Accepted

Solution code:
def main():
    from collections import deque
    N = int(input())
    B = deque(list(map(int, input().split())))
    result = deque([])
    for i in range(N):
        flag = False
        for j in range(len(B), 0, -1):
            b = B[j-1]
            if j == b:
                del B[j-1]
                result.append(b)
                flag = True
                break
        if not flag:
            print(-1)
            return
    for j in range(len(result)-1, -1, -1):
        print(result[j])
main()

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
n = int(input())
B = list(map(int, input().split()))
ans = []
for _ in range(n):
    for i in reversed(range(len(B))):
        if B[i] == i+1:
            x = B.pop(i)
            ans.append(x)
            break
if len(ans) == n:
    print(*ans[::-1], sep='\n')
else:
    print(-1)

Submission status: Accepted

Solution code:
n = int(input())
B = list(map(int, input().split()))
A = []
for b in B:
    A.insert(b-1, b)
if all(A[i] <= i+1 for i in range(n)):
    C = []
    for a in A:
        C.insert(a-1, a)
    if B == C:
        print(*A, sep='\n')
    else:
        print(-1)
else:
    print(-1)

Submission status: Accepted

Solution code:
import sys
readline = sys.stdin.buffer.readline
sys.setrecursionlimit(10 ** 8)
INF = float('inf')
MOD = 10 ** 9 + 7

def main():
    N = int(readline())
    B = list(map(int, readline().split()))
    ans = []
    for _ in range(N):
        L = len(B)
        k = -1
        for i in range(L):
            if B[i]==i+1:
                k = i
        if k == -1:
            print('-1')
            sys.exit()
        else:
            ans.append(B.pop(k))
    for i in range(N)[::-1]:
        print(ans[i])

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from collections import deque
import numpy as np

N = int(input())
B = list(map(int, input().split()))
Ans = deque()
Sa = np.zeros(N)
for a, b in enumerate(B):
    Sa[a] = a-b+1
while 0 in Sa:
    for i in range(N)[::-1]:
        if Sa[i] == 0:
            Ans.appendleft(B[i])
            Sa[i:] -= 1
            break
if len(Ans) != N:
    print(-1)
else:
    for i in range(len(Ans)):
        a = Ans.popleft()
        print(a)

Submission status: Accepted

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

for i,b in enumerate(B):
    if i + 1 < b:
        print(-1)
        exit()

N = len(B)
ans = []
for _ in range(N):
    for i,b in enumerate(B):
        if i+1 == b:
            t = i+1

    B.pop(t-1)
    ans.append(t)
for i in ans[::-1]:
    print(i)

Submission status: Accepted

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

ans = []

for i in range(N):
    L = -1
    for j in range(len(b)):
        if j+1 == b[j]:
            L = j

    if L != -1:
        ansi = b.pop(L)
        ans.append(ansi)

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

Submission status: Accepted

Solution code:
from collections import deque,Counter

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

ans = deque()
while len(lisB) > 0:
    for i in range(len(lisB)-1,-1,-1):
        if lisB[i]-1 == i:
            ans.appendleft(lisB.pop(i))
            break
    else:
        print(-1)
        exit()

print(*ans)

Submission status: Accepted

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

ans = [0] * n
for i in range(1,n+1):
    num = -1
    for j in range(n+1-i):
        if(b[j] == j+1):
            num = j
    if(num == -1):
        print(-1)
        exit()
    ans[-i] = num + 1
    del b[num]

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

Submission status: Accepted

Solution code:
# https://atcoder.jp/contests/agc032/tasks/agc032_a
# 条件を満たす中で数字の大きい方からgreedyに抜けばよい
# もしルールに従って数字が挿入されているならばそのような数字が必ず一つは存在する


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

ans = []
for _ in range(N):
    for i in range(len(B) - 1, -1, -1):
        if i + 1 == B[i]:
            ans.append(B[i])
            del B[i]
            break
    else:
        print(-1)  # 見つからなかったら
        exit()
print(*reversed(ans), sep='\n')

Submission status: Accepted

Solution code:
N = int(input())
b = list(map(int,input().split()))
l = [0]*N
for i in range(N):
  for j in range(min(N,len(b))-1,-1,-1):
    if b[j] == j+1:
      b.pop(j)
      l[i] = j+1
      break
if b == []:
  l.reverse()
  for k in range(N):
    print(l[k])
else:
  print(-1)

Submission status: Accepted

Solution code:
n = int(input())
b = list(map(int, input().split()))
ans = []
while b:
    for i, j in enumerate(reversed(b)):
        if j==len(b)-i:
            ans.append(j)
            del b[len(b)-i-1]
            break
    else:
        print(-1)
        break
else:
    for i in reversed(ans):
        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]:
        if b[i] == i+1:
            a.append(b.pop(i))
            ok = True
            break

    if not ok:
        print('-1')
        exit()

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

Submission status: Accepted

Solution code:
import sys
def input(): return sys.stdin.readline().rstrip()
def main():
    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:
        print('\n'.join(map(str,ans[::-1])))

if __name__=='__main__':
    main()

Submission status: Accepted

Solution code:

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

ans = []
removed = [False] * N

flag = True
while flag:
    flag = False
    
    idx_remove = -1
    idx = 0
    for i in range(N):
        if removed[i]:
            continue

        if idx + 1 == B[i]:
            idx += 1
            idx_remove = i

        else:
            idx += 1
    
    if idx_remove != -1:
        ans.append(B[idx_remove])
        removed[idx_remove] = True
        flag = True


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

Submission status: Accepted

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

ans = []
while len(b) != 0:
    tmp = 0
    for i in range(1, len(b)+1):
        if b[i-1] == i:
            tmp = b[i-1]
    if tmp == 0:
        print(-1)
        exit()
    else:
        ans.append(tmp)
        del b[tmp-1]
        
for i in range(N-1, -1, -1):
    print(ans[i])

Submission status: Accepted

Solution code:
import collections
def main():
    N = int(input())
    B = [0] + [int(_) for _ in input().split()]

    for i in range(1, N+1):
        if i < B[i]:
            print(-1)
            return
    dic = collections.defaultdict(int)
    for i in range(1, N+1):
        x = B[i]
        c = 0
        for j in range(i+1, N+1):
            if B[j] <= x:
                x += 1
                c += 1
        dic[B[i]+c] = B[i]
    out = [dic[key] for key in sorted(dic.keys())]
    print(*out, sep = '\n')
    return

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline  # NOQA
sys.setrecursionlimit(10 ** 7)  # NOQA


def dfs(N, a, i, procedure):
    if i == 0:
        return procedure

    for j in reversed(range(1, i+1)):
        if a[j-1] == j:
            l = a[:]
            del l[j-1]
            p = procedure[:]
            p.append(j)
            ret = dfs(N, l, i-1, p)
            if ret is not None:
                return ret
            break


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

    res = dfs(N, b, N, [])
    if res is None:
        print(-1)
    else:
        print(*res[::-1], sep="\n")


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
n = int(input())
p = list(map(int,input().split()))
ans = []
for i in range(n):
    fla = 0
    for j in reversed(range(n-i)):
        if p[j] == j+1:
            ans.append(p.pop(j))
            fla = 1
            break
    if fla == 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()))
p = []

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

Submission status: Accepted

Solution code:
n=int(input())

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

ans = []

count = 0

while len(ball)>0:
    count += 1
    for i in range(len(ball)-1,-1,-1):
        #print(ball,i)
        if i+1 ==ball[i]:
            move = ball.pop(i)
            ans.insert(0,move)
            break
    if count ==100000:
        break
    

if len(ans) == n:
    for i in ans:
        print(i)
else:
    print(-1)

Submission status: Accepted

Solution code:
from collections import deque

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

op=[0]*N

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

    b=[b[i] for i in range(op[i]-1)]+[b[i] for i in range(op[i],len(b))]

for op1 in op:
    print(op1)

Submission status: Accepted

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

def main():
    N = int(input())
    b = list(map(int, input().split()))
    ans = []
    for i in range(N):
        pos = 0
        for j in range(N-i):
            tmp = j+1 - b[j]
            if tmp < 0:
                print(-1)
                exit()
            elif tmp == 0:
                pos = j
        ans.append(b.pop(pos))
    for a in ans[::-1]:
        print(a)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys

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


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

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

    res = res[::-1]
    for i in res:
        print(i)


if __name__ == '__main__':
    resolve()

Submission status: Accepted

Solution code:
from _collections import deque
n=int(input())
B=list(map(int,input().split()))
ANS=deque([])
cnt=0
while len(B)>0 and cnt<10**5:
    for i in reversed(range(len(B))):
        if i+1==B[i]:
            B=B[:i]+B[i+1:]
            ANS.appendleft(i+1)
            break
    cnt+=1
if len(B)==0:
    for a in ANS:
        print(a)
else:
    print(-1)

Submission status: Accepted

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

result =[]
while len(a) > 0:
    ok = False
    for i in range(len(a)-1, -1, -1):
        if i+1 == a[i]:
            result.append(a[i])
            a.pop(i)
            ok = True
            break
    if not ok:
        result = [-1]
        break
for i in result[::-1]: print(i)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
L = [0 for _ in range(N)]
flg = True
for i in range(N):
  cnt = -1
  for j in range(N-i, 0, -1):
    if j == A[j-1]:
      cnt = j
      break
  if cnt == -1:
    flg = False
    break
  else:
    L[N-i-1] = cnt
    for j in range(cnt, N-i):
      A[j-1] = A[j]
if flg:
  for i in range(N):
    print(L[i])
else:
  print(-1)

Submission status: Accepted

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

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def solve(remains, nums):
    if (len(remains)) == 0:
        [print(num) for num in nums[::-1]]
        exit()
    n_remain = None
    n_num = None
    for i, num in enumerate(remains):
        if i + 1 == num:
            n_remain = remains[:i] + remains[i + 1:]
            n_num = nums + [i + 1]
    if n_remain is None:
        return False
    solve(n_remain, n_num)


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

solve(B, [])
print(-1)

Submission status: Accepted

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

Submission status: Accepted

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

ans = []
fault = False
for _ in range(N):
    for cursol in range(len(b) - 1, -1, -1):
        if b[cursol] == cursol + 1:
            ans.append(b.pop(cursol))
            break
    else:
        fault = True
if fault:
    print(-1)
else:
    ans.reverse()
    for el in ans:
        print(el)

Submission status: Accepted

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

ans = []

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
INF = 10 ** 9
MOD = 10 **9 + 7
import sys
sys.setrecursionlimit(100000000)
dy = (-1,0,1,0)
dx = (0,1,0,-1)
from  collections import deque
from math import factorial

def main():
    n = int(input())
    B = list(map(int,input().split()))
    flag = True
    ans = []
    for i in range(n):
        idx = -1
        for j in range(n - i):
            if B[j] == j + 1:
                idx = j
        if idx == -1:
            flag = False
            break
        ans.append(idx + 1)
        B = B[:idx] + B[idx + 1:]

        if not flag:
            break
    
    print('\n'.join(map(str,ans[::-1])) if flag else -1)
if __name__=='__main__':
    main()

Submission status: Accepted

Solution code:
from collections import deque
n=int(input())
b=list(map(int,input().split()))
a=[]
while b:
    maxpoint=0
    for i in range(len(b)):
        if i+1==b[i]:
            maxpoint=max(maxpoint, i+1)
    if maxpoint==0:
        print(-1)
        exit()
    else:
        a.append(maxpoint)
        b=b[:maxpoint-1]+b[maxpoint:]

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

Submission status: Accepted

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

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

if len(ans) == N:
    print('\n'.join(map(str,ans[::-1])))
else:
    print(-1)

Submission status: Accepted

Solution code:
import sys

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


def solve():
    N = int(rl())
    b = list(map(int, rl().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()
        del b[j]
        ans.append(j + 1)
    ans.reverse()
    print(*ans, 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,0,-1):
  flag = 1
  for j in range(i-1,-1,-1):
    if b[j] == j+1:
      a.append(b.pop(j))
      flag = 0
      break
  if flag:
    print(-1)
    quit()
for i in a[::-1]:
  print(i)

Submission status: Accepted

Solution code:
N,*B = map(int, open(0).read().split())
ans = [0]*N
for i in range(N):
  for j in range(len(B)-1,-1,-1):
    if B[j]==j+1:
      ans[N-1-i] = j+1
      B = B[:j]+B[j+1:]
      break
if B==[]:
  print('\n'.join(map(str,ans)))
else:
  print(-1)

Submission status: Accepted

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

ans = []

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

if len(ans) != n:
    print(-1)
else:
    for i in range(n):
        print(ans[n-1-i])

Submission status: Accepted

Solution code:
N = int(input())
b = list(map(int, input().split()))
ans = []
cnt = 0
while len(b): 
  for j in range(len(b)-1, -1, -1):
    if b[j] == j+1:
      ans += [b.pop(j)]
      break
  cnt += 1
  if cnt >= N+1:
    print(-1)
    exit()
    
for n in ans[::-1]:
  print(n)

Submission status: Accepted

Solution code:
n=int(input())
b=list(map(int,input().split()))
num=[]
for i in range(n):
    num.append([b[i],0]) #0はまだ残ってる　１は取り除いた
flag=True
ans=[]
remove=0 #取り除いた数。
remove_sum=[0]*n
while flag and remove<=n-1:
    remove_ind=-1
    for i in range(n):
        if num[i][1]==0:
            if i-remove_sum[i]==num[i][0]-1:
                remove_ind=i
    if remove_ind!=-1:
        num[remove_ind][1]=1
        remove+=1
        ans.append(num[remove_ind][0])
        for j in range(remove_ind+1,n):
            remove_sum[j]+=1
    else: #取り除くものがなかった
        flag=False
if flag:
    ans=ans[::-1]
    for i in range(n):
        print(ans[i])
else:
    print(-1)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
b = list(map(int, input().split()))
res = []
for _ in range(n):
    idx = 0
    for i in range(len(b)):
        if b[i] == i + 1:
            idx = i + 1
    if idx == 0:
        print(-1)
        exit()
    res.append(idx)
    b.pop(idx-1)
res = res[::-1]
for i in range(n): print(res[i])

Submission status: Accepted

Solution code:
n=int(input())
b=list(map(int,input().split()))
ans=[]
flg=True
while len(b)>0 and flg:
  n1=len(b)
  i=n1
  flg=False
  while i > 0:
    if b[i-1]==i:
      ans.append(b[i-1])
      b1=b[:i-1]
      b1[len(b1):len(b1)]=b[i:]
      b=b1
      i=-1
      flg=True
    else:
      i-=1

if flg==False:
  print(-1)
  #print(ans)
  exit()
for _ in range(n):
  print(ans.pop())

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline

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

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

            ans.append(b.pop(i))
            break

    else:
        print(-1)
        exit()

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