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()))
a = []
flag = 0
answer_flag = 0

while(1):
    count = len(b)
    if(count == 0):
        break
    for i in reversed(range(count)):
        if(b[i] == i + 1):
            a.append(b.pop(i))
            answer_flag = 1
            break
    if(answer_flag):
        answer_flag = 0
        continue
    flag = 1
    break

if(flag == 1):
    print('-1')
else:
    for i in reversed(range(N)):
        print(a[i])

Submission status: Accepted

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

for i in range(n, 0, -1):
    for j in range(i, 0, -1):
        if B[j-1] == j:
            A.append(j)
            del B[j-1]
            break
    else:
        print(-1)
        break
else:
    A = A[::-1]
    for i in range(n):
        print(A[i])

Submission status: Accepted

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

result = []
for bi in B:
    if bi - 1 > len(result):
        print(-1)
        quit()
    result.insert(bi - 1, bi)

for ri in result:
    print(ri)

Submission status: Accepted

Solution code:
def find(sequence):
    result_list = []
    for i in range(len(sequence)):
        for j in reversed(list(range(len(sequence)))):
            if sequence[j] == j + 1:
                result_list.append(sequence.pop(j))
                break
    if len(sequence) != 0:
        return [-1]
    return result_list


if __name__ == "__main__":
    n = input()
    sequence = input()
    data = [int(item) for item in sequence.split(" ")]
    print("\n".join(map(str, reversed(find(data)))))

Submission status: Accepted

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

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

res = [-1]*n
#今考えている数列のindex、ないなら-1
ind = [i for i in range(1,n+1)]
#決定した数
cnt = 0
while cnt != n:
    #judge:変化があるか
    judge = False
    for i in range(n-1,-1,-1):
        if ind[i] == b[i]:
            ind[i] = -1
            res[n-1-cnt] = b[i]
            cnt += 1
            judge = True
            break
        else:
            ind[i] -= 1
    if not judge:
        print(-1)
        exit()
for e in res:
    print(e)

Submission status: Accepted

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


inv_output = list()
for i in range(N, 0, -1):
  for j in range(i, 0, -1):
    if B[j - 1] == j:
      inv_output.append(B.pop(j - 1))
      break
  else:
    print(-1)
    break
else:
  for i in inv_output[::-1]:
    print(i)

Submission status: Accepted

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

a = []
n = N
while len(b):
    index = -1
    for i in range(n):
        if i+1 == b[i]:
            index = i
    if index > -1:
        a.insert(0, b.pop(index))
        n -= 1
    elif index == -1:
        print(index)
        quit()

for e in a:
    print(e)

Submission status: Accepted

Solution code:
"""
A - Limited Insertion
"""
import numpy as np

N = input()
b_list = list(map(int, input().split()))
# N = 9
# b_list = [1, 1, 1, 2, 2, 1, 2, 3, 2]
# b_list = [1,1,1,2,2,1,2,3,2]
# b_list = [1,1,3,1,2]
# b_list = [2,2]


def make_idx(tmp_list):
    return np.arange(1, len(tmp_list)+1, 1)[::-1]

b_list = b_list[::-1]
idx_list = make_idx(b_list)
rm_list = []

while len(b_list):
    for idx, b in zip(idx_list, b_list):
        if idx==b:
            b_list = b_list[::-1]
            b_list.remove(b)
            rm_list.append(b)
            b_list = b_list[::-1]
            break
        elif idx-b<0:
            print(-1)
            exit()
    idx_list = make_idx(b_list)
    
for r in rm_list[::-1]:
    print(r)

Submission status: Accepted

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

N = int(input())
B = list(map(int, input().split()))
ans = []
while len(B) > 0:
  ok = False
  for i in reversed(range(len(B))):
    if B[i] == i+1:
      ok = True
      ans.append(B.pop(i))
      break
  if not ok:
    break
if len(B) > 0:
  print(-1)
else:
  for a in reversed(ans):
    print(a)

Submission status: Accepted

Solution code:
def find(A):
    stack=[]
    while A:
        #print(stack)
        index=-1
        for i in range(len(A)):
            if A[i]==i+1:
                index=i
        
        if index!=-1:
            
            stack+=[index+1]
            A=A[:index]+A[index+1:]
        else:
            return -1
    return stack   
        
        
N=int(input())
A=list(map(int,input().strip().split(' ')))

temp=find(A)

if temp==-1:
    print(temp)
    
else:
    while temp:
        print(temp.pop())

Submission status: Accepted

Solution code:
input()
a=[""]
for i in input().split():
  a.insert(int(i)-1,i)
  # print(a)
print(*[-1]if a[-1]else a)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#!/bin/env python3

IS = lambda: int(input())
IA = lambda: [int(x) for x in input().split()]

N = IS()
B = IA()

def f(b, n):
    ans = []
    while b:
        for i in range(len(b), 0, -1):
            j = i - 1
            if i == b[j]:
                ans.append(i)
                b = b[:j] + b[j+1:]
                break
        else:
            return None
    return list(reversed(ans))

ans = f(B, N)
if ans:
    print(*ans, sep="\n")
else:
    print(-1)

Submission status: Accepted

Solution code:
#!usr/bin/env python3
from collections import defaultdict
from collections import deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
import itertools
sys.setrecursionlimit(10**5)
stdin = sys.stdin
def LI(): return list(map(int, stdin.readline().split()))
def LF(): return list(map(float, stdin.readline().split()))
def LI_(): return list(map(lambda x: int(x)-1, stdin.readline().split()))
def II(): return int(stdin.readline())
def IF(): return float(stdin.readline())
def LS(): return list(map(list, stdin.readline().split()))
def S(): return list(stdin.readline().rstrip())
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007

#A
def A():
    n = II()
    b = LI()
    b = b[::-1]
    ans = deque([])
    while len(b) != 0:
        for num, i in enumerate(b):
            if n - num == i:
                ans.appendleft(i)
                n -= 1
                del b[num]
                break
        else:
            print(-1)
            return
    for i in ans:
        print(i)
    return

#B
def B():
    return

#C
def C():
    return

#D
def D():
    return

#E
def E():
    return

#F
def F():
    return

#G
def G():
    return

#H
def H():
    return

#Solve
if __name__ == '__main__':
    A()

Submission status: Accepted

Solution code:
input();a=['']
for i in input().split():a.insert(int(i)-1,i)
print(*a[-1]and[-1]or a)

Submission status: Accepted

Solution code:
input();a=[""]
for i in input().split():a.insert(int(i)-1,i)
print(*[-1]if a[-1]else a)

Submission status: Accepted

Solution code:
#coding: utf-8
n = int(input())
a = []
b = list(map(int, input().split()))
flag = True
for _ in range(n):
    add = 0
    for j in range(1, len(b)+1):
        if j == b[j-1]:
            add = max(add, j)
    if add == 0:
        flag = False
        break
    a.append(add)
    del b[add-1]
if flag:
    for i in range(len(a)):
        print(a[-i-1])
else:
    print(-1)

Submission status: Accepted

Solution code:
N = int(input())  # 数値入力
b = list(map(int, input().split()))  # リスト入力（数値）  map関数はリストの各要素に対して関数を適用してくれる

b.reverse()
ans = []
while b:
    check = 0

    tmp = 0
    for i in b:
        if i == len(b) - tmp:
            ans.append(b.pop(tmp))
            check = 1
            break

        tmp += 1

    if check == 0:
        check = 2
        break


if check == 2:
    print(-1)
else:
    ans.reverse()
    for i in ans:
        print(i)

Submission status: Accepted

Solution code:
N=int(input())
b=[int(i) for i in input().split()]
A=True
a=[]
for _ in range(N):
  ans=0
  for j in range(len(b)):
    if j+1==b[j]:
      ans=max(ans,j+1)
  if ans==0:
    A=False
    break
  a.append(ans)
  del b[ans-1]
    
a=a[::-1]
if A:
  for i in a:
    print(i)
else:
  print(-1)

Submission status: Accepted

Solution code:
N = int(input())
b = list(map(int, input().split()))
flag = True
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:
        flag = False
        break
if flag:
    for i in range(N-1, -1, -1):
        print(ans[i])
else:
    print(-1)

Submission status: Accepted

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

ans = []
while len(ans) < n:
  pop_i = -1
  for i in range(len(b)):
    if b[i] == i+1:
      pop_i = i
  if pop_i >= 0:
    b.pop(pop_i)
    ans.append(pop_i+1)
  else:
    print(-1)
    break
else:
  for i in range(n)[::-1]:
    print(ans[i])

Submission status: Accepted

Solution code:
a=[0]*int(input())
for i in input().split():a.insert(int(i)-1,i);a.pop()
print(*[a,[-1]][0in a])

Submission status: Accepted

Solution code:
import copy
N = int(input())
A = list(map(int, input().split()))
B = []
for _ in range(N):
    cand = []
    for i, a in enumerate(A):
        if a == i + 1:
            cand.append(i)
    if len(cand) > 0:
        ind = cand[-1]
        B.append(A[ind])
        del(A[ind])
    else:
        print(-1)
        exit()
if len(B) < N:
    print(-1)
else:
    B.reverse()
    for b in B:
        print(b)

Submission status: Accepted

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

ans = []

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

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

Submission status: Accepted

Solution code:
n,*a=map(int,open(0).read().split());b=[0]*n
for i in a:b.insert(i-1,i)
print(*[b[:n],[-1]][0in b[:n]])

Submission status: Accepted

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

for j in range(n):
    for i in range(len(b), 0, -1):
        try:
            if b[i-1] == i:
                b.pop(i-1)
                ans.append(i)
                break
        except: pass 
            
if len(b) == 0:
    [print(ans[-i-1]) for i in range(n)]
else:
    print(-1)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n,*a=map(int,open(0).read().split());b=[0]*n
for i in a:b.insert(i-1,i)
print(*[[-1],b[:n]][sum(a)==sum(b[:n])])

Submission status: Accepted

Solution code:
N=int(input())
l=list(map(int,input().split()))
s=[]
flag=1
while len(l)!=0:
    flag=0
    for i in range(len(l),0,-1):
        if i==l[i-1]:
            p=l[i-1]
            s.append(p)
            l.pop(i-1)
            flag=1
            break
    if flag==0:
        ans=-1
        break

if flag==1:
    ans=reversed(s)
    ans=list(ans)
    for i in ans:
        print(i)
else:
    print(ans)

Submission status: Accepted

Solution code:
INF = 999999999999999999L
EPS = 1e-12

def read():
    return raw_input().strip()

def read_ints():
    return map(int,read().split())

n = int(read())
arr = read_ints()

ans = []

while len(arr) > 0:
    remove = -1 
    for i in range(len(arr)):
        if arr[i] == i+1:
            remove = i 
            
    if remove == -1:
        break 
        
    ans.append(arr[remove])
    del arr[remove]
    
if len(ans) == n:
    for x in reversed(ans):
        print x 
else:
    print -1

Submission status: Accepted

Solution code:
n,*a=map(int,open(0).read().split());b=[0]*n
for i in a:b.insert(i-1,i)
print(*[[-1],b[:n]][sum(a)==sum(b[:n])],sep='\n')

Submission status: Accepted

Solution code:
from collections import deque

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

# 最後に挿入した数字は
# k番目のkとなる

# 1 2 2 1 2 3 2
# 複数通り該当するときは
# 最後の数が該当する
# 0 0 3 0 0 6
# 3を取り除くと
# 0 0 0 0 6 -> 矛盾

ans = deque()

q = deque(enumerate(b, 1))
r = deque()

flg = False
while q:
    idx, bb = q.pop()
    # print(idx, bb, r)
    if idx == bb:
        ans.appendleft(bb)
        if len(ans) == N:
            flg = True
            break
        q.extend(r)
        r = deque()
    else:
        r.appendleft((idx - 1, bb))

if flg:
    for aa in ans:
        print(aa)
else:
    print(-1)

Submission status: Accepted

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

result = []
for i in range(N):
    for j in range(N - i - 1, -1, -1):
        if j + 1 == B[j]:
            result.insert(0, B.pop(j))
            break
    else:
        print(-1)
        quit()

for ri in result:
    print(ri)

Submission status: Accepted

Solution code:
n,*b=map(int,open(0).read().split());a=[]
while b:j=max(i*(i==v)for i,v in enumerate(b,1));b.pop(j-1);a=j<1and[-1]or[j]+a
print(*a)

Submission status: Accepted

Solution code:
n,*b=map(int,open(0).read().split());a=[]
while b:j=max(i*(i==v)for i,v in enumerate(b,1));b.pop(j-1);a=j<1and[-1]or a+[j]
print(*a[::-1])

Submission status: Accepted

Solution code:
import copy
N=int(input())
b=[int(i) for i in input().split()]
 
c = copy.copy(b) #コピーを行う
a = [0]*N
a_n = N-1
 
for n in range(N)[::-1]:#N桁
  for i in range(len(c))[::-1]:#大きい数字からcheck
    if c[i]==i+1:#iが9を超えても成立する
      a[a_n] = c.pop(i)
      #print(c,len(c))
      a_n = a_n-1
      break#このbreakに気づかなかったので、2を取り除いたあとに1も取り除いてしまった
 
if n-1 != a_n:
  print(-1)
  exit()
else:
  for i in range(N):
    print(a[i])

Submission status: Accepted

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

# print(b)

can_make = True
com_list = []
while len(b) > 0 :
    insert_list = []
    for i in range(len(b)):
        if b[i] == i+1:
            insert_list.append(i)
    if not insert_list:
        can_make = False
        break
    com_list.append(b[insert_list[-1]])
    del b[insert_list[-1]]

if can_make:
    for x in reversed(com_list):
        print(x)
else:
    print(-1)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n,*b=map(int,open(0).read().split())
a=[]
while b:
  j=max(i*(i==v)for i,v in enumerate(b,1))
  b.pop(j-1)
  a+=[j]
  if j<1:a=[-1]
print(*a[::-1])

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
N = int(input())
b = [int(i) for i in input().split()]
ans = []
for i in range(len(b)):
    removed = False
    for j in reversed(range(len(b))):
        if b[j] == j + 1:
            ans.append(b.pop(j))
            removed = True
            break
    if not removed:
        print(-1)
        exit()
print('\n'.join(map(str, reversed(ans))))

Submission status: Accepted

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

while i <= len(b):
    test = b[-i]
    if test == len(b)-i+1:
        b.pop(-i)
        ans.append(test)
        i = 1
        continue
    elif i == len(b):
        print(-1)
        exit()
    else:
        i += 1

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

Submission status: Accepted

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

a = []
i = len(b) - 1
while(i >= 0):
    if(i + 1 == b[i]):
        pick = b[i]
        b.remove(pick)
        a = [pick] + a
        i = len(b)
    i -= 1

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

for i in a:
    print(str(i))

Submission status: Accepted

Solution code:
n=int(input())
x=list(map(int,input().split()))
l=[]
while len(x):
    index=-1
    for i,a in enumerate(x):
        if a==i+1:
            index=i
    if index == -1:
        print(-1)
        break
    l.append(x.pop(index))
else:
    for i in reversed(l):
        print(i)

Submission status: Accepted

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

l = []
count = 0

for j in range(n):
    for i in reversed(range(len(b))):
        if b[i] == i+1:
            l.append(b.pop(i))
            count += 1
            break
if count != n:
    print(-1)
else:
    for i in reversed(range(n)):
        print(l[i])

Submission status: Accepted

Solution code:
n,*b=map(int,open(0).read().split())
a=[]
while b:
  j=max(i*(i==v)for i,v in enumerate(b,1))
  if j:
    b=b[:j-1]+b[j:]
    a+=[j]
  else:
    a=[-1]
    break
print(*a[::-1])

Submission status: Accepted

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

a=[]
while len(b) > 0:
    filtered = [i for i in range(len(b)) if i == b[i] - 1]
    if len(filtered) == 0:
        print(-1)
        exit(0)
    j = filtered[-1]
    a.append(b[j])
    del b[j]

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

Submission status: Accepted

Solution code:
N = int(input())
list_b = list(map(int, input().split()))
ans = [0 for i in range(N)]

finish = False

for i in range(N-1, -1, -1):
  mx = -1
  for k in range(i+1):
    if list_b[k] == k+1:
      mx = max(mx, k+1)
  if mx == -1:
    finish = True
    break
  else:
    ans[i] = mx
    for j in range(mx-1, i):
      list_b[j] = list_b[j+1]
    
if not finish:
    for i in range(N):
        print(ans[i])
else:
    print(-1)

Submission status: Accepted

Solution code:
from queue import deque

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

res = True
for i, v in enumerate(b):
	if i + 1 < v:
		res = False
		break

if res:
	l = list(b)
	ans = []
	while l:
		for i in range(len(l), 0, -1):
			if i == l[i-1]:
				ans.append(i)
				del l[i-1]
				break
	for v in reversed(ans):
		print(v)
else:
	print(-1)

Submission status: Accepted

Solution code:
import sys
from collections import deque
import copy
import math
def get_read_func(fileobject):
    if fileobject == None :
        return raw_input
    else:
        return fileobject.readline

def dfs(A, already_use, value_list, vi):
    li = []
    for i in range(len(A)):
        if already_use[i] == True:
            break
        if A[i] == value_list[vi] :
            li.append(i)
    for i in range(len(li) - 1, -1, -1):
        print value_list[vi]
        already_use[li[i]] = True
        dfs(A[li[i] + 1:],  already_use[li[i] + 1:], value_list, vi + 1)

def dfs(A, x):
    li = []
    for i in range(len(A)):
        if already_use[i] == True:
            break
        if A[i] == value_list[vi] :
            li.append(i)
    for i in range(len(li) - 1, -1, -1):
        print value_list[vi]
        already_use[li[i]] = True
        dfs(A[li[i] + 1:],  already_use[li[i] + 1:], value_list, vi + 1)


def main():
    if len(sys.argv) > 1:
        f = open(sys.argv[1])
    else:
        f = None
    read_func = get_read_func(f);
    input_raw = read_func().strip().split()
    [N] = [int(input_raw[0])]
    input_raw = read_func().strip().split()
    A = [int(input_raw[i]) for i in range(N)]

    ans = []
    already_use = [False for i in range(N)]
    while 1:
        if len(A) <= 0:
            break
        is_NG = True
        for i in range(len(A) - 1, -1, -1):
            if A[i] == i + 1:
                is_NG = False
                ans.append(A[i])
                del A[i]
                break
        if is_NG == True:
            print -1
            return
    for i in range(len(ans)-1, -1, -1):
        print ans[i]




if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from collections import deque

if __name__ == '__main__':
    n = int(input())
    b = [-1] + [int(s) for s in input().split()]
    ans = deque()
    for _ in range(n):
        flag = False
        for i in range(len(b) - 1, 0, -1):
            if b[i] == i:
                b.pop(i)
                ans.appendleft(i)
                flag = True
                break
        if flag == False:
            print(-1)
            quit(0)
    for x in ans:
        print(x)

Submission status: Accepted

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

b.reverse()
ans = []
disable = False

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

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

Submission status: Accepted

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


def delitable(numbers):
    return [v for i, v in enumerate(numbers) if v == i + 1]


log = []
while b:
    delit = delitable(b)
    if not delit:
        print(-1)
        break
    del b[delit[-1] - 1]
    log.append(delit[-1])
else:
    log.reverse()
    print("\n".join(map(str, log)))

Submission status: Accepted

Solution code:
n,*b=map(int,open(0).read().split())
a=[]
while n:
  j=-1
  for i in range(n):
    if b[i]==i+1:
      j=i
  if j+1:
    b=b[:j]+b[j+1:]
    a+=[j+1]
  else:
    a=[-1]
    break
  n-=1
print(*a[::-1])

Submission status: Accepted

Solution code:
n,*a=map(int,open(0).read().split());b=[0]*n
for i in a:b.insert(i-1,i)
if sum(b[:n])==sum(a):
 for i in range(n):print(b[i])
else:print(-1)

Submission status: Accepted

Solution code:
import sys

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

ok = 1
for i in range(N):
    if B[i] > i+1:
        ok = 0
if not ok:
    print(-1)
    exit(0)

ans = []
S = [0]*N
for i in range(N):
    rest = i
    for j in range(N-1, -1, -1):
        if S[j] == 1:
            rest -= 1
            continue
        if rest+1 == B[j]:
            ans.append("%d\n" % B[j])
            S[j] = 1
            break
sys.stdout.writelines(ans)

Submission status: Accepted

Solution code:
# 2019/03/23
# AtCoder Grand Contest 032 - A

# Input
n = int(input())
b = list(map(int, input().split()))
anslist = list()
ngflg = False

for idx6 in range(n):
    maxidx = -1
    for idx7 in range(len(b)):
        if b[idx7] == idx7 + 1:
            maxidx = idx7
    
    if maxidx >= 0:
        anslist.append(b.pop(maxidx))
    else:
        ngflg = True

    if ngflg:
        print(-1)
        break

if ngflg == False:
    for idx4 in range(n-1, -1, -1):
        print(anslist[idx4])

Submission status: Accepted

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

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

L.reverse()

if len(B)==0:
	for x in L:
		print x
else:
	print -1

Submission status: Accepted

Solution code:
def int_raw():
    return int(raw_input())

def ss_raw():
    return raw_input().split()

def ints_raw():
    return map(int,ss_raw())



def sol():
    n = int_raw()
    bs = ints_raw()
    ans =[]
    buf = list(bs)
    l = len(buf)
    while l!=0:
        #print l
        old_l = l
        for i in xrange(l,0,-1):
            b = buf[i-1]
            if b == i:
                ans.append(buf.pop(i-1))
                l = len(buf)
                break
        if old_l == l:            
            print -1
            return -1
    for a in reversed(ans):
        print a

sol()

Submission status: Accepted

Solution code:
n,*a=map(int,open(0).read().split());b=[0]*n
for i in a:b.insert(i-1,i)
if sum(b[:n])==sum(a):
    for i in range(n):print(b[i])
else:print(-1)

Submission status: Accepted

Solution code:
n,*a=map(int,open(0).read().split());b=[0]*n
for i in a:
    b.insert(i-1,i)
if sum(b[:n])==sum(a):
    for i in range(n):print(b[i])
else:print(-1)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
B = [int(b) for b in input().split()]
lenB = len(B)
Ans = [None] * N
while lenB > 0:
    for i in reversed(range(lenB)):
        if i+1 == B[i]:
            Ans[lenB-1] = B[i]
            del B[i]
            lenB -= 1
            break
    else:
        print(-1)
        break
else:
    for a in Ans:
        print(a)

Submission status: Accepted

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

    operations = []
    for i in range(N):
        j_remove = -1
        for j in range(N-i):
            if bs[j] == j+1:
                j_remove = j
        if j_remove == -1:
            print("-1")
            exit()
        operations.append(bs.pop(j_remove))
    for n in reversed(operations):
        print(n)

Submission status: Accepted

Solution code:
N=int(input())
b=[int(i) for i in input().split()]
ans=[0]*N
for i in range(N)[::-1]:
  for k in range(i+1)[::-1]:
    if b[k]==k+1:
      ans[i]=b[k]
      break
    if k==0:
      print(-1)
      exit()
  b.pop(k)
for i in ans:
  print(i)

Submission status: Accepted

Solution code:
import math, string, itertools, fractions, heapq, collections, re,  array, bisect, sys, random, time, copy, functools
sys.setrecursionlimit(10**7)
inf = 10 ** 20
eps = 1.0 / 10**10
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 LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def pf(s): return print(s, flush=True)


N = I()
b = LI()

result = []
while True:
    #  print(b)
    for i in range(1, len(b)+1):
        #  print(i)
        if b[-i] == len(b)-i+1:
            #  print('break')
            result.append(b[-i])
            del b[-i]
            break
    else:
        if not b:
            break
        print(-1)
        exit()
for i in result[::-1]:
    print(i)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def solve():
  N = int(input())
  B = [int(i) for i in input().split()]
  ans = [0 for i in range(N)]

  for i in range(N - 1, -1, -1):
    v = -1
    for j in range(i + 1):
      if B[j] == j + 1: v = j
    if v == -1: return [ -1 ]
    ans[i] = v + 1
    B.pop(v)
  return ans

print(*solve(), sep='\n')

Submission status: Accepted

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

Submission status: Accepted

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

for i in range(N, 0, -1):
  for j in range(i, 0, -1):
    if a[j-1] == j:
      b.append(j)
      del a[j-1]
      break
    else:
      pass
  else:
    b = [-1]
    break

# print(b)
b.reverse()

for i in b:
  print(i)

Submission status: Accepted

Solution code:
from collections import Counter
import sys
def zur(z, xx):
    c = 0
    k = 0
    i = 0
    while True:
        i += 1
        if i in xx:
            c += 1
        else:
            k += 1
        if k == z:
            return z + c

def xxxxx(z, xx):
    if z not in xx:
        return z
    while True:
        z -= 1
        if z == 0:
            print(-1)
            sys.exit()
        if z not in xx:
            return z

def mkxp(xs):
    xp = {}
    for i, x in enumerate(xs):
        i = i+1
        if x > i:
            print(-1)
            sys.exit()
        if x not in xp:
            xp[x] = list()
        xp[x].append(i)
    return xp

i = input()
xs = [int(x) for x in input().strip().split(" ")]
xp = mkxp(xs)


x = max(xs)
xo = []
xx = set()

while True:
    last = len(xs)
    if x == 0:
        break
    nxx = set()
    if x in xp:
        for i in xp[x]:
            xxx = zur(x + last - i, xx)
            xo.append((xxx, x))
            nxx.add(xxx)
        last -= len(xp[x])
    xx = nxx | xx
    xs = [int(i) for i in xs if i != x]
    xp = mkxp(xs)
    x -= 1

for i, x in sorted(xo, key=lambda x:x[0]):
    print(x)

Submission status: Accepted

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

stack = []
for i in range(N):
    tmp = []
    flag = True
    for j in range(len(B)):
        if B[j]==j+1:
            tmp.append(j)
    if tmp:
        tmp.sort(reverse=True)
        ans = tmp[0]
        B = B[:ans] + B[ans+1:]
        stack.append(ans+1)
    else:
        flag = False
        break
if flag:
    stack = stack[::-1]
    for i in stack:
        print(i)
else:
    print(-1)