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: Time Limit Exceeded

Solution code:
n = int(input())
b = list(map(str, input().split()))
''.join(b)
ans = []
def dfs(b):
    if len(b) == 1:
        if int(b[0]) == 1:
            return True
    else:
        f = 0
        for j in range(len(b)):
            if int(b[j]) != j + 1:
                f += 1
                break
        if f == len(b):
            for j in range(len(b)):
                ans.append(b[j])
            return True

        for i in range(len(b) - 1, -1, -1):
            if int(b[i]) == i + 1:
                if i == 0:
                    if dfs(b[1:]):
                        ans.append(i + 1)
                        return True
                else:
                    if dfs(b[:i] + b[i+1:]):
                        ans.append(i + 1)
                        return True
        return False
if dfs(b):
    ans.insert(0, 1)
    for i in range(len(ans)):
        print(ans[i])
else:
    print(-1)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
b = list(map(int,input().split()))
c = []
al =False
def isfalse(b):
    for i in range(len(b)):
        if b[i]>i+1:
            return True
    return False

if isfalse(b)  :
    al = True
    print(-1)
def insertA(m,b,c):
    if m == 1:
        if b[0]==1:
            c.append(1)
            return True
    if max(b) > m//2:
        k = range(m)[::-1]
    else:
        k = range(m)
    for i in k:
        if b[i] == i+1:
            bi = b[:i]+b[i+1:]
            if insertA(m-1,bi,c):
                c.append(i+1)
                return True
                break
            else:
                continue
    
    
insertA(n,b,c)
for i in c:
    print(i)
if al == False and len(c)<n:
    print(-1)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
BBB=[int(x) for x in input().split()]
 
BB=[0 for i in range(N)]
def re(B,n):
  global BB
  if n==1:
    if B[0]!=1:
      return -1
    BB[0]=B[0]
    for i in BB:
      print(i)
    exit()
  for i in range(n):
    if B[i]>i+1:
      return 1
    if B[i]==i+1:
      BB[n-1]=B[i]
      re(B[:i]+B[i+1:n],n-1)
  return -1
 
result=re(BBB,N)
print(-1)

Submission status: Time Limit Exceeded

Solution code:
import sys
stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
nn = lambda: list(stdin.readline().split())
ns = lambda: stdin.readline().rstrip()

n = ni()
b = na()

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



b.reverse()

def sr(l,m):
  for u in range(len(l)):
    if l[u] == m:
      return u
  return -1

ans = []
p = 1
k = 0
while(b):
  x = sr(b,p)
  y = sr(b,p+1)
  if p == 1:
    k = x+1
  if x == -1:
    p -= 1
  else:
    ans.append(b.pop(x))
    k -= 1
    if k == 0 and len(b) > 1:
      p = 1
      continue
    if x > y:
      p += 1

for a in ans:
  print(a)

Submission status: Time Limit Exceeded

Solution code:
def checkJ(lst):
    good_jlist = []
    for i in range(len(lst)):
        if lst[len(lst) - i-1] == len(lst) - i:
            good_jlist.append(len(lst) -  i -1)
    return good_jlist
  
def subJ(lst, j):
    return lst[:j] + lst[j+1:]
  
n = int(input())
s = [int(i) for i in input().split()]
stack_list = []
stack_list.append([s,checkJ(s)])
notfound = 0
applylist= []
while True:
#     調べる番号がない場合
    if not stack_list[0][0]:
        notfound = 2
        break
    while not stack_list[0][1]:
        stack_list.pop(0)
        if not stack_list:
            notfound = 1
            break
        if not stack_list[0][0]:
            notfound = 2
            break
        
#     調べるリストがない場合
    if notfound == 1 or notfound == 2:
        break
    
    currentStr = stack_list[0][0]
    applylist = applylist[:int(int(n) - len(currentStr))]
    currentCheckNum = stack_list[0][1].pop(0)
    nextStr = subJ(currentStr,currentCheckNum)
    if checkJ(s):
        stack_list.insert(0,[nextStr,checkJ(nextStr)])
    applylist.append(currentCheckNum + 1)
#     print("current list")
#     print(stack_list)
if notfound == 1:
    print("-1")
elif notfound == 2:
    for i in range(len(applylist)):
        print(applylist[int(n)-i-1])

Submission status: Time Limit Exceeded

Solution code:
import itertools
n = int(input())
b = list(map(int, input().split()))
product_n = list(itertools.product([x for x in range(1, n+1)], repeat=n))
product_list = [list(x) for x in product_n]
possible_list = []
for x in product_list:
    flag = True
    for i in range(n):
        if not x[i] < i+2:
            flag = False
        if flag:
            possible_list.append(list(x))

if b in possible_list:
    b.sort()
    for x in b:
            print(x)
else:
    print(-1)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
b = list(map(str, input().split()))
''.join(b)
ans = []
def dfs(b):
    if len(b) == 1:
        if int(b[0]) == 1:
            return True
    else:
        for i in range(len(b) - 1, -1, -1):
            if int(b[i]) == i + 1:
                if i == 0:
                    if dfs(b[1:]):
                        ans.append(i + 1)
                        return True
                else:
                    if dfs(b[:i] + b[i+1:]):
                        ans.append(i + 1)
                        return True
        return False
if dfs(b):
    ans.insert(0, 1)
    for i in range(len(ans)):
        print(ans[i])
else:
    print(-1)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
b = list(map(int,input().split()))
c = []
al =False
if max(b)>n or sum(b)>(n+1)*n//2 or min(b)>1:
    al = True
    print(-1)
def insertA(m,b,c):
    if m == 1:
        if b[0]==1:
            c.append(1)
            return True
    if max(b) > m//2:
        k = range(m)[::-1]
    else:
        k = range(m)
    for i in k:
        if b[i] == i+1:
            bi = b[:i]+b[i+1:]
            if insertA(m-1,bi,c):
                c.append(i+1)
                return True
                break
            else:
                continue
    
    
insertA(n,b,c)
for i in c:
    print(i)
if al == False and len(c)<n:
    print(-1)

Submission status: Time Limit Exceeded

Solution code:
import sys
import time
import copy

N = int(input())
b = [int(b) for b in input().split()]
h = {}

t1 = time.time()

def f(b):
  t2 = time.time()
  if t2 - t1 > 1.9:
    print(-1)
    sys.exit()
  
  key = "".join([str(x) for x in b])
  if key in h:
    return copy.copy(h[key])
  
  if len(b) == 1:
    h[key] = b if b[0] == 1 else []
    return copy.copy(h[key])
  
  for i in range(len(b)):
    i = len(b) - 1 - i
    if b[i] == i + 1:
      b_sub = copy.copy(b)
      b_i = b_sub.pop(i)
      a = f(b_sub)
      if len(a) != 0:
        a.append(b_i)
        h[key] = a
        return copy.copy(h[key])
  
  h[key] = []
  return copy.copy(h[key])

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

Submission status: Time Limit Exceeded

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]


b_searched = set()
b_unsearched = []


b_searched.add(tuple(b))
delit = delitable(b)

if delit == []:
    print("-1")
for delit_num in delit:
    b_unsearched.append([b[:delit_num - 1] + b[delit_num:], [delit_num]])


while b_unsearched:
    b, log = b_unsearched.pop()

    b_tuple = tuple(b)
    b_searched.add(b_tuple)
    delit = delitable(b)
    if not delit:
        if not b:
            log.reverse()
            print("\n".join(map(str, log)))
            break
        else:
            continue
    for delit_num in delit:
        b_temp = b[:delit_num - 1] + b[delit_num:]
        if not tuple(b_temp) in b_searched:
            b_unsearched.append([b_temp, log + [delit_num]])
else:
    print("-1")

Submission status: Time Limit Exceeded

Solution code:
def check(b, n):
    if (1 == n):
        if (1 == b[0]):
            return [1]
        else:
            return []
    else:
        for (i,b_) in enumerate(b):
            if ((i+1) == b_):
                tmp = list(b)
                del tmp[i]
                res = check(tmp, n-1)
                #print(res, n-1)
                if (0 < len(res)):
                    #print(res, b_)
                    res.append(b_)
                    return res
        return []

def main():
    N = int(input())
    B = list(map(int, input().split()))
    
    res = check(B, N)
    if (0 < len(res)):
        print("\n".join([str(i) for i in res]))
    else:
        print(-1)

main()

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
b = list(map(int,input().split()))
c = []
al =False
if max(b)>n or sum(b)>(n+1)*n//2 or min(b)>1:
    al = True
    print(-1)
def insertA(m,b,c):
    if m == 1:
        if b[0]==1:
            c.append(1)
            return True
    if max(b) > m//2:
        k = range(m)
    else:
        k = range(m)[::-1]
    for i in k:
        if b[i] == i+1:
            bi = b[:i]+b[i+1:]
            if insertA(m-1,bi,c):
                c.append(i+1)
                return True
                break
            else:
                continue
    
    
insertA(n,b,c)
for i in c:
    print(i)
if al == False and len(c)<n:
    print(-1)

Submission status: Time Limit Exceeded

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]


b_searched = set()
b_unsearched = []


b_searched.add(tuple(b))
delit = delitable(b)

if delit == []:
    print("-1")
for delit_num in delit:
    b_unsearched.append([[v for i, v in enumerate(b) if delit_num != i + 1], [delit_num]])

b_unsearched.reverse()

while b_unsearched:
    b, log = b_unsearched.pop()

    b_tuple = tuple(b)
    b_searched.add(b_tuple)
    delit = delitable(b)
    if not delit:
        if not b:
            log.reverse()
            print("\n".join(map(str, log)))
            break
        else:
            continue
    for delit_num in delit:
        b_temp = [v for i, v in enumerate(b) if delit_num != i + 1]
        if not tuple(b_temp) in b_searched:
            b_unsearched.append([b_temp, log + [delit_num]])
else:
    print("-1")

Submission status: Time Limit Exceeded

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

def dfs(stack, B):
    if len(B)==1:
        if B[0]==1:
            stack.append(1)
            return stack
        else: return None
    else:
        tmp = []
        flag = False
        for i, b in enumerate(B):
            stack_ = stack + [b]
            B_ = B[:i] + B[i+1:]
            if b==i+1: tmp.append(dfs(stack_, B_))
        for tp in tmp:
            if tp is not None:
                flag = True
                ans = tp
        if flag:
            return ans
        else:
            return None
ans = dfs([], B)
if ans is None:
    print('-1')
else:
    ans = ans[::-1]
    for i in range(N):
        print(ans[i])

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(100000)

n = int(input())
b = list(map(str, input().split()))
''.join(b)
ans = []
def dfs(b):
    if len(b) == 1:
        if int(b[0]) == 1:
            return True
    else:
        for i in range(len(b)):
            if int(b[i]) == i + 1:
                if i == 0:
                    if dfs(b[1:]):
                        ans.append(i + 1)
                        return True
                else:
                    if dfs(b[:i] + b[i+1:]):
                        ans.append(i + 1)
                        return True
        return False
if dfs(b):
    ans.insert(0, 1)
    for i in range(len(ans)):
        print(ans[i])
else:
    print(-1)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
def last_insert(_b):
    cand = []
    for i, v in enumerate(_b):
        if (i + 1) == v:
            cand.append(i)

    return cand


def limited_insertion(_b):
    if len(_b) == 1 and _b[0] == 1:
        return [1]

    cand = last_insert(_b)
    if len(cand) == 0:
        return -1

    for c in cand:
        __b = [x for i, x in enumerate(_b) if i != c]
        a = limited_insertion(__b)
        if a == -1:
            continue
        else:
            a.append(c+1)
            return a

    return -1


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

a = limited_insertion(b)
if a == -1:
    print(-1)
else:
    for i in a:
        print(i)

Submission status: Time Limit Exceeded

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

global res
global res_print
global flg
res=[0]*len(A)
res_print=[0]*len(A)

def test(X):
    if len(X)==1:
        if X[0]==1:
            res[0]=1
            #res_printに答えを入れる
            #res_print=res
            for s in range(len(res)):
                res_print[s]=res[s]
            return 1
        else:
            return 0
    else:
        flg=[0]*len(X)
        for i in range(len(X)):
            if X[i]==i+1:
                res[len(X)-1]=i+1
                X1=X[:i]
                X1.extend(X[i+1:])
                flg[i]=test(X1)
        return max(flg)

if test(A)== 0:
  print(-1)
else:
  for s in range(len(res)):
    print(res_print[s])

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
b = list(map(int,input().split()))
c = []
al =False
if max(b)>n or sum(b)>(n+1)*n//2 or min(b)>1:
    al = True
    print(-1)
def insertA(m,b,c):
    if m == 1:
        if b[0]==1:
            c.append(1)
            return True
    for i in range(m)[::-1]:
        if b[i] == i+1:
            bi = b[:i]+b[i+1:]
            if insertA(m-1,bi,c):
                c.append(i+1)
                return True
                break
            else:
                continue
    
    
insertA(n,b,c)
for i in c:
    print(i)
if al == False and len(c)<n:
    print(-1)

Submission status: Time Limit Exceeded

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

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

from queue import Queue
q = Queue()
ans = Queue()

import numpy as np
n = np.array(n)


def bfs(b):
    q.put(b)
    ans.put([])

    while q.empty() == False:
        p = q.get()
        a = ans.get()

        if p == []:
            break

        if p[0] != 1:
            continue

        m = len(p)

        for i in range(m-1, -1, -1):
            if p[i] == i+1:
                q.put(p[:i]+p[i+1:])
                ans.put(a+[i+1])


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


bfs(b)

Submission status: Time Limit Exceeded

Solution code:
import sys
import copy

N = int(input())
b = [int(b) for b in input().split()]
h = {}

def f(b):
  key = "".join([str(x) for x in b])
  if key in h:
    return copy.copy(h[key])
  
  if len(b) == 1:
    h[key] = b if b[0] == 1 else []
    return copy.copy(h[key])
  
  for i in range(len(b)):
    i = len(b) - 1 - i
    if b[i] == i + 1:
      b_sub = copy.copy(b)
      b_i = b_sub.pop(i)
      a = f(b_sub)
      if len(a) != 0:
        a.append(b_i)
        h[key] = a
        return copy.copy(h[key])
  
  h[key] = []
  return copy.copy(h[key])

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

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
def main():
    n = int(input())
    b = list(map(int, input().split()))
    s = set()
    q = deque()

    q.append([tuple(b), [0]*n, 0])
    ans = None
    while q:
        now, history, depth = q.popleft()
        if now in s:
            continue
        s.add(now)
        if len(now) == 0:
            ans = history
            break
        for i, j in enumerate(now):
            if i+1 == j:
                tmp = history[::]
                tmp[depth] = i+1
                q.appendleft([tuple(now[:i] + now[i+1:]), tmp, depth+1])
    if ans is None:
        print(-1)
    else:
        for i in ans[::-1]:
            if i == 0:
                break
            print(i)
if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
import itertools

n = input()
num_list = [int(i) for i in input().split()] 

is_possible = False
permutations = list(set(itertools.permutations(num_list)))

for order_list in permutations:
  output_list = []
  
  if order_list[0] != 1:
    continue
  
  for x in order_list:
    output_list.insert(x - 1, x)
  
  if output_list == num_list:
    is_possible = True
    break
    
if is_possible:
  for out in order_list:
      print(out)
else:
  print(-1)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
b = list(map(str, input().split()))
''.join(b)
ans = []
def dfs(b):
    if len(b) == 1:
        if int(b[0]) == 1:
            return True
    else:
        for i in range(len(b)):
            if int(b[i]) == i + 1:
                if i == 0:
                    if dfs(b[1:]):
                        ans.append(i + 1)
                        return True
                else:
                    if dfs(b[:i] + b[i+1:]):
                        ans.append(i + 1)
                        return True
        return False
if dfs(b):
    ans.insert(0, 1)
    for i in range(len(ans)):
        print(ans[i])
else:
    print(-1)

Submission status: Time Limit Exceeded

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]


b_searched = set()
b_unsearched = []


b_searched.add(tuple(b))
delit = delitable(b)

if delit == []:
    print("-1")
for delit_num in delit:
    b_unsearched.append([[v for i, v in enumerate(b) if delit_num != i + 1], [str(delit_num)]])

while b_unsearched:
    b, log = b_unsearched.pop()

    b_tuple = tuple(b)
    b_searched.add(b_tuple)
    delit = delitable(b)
    if not delit:
        if not b:
            log.reverse()
            print("\n".join(log))
            break
        else:
            continue
    for delit_num in delit:
        b_temp = [v for i, v in enumerate(b) if delit_num != i + 1]
        if not tuple(b_temp) in b_searched:
            b_unsearched.append([b_temp, log + [str(delit_num)]])
else:
    print("-1")

Submission status: Time Limit Exceeded

Solution code:
import itertools

n = input()
num_list = [int(i) for i in input().split()] 

is_possible = False

for order_list in list(itertools.permutations(num_list)):
  output_list = []
  
  if order_list[0] != 1:
    continue
  
  for x in order_list:
    output_list.insert(x - 1, x)
  
  if output_list == num_list:
    is_possible = True
    break
    
if is_possible:
  for out in order_list:
      print(out)
else:
  print(-1)

Submission status: Time Limit Exceeded

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

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

mem = {}
def rec(arr):
    if len(arr)==1:
        if arr[0] == 1:
            return [1]
        else:
            return False
    h = hash(tuple(arr))
    if h in mem:
        return mem[h]
    for i,a in reversed(list(enumerate(arr))):
        if i+1 == a:
            res = rec(arr[:i] + arr[i+1:])
            if res:
                res.append(i+1)
                mem[h] = res
                break
    else:
        mem[h] = False
    return mem[h]

ans = rec(B)
if ans:
    print(*ans,sep='\n')
else:
    print(-1)

Submission status: Time Limit Exceeded

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

def last_insert(_b):
    cand = []
    for i, v in enumerate(_b):
        if (i + 1) == v:
            cand.append(i)

    return cand


def limited_insertion(_b):
    if len(_b) == 1 and _b[0] == 1:
        return [1]

    cand = last_insert(_b)
    if len(cand) == 0:
        return -1

    for c in cand:
        __b = copy.deepcopy(_b)
        __b.pop(c)
        a = limited_insertion(__b)
        if a == -1:
            continue
        else:
            a.append(c+1)
            return a

    return -1


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

a = limited_insertion(b)
if a == -1:
    print(-1)
else:
    for i in a:
        print(i)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
list_b = list(map(int, input().split()))
aaa = [[1, 1], [1, 2]]
ans = [[1, 1], [1, 2]]
for i in range(2,N):
  j = []
  f = []
  for d in range(len(aaa)):
    s = []
    t = []
    for k in range(i+1):
      a = aaa[d].copy()
      b = ans[d].copy()
      a.insert(k, k+1)
      b.append(k+1)
      s.append(a)
      t.append(b)
    j += s
    f += t
  aaa = j
  ans = f

if list_b in aaa:
  index = aaa.index(list_b)
  anser = ans[index]
  for i in range(N):
    print(anser[i])
else:
  print('-1')

Submission status: Time Limit Exceeded

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

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

mem = {}
def rec(arr):
    if len(arr)==1:
        if arr[0] == 1:
            return [1]
        else:
            return False
    h = hash(tuple(arr))
    if h in mem:
        return mem[h]
    for i,a in reversed(list(enumerate(arr))):
        if i+1 == a:
            _arr = arr[:]
            del _arr[i]
            res = rec(_arr)
            if res:
                res.append(i+1)
                mem[h] = res
                break
    else:
        mem[h] = False
    return mem[h]

ans = rec(B)
if ans:
    print(*ans,sep='\n')
else:
    print(-1)

Submission status: Time Limit Exceeded

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


for i, b in enumerate(B, start=1):
  if b > i:
    print(-1)
    exit()


bnn = collections.defaultdict(
  lambda: collections.defaultdict(dict)
)
def inv_sousa(B, N, now=tuple()):
  if N == 0:
    yield now
  else:
    if now in bnn[B][N] or max(B) > N:
      return
    bnn[B][N][now] = False

    for i in set(B):
      idx = i - 1
      if B[idx] == i:
        yield from inv_sousa(B[:idx] + B[i:], N - 1, now=now + (B[idx],))


for x in inv_sousa(tuple(B), N):
  if x:
    for i in x[::-1]:
      print(i)
    break
else:
  print(-1)

Submission status: Time Limit Exceeded

Solution code:

def chousa(b):
    poped = ["x"]*len(b)
    a = []
    isnt_sorted = len(b)
    while isnt_sorted:
        for i,v in enumerate(b[::-1]):
            if v == 1 and poped[len(b)-i-1] == "x":
                a.insert(v-1, [len(b)-i,v])
                poped[len(b)-i-1] = "o"
                isnt_sorted -= 1
                break
            elif len(a) >= v-1 and a[v-2][0] < len(b)-i and poped[len(b)-i-1] == "x":
                a.insert(v-1, [len(b)-i,v])
                poped[len(b)-i-1] = "o"
                isnt_sorted -= 1
                break

        print(v)
def main():
    N = int(input())
    b = list(map(int,input().split()))
    chousa(b)
if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
import copy


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


def insertion(n, x):
    if not x:
        return True
    for i in range(n):
        y = copy.copy(x)
        if y[i] == i + 1:
            y.pop(i)
            ope.insert(0, i + 1)
            if insertion(n - 1, y):
                return True
            else:
                ope.pop(0)
    return False



if insertion(n, b):
    for i in ope:
        print(i)
else:
    print(-1)

Submission status: Time Limit Exceeded

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

ans = []
def solve(b,i):
    if i == 0:
        return True
    for j in range(i-1,-1,-1):
        if b[j] == j+1:
            p = b[:j] + b[j+1:]
            if solve(p,i-1):
                ans.append(j+1)
                return True


    return False

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

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
list_b = list(map(int, input().split()))
aaa = []
ans = []
for i in range(N):
  if i == 0:
    aaa.append(1)
    ans.append(1)
  elif i == 1:
    s = []
    f = []
    for h in range(i+1):
      a = aaa.copy()
      b = ans.copy()
      a.insert(h, h+1)
      b.append(h+1)
      s.append(a)
      f.append(b)
    aaa = s
    ans = f
  else:
    j = []
    f = []
    for d in range(len(aaa)):
      s = []
      t = []
      for k in range(i+1):
        a = aaa[d].copy()
        b = ans[d].copy()
        a.insert(k, k+1)
        b.append(k+1)
        s.append(a)
        t.append(b)
      j += s
      f += t
    aaa = j
    ans = f

if list_b in aaa:
  index = aaa.index(list_b)
  anser = ans[index]
  for i in range(N):
    print(anser[i])
else:
  print('-1')

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
b = list(map(int,input().split()))
c = []
al =False
if max(b)>n or sum(b)>(n+1)*n//2 or min(b)>1:
    al = True
    print(-1)
def insertA(m,b,c):
    if m == 1:
        if b[0]==1:
            c.append(1)
            return True
    for i in range(m)[::-1]:
        if b[i] == i+1:
            bi = b[:i]+b[i+1:]
            if insertA(m-1,bi,c):
                c.append(i+1)
                return True
                break
    
    
insertA(n,b,c)
for i in c:
    print(i)
if al == False and len(c)<n:
    print(-1)

Submission status: Time Limit Exceeded

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]


# b_searched = set()
b_unsearched = []


# b_searched.add(b)
delit = delitable(b)

if delit == []:
    print("-1")
for delit_num in delit:
    b_unsearched.append([[v for i, v in enumerate(b) if delit_num != i + 1], [str(delit_num)]])

while b_unsearched:
    b, log = b_unsearched.pop()

    # b_searched.add(b)
    delit = delitable(b)
    if not delit:
        if not b:
            log.reverse()
            print("\n".join(log))
            break
        else:
            continue
    for delit_num in delit:
        b_unsearched.append([[v for i, v in enumerate(b) if delit_num != i + 1], log + [str(delit_num)]])
else:
    print("-1")

Submission status: Time Limit Exceeded

Solution code:

import sys
input = sys.stdin.readline

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

from queue import Queue
q = Queue()
ans = Queue()

import numpy as np
n = np.array(n)


def bfs(b):
    q.put(b)
    ans.put([])

    while q.empty() == False:
        p = q.get()
        a = ans.get()

        if p == []:
            break

        if p[0] != 1:
            continue

        for i, pp in enumerate(p):
            if pp == i+1:
                q.put(p[:i]+p[i+1:])
                ans.put(a+[i+1])


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


bfs(b)

Submission status: Time Limit Exceeded

Solution code:
def checkJ(lst):
    good_jlist = []
    for i in range(len(lst)):
        if lst[i] == i+1:
            good_jlist.append(i)
    return good_jlist

def subJ(lst, j):
    return lst[:j] + lst[j+1:]

n = int(input())
s = [int(i) for i in input().split()]
stack_list = []
stack_list.append([s,checkJ(s)])
notfound = 0
applylist= []
while True:
#     調べる番号がない場合
    if not stack_list[0][0]:
        notfound = 2
        break
    while not stack_list[0][1]:
        stack_list.pop(0)
        if not stack_list:
            notfound = 1
            break
        if not stack_list[0][0]:
            notfound = 2
            break
        
#     調べるリストがない場合
    if notfound == 1 or notfound == 2:
        break
    
    currentStr = stack_list[0][0]
    applylist = applylist[:int(int(n) - len(currentStr))]
    currentCheckNum = stack_list[0][1].pop(0)
    nextStr = subJ(currentStr,currentCheckNum)
    if checkJ(s):
        stack_list.insert(0,[nextStr,checkJ(nextStr)])
    applylist.append(currentCheckNum + 1)
#     print("current list")
#     print(stack_list)
if notfound == 1:
    print("-1")
elif notfound == 2:
    for i in range(len(applylist)):
        print(applylist[int(n)-i-1])

Submission status: Time Limit Exceeded

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

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

mem = {}
def rec(arr):
    if len(arr)==1:
        if arr[0] == 1:
            return [1]
        else:
            return False
    h = hash(tuple(arr))
    if h in mem:
        return mem[h]
    for i,a in enumerate(arr):
        if i+1 == a:
            _arr = arr[:]
            del _arr[i]
            res = rec(_arr)
            if res:
                res.append(i+1)
                mem[h] = res
                break
    else:
        mem[h] = False
    return mem[h]

ans = rec(B)
if ans:
    print(*ans,sep='\n')
else:
    print(-1)

Submission status: Time Limit Exceeded

Solution code:
import itertools
from functools import lru_cache

N = int(input())
b = [int(_b) for _b in input().strip().split(' ')]

#print(b)

@lru_cache(maxsize=1000)
def makeList(l):
  #result = [1]
  result = []
  for i in l:
    result.insert(i-1, i)
  return result

def isSameList(l1,l2):
  if len(l1) != len(l2):
    return False
  for a,b in zip(l1,l2):
    if a != b:
      return False
  return True

def isValid(l):
  for i in range(len(l)):
    if not l[i] <= i+1:
      return False
  return True

if not isValid(b):
  print(-1)
  exit()

#for l in itertools.permutations(b[1:]):
for l in itertools.permutations(b):
  if not isValid(l):
    continue
  result = makeList(l)
  #print(result)
  if isSameList(result, b):
    #print(1)
    for i in l:
      print(i)
    exit()
print(-1)

Submission status: Time Limit Exceeded

Solution code:
def BFS(Bs,del_idx,result):
    if Bs==[1]:
        return result
    
    if del_idx!=None:
        del Bs[del_idx]

    for i,b in enumerate(Bs):
        if i+1==b:
            DBs=Bs.copy()
            result.append(i+1)
            out=BFS(DBs,i,result)
            if out:
                return out
            else:
                del result[-1]

    return None


def Solve():
    output=[]
    N=int(input())
    Bs=[int(x) for x in input().split()]
    return BFS(Bs,None,[])
        

        
result=Solve()
if result:
    result.reverse()
    for r in result:
        print(r)
else:
    print(-1)