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())
bb = list(map(int, input().split()))

def shorten(l, count, opr):
    next_opr = []
    if count==n:
        return opr
    for x in set(l):
        if len(l)>=x:
            if l[x-1] == x:
                l_ = l[:]
                l_.pop(x-1)
                # print(x, l_, count+1, opr+[x])
                next_opr = shorten(l_, count+1, opr+[x])
    return next_opr
ans = shorten(bb,0,[])[::-1]
if ans == []:
    print(-1)
else:
    for x in ans:
        print(x)

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()

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:

import sys
input = sys.stdin.readline

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

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

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:
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
  elif now not in bnn[B][N]:
    for i in range(1, N + 1):
      idx = i - 1
      if B[idx] <= i:
        yield from inv_sousa(B[:idx] + B[i:], N - 1, now=now + (B[idx],))
    bnn[B][N][now] = False


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:
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):
        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:
import copy

def xxx(b):
    #print(b)
    for i in range(len(b)):
        #print(b[i], i+1)
        
        if b[i] > i+1:
            #print("aa")
            return -1

        if b[i] == i+1:
            if len(b) == 1:
                print(i+1)
                return 0
            
            next_b = copy.deepcopy(b)
            next_b.pop(i)
            r = xxx(next_b)
            if r == 0:
                print(i+1)
                return 0
            
    return -1
        

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

r = xxx(b)
if r == -1:
    print(-1)

Submission status: Time Limit Exceeded

Solution code:
def f(N, a):
  if N == 1:
    if a[0] == 1:
      return [1]
    else:
      return -1
  count = 0
  for j in range(1, N, 1):
    if j == a[j-1]: # 全て
      count += 1
      ans = f(N-1, a[:j-1]+a[j:])
      if ans != -1:
        ans.append(j)
        return ans
  #--j == Nは別で
  j = N
  if j == a[j-1]: # 全て
      count += 1
      ans = f(N-1, a[:j-1])
      if ans != -1:
        ans.append(j)
        return ans
  return -1
  
if __name__ == "__main__":
  N = int(input())
  a = list(int(i) for i in input().split()) 
  ans = f(N, a)
  if ans == -1:
    print(-1)
  else:
    for i in ans:
      print(i)

Submission status: Time Limit Exceeded

Solution code:
import sys
import copy

N = int(input())
b = [int(b) for b in input().split()]
a = []

def f(b):
  if len(b) == 1:
    return(b if b[0] == 1 else [])
  for i in range(len(b)):
    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)
        return(a)
  return([])

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:
import sys
input = sys.stdin.readline

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

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

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

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

        if p == []:
            break

        m = len(p)

        for i in range(m):
            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:
from collections import deque
def main():
    n = int(input())
    b = list(map(int, input().split()))
    q = deque()

    q.append([b[::], [0]*n, 0])
    ans = None
    while q:
        now, history, depth = q.popleft()
        if len(now) == 0:
            ans = history
            break
        for i in range(len(now))[::-1]:
            if i+1 == now[i]:
                tmp = history[::]
                tmp[depth] = i+1
                q.appendleft([[now[k] for k in range(len(now)) if k != i], 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 copy

def xxx(b):
    #print(b)
    for i in range(len(b)):
            
        
        if b[i] == i+1:
            if len(b) == 1:
                print(i+1)
                return 0
            
            next_b = copy.deepcopy(b)
            next_b.pop(i)
            r = xxx(next_b)
            if r == 0:
                print(i+1)
                return 0
            
    return -1
        

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

r = xxx(b)
if r == -1:
    print(-1)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
b = list(map(int,input().split()))
c = []
if max(b)>n or sum(b)>(n+1)*n//2 or min(b)>1:
    print(-1)
def insertA(m,b,c):
    if m == 1:
        if b[0]==1:
            c.append(1)
            return True
    for i in range(m):
        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)

Submission status: Time Limit Exceeded

Solution code:
"""
A - Limited Insertion
"""


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

    if ans == -1:
        print(ans)
    else:
        for a in list(reversed(ans)):
            print(a)
        # ans = list(reversed(ans))
        # ans = [str(i) for i in ans]
        # print(' '.join(ans))


def make_list(b: list, x: int, ans: list) -> list:
    re = -1
    # print('try {}'.format(x))
    # print(b)

    if len(b) == 0:
        return ans

    for i, element in enumerate(b, 1):
        if i == element:
            b_c = b.copy()
            ans_c = ans.copy()
            ans_c.append(b_c[i - 1])
            b_c.remove(element)  # 除去対象は自分より前に同じ要素が存在しない
            # print('match element {}'.format(element))
            # print(ans)
            tmp = make_list(b_c, x + 1, ans_c)
            if tmp != -1:
                re = tmp
                break
    # print('return {} is '.format(x), re)
    return re


if __name__ == '__main__':
    solve()

Submission status: Time Limit Exceeded

Solution code:
def remove(data):
    global result
    pos = min(len(data), max(data))
    for n in range(pos-1, -1, -1):
        if data[n] == n + 1:
            result.append(n + 1)
            if len(data) == 1:
                #print('OK')
                return True
            else:
                #print('remove {0} '.format(n + 1), end='')
                #print(data[:n] + data[n+1:])
                if remove(data[:n] + data[n+1:]):
                    return True
    else:
        #print('try again')
        if len(result) > 0:
            result.pop(-1)
        return False

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

result = []

if remove(B):
    result.reverse()
    for res in result:
        print(res)
else:
    print(-1)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
"""
A - Limited Insertion
"""


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

    if ans == -1:
        print(ans)
    else:
        ans = list(reversed(ans))
        ans = [str(i) for i in ans]
        print(' '.join(ans))


def make_list(b: list, x: int, ans: list) -> list:
    re = -1
    # print('try {}'.format(x))
    # print(b)

    if len(b) == 0:
        return ans

    for i, element in enumerate(b, 1):
        if i == element:
            b_c = b.copy()
            ans_c = ans.copy()
            ans_c.append(b_c[i - 1])
            b_c.remove(element)  # 除去対象は自分より前に同じ要素が存在しない
            # print('match element {}'.format(element))
            # print(ans)
            tmp = make_list(b_c, x + 1, ans_c)
            if tmp != -1:
                re = tmp
                break
    # print('return {} is '.format(x), re)
    return re


if __name__ == '__main__':
    solve()

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8

input()

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

def f (B, ans):
  if len(B) == 0:
    return ans
  for i in range(len(B)):
    if B[i] <= len(B) and B[i] == i+1 :
      B2 = B[:]
      ans2 = ans[:]
      ans2.append(B2.pop(B[i]-1))
      result = f(B2, ans2)
      if result is not None: 
        return result

result = f(B_, [])
if result is None:
  result = [-1]
for s in result[::-1]:
  print(s)

Submission status: Time Limit Exceeded

Solution code:
import copy

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

memo = {}

def dfs(b):
    if len(b) == 0:
        return []

    if tuple(b) in memo:
        return memo[b]

    for i, c in enumerate(b):
        if c>len(b):
            return None

    ans = None
    for i, c in enumerate(b):
        if c == i+1:
            r = dfs(b[:i]+b[i+1:])
            if r is not None:
                ans = [i]+r
                memo[tuple(b)] = ans
                break
    return ans

ans = dfs(b)
if ans is None:
    print(-1)
else:
    ans.reverse()
    for a in ans:
        print(a+1)

Submission status: Time Limit Exceeded

Solution code:
def remove(data):
    global result
    for n in range(len(data)-1, -1, -1):
        if data[n] == n + 1:
            result.append(n + 1)
            if len(data) == 1:
                #print('OK')
                return True
            else:
                #print('remove {0} '.format(n + 1), end='')
                #print(data[:n] + data[n+1:])
                if remove(data[:n] + data[n+1:]):
                    return True
    else:
        #print('try again')
        if len(result) > 0:
            result.pop(-1)
        return False

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

result = []

if remove(B):
    result.reverse()
    for res in result:
        print(res)
else:
    print(-1)

Submission status: Time Limit Exceeded

Solution code:

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

def step(a,c):
    return a[:c-1:]+[c]+a[c-1::]

def create_a(command):
    a=[]
    for c in command:
        if len(a)+1<c:
            return False
        a=step(a,c)
    return a


def creatable(b):
    if b==[1]:
        return True,[1]
    if len(b)==1:
        return False,[1]

    judge=False
    history=[]
    for i in range(len(b)):
        if b[i]==i+1:
            judge,history=creatable(b[:i:]+b[i+1::])
            if judge:
            	history.append(i+1)
        else:
            continue
        if judge:
            break
    return judge,history



judge,history=creatable(b)

if judge:
    for h in history:
        print(h)
else:
    print(-1)

Submission status: Time Limit Exceeded

Solution code:
import copy

n = int(input())

goal = list(map(int, input().split(" ")))

output = -1

def searchAns(goal, temp, left, proc):
  #print(temp, left, proc)
  if len(left) == 0:
    if temp == goal:
      return proc
    else:
      return -1
  for num in left:
    #print(num, temp, left, proc)
    if num <= len(temp) + 1:
      copyTemp = copy.copy(temp)
      copyTemp.insert(num-1, num)
      copyLeft = copy.copy(left)
      copyLeft.remove(num)
      copyProc = copy.copy(proc)
      copyProc.append(num)
      result = searchAns(goal, copyTemp, copyLeft, copyProc)
      if result != -1:
        return result
  else:
    return -1
    
proc = searchAns(goal, [], goal, [])

if proc == -1:
  print(-1)
else:
  for num in proc:
    print(num)

Submission status: Time Limit Exceeded

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

def step(a,c):
    return a[:c-1:]+[c]+a[c-1::]

def create_a(command):
    a=[]
    for c in command:
        if len(a)+1<c:
            return False
        a=step(a,c)
    return a


def creatable(b):
    if b==[1]:
        return True,[1]
    if len(b)==1:
        return False,[1]

    judge=False
    history=[]
    for i in range(len(b)):
        if b[i]==i+1:
            judge,history=creatable(b[:i:]+b[i+1::])
            history.append(i+1)
        else:
            continue
        if judge:
            break
    return judge,history



judge,history=creatable(b)

if judge:
    for h in history:
        print(h)
else:
    print(-1)

Submission status: Time Limit Exceeded

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

def dfs(c):
  if len(c) == 0:
    return []

  for i in range(1, len(c)+1):
    if c[i-1] > i:
      return False

    if c[i-1] == i:
      cc = c.copy()
      del cc[i-1]
      res = dfs(cc)
      if res is not False:
        res.append(i)
        return res

  return False

res = dfs(b)
if res is False:
  print(-1)
else:
  for i in res:
    print(i)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
def main():
    n = int(input())
    b = list(map(int, input().split()))
    q = deque()
    q.append([b[::], [0]*n, 0])
    ans = None
    while q:
        now, history, depth = q.popleft()
        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([[now[k] for k in range(len(now)) if k != i], 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 sys
input = sys.stdin.readline

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

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

def bfs(b):
    q.put(b)

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

        if p == []:
            break

        if ans.empty() == False:
            a = ans.get()
        else:
            a = []

        m = len(p)

        for i in range(m):
            if p[i] == (i+1):
                pp = p[:]
                pp.remove(i+1)
                q.put(pp)

                aa = a[:]
                aa.append(i+1)
                ans.put(aa)

    if p == []:
        a = ans.get()
        a = a[::-1]

        for aa in a:
            print(aa)
    else:
        print(-1)


bfs(b)

Submission status: Time Limit Exceeded

Solution code:
def main():
    N = int(input())
    B = list(map(int, input().split()))
    
    Bs = [B]
    Rs = [[]]
    for i in range(N):
        Bs_new = []
        Rs_new = []
        for b,r in zip(Bs,Rs):
            for j in range(N-i):
                if (b[j] == j+1):
                    tmp = list(b)
                    tmp2 = list(r)
                    tmp2.insert(0,tmp.pop(j))
                    Bs_new.append(tmp)
                    Rs_new.append(tmp2)
        Bs = Bs_new
        Rs = Rs_new
    
    if (0 < len(Rs)):
        R = Rs[0]
        print("\n".join([str(i) for i in R]))
    else:
        print(-1)
main()

Submission status: Time Limit Exceeded

Solution code:
def remove(data):
    global result
    for n in range(len(data)):
        if data[n] == n + 1:
            result.append(n + 1)
            if len(data) == 1:
                #print('OK')
                return True
            else:
                #print('remove {0} '.format(n + 1), end='')
                #print(data[:n] + data[n+1:])
                if remove(data[:n] + data[n+1:]):
                    return True
    else:
        #print('try again')
        if len(result) > 0:
            result.pop(-1)
        return False

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

result = []

if remove(B):
    result.reverse()
    for res in result:
        print(res)
else:
    print(-1)

Submission status: Time Limit Exceeded

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

generate_seq = []


def is_ok(b):
    len_b = len(b)
    if len_b == 1 and b[0] == 1:
        generate_seq.append(1)
        return True

    processing = []
    for idx in range(len_b):
        if b[idx] != idx + 1:
            processing.append(False)
        else:
            b_tmp = b[:]
            del b_tmp[idx]
            tmp = is_ok(b_tmp)
            if tmp:
                generate_seq.append(idx + 1)
                return True
    return False


result = is_ok(b_list)
if result:
    print('\n'.join(list(map(str, generate_seq))))
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 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:
inpl = lambda: list(map(int,input().split()))
N = int(input())
b = inpl()

def solution(chosen=[0]*N, n=0):
    if n == N:
        return []
    c = 0
    for i in range(N):
        if chosen[i] == 0 and b[i] == i + 1 - c:
            chosen[i] = 1
            s = solution(chosen, n+1)
            if s is None:
                chosen[i] = 0
            else:
                s.append(b[i])
                return s
        elif chosen[i] == 1:
            c += 1
    return None

ans = solution()
if ans is None:
    print(-1)
else:
    for i in range(N):
        print(ans[i])

Submission status: Time Limit Exceeded

Solution code:
from bisect import bisect_left, bisect_right
from collections import Counter, defaultdict, deque, OrderedDict
from copy import deepcopy
from functools import lru_cache, reduce
from math import ceil, floor
from sys import setrecursionlimit

import heapq
import itertools
import operator


inf = float('inf')


def get_int():
    return int(input())


def get_float():
    return float(input())


def get_str():
    return input().strip()


def get_list_of_int():
    return [int(i) for i in input().split()]


def get_list_of_float():
    return [float(f) for f in input().split()]


def get_list_of_char():
    return list(input().strip())


def get_data(n, order=None, **kwargs):
    if not order:
        order = range(len(kwargs))
    it = iter(order)
    var, fn = zip(*sorted(sorted(kwargs.items()), key=lambda _: next(it)))

    rows = []
    for _ in range(n):
        rows.append(input().split())
    columns = zip(*rows)
    exec("global "+", ".join(var)+"\n"+"\n".join(
        v+"=list(map(fn[{}], data[{}]))".format(i, i)
        for i, v in enumerate(var)),
        globals(),
        {"fn": list(fn), "data": list(columns)}
    )
    return


# inputs
N = 0
B = []


def set_inputs():
    global N, B
    N = get_int()
    B = get_list_of_int()
    return


memo = dict()


# n: int, b: List[int]
def solve(b):
    if len(b) == 0:
        return []
    s = str(b)
    if memo.get(s, None):
        return memo[s]
    possible_indices = []
    for i, v in enumerate(b):
        if i + 1 == v:
            possible_indices.append(i)
    if len(possible_indices) == 0:
        memo[s] = [-1]
        return [-1]
    for i in possible_indices:
        ans = solve(b[:i] + b[i+1:])
        if len(ans) == 1 and ans[0] == -1:
            continue
        memo[s] = ans + [i+1]
        return ans + [i+1]
    memo[s] = [-1]
    return [-1]


def main():
    setrecursionlimit(100000)
    set_inputs()
    for i in solve(B):
        print(i)
    return


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

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


    return False

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

Submission status: Time Limit Exceeded

Solution code:
def main():
    N = int(input())
    B = list(map(int, input().split()))
    
    Bs = [B]
    Rs = [[]]
    for i in range(N):
        Bs_new = []
        Rs_new = []
        for b,r in zip(Bs,Rs):
            for j in range(N-i):
                if (b[j] == j+1):
                    tmp = b.copy()
                    tmp2 = r.copy()
                    tmp2.insert(0,tmp.pop(j))
                    Bs_new.append(tmp)
                    Rs_new.append(tmp2)
        Bs = Bs_new
        Rs = Rs_new
    
    if (0 < len(Rs)):
        R = Rs[0]
        print("\n".join([str(i) for i in R]))
    else:
        print(-1)
main()

Submission status: Time Limit Exceeded

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

generate_seq = []


def is_ok(b):
    len_b = len(b)
    if len_b == 1 and b[0] == 1:
        generate_seq.append(1)
        return True

    processing = []
    for idx in range(len_b):
        if b[idx] != idx + 1:
            processing.append(False)
        else:
            b_tmp = b[:]
            del b_tmp[idx]
            processing.append(is_ok(b_tmp))
    if any(processing):
        generate_seq.append(processing.index(True) + 1)
        return True
    return False


result = is_ok(b_list)
if result:
    print('\n'.join(list(map(str, generate_seq))))
else:
    print(-1)

Submission status: Time Limit Exceeded

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

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


    return

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

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
n = int(input())
b = list(map(int, input().split()))
q = deque()
q.append([b[::], []])
ans = None
while q:
    now, history = q.popleft()
    if len(now) == 0:
        ans = history
        break
    for i, j in enumerate(now):
        if i+1 == j:
            q.appendleft([now[:i] + now[i+1:], history + [i+1]])
if ans is None:
    print(-1)
else:
    for i in ans[::-1]:
        print(i)

Submission status: Time Limit Exceeded

Solution code:
import sys

sys.setrecursionlimit(10 ** 9)


def rec(arr, l, lst):
    # lstは操作の逆順
    # print(arr, l, lst[::-1])
    if l == 0:
        for p in lst[::-1]:
            print(p)
        exit()
        return

    for i, aa in enumerate(arr, 1):
        # 1-indexed
        if aa == i:
            # print(aa, '除去')
            # 最後に入れた数は、何番目と数の大きさが一致する
            rec(arr[:i - 1] + arr[i:], l - 1, lst + [i])
        elif aa > i:
            # 何番目よりも数が大きい
            return
    return


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

rec(b, N, [])

print(-1)

Submission status: Time Limit Exceeded

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

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

def rec(arr):
    if len(arr)==1:
        if arr[0] == 1:
            return [1]
        else:
            return False
    for i,a in enumerate(arr):
        if i+1 == a:
            res = rec(arr[:i] + arr[i+1:])
            if res:
                res.append(i+1)
                return res
    return False

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

Submission status: Time Limit Exceeded

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

def dfs(c):
  if len(c) == 0:
    return []

  for i in range(1, len(c)+1):
    if c[i-1] == i:
      cc = c.copy()
      del cc[i-1]
      res = dfs(cc)
      if res is not False:
        res.append(i)
        return res
  return False

res = dfs(b)
if res is False:
  print(-1)
else:
  for i in res:
    print(i)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
n = int(input())
b = list(map(int, input().split()))
q = deque()
q.append([b[::], []])
ans = None
while q:
    now, history = q.popleft()
    if len(now) == 0:
        ans = history
        break
    for i, j in enumerate(now):
        if i+1 == j:
            q.append([now[:i] + now[i+1:], history + [i+1]])
if ans is None:
    print(-1)
else:
    for i in ans[::-1]:
        print(i)

Submission status: Time Limit Exceeded

Solution code:
import copy

def dfs(i, operations, rest_bs):
    if i == 0 and len(rest_bs) == 0:
        return operations

    for k, b in enumerate(rest_bs):
        j = k + 1
        if j <= i and j == b:
            copied_operations = copy.deepcopy(operations)
            copied_operations.append(j)
            res = dfs(i-1, copied_operations, rest_bs[:k] + rest_bs[k+1:])
            if res != -1:
                return res
    return -1


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

    res = dfs(N, [], bs)
    if res == -1:
        print(-1)
    else:
        for n in reversed(res):
            print(n)

Submission status: Time Limit Exceeded

Solution code:
from bisect import bisect_left, bisect_right
from collections import Counter, defaultdict, deque, OrderedDict
from copy import deepcopy
from functools import lru_cache, reduce
from math import ceil, floor
from sys import setrecursionlimit

import heapq
import itertools
import operator


inf = float('inf')


def get_int():
    return int(input())


def get_float():
    return float(input())


def get_str():
    return input().strip()


def get_list_of_int():
    return [int(i) for i in input().split()]


def get_list_of_float():
    return [float(f) for f in input().split()]


def get_list_of_char():
    return list(input().strip())


def get_data(n, order=None, **kwargs):
    if not order:
        order = range(len(kwargs))
    it = iter(order)
    var, fn = zip(*sorted(sorted(kwargs.items()), key=lambda _: next(it)))

    rows = []
    for _ in range(n):
        rows.append(input().split())
    columns = zip(*rows)
    exec("global "+", ".join(var)+"\n"+"\n".join(
        v+"=list(map(fn[{}], data[{}]))".format(i, i)
        for i, v in enumerate(var)),
        globals(),
        {"fn": list(fn), "data": list(columns)}
    )
    return


# inputs
N = 0
B = []


def set_inputs():
    global N, B
    N = get_int()
    B = get_list_of_int()
    return


# n: int, b: List[int]
def solve(b):
    if len(b) == 0:
        return []
    insertable_indices = []
    for i, v in enumerate(b):
        if i + 1 == v:
            insertable_indices.append(i)
    if len(insertable_indices) == 0:
        return [-1]
    for i in insertable_indices:
        ans = solve(b[:i] + b[i+1:])
        if len(ans) == 1 and ans[0] == -1:
            continue
        return ans + [i+1]
    return [-1]


def main():
    setrecursionlimit(100000)
    set_inputs()
    for i in solve(B):
        print(i)
    return


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
B=list(map(int,input().split()))
def DP(L):
	if L==[]:
		return 1,[]
	ret=[]
	for i in range(len(L))[::-1]:
		if L[i]==i+1:
			tL=L[:]
			del tL[i]
			f,dp=DP(tL)
			if f==1:
				return 1,[i+1]+dp
	return -1,[]
f,dp=DP(B)
if f==1:
	for d in dp[::-1]:
		print(d)
else:
	print(-1)