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 = tuple(map(lambda n: int(n) - 1, input().split()))

minlen = 10**9
for k in range(1, N+1):
    # kは残す数
    A_ = A[:]
    for j in range(k):
        if j in A_:
            nf = A_.index(j)
            A_ = A_[nf:]
        else:
            break
    else:
        minlen = min(minlen, N - k)

if minlen >= N:
    print(-1)
else:
    print(minlen)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = tuple(map(lambda n: int(n) - 1, input().split()))

minlen = 10**9
for i in range(1, 2**N):
    remain = 0
    for j in range(N):
        if (i >> j) & 1:
            # 残す
            if A[j] == remain:
                remain += 1
            else:
                break
    else:
        minlen = min(minlen, (N - remain))

if minlen >= N:
    print(-1)
else:
    print(minlen)

Submission status: Time Limit Exceeded

Solution code:
def main():
    import sys
    input = sys.stdin.readline

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

    import itertools

    indices = [i for i in range(N)]
    for i in range(N):
        for v in itertools.combinations(indices, i):
            v = list(v)
            # print(v)
            tmp_l = get_l(A, v)
            # print(tmp_l)
            if judge(tmp_l):
                print(i)
                exit()
    print(-1)


def get_l(l, rm_ind):
    ans = []
    for i, a in enumerate(l):
        if i in rm_ind:
            continue
        ans.append(a)
    return ans


def judge(l):
    for i, a in enumerate(l):
        if i+1 != a:
            return False
    return True


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

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

if not 1 in (a):
  print(-1)
else:
  ans = 1
  for i in range(1, n+1):
    idx = a.index(i)
    ans += len(a[:idx]) - 1
    a = a[idx:]
  
    if not i+1 in a:
      print(ans + len(a) - 1)
      break

Submission status: Time Limit Exceeded

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

def main():
    N = int(input())
    *a, = map(int, input().split())

    max_i = 0
    for i in range(1, max(a) + 1):
        if i in a:
            ind = a.index(i)
            if ind >= 0:
                a = a[ind:]
                max_i = i
            else:
                break
        else:
            break
        
    if max_i == 0:
        print(-1)
    elif max_i == N:
        print(0)
    else:
        print(N - max_i)


main()

Submission status: Time Limit Exceeded

Solution code:
n = int(input())

a = list(map(int,input().split()))
i = 0
c = 0
while(i < len(a)):
    if(a[i] == i+1):
        i += 1
    else:
        a.pop(i)
        c += 1

if(len(a) == 0):print(-1)
else:print(c)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
#初期入力
import sys
N = int(input())
A=[int(i) for i in input().split()]
#

i=0
i_index =0
brick_destroy =0
brick_remain =[]
A_now =A
while i <N:
    i +=1 #探す「文字」
    if i not in A_now:
        break
    else:
        i_index += A_now.index(i)
        brick_remain.append(i_index)
        if i_index >= N-1:
            break
        #print(brick_destroy)
        #print(i,end=" ")
        A_now = A[i_index :] #見つけた字の右側のみ見る

brick_destroy = N - len(brick_remain)
if brick_remain==[]:
    print(-1)
else:
    print(brick_destroy)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
a = list(map(int,input().split()))
count = 0
k = 1
for i in range(N):
    if a[0] != k:
        a.pop(0)
        count += 1
    else:
        a.pop(0)
        k += 1
if k == 1:
    print(-1)
else:
    print(count)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
a = list(map(int,input().split()))
count = 0
tmp = 0
while(1):
    if a[0] == 1:
        tmp = a[0]
        break
    else:
        count += 1
        a.pop(0)
        if not a:
            break
if tmp == 1:
    for i in range(1,N-count):
        if a[i] == tmp + 1:
            tmp = a[i]
        else:
            count += 1
    print(count)
else:
    print(-1)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
a=list(map(int,input().split()))
for i in range(n):
    while len(a)>i:
        if a[i]!=i+1 and a!=[]:
            a=a[:i]+a[i+1:]
        else:
            break
    if a==list(range(1,i+1)) or a==[]:
        #print('Yay!')
        break
#print(a)
print(n-len(a) if a!=[] else -1)

Submission status: Time Limit Exceeded

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

if not 1 in A:
    print(-1)
    sys.exit()

i_min = A.index(1)
m = 1
cnt = 1

for i in range(i_min, len(A)):
    
    if m + 1 in A[i_min + 1:]:
        i_min = A.index(m + 1)
        m += 1
        cnt += 1
        
    if i_min == 0:
        break

print(len(A) - cnt)

Submission status: Time Limit Exceeded

Solution code:
def myAnswer(N:int,A:list) -> int:
   for bit in range(2**N - 1):
      i = 1
      counter = 0
      for n in range(N): 
         if(bit & (1<<n)):
            counter += 1
            continue
         else:
            if( A[n] != i):
               break
            i+=1

      else:
         return counter
   return -1


def modelAnswer():
   tmp=1
def main():
   N = int(input())
   A = list(map(int,input().split()))
   print(myAnswer(N,A[:]))

if __name__ == '__main__':
   main()

Submission status: Time Limit Exceeded

Solution code:
# need to solve this by C++ to avoid TLE

import sys
si = sys.stdin.readline
so = sys.stdout.write


def main():
    n = int(si())
    l = [int(e) for e in si().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:
num=list(map(int, input().split()))
num=list(map(int, input().split()))
n=1
count=0
while True:
    if len(num)==n-1:
        print(count)
        break
    elif num[n-1]==n:
        n+=1
    elif len(num)==1 & n==1:
        print("-1") 
        break
    else:
        num.pop(n-1)
        count+=1

Submission status: Time Limit Exceeded

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

now = 1
ans = 0

for i in range(n):
  if a[now-1] != now:
    del a[0]
    ans += 1
  elif a[now-1] == now:
    now += 1

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

Submission status: Time Limit Exceeded

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

now = 1
ans = 0

for i in range(n):
  if a[now-1] != now:
    del a[0]
    ans += 1
  elif a[now-1] == now:
    now += 1

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

Submission status: Time Limit Exceeded

Solution code:
# 初期入力
N = int(input())
#A = list(map(int, input().split()))
A = list(map(int, input().split()))
#　1の左側は砕く、以降数字が大きくなるほど右側にあればいいが、順番が違うと砕く必要あり。
from copy import deepcopy
x = 0
y =0
z = 0
for i in range(1,N+1):
    if i in A[x:]:
        y = A[x:].index(i ) 
        #z.append(y)
        z +=1
        x += y
        #if y==0:
         #   break

    else:
        y = -1
        break
#
renga_kudaku = 0
if z ==0 and y==-1:
    renga_kudaku = -1
elif z ==0:
    renga_kudaku = 0
else:
    renga_kudaku=N-z # 求める物＝全体ー残る要素数、zは残る文字の配列
print(renga_kudaku)

Submission status: Time Limit Exceeded

Solution code:
# 初期入力
N = int(input())
#A = list(map(int, input().split()))
A = list(map(int, input().split()))
#　1の左側は砕く、以降数字が大きくなるほど右側にあればいいが、順番が違うと砕く必要あり。
from copy import deepcopy
x = 0
y =0
z = []
for i in range(1,N+1):
    if i in A[x:]:
        y = A[x:].index(i ) 
        z.append(y)
        x += y
        #if y==0:
         #   break

    else:
        y = -1
        break
#
renga_kudaku = 0
if len(z) ==0 and y==-1:
    renga_kudaku = -1
elif len(z) ==0:
    renga_kudaku = 0
else:
    renga_kudaku=N-len(z) # 求める物＝全体ー残る要素数、zは残る文字の配列
print(renga_kudaku)

Submission status: Time Limit Exceeded

Solution code:
# 初期入力
N = int(input())
#A = list(map(int, input().split()))
A = list(map(int, input().split()))

#　1の左側は砕く、以降数字が大きくなるほど右側にあればいいが、順番が違うと砕く必要あり。
from copy import deepcopy
#B =deepcopy(A)
#B = "".join(A)
x = 0
y =0
z = []
for i in range(1,N+1):
    if i in A:
        y = A.index(i ) 
        z.append(y)
        x = y
        del A[:x] 
    else:
        y = -1
        break
#
renga_kudaku = 0
if len(z) ==0 and y==-1:
    renga_kudaku = -1
elif len(z) ==0:
    renga_kudaku = 0
else:
    renga_kudaku=sum(z)-len(z) +1 +(len(A)-1) # 1の左側はリストの「０」あるので＋１、len(B)-1=最後の「B]の右側の文字数
print(renga_kudaku)

Submission status: Time Limit Exceeded

Solution code:
# 初期入力
N = int(input())
#A = list(map(int, input().split()))
A = list(map(str, input().split()))

#　1の左側は砕く、以降数字が大きくなるほど右側にあればいいが、順番が違うと砕く必要あり。
from copy import deepcopy
#B =deepcopy(A)
B = "".join(A)
x = 0
y =0
z = []
for i in range(1,N+1):
    y = B.find(str(i) ) 
    if y ==-1:
        break
    else:
        z.append(y)
        x = y
        B_list =[i for i in B] # 文字を消すために文字→リスト
        del B_list[:x] 
        B ="".join(B_list) # 再度リスト→文字（・・・悲しい作業）

#
renga_kudaku = 0
if len(z) ==0 and y==-1:
    renga_kudaku = -1
elif len(z) ==0:
    renga_kudaku = 0
else:
    renga_kudaku=sum(z)-len(z) +1 +(len(B)-1) # 1の左側はリストの「０」あるので＋１、len(B)-1=最後の「B]の右側の文字数
print(renga_kudaku)

Submission status: Time Limit Exceeded

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

i = 1
j = 0

while j < N and i in set(a[j:]):
    j = a[j:].index(i) + j
    # print(i, j)
    i += 1

if 1 in set(a):
    print(N - i + 1)
else:
    print(-1)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
a = list(map(int, input().split()))
ans = 0
i = 1
while a!=[]:
    if a[0]==i:
        i += 1
    else:
        ans += 1
    del a[0]
print(-1 if i==1 else ans)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
nums=input().rstrip().split(" ")
#print(nums)
count=0
for num in nums:
    if num!="1":
        count+=1
if count==n:
    print("-1")
i=1
while nums!=[]:
    for j in range(len(nums)):
        if int(nums[j])!=i:
            nums[j]="0"
            #print(nums)
        else:
            nums=[i for i in nums if i!="0"]
            if nums.count(str(i))>=1:
                nums.remove(str(i))
                i=i+1
                #print(nums)
                #print(i)
            break
        
'''nums=[i for i in nums if i!="0"]
if nums.count(str(i))>=1: nums.remove(str(i))
    i=i+1'''
#print(nums)
#print(i)
ans=n-i+1
print(ans)

Submission status: Time Limit Exceeded

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

if 1 not in a:
    print(-1)
else:   
    for i in range(1,n+1):
        if i in a[k:]:
            j = j + 1
            k = a[k:].index(i) + k
        else:
            break
    print(n-j)

Submission status: Time Limit Exceeded

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

ans=0
p=0

while p!=len(a):
	if a[p]==p+1:
		p=p+1
	else:
		del a[p]
		ans=ans+1
if p==0:
	if len(a)!=0:
		print(0)
	else:
		print(-1)
else:
	print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
a = []
a = list(map(int,input().split()))
index = 0
count = 0
searchNum = 1
for j in range(N):
    if not 1 in a:
        count = -1
        break
    if a[j] ==searchNum:
        searchNum += 1
        continue
    else:
        count +=1
print(count)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
a = []
a = list(map(int,input().split()))
index = 0
count = 0
searchNum = 1
for j in range(N+1):
    if searchNum ==1:
        if not searchNum in a:
            searchNum = N +1
            break
    if not searchNum in a[index:]:
        searchNum -= 1
        break
    else:
        index = a[index:].index(searchNum)
        searchNum += 1
        continue

print(int(N -searchNum))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
a = []
a = list(map(int,input().split()))
index = 0
count = 0
searchNum = 1
for j in range(N+1):
    if searchNum ==1:
        if not searchNum in a:
            searchNum = N +1
            break
    if not searchNum in a[index:N]:
        searchNum -= 1
        break
    if j + index < N:
        if a[index + j] == searchNum:
            index = index + j
            searchNum += 1
            continue
    else:
        break

print(int(N -searchNum))

Submission status: Time Limit Exceeded

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

for i in range(N):
    k = a.pop(0)
    if k == K + 1:
        K = k
    else:
        ans += 1

if K == 0:
    ans = -1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
a = list(map(int, input().split()))
count = 0
if 1 not in a:
    print(-1)
else:
    count += a.index(1)
    prev_index = a.index(1)
    for i in range(2,N+2):
        if i not in a[prev_index:N]:
            count += N - (prev_index + 1)
            print(count)
            break
        else:
            two_ago_index = prev_index
            prev_index = a.index(i,two_ago_index)
            count += prev_index - two_ago_index -1

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
a = list(map(int, input().split()))
count = 0
if 1 not in a:
    print(-1)
else:
    count += a.index(1)
    prev_index = a.index(1)
    for i in range(2,N+2):
        if i not in a[prev_index:N]:
            count += N - (prev_index + 1)
            print(count)
            break
        else:
            count += a.index(i,prev_index) - prev_index -1
            two_ago_index = prev_index
            prev_index = a.index(i,prev_index)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
a = list(map(int, input().split()))
count = 0
if 1 not in a:
    print(-1)
else:
    count += a.index(1)
    prev_index = a.index(1)
    for i in range(prev_index):
        a[i] = 0
    for i in range(2,N+2):
        if i not in a:
            count += N - (prev_index + 1)
            print(count)
            break
        else:
            count += a.index(i) - prev_index -1
            two_ago_index = prev_index
            prev_index = a.index(i)
            for j in range(two_ago_index,prev_index+1):
                a[j] = 0

Submission status: Time Limit Exceeded

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


for i in range(n+1):
    #k=a.index(i+1)
    #print(k,i)
    try:
        k=a.index(i+1)
        a=a[k+1:]
        #print(a)
    except:
        if i==0:
            print(-1) 
            break
        else:
            print(n-i)
            break

Submission status: Time Limit Exceeded

Solution code:
n = int(input())

tmp = [int(s) for s in input().split()]

A = {}
for i in range(n):
    A[i] = tmp[i]

#print(A)
def solve(A,n):
    keys = [k for k, v in A.items() if v == 1]
    
    if(len(keys) == 0):
        print(-1)
        return
    else:
        index = min(keys)
    
    #print(index)
    val = 2
    while True:
        #print(val,index)
        keys = [k for k, v in A.items() if v == val]
        if(len(keys) != 0):
            #print(min(keys))
            tmp = [s for s in keys if(s >= index)]
            if(len(tmp) != 0):
                index = min(tmp)
                val += 1
            else:
                #print("hi")
                val -= 1
                break
        else:
            
            val -= 1
            break
    #print(val,index)
    print(n-val)

solve(A,n)

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3
import sys


def solve(N: int, a: "List[int]"):
    if 1 not in a:
        print(-1)
        return
    ans = 0
    for i in range(1, N):
        try:
            idx = a.index(i)
            a = a[idx:]
            ans += idx if i == 1 else idx - 1
        except Exception:
            ans += len(a) - 1
            break
    print(ans)
    return


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    a = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    solve(N, a)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3
import sys


def solve(N: int, a: "List[int]"):
    if 1 not in a:
        print(-1)
        return
    ans = 0
    for i in range(1, N):
        try:
            idx = len(a) - 1 - a[::-1].index(i)
            a = a[idx:]
            ans += idx if i == 1 else idx - 1
        except Exception:
            ans += len(a) - 1
            break
    print(ans)
    return


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    a = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    solve(N, a)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
def search_index(a, count):
    for i in range(len(a)):
        if a[i] == count:
            return i
    raise ValueError

n = int(input())
a = [int(i) for i in input().split()]
count = 1
while a:
    try:
        index = search_index(a, count)
        count += 1
        a = a[index:]
    except ValueError:
        if count == 1:
            ans = -1
        else:
            ans = n - (count - 1)
        a = []
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
a = list(map(int, input().split()))
count = 1
broke = 0
suceed = False
while True:
    if count in a:
        suceed = True
        broke += a.index(count)
        a = a[a.index(count) + 1:]
        count += 1
    else:
        broke += len(a)
        break
if suceed:
    print(broke)
else:
    print(-1)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
a=list(map(int,input().split()))
x=1
for i in range(n):
  if a[0]==x:
    x+=1
  a.pop(0)

if x==1:
  ans=-1
else:
  ans=n-x+1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())

nums = list(map(int, input().split()))
l = [int(x) for x in range(1, n + 1)]

i = 1
bb = 0
ANS = 1
while True:
    if (i == 1) and (nums == l):
        ANS = 0
        break
    if i in nums:
        i_index = nums.index(i)
        bb += len(nums[:i_index])
        nums = nums[i_index + 1:]
        i += 1
    else:
        bb += len(nums)
        break

if ((bb > 0) and bb != n) or ((ANS == 0)):
    print(bb)
else:
    print(-1)

Submission status: Time Limit Exceeded

Solution code:
import sys
N=int(input())
a = list(map(int,input().split()))
cnt=0
j=0
if((1 in a)==False):
    print(-1)
    sys.exit()
i=1
while(a):
    if(a[0]!=i):
        cnt+=1
        a.pop(0)
    elif(a[0]==i):
        i+=1
        a.pop(0)


print(cnt)

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**6)

def main():
    n = int(input())
    a = [int(_) for _ in input().split()]
    idx = -1
    ans = n

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

    if ans == n:
        ans = -1

    print(ans)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**6)

def main():
    n = int(input())
    a = [int(_) for _ in input().split()]
    idx = -1
    ans = n

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

    if ans == n:
        ans = -1

    print(ans)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

i=1
count=0

if 1 in a:
    while len(a)>0:
        if i in a:
            del a[:a.index(i)]
            i+=1

        else:
            break
    #print(i)

    print(N-(i-1))
else:
    print(-1)

Submission status: Time Limit Exceeded

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

i=0
count=0

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

Submission status: Time Limit Exceeded

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

ans = 0
i = 0
while i < len(A):
    if (i + 1) == A[i]:
        i += 1
    else:
        A.pop(i)
        ans += 1

if A:
    print(ans)
else:
    print(-1)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = [int(i) for i in input().split()]
count = 1
while a:
    try:
        index = a.index(count)
        count += 1
        a = a[index:]
    except ValueError:
        if count == 1:
            ans = -1
        else:
            ans = n - (count - 1)
        a = []
print(ans)

Submission status: Time Limit Exceeded

Solution code:
def main():
    n=int(input())
    aa=list(map(int,input().split()))
    i=1
    while True:
        try:
            j=aa.index(i)
        except ValueError:
            break
        aa=aa[j+1:]
        i+=1
    if i<=1:
        print(-1)
    else:
        print(n-i+1)

main()

Submission status: Time Limit Exceeded

Solution code:
N = int(input())

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

count = 0
S = 0
while count <= N:
    for i in range(S,N):
        try:
            if line[i] == i + 1:
                S = i
            else:
                line.pop(i)
                break
        except:
            break
    count += 1


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

Submission status: Time Limit Exceeded

Solution code:
N = int(input())

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

count = 0
while count <= N:
    for i in range(N):
        try:
            if line[i] == i + 1:
                True
            else:
                line.pop(i)
                break
        except:
            break
    count += 1


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

Submission status: Time Limit Exceeded

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

num = 1
pos = 0
count = 0

while pos < len(a_list):
    if num != a_list[pos]:
        del a_list[pos]
        count += 1
    else:
        num += 1
        pos += 1

if len(a_list) == 0:
    print(-1)
else:
    print(count)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
a=[int(s) for s in input().split()]
if(((1 in a)==False)or(a.index(1)==n-1 and n>1)):
    print(-1)
else:
    i=1
    while(i in a):
        s=a.index(i)
        a = a[s+1:]
        i=i+1
    print(n-i+1)

Submission status: Time Limit Exceeded

Solution code:
def snk_check(lst):
    flag = 0
    if len(lst) == 1:
        return lst

    for i in range(len(lst) - 1):
        if lst[i] + 1 != lst[i+1]:
            del lst[i+1]
            flag = 1
            break
    if flag == 1:
        ans = lst[i:]  # breakした = 不合格、再検査が必要
    else:
        ans = [lst[-1]]  # breakせずに完走した = 合格,末尾がリスト長と一致するので、それだけ入ってるリストを返す
    return ans


def my_index(l, x, default=None):
    if x in l:
        return l.index(x)
    else:
        return default


def cut_before_first_one(lst):
    place = my_index(lst, 1)
    if place is not None:
        return lst[place:]
    else:
        return None


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

    cut_list = cut_before_first_one(An)
    if cut_list is not None:
        snk_safe_list = []
        while len(snk_safe_list) != 1:
            snk_safe_list = snk_check(cut_list)
        print(len(An) - snk_safe_list[0])
    else:
        print(-1)


main()

Submission status: Time Limit Exceeded

Solution code:
# coding:utf-8

import sys
import math
import time
import numpy as np
import collections
from collections import deque
import queue
import copy

# 最大公約数
def gcd(m,n):
  x = max(m,n)
  y = min(m,n)
  if x%y == 0:
    return y
  else:
    while x%y != 0:
      z = x%y
      x = y
      y = z
    else:
      return z

# 幅優先探索(BFS)でスタート座標(x, y)からの最大距離返す
def bfs(SS, x, y, H, W):#入力(座標系(迷路), x, y, xの端, yの端)
  x_next = [0, 0, -1, 1]
  y_next = [1, -1, 0, 0]
  tempx = x
  tempy = y
  mlength = 0
  q = deque()
  xytuple = (x, y, 0)
  q.append(xytuple)


  while q:
    xytuple = q.popleft()
    SS[xytuple[0]][xytuple[1]] = "x" #探索した場所は探索できないようにする
    for i in range(4): #このforで上下左右を調べてる
      tempx = xytuple[0] + x_next[i]
      tempy = xytuple[1] + y_next[i]
      #print(tempx, tempy, xytuple[2])
      #print(S)
      if(0 <= tempx and tempx < H and 0 <= tempy and tempy < W and SS[tempx][tempy] == "."):
        temptuple = (tempx, tempy, xytuple[2]+1)
        SS[temptuple[0]][temptuple[1]] = "x" #探索した場所は探索できないようにする
        mlength = max(mlength, xytuple[2] +1)
        q.append(temptuple)
      #print(q)
  return mlength

def brick(a,n,m,N):#ABC 148
  #n番目以降にmがあるか調べる
  if not m in a[n:]:
    print(N-(m-1))
  else:
    b = copy.deepcopy(a)
    brick(b,b[n:].index(m),m+1,N)

#X = str(input()).split()
N = int(input())
A = str(input()).split()
a = []

for i in range(N):
  a.append(int(A[i]))

if not 1 in a:
  print(-1)
else:
  b = copy.deepcopy(a)
  brick(b,b.index(1),2,N)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A_list = list(map(int, input().split()))
i = 0
while(True):
    try:
        if A_list[i] != i+1:
            del A_list[i]
        else:
            i += 1
    except:
        if A_list == []:
            print(-1)
        else:
            print(N-len(A_list))
        exit()

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
L=input().split()
L=list(map(int,L))
# print(N)
# print(L)
# print("")
count=0
i=0
while i+1 in L:
        K=L.index(i+1)
        # print(i+1)
        # print("は")
        # print(K+1)
        # print("番目")
        # print(L)
        # print("↓")
        L=L[K+1:]
        # print(L)
        count+=1
        # print("count")
        # print(count)
        # print("")
        i+=1
if count==0:
    print(-1)
else:
    print(N-count)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
def t(a):
    b = 0
    n = 0
    while n < len(a):
        if a[n] == n + 1:
            n += 1
        else:
            a.pop(n)
            b += 1
    return b if len(a) != 0 else -1


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

Submission status: Time Limit Exceeded

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

if a.count(1) == 0:
    print(-1)
else:
    i = 1
    idx = 0
    while idx < n:
        if i in a[idx:]:
            idx += a[idx:].index(i) + 1
            i += 1
        else:
            break

    print(n-i+1)

Submission status: Time Limit Exceeded

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

i = 0
while i < len(a):
    if a[i] != i+1:
        a.remove(a[i])
        continue
    
    i += 1

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
n=int(input())
a=list(map(int,input().split()))
b=len(a)
c=0
a=np.array(a)
for i in range(1,n+1):
  for j in range(n):
    if len(a)==0:
      break
    elif a[j]==i:
      a=np.delete(a,slice(0,j+1))
      c+=1
      break
    elif j+1==len(a):
      break
  if c!=i:
    break
if c==0:
  print(-1)
else:
  print(b-c)

Submission status: Time Limit Exceeded

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

if not 1 in A:
    res = -1
else:
    res = 0
    for n in range(1, N+1):
        for i, a in enumerate(A):
            if a == n:
                break
            else:
                res += 1
        A = A[i+1:]
print(res)