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:

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

    while True:
        del_index = -1
        for k in range(len(b)):
            if b[k] == k + 1:
                del_index = max(del_index, k)
        if del_index < 0:
            break
        else:
            del_list.append(b[del_index])
            b.pop(del_index)

    if len(b) > 0:
        print(-1)
    else:
        for key in del_list[::-1]:
            print(key)

solve()

Submission status: Accepted

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

res = []
while len(B) > 0:
    idx = -1
    for i in range(len(B) - 1, -1, -1):
        if i + 1 == B[i]:
            idx = i
            break
    if idx == -1:
        print(-1)
        exit(0)
    else:
        res.append(B[idx])
        tmp = []
        for i, b in enumerate(B):
            if i != idx:
                tmp.append(b)
        B = tmp

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

Submission status: Accepted

Solution code:
n = int(input())
b =input().split()
ans = []
while len(b) > 0:
    ng = True
    
    for i in range(len(b)-1,-1,-1):
        if b[i] == str(i+1):
            ans.append(i+1)
            b = b[:i]+b[i+1:]
            
            
            ng = False
            break
    if ng:
        print(-1)
        exit(0)
if len(ans) == 0:
    print(-1)
else:
    ans = ans[::-1]
    print(*ans)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
from operator import itemgetter
sys.setrecursionlimit(10000000)
INF = 10**30
def main():
    n = int(input().strip())
    v = list(map(int, input().strip().split()))
    ans = []
    while len(v) > 0:
        flag = False
        for j in reversed(range(len(v))):
            # print(j)
            if j+1 == v[j]:
                ans.append(v[j])
                del v[j]
                flag = True
                break
        if not flag:
            print(-1)
            return
    for c in reversed(ans):
        print(c)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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



def solve(N,B):
    ans = []
    for _ in range(N):
        m = max(B)
        if m > len(B):
            return [-1]
        for i in range(m,0,-1):
            if B[i-1]==i:
                B.pop(i-1)
                ans.append(i)
                break
        else:
            return [-1]
    return ans
ans = solve(N,B)

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

Submission status: Accepted

Solution code:
n = int(input())
b = list(map(int, input().split()))
ans = []
index = 0
count = 0
while count <= 10000 and len(b) > 0:
    index = len(b) - 1
    while index >= 0:
        if index + 1 == b[index]:
            ans.append(b[index])
            del b[index]
            index = len(b) - 1
            continue
        index -= 1
        count += 1
if len(ans) == n:
    ans.reverse()
    for i in range(n):
        print(ans[i])
else:
    print(-1)

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    b = list(map(int, input().split()))
    ans = []
    f = True
    for i in reversed(range(n)):
        F = True
        for j in reversed(range(i+1)):
            if b[j] == j+1:
                ans.append(b.pop(j))
                F = False
                break
        if F:
            f = False
            break
    if f:
        for v in reversed(ans):
            print(v)
    else:
        print(-1)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
if __name__ == '__main__':
    n = int(input())
    b = list(map(int, input().split()))
    ans = []
    for i in range(n):
        if (i + 1) < b[i]:
            print(-1)
            exit(0)
    for i in range(n):
        for j in range(len(b) - 1, -1, -1):
            if b[j] == (j + 1):
                ans.append(b[j])
                del b[j]
                break
        if len(b) != (n - i - 1):
            print(-1)
            exit(0)
    ans.reverse()
    for i in ans:
        print(i)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**8)
def ii(): return int(sys.stdin.readline())
def mi(): return map(int, sys.stdin.readline().split())
def li(): return list(map(int, sys.stdin.readline().split()))
def li2(N): return [list(map(int, sys.stdin.readline().split())) for _ in range(N)]
def dp2(ini, i, j): return [[ini]*i for _ in range(j)]
def dp3(ini, i, j, k): return [[[ini]*i for _ in range(j)] for _ in range(k)]
#import bisect #bisect.bisect_left(B, a)
#from collections import defaultdict #d = defaultdict(int) d[key] += value

def main():
    N = ii()
    B = li()

    '''
    flag = [0]*N
    l = N
    i = ind = N-1
    while True:
        if S[ind] == i:
            flag[ind] = 0

        while flag[i]:
            i -= 1
        ind = i
    '''
    ans = []
    for i in range(N):
        flag = 1
        for j in reversed(range(N-i)):
            if B[j] == j+1:
                a = B.pop(j)
                ans.append(a)
                flag = 0
                break
        if flag:
            print('-1')
            exit()

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

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
N = int(input())
*l, = map(int, input().split())

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

Submission status: Accepted

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

for i in range(N):
  c = len(b)
  for j in range(c):
    if b[c-j-1]==c-j:
      d.append(b.pop(c-j-1))
      break
      
if b==[]:
  for num in d[::-1]:
    print(num)
else:
  print(-1)

Submission status: Accepted

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

ans = []
flg = True
for x in range(n):
    memo = -1
    for i in range(len(b)):
        if b[i] == i+1:
            memo = i
    if memo == -1:
        flg = False
        break
    ans.append(memo+1)
    b.pop(memo)

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

Submission status: Accepted

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

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

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


main()

Submission status: Accepted

Solution code:
from collections import deque
N = int(input())
b = [int(x) - 1 for x in input().split()]
d = deque()
while b:
  popped = -1
  for i in range(len(b))[::-1]:
    if b[i] == i:
      d.appendleft(i + 1)
      popped = b.pop(i)
      break
  if popped == -1:
    print(-1)
    exit()
print(*d, sep="\n")

Submission status: Accepted

Solution code:
def resolve():
    '''
    code here
    '''
    import collections
    
    N = int(input())
    b_list = [int(item) for item in input().split()]
    a = collections.deque()

    is_True = True

    while is_True:
        stack = collections.deque()
        for i in range(len(b_list)):
            if i+1 == b_list[i]:
                # print(b_list[i])
                stack.append(b_list[i])
                # print(stack)
        if len(stack) == 0:
            is_True = False
            a = [-1]
        else:
            temp = stack.pop()
            a.append(temp)
            del b_list[temp-1]
            # print(b_list)

        if len(b_list) == 0:
            is_True = False

    for i in reversed(a):
        print(i)
if __name__ == "__main__":
    resolve()

Submission status: Accepted

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

ans=[]
while q:
    n=len(q)
    for i in range(0,n):
        if q[n-i-1]==n-i:
            ans.append(n-i)
            if i==0:
                q=q[:n-1]
                break
            else:
                q=q[:n-i-1]+q[n-i:]
                break
    else:
        print(-1)
        break
else:
    for i in range(0,N):
        print(ans[-i-1])

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
nums = list(map(int,input().split()))
ans = []
for _ in range(N):
    ma = -1
    ma_ind = -1
    l = len(nums)

    for i in range(l):
        if nums[i] == i + 1:
            ma_ind = i
    if ma_ind == -1:
        print(-1)
        exit(0)
    temp = nums.pop(ma_ind)
    ans.append(temp)
ans.reverse()
print("\n".join(map(str,ans)))

Submission status: Accepted

Solution code:
from collections import deque
N=int(input())
b=list(map(int,input().split()))
flag=0
op=deque()
#1→12→122→1232→11232→121232→1221232→11221232→111221232
#print(b)
while len(b)>0:
    for i in range(len(b)-1,-1,-1):
        if b[i]==i+1:
            #print(b[i])
            op.appendleft(b.pop(i))
            flag=1
            break
    if flag==0:
        break
    flag=0
    #print(op,b)
#print(op)
if len(b)==0:
    for i in range(N):
        print(op.popleft())
else:
    print("-1")

Submission status: Accepted

Solution code:
import sys

n = int(sys.stdin.readline())
b = list(map(int, sys.stdin.readline().split()))
ans = []

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

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

Submission status: Accepted

Solution code:
def count_section_by_zero(data):
  count = 0
  flg = False
  start = 0
  for i, d in enumerate(data):
    if flg is False and d != 0:
      count += 1
      flg = True
      
    if d == 0:
      flg = False
  return count

def input_list():
  return list(map(int, input().split()))

def input_list_str():
  return map(str, input().split())

def lcm_base(x, y):
    return (x * y) // fractions.gcd(x, y)

def lcm_list(numbers):
    return reduce(lcm_base, numbers, 1)

def gcd(*numbers):
    return reduce(fractions.gcd, numbers)

def gcd_list(numbers):
    return reduce(fractions.gcd, numbers)

def combinations_count(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

# 2で割り切れる回数
def divide_two(arg):
  c = 0
  while True:
    if c >= 2:
      break
    if arg % 2 != 0:
      break
    arg //= 2
    c += 1
  return c

# 素因数分解
def prime_factorize(n):
    a = []
    while n % 2 == 0:
        a.append(2)
        n //= 2
    f = 3
    while f * f <= n:
        if n % f == 0:
            a.append(f)
            n //= f
        else:
            f += 2
    if n != 1:
        a.append(n)
    return a

def main():
  n = int(input())
  b = input_list()
  sb = list(sorted(list(set(b)), reverse=True))
  a = []
  def get(sb, b):
    for x in sb:
      if x > len(b):
        continue
      if b[x-1] == x:
        return x-1
    return -1
  for i in range(n):
    index = get(sb, b)
    if index == -1:
      print(-1)
      exit(0)
    a.append(b[index])
    del b[index]
  
  for v in a[::-1]:
    print(v)
      
  
  
import math
import fractions
import collections
import itertools
from functools import reduce
main()

Submission status: Accepted

Solution code:
n = int(input())
A = list(map(int, input().split()))
B = []
for i in range(n):
    f = True
    for j in range(len(A)):
        if A[len(A)-1-j] == len(A)-j:
            f = False
            B.append(len(A)-j)
            del A[len(A)-j-1]
            break
    if f: break
if len(A) > 0:print(-1)
else: print(*B[::-1], sep="\n")

Submission status: Accepted

Solution code:
N = int(input())
arr = list(map(int,input().split()))
ans = []
for i in arr:
    if len(ans) >= i - 1:
        ans.insert(i-1,i)
    else:
        print(-1)
        exit()
for i in ans:
    print(i)

Submission status: Accepted

Solution code:
def main():
    length = int(input())
    target = list(map(int, input().split()))
    max_num = max(target)
    answer = []
    for _ in range(length):
        for i in range(max_num - 1, -1, -1):
            if len(target) >= i + 1 == target[i]:
                target.pop(i)
                answer.append(i + 1)
                break
        else:
            print(-1)
            answer = []
            break
    if answer:
        for i in range(-1, -length - 1, -1):
            print(answer[i])


if __name__ == '__main__':
    main()

Submission status: Accepted

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

#与えられた数列から、j番目の数がjのものを削除していくのと同義
ans = []
while len(B) > 0:
    can_delete = False
    for i in range(len(B) - 1, -1, -1):
        if i + 1 == B[i]:
            ans.append(B[i])
            del B[i]
            can_delete = True
            break
    if not can_delete:
        break

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

Submission status: Accepted

Solution code:
import sys


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


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


def check(B, cnt):
    n = len(B)
    for i in range(n):
        if B[n - i - 1] == (n - i):
            hand.append(B[n - i - 1])
            B = B[: n - i - 1] + B[n - i :]
            break
    if len(B) != n and len(B) > 0:
        return B
    else:
        return False


cnt = 1
while B:
    B = check(B, cnt)
    # print(B)

if len(hand) < N:
    print(-1)
    sys.exit()
answer = hand[::-1]
for i in range(N):
    if answer[i] > i + 1:
        print(-1)
        sys.exit()
print(*answer, sep="\n")

Submission status: Accepted

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


ans=[]
for _ in range(n):
    for idx in range(1,len(b)+1)[::-1]:
        if idx==b[idx-1]:
            ans+=idx,
            del b[idx-1]
            break


if len(b)>0:
    print(-1)
    exit()

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

Submission status: Accepted

Solution code:
from collections import deque

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

if B[0] != 1:
    print(-1)
    exit()
if N == 1:
    print(1)
    exit()

# B[0]は絶対1だから後で加える
B.pop(0)

ans = deque()
while B:
    tmp = -1
    for i,b in enumerate(B):
        if i == 0 and b == 1:
            tmp = i
        elif b == i + 2:
            tmp = i
    if tmp < 0:
        print(-1)
        exit()
    ans.appendleft(B.pop(tmp))

ans.appendleft(1)

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

Submission status: Accepted

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

for i in range(n):
    delete = max([a[j] if (j == a[j] - 1) else 0 for j in range(len(a))])
    if (delete == 0):
        print(-1)
        exit()
    else:
        ans.append(delete)
        a.pop(delete-1)

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

Submission status: Accepted

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

def solve(arr):
  ret = []
  while len(arr) > 0:
    before = len(arr)
    for i in reversed(range(len(arr))):
      if arr[i] == i + 1:
        ret.append(arr.pop(i))
        break
    if len(arr) == before:
      return None
  return list(reversed(ret))

A = solve(B)
#print(A)

if A == None:
  print(-1)
else:
  for a in A:
    print(a)

Submission status: Accepted

Solution code:
# import bisect
# from collections import Counter, defaultdict, deque, namedtuple
# import copy
# from heapq import heappush, heappop, heapify
# from fractions import gcd
import itertools
# from operator import attrgetter, itemgetter
# import math

import sys

# import numpy as np

ipti = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)

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

    res = [0] * n

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

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




if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
# 数列の操作で入力の配列を作ることができるか、と言う形式の問題(構築？)
# 考え方としては、一意に定まるとして考えることが定石
# 特徴的なケース1,nを考えたが、有効そうな案は浮かばず
# 前からi番目がj の時、これはi-j+1番以降に出されたことがわかる
# 前から順番に埋めて行ったら解けそう
# b[i-1]<=iなら絶対いけるのでは？
n = int(input())
b = list(map(int,input().split()))
for i in range(n):
    if b[i] > i+1: 
        print(-1)
        exit()
ans = []
add = [0]*n
for cnt in range(n):
    for i in range(n-1,-1,-1):
        if b[i] == i+1:
            ans.append(b[i]-add[i])#末尾
            b[i] = -1#２度と合わないように
            for j in range(i+1,n):
                b[j] += 1
                add[j] += 1
            break
for i in range(n-1,-1,-1):
    print(ans[i])

Submission status: Accepted

Solution code:
N=int(input())
B=[int(i) for i in input().split()]
TF=[True for _ in range(N)]
I=list(reversed(range(N)))
ans=[]
d=0
while N!=d:
  # print(TF)
  flag=True # -1 flag
  k=d
  for i in I:
    # print(d,k,i)
    if TF[i]:
      if i+1-k==B[i]:
        TF[i]=False
        flag=False
        ans.append(i)
        d+=1
        break
    else:
      k-=1
  if flag:
    ans=-1
    break
if ans==-1:
  print(ans)
else:
  for i in reversed(ans): print(B[i])

Submission status: Accepted

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

tmp=-1
idx=0
count=1
flag=True
ans=[]
while(len(b)):
    #print(idx)
    if idx<len(b):
        if b[idx]==count:
            tmp=idx
            idx+=1
            count+=1
        else:
            idx+=1
            count+=1
    if idx>(len(b)-1):
        if tmp==-1:
            flag=False
            break
        else:
            #print(tmp)
            ans.append(b[tmp])
            del b[tmp]
            tmp=-1
            idx=0
            count=1
#print(ans)
if flag:
    for i in range(len(ans)):
        print(ans[len(ans)-1-i])
else:
    print(-1)

Submission status: Accepted

Solution code:
n = int(input())
dat = list(map(int, input().split()))
f = True
res = []
while len(dat) > 0:
    t = -1
    for i in range(len(dat)):
        if i+1 == dat[i]:
            t = i
    if t == -1:
        f = False
        break
    res.append(dat[t])
    del dat[t]
if f is False:
    print(-1)
else:
    res.reverse()
    print("\n".join(list(map(str, res))))

Submission status: Accepted

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

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
"""
index = j を満たすもののうち最大のものを消していく
→消した番号の逆順が答え
"""
n = int(input())
b = list(map(int, input().split()))

ans = []

while len(b) != 0:
    index = -1
    for j in range(len(b)):
        if j+1 == b[j]:
            index = j

    if index == -1:
        print(-1)
        exit()
    else:
        ans.append(b.pop(index))

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

Submission status: Accepted

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

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

Submission status: Accepted

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

L = []

while B:
    temp = []
    for i in range(len(B)):
        if B[i] == i+1:
            temp.append(i+1)
    if temp:
        L.append(max(temp))
        B.remove(max(temp))
    else:
        break

if B:
    print(-1)
else:
    L.reverse()
    for l in L:
        print(l)

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!
# coding: utf-8
from fractions import gcd 
from functools import reduce
import sys
sys.setrecursionlimit(200000000)
from inspect import currentframe


# my functions here!
#標準エラー出力
def printargs2err(*args):
    names = {id(v):k for k,v in currentframe().f_back.f_locals.items()}
    print(', '.join(names.get(id(arg),'???')+' : '+repr(arg) for arg in args),file=sys.stderr)
def debug(*args):
    print(*args,file=sys.stderr)

def printglobals():
    for (symbol, value) in globals().items():
        print('symbol="%s"、value=%s' % (symbol, value),file=sys.stderr)
def printlocals():
    for (symbol, value) in locals().items():
        print('symbol="%s"、value=%s' % (symbol, value),file=sys.stderr)
#入力（後でいじる）
def pin(type=int):
    return map(type,input().split())
     
#input

                
def resolve():
    N=int(input())
    b=list(pin())
    ans=[]
    t=0
    for elem in range(N):
        for i in range(N-t,0,-1):
            if b[i-1]==i:
                ans.append(i)
                b.pop(i-1)
                t+=1
                break
    if len(ans)!=N:print("-1")
    else:
        for wa in reversed(ans):
            print(wa)
        #print([["NA","YYMM"],["MMYY","AMBIGUOUS"]][cMMYY][cYYMM])
#if __name__=="__main__":resolve()

"""
#printデバッグ消した？
#前の問題の結果見てないのに次の問題に行くの？
"""
"""
お前カッコ閉じるの忘れてるだろ
"""
import sys
from io import StringIO
import unittest

class TestClass(unittest.TestCase):
    def assertIO(self, input, output):
        stdout, stdin = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(input)
        resolve()
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, stdin
        self.assertEqual(out, output)
    def test_入力例_1(self):
        input = """3
1 2 1"""
        output = """1
1
2"""
        self.assertIO(input, output)
    def test_入力例_2(self):
        input = """2
2 2"""
        output = """-1"""
        self.assertIO(input, output)
    def test_入力例_3(self):
        input = """9
1 1 1 2 2 1 2 3 2"""
        output = """1
2
2
3
1
2
2
1
1"""
        self.assertIO(input, output)

if __name__=="__main__":resolve()

Submission status: Accepted

Solution code:
n = int(input())
B = list(map(int, input().split()))
ret = []
enabled = True

def get_latest():
  for i in range(len(B)):
    if len(B) - i == B[len(B) - i - 1]:
      break
  else:
    global enabled
    enabled = False
  ret.append(B.pop(len(B) - i - 1))
  if B:
    get_latest()

get_latest()
if enabled:
  for i in range(n):
    print(ret[n - i - 1])
else:
  print(-1)

Submission status: Accepted

Solution code:
import sys
n = int(input())
b_ls = [int(i) for i in sys.stdin.readline().split()]
cnt = 0
res = []
while len(b_ls) > 0:
    flg = True
    for i in range(n - cnt - 1, -1, -1):
        if b_ls[i] == i + 1:
            cnt += 1
            res.append(i+1)
            b_ls.pop(i)
            flg = False
            break
    if flg:
        res = -1
        break
if res == -1:
    print(res)
else:
    for b in res[::-1]:
        print(b)

Submission status: Accepted

Solution code:
N = int(input())
blist = list(map(int, input().split()))
ans = []
for i in range(N):
    for index in range(N-i-1, -1, -1):
        if blist[index] == index + 1:
            ans.append(blist.pop(index))
            break
    else:
        print(-1)
        break
else:
    for num in ans[::-1]:
        print(num)

Submission status: Accepted

Solution code:
import sys

N = int(input())
#Bs = list(map(int, input().split()))
Bs = [B - 1 for B in map(int, input().split())]

anss = []
for _ in range(N):
    iNow = -1
    for i in reversed(range(len(Bs))):
        if Bs[i] == i:
            iNow = i
            break
    else:
        print(-1)
        sys.exit()

    anss.append(iNow)

    Bs = Bs[:iNow] + Bs[iNow+1:]

print('\n'.join(map(str, [ans+1 for ans in anss[::-1]])))

Submission status: Accepted

Solution code:
# inserton
n=int(input())
lists=list(map(int,input().split()))
flag=True
for i in range(n):
    if i+1<lists[i]:
        flag=False
        break
if not flag:
    print(-1)
else:
    #if flag
    ans=[]
    for i in range(n):
        for j in range(n-1-i,-1,-1):
            if lists[j]==j+1:
                ans.append(lists[j])
                del lists[j]
                break
    ans=ans[::-1]
    for some in ans:
        print(some)

Submission status: Accepted

Solution code:
N=int(input())
x=[0]
inp=input().split()
for k in range(N):
  x.append(int(inp[k]))
y=[]
OK=True
for k in range(N):
  ok=False
  for i in range(len(x)-1,0,-1):
    if x[i]==i:
      y.insert(0,i)
      del x[i]
      ok=True
      break
  if ok==False:
    print(-1)
    OK=False
    break
if OK==True:
  for k in range(len(y)):
    print(y[k])

Submission status: Accepted

Solution code:
def main():
    N=int(input())
    b=list(map(int,input().split()))
    result_inv=[]
    result_flag=True
    for _ in range(N):
        flag=False
        for i in range(len(b)):
            if i+1==b[i]:
                j=i
                flag=True
        if flag:
            a=b.pop(j)
            result_inv.append(a)
            flag=False
        else:
            result_flag=False
            break

    if result_flag:
        result=result_inv[::-1]
        for i in range(N):
            print(result[i])
    else:
        print(-1)

if __name__=="__main__":
    main()

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    B = [int(i) for i in input().split()]
    ans = []
    while B:
        for i in range(len(B))[::-1]:
            if B[i] == i+1:
                ans.append(B[i])
                del B[i]
                break
        else:
            return print(-1)
    print(*ans[::-1], sep="\n")


if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
for i in range(N):
    A[i] -= 1
ans = []
for _ in range(N):
    M = len(A)
    for i in range(M - 1, -1, -1):
        if A[i] > i:
            print(-1)
            exit()
        if A[i] == i:
            ans.append(A[i] + 1)
            A.pop(i)
            break
    if len(ans) == N:
        break
ans.reverse()
for a in ans:
    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(n-1-i,-1,-1):
        if b[j]==j+1:
            t=b.pop(j)
            ans.append(t)
            break
    else:
        print(-1)
        exit()

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

Submission status: Accepted

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

cant = 0
stack = []
for i in range(n, 0, -1):
        tmp = len(a)
        for j in range(len(a)-1, -1, -1):
                if j+1==a[j]:
                        if j>i: cant = 1
                        a.pop(j)
                        stack.append(j+1)
                        break
        if len(a)==tmp: cant = 1

if cant: print(-1)
else:
        for i in stack[::-1]: print(i)

Submission status: Accepted

Solution code:
N,*B=map(int, open(0).read().split())
res=[]
while B:
    C=[i-b for i,b in enumerate(B,1)]
    if any(c<0 for c in C):
        print(-1)
        break
    D=[i for i,c in enumerate(C) if c==0]
    if D:
        res.append(D[-1]+1)
        B=B[:D[-1]]+B[D[-1]+1:]
    else:
        print(-1)
        break
else:
    print(*res[::-1], sep="\n")

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# 操作を逆順に考える
# j番目の数がjなら取り除ける
# 取り除けるものが複数ある場合、最も右のを取り除かないと、番号がズレて取り除けなくなる
# 1,2,3
# 1を取り除くと
# 2,3になってダメ
# 順番に操作して、逆順に出力

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

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

Submission status: Accepted

Solution code:
n=int(input())
a=[]
b=list(map(int,input().split()))
for i in range(n):
  for j in range(n)[n-i-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())
a=[]
b=list(map(int,input().split()))
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)