Problem:


Score : 400 points

Problem Statement

We have N bricks arranged in a row from left to right.

The i-th brick from the left (1 \leq i \leq N) has an integer a_i written on it.

Among them, you can break at most N-1 bricks of your choice.

Let us say there are K bricks remaining. Snuke will be satisfied if, for each integer i (1 \leq i \leq K), the i-th of those brick from the left has the integer i written on it.

Find the minimum number of bricks you need to break to satisfy Snuke's desire. If his desire is unsatisfiable, print -1 instead.

Constraints

All values in input are integers.

1 \leq N \leq 200000

1 \leq a_i \leq N

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 ... a_N

Output

Print the minimum number of bricks that need to be broken to satisfy Snuke's desire, or print -1 if his desire is unsatisfiable.

Sample Input 1

3
2 1 2

Sample Output 1

1

If we break the leftmost brick, the remaining bricks have integers 1 and 2 written on them from left to right, in which case Snuke will be satisfied.

Sample Input 2

3
2 2 2

Sample Output 2

-1

In this case, there is no way to break some of the bricks to satisfy Snuke's desire.

Sample Input 3

10
3 1 4 1 5 9 2 6 5 3

Sample Output 3

7

Sample Input 4

1
1

Sample Output 4

0

There may be no need to break the bricks at all.



Sample input:
3
2 1 2


Sample output:
1

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
a_list = [int(num) for num in input().split()]

is_ok = False
index = 0
count = 0
while True:
    target = a_list[index]
    if target == index + 1:
        is_ok = True
        index += 1
    else:
        a_list.pop(index)
        count += 1
    if not a_list:
        break
    if index == len(a_list):
        break
if not index:
    print(-1)
else:
    print(count)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
a=[int(x) for x in input().split()]
count=0
flg=1
for i in range(n):
		if a[0]!=flg:
			count+=1
		else:
			flg+=1
		del a[0]

if n==count:
	count=-1

print(count)

Submission status: Time Limit Exceeded

Solution code:
input()
a=list(map(int,input().split()))
c=i=0
while i < len(a):
	if a[i] != i+1:
		a.pop(i)
		c+=1
		continue
	i+=1
print(c if len(a) else"-1")

Submission status: Time Limit Exceeded

Solution code:
# from collections import deque

N=int(input())
a=list(map(int,input().split()))
# a=deque(a)
cnt=0
for i in range(N):
  if len(a)<=i:
    break
  while i<len(a) and a[i]!=i+1:
    a.pop(i)
    cnt+=1
    # print(i,a,cnt)
print(cnt if len(a)!=0 else -1)

Submission status: Time Limit Exceeded

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

target_number=1
i=0
while len(numbers)>i+1:
  if(numbers[i]!=target_number):
    numbers.pop(target_number-1)
  elif(numbers[i]==target_number):
    i+=1
    target_number+=1

if(len(numbers)>1):
  print(N-target_number)
  
elif(len(numbers)==1):
  if(numbers[0]==1):
    print(N-1)
  elif(numbers[0]!=1):
    print(-1)

Submission status: Time Limit Exceeded

Solution code:
def main():
	n = int(input())
	# b = int(input())
	a = list(map(int,input().split()))
	# A = [list(map(int, input().split())) for _ in range(n)]
	# print(a)
	ans = 0
	kijun=1
	for i in range(1,n+1):
		# print("i = {}".format(i))
		if i > len(a):
			break
		for s in range(n):
			# print("s = {}".format(s))
			if a[i-1]==kijun:
				kijun += 1
				break
			else:
				a.pop(i-1)
				ans += 1
				# print(a)
			# print(i,s)
		
	if ans == n:
		print(-1)
	else:
		print(ans)
	




if __name__ == '__main__':
	main()

Submission status: Time Limit Exceeded

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


def main():
    N = int(input())
    A = list(map(int, input().split()))

    ans = 0
    if 1 not in A:
        ans = -1
    else:
        current_index = A.index(1)
        for i in range(2, N + 1):
            if i not in A[current_index + 1:]:
                ans = N - i + 1
                break
            else:
                current_index = A[current_index + 1:].index(i) + current_index

    print(ans)


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
# 1行目の入力を数値として扱う
N = int(input())
# 2行目の入力を数値のリストとして扱う
a = list(map(int, input().split()))

# 答えの準備
res = 0

# ループ
cnt = 0
i = 0
while(cnt < N):
    cnt = cnt + 1
    if a[i] == i + 1:
        i = i + 1
    else:
        del a[i]
        res = res + 1

# 答えの出力
if len(a) == 0:
    res = -1
print(res)

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8

N = int(input())
A = [int(x) for x in input().split(" ")]

k = 0
l = -1

for i in range(1, N+1):
    templ = l
    for j in range(k, N):
        if A[j] == i:
            k = j
            l = i
    if templ == l:
        break

if l == -1:
    print(-1)
else:
    print(N - l)

Submission status: Time Limit Exceeded

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

j=0
c=0
for i in range(len(a)):
    if(a[j]!=j+1):
        del(a[j])
        c+=1
    else:
        j+=1
if(n==c):
    print(-1)
else:
    print(c)

Submission status: Time Limit Exceeded

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

count = 0
if 1 in x:
    try:
        for i in range(n):
            while x[i]!=(i+1):
                del x[i]
                count += 1            
        print(count)
    except:
        print(count)
else:
    print(-1)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int, input().split()))
adder = 0
if (list(range(1, N + 1)) == A):
    print(0)
else:
    for i in range(1, N + 1):
        aa = len(A)
        for j in range(len(A)):
            if A[j] == i:
                A = A[(j + 1):]
                break
        if (len(A) == 0):
            break
        if j == (aa - 1):
            adder = 1
            break
    if i != 1:
        print(N - i + adder)
    else:
        print(-1)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
aL = list([int(i) for i in input().split()])
i = 0
while True:
    if i == len(aL):
        break
    else:
        if aL[i] == i+1:
            i += 1
        else:
            aL.pop(i)

L = len(aL)
flag = 0
for i in range(L):
    if aL[i] != i+1:
        flag = 1

if flag == 1 or L == 0:
    print(-1)
else:
    print(N-L)

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python

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

flag = False
if 1 in a:
    flag = True

a_copy = a
num = 0
i = 0
while True:
    num += 1
    if num in a_copy:
        # print(num, 'is in a_copy', a_copy.index(num))
        a_copy = a_copy[a_copy.index(num):]
        # print('a_copy', a_copy)
    else:
        # print('break')
        # print('num = ', num)
        break

if flag:
    print(len(a) - (num - 1))
else:
    print(-1)

Submission status: Time Limit Exceeded

Solution code:
def main():
    # input = sys.stdin.readline
    N = int(input())
    raw_list = list(map(int, input().split()))
    not_found_number = 0

    count = 0

    for i in range(1, N+1):
        found = False
        for idx, v in enumerate(raw_list):
            if i == v:
                raw_list = raw_list[idx+1:]
                count += idx
                found = True
                break
        if not found:
            count += len(raw_list)
            not_found_number = i
            break

    if not_found_number == 1:
        count = -1

    print(count)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
a=list(map(int,input().split()))
c=0
d=0
for i in range(1,n+1):
  if i in a[c:]:
    c+= a[c:].index(i)
    d+=1
  else:
    break
print(-1 if d==0 else n-d)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
number = list(map(int,input().split()))
count = 0
if not 1 in number:
    print(-1)
else:
    for i in range(N):
        now = i+1
        if now in number:
            #number.index(now)が左から数えたときのnowのindex番号
            count += number.index(now)
            number[:number.index(now)+1]=[]

        else:
            count += len(number)
            break
    print(count)

Submission status: Time Limit Exceeded

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

i = 1
if 1 in a:
    while i in a:
        del a[:a.index(i)]
        i += 1
    print(n-i+1)
else:
    print(-1)

Submission status: Time Limit Exceeded

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

ans=-1
for i in range(1,n+1):
    if i in renga:
        idx=renga.index(i)
        renga=renga[idx+1:]
        brick=[renga[j] for j in range(len(renga)) if renga[j]>i]
        ans=i
        renga=brick
        #print(renga)
        continue
    else:
        break

if ans!=-1:
    ans=n-ans

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
N = int(input())
a = list(map(int,input().split()))

for i in range (1,N+1):
  if i in a:
    x1 = a.index(i)
    a = a[x1:N+1]
  else:
    ans = N - i + 1
    if ans == N:
      print(-1)
      sys.exit()
    else:
      print(N-i+1)
      sys.exit()
print(0)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(map(int, input().split()))
i = 0
while 1:
    if i >= len(a):
        break
    if a[i] != i+1:
        a.remove(a[i])
    else:
        i += 1
if len(a) == 0:
    print(-1)
else:
    print(n-len(a))

Submission status: Time Limit Exceeded

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

n = int(input())
a = list(map(int,input().split()))
cnt = 0
s = 0

def ers(a,cnt,s):
    if len(a) == 0:
        cnt = -1
        return cnt
    for i in range(s,len(a)):
        if i+1 != a[i]:
            a.pop(i)
            cnt+=1
            return ers(a,cnt,s)
        else:
            s += 1
    return cnt

res = ers(a,cnt,s)

print(str(res))

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8
# submission # - User: herp_sy
# https://atcoder.jp/contests/
#
# lang: Python3 (3.4.3)

import math
import statistics
import numpy as np
import queue

n = int(input())
a = list(int(i) for i in input().split())
p = 0
err_avoid = 0

a.append(-1)
for i in range(2 * n):
  if a[p] == -1:
    break
  elif p != (a[p] - 1):
    err_avoid = a.pop(p)
  else:
    p += 1

if len(a) == 1 and a[0] == -1:
  print(a.pop())
else:
  print(n - len(a) + 1)

Submission status: Time Limit Exceeded

Solution code:
def main(n: int, a: list):
    ans = 0

    i = 1

    if 1 not in a:
        print(-1)
        return

    while len(a) > 0:
        if a[0] != i:
            a.pop(0)
            ans += 1
            continue
        i += 1
        a.pop(0)

    print(ans)


if __name__ == "__main__":
    n = int(input())
    a = list(map(int, input().split()))

    main(n, a)

Submission status: Time Limit Exceeded

Solution code:
num = int(input())
input_data = input().split(' ')
data = [int(input_data[i]) for i in range(len(input_data))]
n = 1
key = 0
while(n in data):
    key = 1
    data = data[data.index(n):]
    n += 1

if(key == 0):
    print(-1)
else:
    print(num - n + 1)

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8
n = int(input())
renga_list = list(map(int, input().split()))

tmp_i = 0
i = 1
while(True):
#    print("i="+str(i))
#    renga_list = renga_list[tmp_i:]
#    print("renga_list="+str(renga_list))
    if i in renga_list[tmp_i:]:
        tmp_i = renga_list[tmp_i:].index(i) + tmp_i
        i += 1
    else:
        break

result = n - i + 1
if n == result:
    print(-1)
else:
    print(result)

Submission status: Time Limit Exceeded

Solution code:
def my_index(l, x, default=-1):
    if x in l:
        return l.index(x)
    else:
        return default


N = int(input())
A = list(map(int,input().split()))
B = []
count = 0
if not 1 in A:
    print(-1)
else:
    for i in range(N):
        if B:
            m = my_index(A[B[-1]+1:],i+1)
        else:
            m = my_index(A,i+1)
        
        if m !=-1:
            B.append(m)
        else:
            break

    k = len(A) - len(B)
    print(k)

Submission status: Time Limit Exceeded

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

counter=0

for i in range(N):
    try:
        index = NL.index(i+1)
    except:
        break
    else:
        NL = NL[index+1:]
        counter += 1


if(counter == 0):
    print(-1)
else:
    print(N - counter)

Submission status: Time Limit Exceeded

Solution code:
# 整数の入力
N = int(input())

# スペース区切りの整数の入力
datas = list(map(int, input().split()))

# non_conflict = list(set(datas))
# conf_num = len(datas) - len(non_conflict)

# blocks = sorted(datas)
blocks = list(datas)

cnt = 0
i = 1
while i <= len(blocks):
    if blocks[i-1] != i:
        cnt += 1
        blocks.pop(i-1)
        continue
    i += 1



# 出力
if cnt == len(datas):
    print(-1)
else:
    print(cnt)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
# スペース区切りの整数の入力
a = list(map(int, input().split()))

idx = 0
out = 0
for i in range(1, N + 1):
    tmp = a[idx:]
    if i in tmp:
        idx = tmp.index(i) + 1
        out = i
    else:
        break
if out == 0:
    print(-1)
else:
    print(N-out)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
l=list(map(int,input().split()))
m=0
for i in range(n):
    k=n
    for j in range(n-i):
        if (n-i-j) in l[:k]:
            k=(k-1)-l[:k][::-1].index(n-i-j)
        else:
            break
        if j==n-i-1:
            m=n-i
    if not m==0:
        break
if not m==0:
    print(n-m)
else:
    print(-1)

Submission status: Time Limit Exceeded

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

if 1 in a:
    ind=a.index(1)
    stop=1
    for i in range(2,N+1):
        if i in a[ind+1:]:
            ind+=a[ind+1:].index(i)
            continue
        else:
            stop=i-1
            break
    print(N-stop)
else:
    print(-1)

Submission status: Time Limit Exceeded

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

n = int(input())
a = list(map(int,input().split()))
cnt = 0

def ers(a,cnt):
    if len(a) == 0:
        cnt = -1
        return cnt
    for i in range(len(a)):
        if i+1 != a[i]:
            a.pop(i)
            cnt+=1
            return ers(a,cnt)
    return cnt

res = ers(a,cnt)

print(str(res))

Submission status: Time Limit Exceeded

Solution code:
import math
N = int(input())
a = list(map(int, input().split()))
index = 0
break_ = 0
while break_ < N - 1:
  if index == len(a):
    break
  elif index + 1 != a[index]: #一致しなかったら壊す
    a.pop(index)
    break_ += 1
  else: #一致したら次
    index += 1
#print(index)
#print(break_)
#print([i + 1 for i in range(N - break_)])
#print(a)
if [i + 1 for i in range(N - break_)] != a:
  print(-1)
else:
  print(break_)

Submission status: Time Limit Exceeded

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

Y = X[::-1]
Y = tuple(Y)

for k in range(N):
    try:
        i = Y.index(k+1)
        Y = Y[:i]
    except:
        i = -1
        leng = k
        break
    leng = k + 1

if leng == 0:
    ans = -1
else:
    ans = N - leng
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import heapq

class Graph_CONNECT:
    """
    接続リスト方式
    """
    def __init__(self, N):
        self.number_of_node = N
        self.neighbour_list = [[] for _ in range (N)]

    def connect(self, from_node, to_node, weight):
        """
        from_nodeからto_nodeに長さweightの辺を追加する
        """
        self.neighbour_list[from_node].append((to_node, weight))

    def dijkstra(self, source_node, target_node = None):
        """
        source_nodeからtarget_nodeへの最短路計算
        source_nodeの入次数は0と仮定する
        """
        INF = 1 << 32
        dist = [INF] * self.number_of_node
        #prev = [-1] * self.number_of_node
        dist[source_node] = 0
        hq = []
        heapq.heappush(hq, (0, source_node))
        while len(hq) > 0:
            d, from_node = heapq.heappop(hq)
            if dist[from_node] < d:
                continue
            for to_node, weight in self.neighbour_list[from_node]:
                """
                weight = adj[from_node][to_node]
                # 辺がないところはスキップ
                if weight < 0:
                    continue
                """
                if dist[to_node] == INF or dist[from_node] + weight < dist[to_node]:
                    dist[to_node] = dist[from_node] + weight
                    heapq.heappush(hq, (dist[to_node], to_node))
                    #prev[to_node] = from_node
        if target_node is None:
            return dist
        else:
            return dist[target_node]

N = int(input().strip()) 
A = list(map(int, input().strip().split()))
A.insert(0,0)

graph = Graph_CONNECT(N + 1)
for i in range(N+1):
    for j in range(i+1, N+1):
        if A[i] + 1 == A[j]:
            graph.connect(i,j,-1)

dist = graph.dijkstra(0)

ans = min(dist)
if ans >= 0:
    print(-1)
else:
    print(N+ans)

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8
# submission # - User: herp_sy
# https://atcoder.jp/contests/
#
# lang: Python3 (3.4.3)

import math
import statistics
import numpy as np
import queue

n = int(input())
a = list(int(i) for i in input().split())
p = 0
err_avoid = 0

a.append(-1)
for i in range(3 * n):
  # print(a)
  if a[p] == -1:
    break
  elif p != (a[p] - 1):
    err_avoid = a.pop(p)
  else:
    p += 1

if len(a) == 1 and a[0] == -1:
  print(a.pop())
else:
  print(n - len(a) + 1)

Submission status: Time Limit Exceeded

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

k = 1
s = 0
count = 0


for i in range(n):
    if k in a:
        s = a.index(k)
        a = a[s:]
        k += 1

if 1 in m:
    print(n-(k-1))
else:
    print(-1)

Submission status: Time Limit Exceeded

Solution code:
import sys
n = int(input())
lis = list(map(int, input().split()))
# print(li)
start_index = 0
has_found_number = 0
for i in range(1,n+1):
    if i in lis:
        lis = lis[lis.index(i):]
        has_found_number = i
        continue
    else:
        break
if has_found_number == 0:
    print(-1)
else:
    print(n-has_found_number)

Submission status: Time Limit Exceeded

Solution code:

def main():
    N = int(input())
    A = [int(i) for i in input().split(" ")]
    #A = input()
    
    size = len(A)
    i=1
    count = 0
    while size >=1:
        try:
            ind = A.index(i)
        except ValueError:
            if i ==1:
                print(-1)
                return 0
            else:
                count += len(A)
                break
        count += ind
        A=A[ind+1:]
        i = i + 1
        size = len(A)
    print(count)


if __name__=="__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys

sys.setrecursionlimit(4100000)
input = sys.stdin.readline


def f(l, c, s, n):
    if not l:
        if s == n:
            return -1
        return s

    if l[0] != c:
        return f(l[1:], c, s + 1, n)
    return f(l[1:], c + 1, s, n)

def main():
    n = int(input()[:-1])
    l = list(map(int, input()[:-1].split(" ")))

    c = 1
    s = 0
    while not l:
        if l[0] != c :
            del l[0]
            s += 1
        else:
            c += 1
        del l[0]


    res = s
    if s == n:
        res = -1

    print(f(l, 1, 0, n))

main()

Submission status: Time Limit Exceeded

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

if a == [1]:
  print(0)
  exit()

if 1 not in a:
  print("-1")
  exit()
else:
  nokosu = 1
  a=a[a.index(1)+1:]

for i in range(2,n+1):
  if i not in a:
    print(n - nokosu)
    exit()
  else:
    nokosu += 1
    if len(a)==1:
      print(n - nokosu)
      exit()
    a = a[a.index(i)+1:]

Submission status: Time Limit Exceeded

Solution code:
import heapq

N = int(input().strip())
A = list(map(int, input().strip().split()))
A.insert(0,0)
#print(A)
INF = 1 << 32
adj = [[INF for i in range(N+1)] for j in range(N+1)]
for i in range(N+1):
    for j in range(i+1, N+1):
        if A[i] + 1 == A[j]:
            adj[i][j] = 1

#print(adj)

dist = [INF] * (N + 1)
dist[0] = 0
hq = []
heapq.heappush(hq, (0, 0))
while len(hq) > 0:
    d, from_node = heapq.heappop(hq)
    if dist[from_node] < d:
        continue
    for to_node in range(N + 1):
        weight = adj[from_node][to_node]
        # 辺がないところはスキップ
        if weight == INF:
            continue
        weight = - weight
        if dist[to_node] == INF or dist[from_node] + weight < dist[to_node]:
            dist[to_node] = dist[from_node] + weight
            heapq.heappush(hq, (dist[to_node], to_node))
#print(dist)
ans = min(dist)
if ans >= 0:
    print(-1)
else:
    print(N+ans)

Submission status: Time Limit Exceeded

Solution code:
N, *L,  = [list(map(int, s.split())) for s in open(0)]
L = L[0]
i = 0
l = N[0]
cnt = 0
while l > 0 and l - 1 >= i:
    if L[i] != i + 1:
        L.pop(i)
        cnt += 1
        i = 0
    else:
        i += 1
    l = len(L)
if cnt == N[0]:
    print(-1)
else:
    print(cnt)

Submission status: Time Limit Exceeded

Solution code:
x = int(input())
result = 0
ary = list(map(int,input().split()))
miss = False
i = 1
while(True):
  if (i not in ary):
    if (i == 1):
      miss = True
    break
  b = ary.index(i)
  i += 1
  ary = ary[b+1:]

if miss:
  print(-1)
else:
  print(x - i + 1)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
a=list(map(int,input().split()))
break_count=0
selector=1
while len(a)>=selector:
    if a[selector-1]==selector:
        selector+=1
    else:
        a.pop(selector-1)
        break_count+=1
print(break_count if len(a)!=0 else -1)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
a=list(map(int,input().split()))
c=0
d=0
for i in range(1,n+1):
  if i in a[c:]:
    c+= a[c:].index(i)
    d+=1
  else:
    break
print(-1 if d==0 else n-d)

Submission status: Time Limit Exceeded

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

ans=-1
for i in range(1,n+1):
    if i in renga:
        idx=renga.index(i)
        renga=renga[idx+1:]
        ans=i
        continue
    else:
        break

if ans!=-1:
    ans=n-ans

print(ans)

Submission status: Time Limit Exceeded

Solution code:
x = int(input())
result = 0
ary = list(map(int,input().split()))
miss = False
i = 1
while(True):
  if (i not in ary):
    if (i == 1):
      miss = True
    break
  b = ary.index(i)
  i += 1
  ary = ary[b+1:]

if miss:
  print(-1)
else:
  print(x - i + 1)

Submission status: Time Limit Exceeded

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

check_num = 1
delete_num = 0
while len(aaa) > 0:
  tmp_num = aaa.pop(0)
  if tmp_num == check_num:
    check_num += 1
  else:
    delete_num += 1
    
if delete_num == n:
  print(-1)
else:
  print(delete_num)

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
n = int(input())
a = list(map(int,input().split()))
count = 0

if 1 in a:
    pass
else:
    print(-1)
    exit()
    
for i in range(1,n+1):
    if i in a:
        indx = a.index(i)
        count += indx
        a = a[indx+1:]
        # print(count)
        # print(a)
    else:
        count += len(a)
        break

print(count)

Submission status: Time Limit Exceeded

Solution code:
import sys

def main():
    n=int(input())
    l=[int(e) for e in list(sys.stdin.readline().split())]
    i=1
    if i not in l:
        print(-1)
        return
    count=0
    while i in l:
        idx_i=l.index(i)
        count+=idx_i
        l=l[idx_i+1:]
        i+=1

    print(count+len(l))

main()

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8
n = int(input())
renga_list = list(map(int, input().split()))

tmp_i = 0
i = 1
while(True):
#    print("i="+str(i))
    renga_list = renga_list[tmp_i:]
#    print("renga_list="+str(renga_list))
    if i in renga_list:
        tmp_i = renga_list.index(i)
        i += 1
    else:
        break

result = n - i + 1
if n == result:
    print(-1)
else:
    print(result)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(map(int, input().split()))
cnt = 0
for i in range(1, n + 1):
    if i in a:
        cnt += 1
        a = a[a.index(i):]
    else:
        break
if cnt == 0:
    print(-1)
else:
    print(n - cnt)

Submission status: Time Limit Exceeded

Solution code:
def main():
    n=int(input())
    a=list(map(int,input().split()))
    if 1 not in a:
        print(-1)
    else:
        ans = a.index(1)
        k = ans
        for i in range(2,n):
            a = a[k+1:]
            if i not in a:
                ans += len(a)
                print(ans)
                exit()
            else:
                k = a.index(i)
                ans += k
        print(ans)
if __name__=="__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
alist = list(map(int, input().split()))
blist = [a for a in alist]
alen = len(alist)

n = 0
i = 1
i2 = 0
all = 0

while all < N:
    if alist[i2] != i:
        n += 1
        alist.pop(i2)
    else:
        i += 1
        i2 += 1
    all += 1

if len(alist) == 0:
    print(-1)
else:
    print(n)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
A=list(map(int,input().split()))
count = 0
position = 0
while position < N:
  for j in range(1,N+1):
    for i in range(position,N):
      if A[i] == j:
        count = count + 1
        break
      else:
        position = position + 1

if count != 0:
    print(N-count)
else:
    print(-1)

Submission status: Time Limit Exceeded

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

count = 0
i = 0
while i < len(a):
    if a[i] != i + 1:
        a.pop(i)
        count += 1
    else:
        i += 1
if count < N:
    print(count)
else:
    print(-1)

Submission status: Time Limit Exceeded

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

A = [a for i, a in enumerate(A) if a <= i+1]
k = 0

while k < len(A):
    if A[k] != k+1:
        A.pop(k)
    else:
        k += 1

if len(A) == 0:
    print(-1)
else:
    print(N - len(A))

Submission status: Time Limit Exceeded

Solution code:
def search(a,x):
    n=len(a)
    for i in range(n):
        if a[n-i-1]==x:
            return a[n-i:n]
    return False

n=int(input())
a=input().split()
min1=n
for i in range(n):
    a[i]=int(a[i])
for i in range(1,n+1):
    a=search(a,i)
    if a==False and i!=1:
        print(n-i+1)
        break
    elif a==False:
        print(-1)
        break
if a!=False:
    print(0)

Submission status: Time Limit Exceeded

Solution code:
import sys
n=int(input())
a=list(map(int,input().split()))
s=0
b=0
while 1:
    t=True
    for i in range(len(a)):
        if a[i]!=i+1:
            t=False
            break
    if t:
        print(b)
        sys.exit()
    if a[s]!=s+1:
        del a[s]
        b+=1
        if len(a)==0:
            print(-1)
            sys.exit()
    else:
        s+=1

Submission status: Time Limit Exceeded

Solution code:
#import time
#t1=time.time()

n=int(input())
a=list(map(int,input().split()))
count=0
flag=1
a.reverse()
l=a

for i in range(n):
  if a[-i-1]==flag:
    flag+=1
  else:
    l=l[:-1]
    count+=1

if count==n:
  print(-1)
else:
  print(count)

#t2=time.time()
#print(t2-t1)

Submission status: Time Limit Exceeded

Solution code:
x = int(input())
result = 0
ary = list(map(int,input().split()))
broken = 0
i = 1
while(True):
#  print("----")
#  print(ary)
#  print("i:",i)
  if (i not in ary):
    if (i == 1):
      broken = -1
      break
    else:
      broken += len(ary)
      break
  b = ary.index(i)
  broken += b
#  print("b:",b)
#  print("broken:",broken)
  i += 1
  ary = ary[b+1:]
  if (i+broken > x):
    break
print(broken)

Submission status: Time Limit Exceeded

Solution code:

def main():
    N = int(input())
    A = [int(i) for i in input().split(" ")]

    size = len(A)
    i=1
    count = 0
    while size >=1:
        try:
            ind = A.index(i)
        except ValueError:
            if i ==1:
                print(-1)
                return 0
            else:
                count += len(A)
                break
        count += ind
        A=A[ind+1:]
        i = i + 1
        size = len(A)
    print(count)


if __name__=="__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys

sys.setrecursionlimit(4100000)
input = sys.stdin.readline


def f(l, c, s, n):
    if not l:
        if s == n:
            return -1
        return s

    if l[0] != c:
        return f(l[1:], c, s + 1, n)
    return f(l[1:], c + 1, s, n)

def main():
    n = int(input()[:-1])
    l = list(map(int, input()[:-1].split(" ")))
    print(f(l, 1, 0, n))

main()

Submission status: Time Limit Exceeded

Solution code:
from copy import deepcopy
from sys import exit,setrecursionlimit
import math
from collections import defaultdict,Counter,deque
from fractions import Fraction as frac
import fractions
from functools import reduce
from operator import mul
import bisect
import sys
import logging
import heapq
import itertools

logging.basicConfig(level=logging.ERROR)

input = sys.stdin.readline

setrecursionlimit(1000000)


def main():
	n = int(input())
	a = list(map(int,input().split()))
	if(1 not in a):
		print(-1)
		return
	c = 1
	for i in a:
		if(i==c):
			c+=1
	for i in range(1,c):
		if(i not in a):
			print(-1)
			return
	print(n-c+1)

def factorization(n):
	if n<2:
		return []
	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

def cmb(n,r):
    r = min(n-r,r)
    if r == 0: return 1
    over = reduce(mul, range(n, n - r, -1))
    under = reduce(mul, range(1,r + 1))
    return over // under

def zip(a):
	mae = a[0]
	ziparray = [mae]
	for i in range(1,len(a)):
		if(mae != a[i]):
			ziparray.append(a[i])
			mae = a[i]
	return ziparray

def is_prime(n):
	if n < 2: return False

	for k in range(2, int(math.sqrt(n)) + 1):
		if n % k == 0:
			return False

	return True

def list_replace(n,f,t):
	return [t if i==f else i for i in n]

def base_10_to_n(X, n):
	X_dumy = X
	out = ''
	while X_dumy>0:
		out = str(X_dumy%n)+out
		X_dumy = int(X_dumy/n)
	if(out == ''): return '0'
	return out

def gcd(numbers):
	return reduce(math.gcd, numbers)

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

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

class Queue():
	def __init__(self):
		self.q = deque([])
	def push(self,i):
		self.q.append(i)
	def pop(self):
		return self.q.popleft()
	def size(self):
		return len(self.q)
	def debug(self):
		return self.q

class Stack():
	def __init__(self):
		self.q = []
	def push(self,i):
		self.q.append(i)
	def pop(self):
		return self.q.pop()
	def size(self):
		return len(self.q)
	def debug(self):
		return self.q

class graph():
	def __init__(self):
		self.graph = defaultdict(list)
	def addnode(self,l):
		f,t = l[0],l[1]
		self.graph[f].append(t)
		self.graph[t].append(f)
	def rmnode(self,l):
		f,t = l[0],l[1]
		self.graph[f].remove(t)
		self.graph[t].remove(f)
	def linked(self,f):
		return self.graph[f]

class dgraph():
	def __init__(self):
		self.graph = defaultdict(set)
	def addnode(self,l):
		f,t = l[0],l[1]
		self.graph[f].append(t)
	def rmnode(self,l):
		f,t = l[0],l[1]
		self.graph[f].remove(t)
	def linked(self,f):
		return self.graph[f]

class PriorityQueue():
	def __init__(self):
		self.queue = []
	def push(self,i):
		heapq.heappush(self.queue,-i)
	def pop(self):
		return -1 * heapq.heappop(self.queue)
	def sum(self):
		return -sum(self.queue)

main()