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: Wrong Answer

Solution code:
import sys

input = sys.stdin.readline

n = int(input())

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

sort_b_list = sorted(b_list)

for i in range(1,n+1):
    if i < sort_b_list[i-1]:
        print(-1)
        exit(0)

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import bisect,collections,copy,itertools,math,string
import sys
def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())
def main():
    n = I()
    b = LI()
    ans_lst = []

    while len(b)!=0:
        lst = collections.deque([])
        mx = 1
        phase = False
        while len(b)!=0:
            nxt = b.pop()
            mx = max(mx, nxt)
            if mx==1:
                lst.appendleft(nxt)
            else:
                phase |= nxt == 1
                if phase and nxt != 1:
                    b.append(nxt)
                    break
                else:
                    lst.appendleft(nxt)

        ctn = True
        up = True
        now_mx = 0
        now_mn = mx
        lenth = 0
        for x in lst:
            if up:
                if x>=now_mx:
                    if x==mx:
                        up = False
                    else:
                        lenth += 1
                else:
                    ctn = False
                    break
            else:
                if x<=now_mn:
                    now_mn = min(now_mn, x)
                else:
                    ctn = False
                    break
        
        if not ctn or lenth+1<mx or lst[0] != 1:
            print(-1)
            exit(0)
        
        gr = [[k,len(list(g))] for k, g in itertools.groupby(lst)]
        com = len(lst)
        phase = False
        down = False
        now_len = 0
        i = 0

        while com!= 0:
            nxt = lst[-1]
            if not phase:
                if now_len+1 < nxt:
                    if not down:
                        if gr[i][0] == mx:
                            down = True
                            i -= 1
                        else:
                            ans_lst.append(gr[i][0])
                            now_len += 1
                            com -= 1
                            gr[i][1] -= 1
                            i += 1
                    else:
                        if gr[i][1] == 0:
                            i -= 1
                        else:
                            ans_lst.append(gr[i][0])
                            now_len += 1
                            com -= 1
                            gr[i][1] -= 1
                else:
                    ans_lst.append(nxt)
                    com -= 1
                    lst.pop()
                    phase |= nxt == mx
            else:
                if nxt == mx:
                    ans_lst.append(nxt)
                    com -= 1
                    lst.pop()
                else:
                    if not down:
                        if gr[i][0] == mx:
                            down = True
                            i -= 1
                        else:
                            ans_lst.append(gr[i][0])
                            com -= 1
                            gr[i][1] -= 1
                            i += 1
                    else:
                        if gr[i][1] == 0:
                            i -= 1
                        else:
                            ans_lst.append(gr[i][0])
                            com -= 1
                            gr[i][1] -= 1

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

main()

Submission status: Wrong Answer

Solution code:
import bisect,collections,copy,itertools,math,string
import sys
def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())
def main():
    n = I()
    b = LI()
    ans_lst = []

    while len(b)!=0:
        lst = collections.deque([])
        mx = 1
        phase = False
        while len(b)!=0:
            nxt = b.pop()
            mx = max(mx, nxt)
            if mx==1:
                lst.appendleft(nxt)
            else:
                phase |= nxt == 1
                if phase and nxt != 1:
                    b.append(nxt)
                    break
                else:
                    lst.appendleft(nxt)

        ctn = True
        up = True
        now_mx = 0
        now_mn = mx
        lenth = 0
        for x in lst:
            if up:
                if x>=now_mx:
                    if x==mx:
                        up = False
                    else:
                        lenth += 1
                else:
                    ctn = False
                    break
            else:
                if x<=now_mn:
                    now_mn = min(now_mn, x)
                else:
                    ctn = False
                    break
        
        if not ctn or lenth+1<mx:
            print(-1)
            exit(0)
        
        gr = [[k,len(list(g))] for k, g in itertools.groupby(lst)]
        com = len(lst)
        phase = False
        down = False
        now_len = 0
        i = 0

        while com!= 0:
            nxt = lst[-1]
            if not phase:
                if now_len+1 < nxt:
                    if not down:
                        if gr[i][0] == mx:
                            down = True
                            i -= 1
                        else:
                            ans_lst.append(gr[i][0])
                            now_len += 1
                            com -= 1
                            gr[i][1] -= 1
                            i += 1
                    else:
                        if gr[i][1] == 0:
                            i -= 1
                        else:
                            ans_lst.append(gr[i][0])
                            now_len += 1
                            com -= 1
                            gr[i][1] -= 1
                else:
                    ans_lst.append(nxt)
                    com -= 1
                    lst.pop()
                    phase |= nxt == mx
            else:
                if nxt == mx:
                    ans_lst.append(nxt)
                    com -= 1
                    lst.pop()
                else:
                    if not down:
                        if gr[i][0] == mx:
                            down = True
                            i -= 1
                        else:
                            ans_lst.append(gr[i][0])
                            com -= 1
                            gr[i][1] -= 1
                            i += 1
                    else:
                        if gr[i][1] == 0:
                            i -= 1
                        else:
                            ans_lst.append(gr[i][0])
                            com -= 1
                            gr[i][1] -= 1

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

main()

Submission status: Wrong Answer

Solution code:
import numpy as np
N = int(input())
B = np.array(list(map(int, input().split())))

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

P = np.array([False] * N)
S = np.append(B[:-1] - B[1:], [0])
print(1)
P[0] = True
while not np.all(P):
    # print(f'{S=}')
    for idx in range(N - 1, -1, -1):
        if not P[idx] and B[idx] <= P.sum() + 1 and S[idx] <= 0:
            for i in range(N - 1):
                if P[i + 1]:
                    S[i] -= 1
            P[idx] = True
            print(B[idx])
            break
    else:
        break
for idx in range(N - 1, -1, -1):
    if not P[idx]:
        print(B[idx])

Submission status: Wrong Answer

Solution code:
import numpy as np
N = int(input())
B = np.array(list(map(int, input().split())))

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

P = np.array([False] * N)
S = np.append(B[:-1] - B[1:], [0])
while not all(P):
    # print(f'{S=}')
    for idx in range(N - 1, -1, -1):
        if not P[idx] and B[idx] <= P.sum() + 1 and S[idx] <= 0:
            for i in range(N - 1):
                if P[i + 1]:
                    S[i] -= 1
            P[idx] = True
            print(B[idx])
            break

# 1 2 3 4 5 6 7 8 9

# 1 1 1 1 1 1 1 1 2

# 1 1 3 2

# 1 2 3 2

# # 最初は必ず１
# # 後ろから見たら楽？
# # i 番目に i - 1の値がない限り必ずできる？
# 一番最後にしたい値が小さくてその隣が大きいと無理かも

### 3 2 のとき 3 - 2 = 1 回待機する。何回待機しないといけないかをPに保存するように変更

Submission status: Wrong Answer

Solution code:
N=int(input())
B=list(map(int,input().split()))
if B[0]!=1:
    print(-1)
else:
    flag=0
    for n in range(N-1):
        if abs(B[n]-B[n+1])>1:
            flag=1
            break
    if flag == 1:
        print(-1)
    else:
        A = ''
        for b in B:
            A += str(b)
        numbers = A.split('1')
        del numbers[0]
        reverse_numbers = list(reversed(numbers))
        answers=''
        for number in reverse_numbers:
            if number=='':
                answers+='1'
            else:
                num=''.join(sorted(number))
                num='1'+num
                answers+=num
        for answer in answers:
            print(answer)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))
b = 0; c = [[] for _ in range(n)]; d = []; e = 0
for i in range(n):
    if a[i] > i+1: b = 1
if b == 1: print(-1)
else:
    for i in range(n): c[i+1-a[i]].append(a[i])
    for i in range(n): c[i].sort()
    for i in range(n):
        for j in range(len(c[-i-1])): d.append(c[-i-1][j])
    for i in range(n): e = max(e, d[i]-i-1)
    f = d[:e+1]; g = d[e+1:]; f.sort(); d = f+g
    print(*d, sep="\n")

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
from collections import deque
from collections import Counter
import math

def readInt():
	return int(input())
def readInts():
	return list(map(int, input().split()))
def readChar():
	return input()
def readChars():
	return input().split()

n = readInt()
b = readInts()

d = deque()

def renzoku(arr):
	i = -1
	boo = 1
	for i in range(1,len(arr)+1):
		if arr[i-1] != i:
			boo = 0
			break
	if i-1+boo==0:
		print(-1)
		exit()
	return i-1+boo


for i in range(len(b)):
	r = renzoku(b)
	#print(b)
	#print(r)
	d.append(r)
	del b[r-1]

if -1 in d:
	print(-1)
	exit()

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

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
from collections import deque
from collections import Counter
import math

def readInt():
	return int(input())
def readInts():
	return list(map(int, input().split()))
def readChar():
	return input()
def readChars():
	return input().split()

n = readInt()
b = readInts()

d = deque()

def renzoku(arr):
	i = -1
	boo = 1
	for i in range(1,len(arr)+1):
		if arr[i-1] != i:
			boo = 0
			break
	if i==1 and boo==0:
		print(-1)
		exit()
	return i-1+boo


for i in range(len(b)):
	r = renzoku(b)
	#print(b)
	d.append(r)
	del b[r-1]

if -1 in d:
	print(-1)
	exit()

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

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
from collections import deque
from collections import Counter
import math

def readInt():
	return int(input())
def readInts():
	return list(map(int, input().split()))
def readChar():
	return input()
def readChars():
	return input().split()

n = readInt()
b = readInts()

d = deque()

def renzoku(arr):
	i = -1
	boo = 1
	for i in range(1,min(101,len(arr)+1)):
		if arr[i-1] != i:
			boo = 0
			break
	return i-1+boo


for i in range(len(b)):
	r = renzoku(b)
	#print(b)
	d.append(r)
	del b[r-1]

if 0 in d:
	print(-1)
	exit()

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

for i in b:
    if len(ans) >= i-1:
        ans.insert(i-1, i)
    else:
        print(-1)
        break
for i in ans:
    print(i, sep='\n')

Submission status: Wrong Answer

Solution code:
from collections import Counter
from collections import deque

N = int(input())
result_map = [0] * N

for b in input().split():
    result_map[int(b)-1] += 1

ok_flag = False
before = 1
result = []
for key, i in enumerate(result_map):
    if before == 0 and i >= 1:
        print(-1)
        exit()

    before = i
    result.extend([key + 1] * i)

for i in result:
    print(i)

Submission status: Wrong Answer

Solution code:
N = int(input())
b = [0] + list(map(int, input().split()))
#print(b[N])
flag = [1] * (N + 1) #抜き取った番号を記録する配列
ans = []
i = N
_i = 0
while True:
    #print(i - _i)
    if b[i - _i] > i - _i:
        print(-1)
        break
    elif b[i - _i] == i - _i:
        ans.append(i - _i)
        del b[i - _i]
        i -= 1
        _i = 0
    else:
        _i += 1
    if _i >= i:
        #print(list(reversed(ans)))
        break
for a in reversed(ans):
    print(a)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

p = [[] for _ in range(n)]
possible = True

for i in range(n):
    val = a[i]
    passed = i+1-val

    if passed < 0:
        possible = False
        break

    p[n-1-passed].append(val)

if possible:
    ans = [0 for _ in range(n)]

    for i in range(n-1, -1, -1):
        cur = sorted(p[i])
        ans[i] = cur.pop()

        p[i-1].extend(cur)

    for num in ans:
        print(num)
else:
    print(-1)

Submission status: Wrong Answer

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

le = 0
b = b[::-1]
for i in range(n):
    if b[i] > 1:
        x = i+b[i]-1
        if x >= n or a[x] > 0:
            print(-1)
            exit()
        a[x] = b[i]
    else:
        while le < n and a[le] != 0:
            le += 1
        if le >= n:
            print(-1)
            exit()
        a[le] = 1
    
for i in a:
    print(i)

Submission status: Wrong Answer

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

while le < n:
    l = []
    while le < n and b[le] != 1:
        l.append(b[le])
        le += 1
    if le >= n:
        print(-1)
        exit()
    l.append(1)
    le += 1
    
    d = 1
    now = l[0]
    count = [0]*(n+1)
    for i in l:
        if abs(i-now) > 1:
            print(-1)
            exit()
        if d == 1:
            if now <= i:
                now = i
            else:
                now = i
                d = -1
        else:
            if now >= i:
                now = i
            else:
                print(-1)
                exit()
        count[i] += 1
    f = []
    s = []
    
    l = l[::-1]+[0]
    now = 1
    c = 1
    for i in l[1:]:
        if now == i:
            c += 1
        elif now < i:
            for k in range(count[now]-c+1):
                f.append(now)
            for k in range(c-1):
                s.append(now)
            now = i
            c = 1
        else:
            for k in range(count[now]-c+1):
                f.append(now)
            for k in range(c-1):
                s.append(now)
            break
    
    f += s
    for i in f:
        ans.append(i)
for i in ans:
    print(i)

Submission status: Wrong Answer

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

ans = []
while True:
    if len(B) == 1:
        if B[0] == 1:
            ans.append(1)
            break
        else:
            print(-1)
            exit(0)

    flag = False
    for i in range(len(B)):
        if B[i] == i+1:
            flag = True
        elif flag:
            B.pop(i-1)
            ans.append(i)
            break
        else:
            print(-1)
            exit(0)
    else:
        if flag:
            ans.append(B[-1])
            B.pop(-1)


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

Submission status: Wrong Answer

Solution code:
from math import ceil,floor,factorial,gcd,sqrt,log2,cos,sin,tan,acos,asin,atan,degrees,radians,pi,inf,comb
from itertools import accumulate,groupby,permutations,combinations,product,combinations_with_replacement
from collections import deque,defaultdict,Counter
from bisect import bisect_left,bisect_right
from operator import itemgetter
from heapq import heapify,heappop,heappush
from queue import Queue,LifoQueue,PriorityQueue
from copy import deepcopy
from time import time
from functools import reduce
import string
import sys
sys.setrecursionlimit(10 ** 7)
def input() : return sys.stdin.readline().strip()
def INT()   : return int(input())
def MAP()   : return map(int,input().split())
def LIST()  : return list(MAP())

n = INT()
b = LIST()
ans = [1]*b.count(1)

for i in range(2, n+1):
    targ = 0
    for j in range(n-1, -1, -1):
        if b[j] == i - 1:
            targ += 1
        if b[j] == i:
            count = 0
            for k in range(len(ans)):
                if ans[k] == i - 1:
                    count += 1
                if count == targ + 1:
                    ans.insert(k+1, i)
                    break
            else:
                print(-1)
                exit()

for x in ans:
    print(x)

Submission status: Wrong Answer

Solution code:
from math import ceil,floor,factorial,gcd,sqrt,log2,cos,sin,tan,acos,asin,atan,degrees,radians,pi,inf,comb
from itertools import accumulate,groupby,permutations,combinations,product,combinations_with_replacement
from collections import deque,defaultdict,Counter
from bisect import bisect_left,bisect_right
from operator import itemgetter
from heapq import heapify,heappop,heappush
from queue import Queue,LifoQueue,PriorityQueue
from copy import deepcopy
from time import time
from functools import reduce
import string
import sys
sys.setrecursionlimit(10 ** 7)
def input() : return sys.stdin.readline().strip()
def INT()   : return int(input())
def MAP()   : return map(int,input().split())
def LIST()  : return list(MAP())

n = INT()
b = LIST()
ans = [1]*b.count(1)

for i in range(2, n+1):
    targ = 0
    for j in range(n-1, -1, -1):
        if b[j] == i - 1:
            targ += 1
        if b[j] == i:
            count = 0
            for k in range(len(ans)-1, -1, -1):
                if ans[k] == i - 1:
                    count += 1
                if count == targ + 1:
                    ans.insert(k, i)
                    break
            else:
                print(-1)
                exit()
                
for x in ans[::-1]:
    print(x)

Submission status: Wrong Answer

Solution code:
n=int(input())
b=list(map(int,input().split()))
flag=1
for i in range(n):
  if b[i]>i+1:flag=0
  for j in range(1,b[i]):
    if b[i-j-1]>b[i]:flag=0
if flag==0:
  print(-1)
  exit()
a=[]
r=n-1
while r>0:
  cnt=0
  while b[r]==1 and r>=0:
    r-=1
    cnt+=1
  a+=[1]*cnt
  if r==-1:break
  l,i=r,r-1
  while b[l]>1:
    if i-b[i]<l:l=i-b[i]+1
    i-=1
  c=[1]
  while l<r:
    if b[l]+1==b[r]:
      c+=[b[r]]
      r-=1
    else:
      l+=1
      c+=[b[l]]
  a+=c
  r=n-len(a)-1
for i in a:
  print(i)

Submission status: Wrong Answer

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

ans = []
tmp = 0

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

for i in range(1,n):
    if b[i] == 1:
        li = b[tmp:i]
        tmp = i
        a = [-1] * len(li)
        for j in range(100, 0, -1):
            flag = False
            for k,e in enumerate(li):
                if e == j:
                    flag = True
                    cnt = k - j + 1
                    if cnt < 0:
                        print(-1)
                        exit()
                    idx = -1
                    while cnt >= 0:
                        if a[idx] != -1 and a[idx] != e:
                            idx -= 1
                        else:
                            if cnt == 0:
                                a[idx] = e
                                break
                            if a[idx] == -1 or a[idx] == e:
                                cnt -= 1
                            idx -= 1
            if flag:
                li = [i for i in li if i != j]
        ans = a + ans

li = b[tmp:]
tmp = i
a = [-1] * len(li)
for j in range(100, 0, -1):
    flag = False
    for k,e in enumerate(li):
        if e == j:
            flag = True
            cnt = k - j + 1
            if cnt < 0:
                print(-1)
                exit()
            idx = -1
            while cnt >= 0:
                if a[idx] != -1 and a[idx] != e:
                    idx -= 1
                else:
                    if cnt == 0:
                        a[idx] = e
                        break
                    if a[idx] == -1 or a[idx] == e:
                        cnt -= 1
                    idx -= 1
    if flag:
        li = [i for i in li if i != j]
ans = a + ans

for i in ans:
    print(i)

Submission status: Wrong Answer

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

ans = []

while bb:
    for i,v in zip(range(len(bb),0,-1),bb[::-1]):
        print(i,v)
        if i==v:
            ans.append(v)
            del bb[i-1]
            break

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

Submission status: Wrong Answer

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

ans = []
for i,b in enumerate(sorted(B), 1):
    if i < b:
        print(-1)
        exit()
    else:
        ans.append(b)

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
# https://atcoder.jp/contests/agc032/tasks/agc032_a

n = int(input())
nums = [int(i) for i in input().split()]
nums.sort()
ans = []
for i, num in enumerate(nums, start=1):
    if num <= i:
        ans.append(num)
    else:
        print(-1)
        break
else:
    for a in ans:
        print(a)

Submission status: Wrong Answer

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

ans = []
while N > 0:
  p = False
  for i in range(N)[::-1]:
    b = B[i]
    if b == (i+1):
      ans.append(b)
      p = i
      break
  
  if not p:
    N -= 1
  else:
    newB = []
    for i in range(N):
      if i != p:
        newB.append(B[i])
    
    B = newB
    break
    
if N != 0:
  print(-1)
else:
  print("\n".join(map(str,ans[::-1])))

Submission status: Wrong Answer

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

ans = []
while N > 0:
  ok = False
  for i in range(N)[::-1]:
    b = B[i]
    if b == (i+1):
      ans.append(b)
      ok = True
      break
  
  if ok:
    N -= 1
  else:
    break
    
if N != 0:
  print(-1)
else:
  print("\n".join(map(str,ans[::-1])))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import sys
import heapq
import re
from itertools import permutations
from bisect import bisect_left, bisect_right
from collections import Counter, deque
from math import factorial, sqrt, ceil, gcd
from functools import lru_cache, reduce
from decimal import Decimal
from operator import mul

INF = 1 << 60
MOD = 1000000007
sys.setrecursionlimit(10 ** 7)


# UnionFind
class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x

    def size(self, x):
        return -self.parents[self.find(x)]

    def same(self, x, y):
        return self.find(x) == self.find(y)

    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

    def group_count(self):
        return len(self.roots())

    def all_group_members(self):
        return {r: self.members(r) for r in self.roots()}

    def __str__(self):
        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())

# ダイクストラ
def dijkstra_heap(s, edge, n):
    #始点sから各頂点への最短距離
    d = [10**20] * n
    used = [True] * n #True:未確定
    d[s] = 0
    used[s] = False
    edgelist = []
    for a,b in edge[s]:
        heapq.heappush(edgelist,a*(10**6)+b)
    while len(edgelist):
        minedge = heapq.heappop(edgelist)
        #まだ使われてない頂点の中から最小の距離のものを探す
        if not used[minedge%(10**6)]:
            continue
        v = minedge%(10**6)
        d[v] = minedge//(10**6)
        used[v] = False
        for e in edge[v]:
            if used[e[1]]:
                heapq.heappush(edgelist,(e[0]+d[v])*(10**6)+e[1])
    return d

# 素因数分解
def factorization(n):
    arr = []
    temp = n
    for i in range(2, int(-(-n**0.5//1))+1):
        if temp%i==0:
            cnt=0
            while temp%i==0:
                cnt+=1
                temp //= i
            arr.append([i, cnt])

    if temp!=1:
        arr.append([temp, 1])

    if arr==[]:
        arr.append([n, 1])

    return arr

# 組合せnCr
def combinations_count(n, r):
    if n < r:
        return 0
    r = min(r, n - r)
    numer = reduce(mul, range(n, n - r, -1), 1)
    denom = reduce(mul, range(1, r + 1), 1)
    return numer // denom

# 2数の最小公倍数
def lcm(x, y):
    return (x * y) // gcd(x, y)

# リストの要素の最小公倍数
def lcm_list(numbers):
    return reduce(lcm, numbers, 1)

# リストの要素の最大公約数
def gcd_list(numbers):
    return reduce(gcd, numbers)

# 素数判定
def is_prime(n):
    if n <= 1:
        return False
    p = 2
    while True:
        if p ** 2 > n:
            break
        if n % p == 0:
            return False
        p += 1
    return True


# limit以下の素数を列挙
def eratosthenes(limit):
    A = [i for i in range(2, limit+1)]
    P = []

    while True:
        prime = min(A)
        
        if prime > sqrt(limit):
            break
            
        P.append(prime)
            
        i = 0
        while i < len(A):
            if A[i] % prime == 0:
                A.pop(i)
                continue
            i += 1
            
    for a in A:
        P.append(a)
            
    return P

# 同じものを含む順列
def permutation_with_duplicates(L):

    if L == []:
        return [[]]

    else:
        ret = []

        # set（集合）型で重複を削除、ソート
        S = sorted(set(L))

        for i in S:

            data = L[:]
            data.remove(i)

            for j in permutation_with_duplicates(data):
                ret.append([i] + j)

        return ret

def make_divisors(n):
    lower_divisors , upper_divisors = [], []
    i = 1
    while i*i <= n:
        if n % i == 0:
            lower_divisors.append(i)
            if i != n // i:
                upper_divisors.append(n//i)
        i += 1
    return lower_divisors + upper_divisors[::-1]

# ここから書き始める
n = int(input())
b = sorted(list(map(int, input().split())))
for i in range(n):
    if b[i] > i + 1:
        print(-1)
        break
else:
    for i in b:
        print(i)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n=int(input())
L=list(map(int,input().split()))
ans=[]
def mal(x):
    if len(x)==0:
        return []
    else:
        t=max(x)
        i=x.index(t)
        if i<t-1:
            print(-1)
            exit()
        after=x[:i-t+1]
        before=x[i-t+1:]
        before.remove(t)
        return mal(before)+[t]+mal(after)
ans=mal(L)

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

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
from collections import *

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

for i in range(N):
    if b[i]==1:
        l.append(now)
        now = [1]
    else:
        now.append(b[i])

l.append(now)

for li in l[::-1]:
    for i in range(len(li)):
        if li[i]>i+1:
            print(-1)
            exit()
    
    li.sort()
    
    for lii in li:
        print(lii)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
from collections import *

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

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

l = []
now = []

for i in range(N):
    if b[i]==1:
        l.append(now)
        now = [1]
    else:
        now.append(b[i])

l.append(now)

for li in l[::-1]:
    li.sort()
    
    for lii in li:
        print(lii)