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

Solution code:
N = int( input() )
A = list( map( int, input().split() ) )
#dp = [ 10**4 for x in range(N) ]
#dp[i] i+1まで数える最短のmonotonically increasing列の位置

#if not ( 1 in A ) :
#   print(-1)
#   exit()

for i in range(N+1) :
    if i+1 in A : 
#       dp[i]=A.index(i+1)
       A = A[A.index(i+1)+1:]
    else :
       print(N-(i+1))
       exit()

      #break
#if i == 0:
#   print( -1 )
#else: 
#   print( N - i )

Submission status: Wrong Answer

Solution code:
a = int(input())
b = input().split()
l = []

b = list(map(int, b))

for i in range(1,a):

    if i in b:
        c = b.index(i)
        l.append(b[c])
        del b[0:c]

    else:
        break

c = (a - len(l))
if len(b) == len(l):
    print(0)
elif len(l) ==0:
    print('-1')
else:
    print(c)

Submission status: Wrong Answer

Solution code:
from collections import deque


def main():
    N = int(input())
    a = [int(i) for i in input().split()]
    a = deque(a)
    b = deque()

    ans = -1
    flg = 0
    for i in range(N):
        while len(a) > 1:
            if flg:
                break
            top_a = a.popleft()
            if top_a == i + 1:
                b.append(top_a)
                if len(a) == 0:
                    ans = N - len(b)
                    flg = 1
                    break
                else:
                    break

    print(ans)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
n = int(input())
a = [int(x) for x in input().split()]
res = 0
flag = False
ng = False

for n_cnt in range(0,n):
  ng = False
  for i in range(0,10 if n - n_cnt > 10 else n - n_cnt):
    if not a[i + n_cnt] == i + 1:
      ng = True
      break
  if ng:
    continue
  res = n_cnt
  flag = True

print(res if flag else -1)

Submission status: Wrong Answer

Solution code:
N = int(input())
input_nums = list(map(int, input().split()))
index_num = 0
flag = False

for i in range(N):
    try:
        nat_num_index = input_nums.index(index_num + 1)
        flag = True
    except:
        nat_num_index = 0
        
    if(nat_num_index is not 0):
        del input_nums[0:nat_num_index + 1]
        index_num += 1

if(index_num == 0 and flag == False):
    print(-1)
elif(index_num == 0 and flag == True):
    print(0)
else:
    print(N - index_num)

Submission status: Wrong Answer

Solution code:
from collections import deque


def main():
    N = int(input())
    a = [int(i) for i in input().split()]
    a = deque(a)
    b = deque()

    ans = -1
    flg = 0
    for i in range(N):
        while len(a) > 0:
            if flg:
                break
            top_a = a.popleft()
            if top_a == i + 1:
                b.append(top_a)
                if len(a) == 0:
                    ans = N - len(b)
                    flg = 1
                    break
                else:
                    break

    print(ans)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
a = int(input())
b = list(map(int, input().split()))
l = []
h = 0
z = 0
break_loop = False
for i in range(a):
    for j in range(z+1, a):
        l.append(i+1 == b[j])
        if l.count(True) > 0:
            z = j
            h += 1
            l = []
            break
        elif j == a-1 and l.count(True) == 0:
            break_loop = True
            break
    if break_loop:
        break
if break_loop:
    print(-1)
else:
    print(a-h)

Submission status: Wrong Answer

Solution code:
N = int(input())
a = list(map(int,input().split()))
o = 1
for i in range(N):
    if o in a:
        o += 1
if o == 1:
    print(-1)
elif o == 2:
    print(0)
else:
    print(o)

Submission status: Wrong Answer

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

l = len(A)
f = [0] * l


f[0] = 0
flag=0
if A[0] == 1:
    flag = 1

for i in range(1, l):
    if A[0] == 1 and f[i-1]==0:
        if A[i] == i+1:
            f[i] = 0
        else:
            f[i] = 1
    elif f[i-1] != 0:
        if A[i] == i - f[i-1] + 1:
            f[i] = f[i-1]
        else:
            f[i] = f[i-1] + 1 
    else:
        if A[i] == 1:
            f[i] = 1
        else:
            f[i] = 0

if f[l-1]==0 and flag==0:
    print(int(-1))
    exit(0)

print(f[l-1])

Submission status: Wrong Answer

Solution code:
N = int(input())
a_list = list(map(int,input().split()))
sum = 0

if N ==1 and 1 in a_list:
    print(0)

for m in range(1,N):
    #print(m)
    if m in a_list:
        k = a_list.index(m)
        print(k)
        sum += k -m + 1
        #print(sum)
        if k != 0:
            del a_list[m-1:k]
            #print(a_list)
        if m == len(a_list):
            print(sum)
            break

    elif m == 1:
        print(-1)
        break

    else:
        sum += len(a_list) - 1
        print(sum)
        break

Submission status: Wrong Answer

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

    S = sorted(set(A), key=A.index)

    if not S.__contains__(1):
        print(-1)
        return

    if len(A) == 1:
        print(0)
        return

    T = [i for i in range(1, N)]
    if A == T:
        print(0)
        return

    ans = N - len(S)
    i = 1
    while i < len(S):
        if S[i - 1] == i:
            i += 1
        else:
            ans += S.index(i)
            del S[i - 1 : S.index(i)]

    print(ans - 1)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
N = int(input())
A = [int(x) for x in input().split()]

S=0
ans =-1

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

print(ans)

Submission status: Wrong Answer

Solution code:
kazu=int(input())
mono=input().split()
n=0
cnt=1
prev=0
br=-1
for n in range(kazu):
  if mono[n]==str(cnt):
    if br==-1:
      br=0
    br=br+n-prev
    prev=n
    cnt=cnt+1
print(str(br))

Submission status: Wrong Answer

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

N = int(input())
s1 = [int(x) for x in input().split()]
Ans = set()

for i in range(1, N+1):
    for k in range(i-1,N):
        if i == s1[k]:
            Ans.add(i)
            break
    if len(Ans) != i:
        break
        
if len(Ans) == 0:
    print(-1)
elif s1 ==[1]*N and N >1:
    print(1)
else:
    print(N-len(Ans))

Submission status: Wrong Answer

Solution code:
N = int(input())
a = list(map(int,input().split()))
count = 0
start = 0
for i in range(N):
    num = i + 1
    for j in range(start,N):
        if a[j] == num:
            start = j+1
            break;
        else:
            count += 1
    if count == N:
        break

if count == N:
    print("-1")
else:
    print(count)

Submission status: Wrong Answer

Solution code:
N = int(input())
a = list(map(int, input().split()))
K = a[-1]

if K == 1:
  print(N-1)
  exit()

cnt = 1

for i in range(N-1):
  if a[i] == cnt:
    cnt += 1

if cnt == K:
  print(N-K)
else:
  print(-1)

Submission status: Wrong Answer

Solution code:
num = int(input())
input_data = input().split(' ')
if("1" in input_data):
    input_data.insert(0,1)
    ref = 1
    for i in range(len(input_data)):
        if(int(input_data[i]) - ref == 1):
            ref += 1

    print(num - ref)
else:
    print(-1)

Submission status: Wrong Answer

Solution code:
num, bricks, count, smash = int(input()), [int(x) for x in input().split(" ")], 1, 0
for x in bricks:
    if count == x: count = 1
    else: smash += 1
print(smash if smash != len(bricks) else 0 if len(bricks) == 1 else -1)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(input().split())
B = ''.join(A)
K = len(set(A))
i = 1
a = 0
b = []
while i < min(N+1,K+1):
    j = B.find(str(i))
    if j == -1:
        break
    a += j+1
    b.append(a)
    B = B[j+1:]
    if a == N:
        break
    i += 1
    
if b == []:
    print(-1)
else:
    print(N-len(b))

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
def print_ans(N, ar):
    """Test Case
    >>> print_ans(3, [2, 1, 2])
    1
    >>> print_ans(3, [2, 2, 2])
    -1
    >>> print_ans(10, [3, 1, 4, 1, 5, 9, 2, 6, 5, 3])
    7
    >>> print_ans(1, [1]) 
    0
    """
    if not(1 in ar):
        print(-1)
    else:
        index = 0
        ans = 0
        counter = 1
        flg = True
        while flg:
            try:
                index = ar.index(counter)
                ans += index
                ar = ar[index+1:]
                counter += 1
            except:
                break
        print(ans)

if __name__ == '__main__':
    N = int(input().rstrip())
    ar = list(map(int, input().rstrip().split()))
    print_ans(N, ar)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
# sys.setrecursionlimit(100000)


def main():
    N = int(input())
    a_l = str(input().rstrip())
    pos = a_l.find("1")
    if pos == -1:
        print(-1)
        sys.exit()
    counter = 1
    for i in range(2, N + 1):
        pos = a_l[pos:].find(str(i))
        if pos != -1:
            counter += 1
        else:
            break
    print(N - counter)
    sys.exit()


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import sys
N = int(input())
a_list = list(map(int, input().split())) #[3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 1]

ans = 0
value = 1

try:
    a_list.index(1)
except:
    print(-1)
    sys.exit(0)

idx = 0
value = 0

for k, v in enumerate(a_list):
    if idx <= k and value+1 == v:
        value += 1

Submission status: Wrong Answer

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

c = 0
for i in range(max(A)):
    if i+1 in set(A):
        c += 1
        A = A[A.index(i+1):]
        if max(A) == i+1:
            print(N-c)
            exit()
    else:
        print(-1)
        exit()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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


min_indexlist = []

m = min(a_list)

f = False


for i in range(len(a_list)):

    if(f==False):
        if(a_list[i] == 1):
            min_indexlist.append(i)
            f = True

    if(f==True):
        if( a_list[i] == m+1):
            m = a_list[i]
            min_indexlist.append(i)
            

#砕いたレンガの後のリスト
crash_list = []
for i in range(len(min_indexlist)):
    crash_list.append(a_list[min_indexlist[i]])

#print(crash_list)

#砕いたレンガの数
crash = n - len(min_indexlist)


frag=True

for i in range(len(crash_list)):
    if(crash_list[i] != i+1):
        frag = False


if(frag == True):
    print(crash)
elif(frag == False):
    print(-1)

Submission status: Wrong Answer

Solution code:
import numpy as np
from collections import deque
n = int(input())
a= list(map(int,input().split()))
num = [i+1 for i in range(n)]

 
cnt=0
while a!=num:
  if len(a)<=0:
    print(-1)
    exit()
  for i in range(len(a)):
    if a[i] is not num[i]:
      cnt+=1
      a.pop(i)
      num.pop(-1)
      break
#print(a)
print(cnt)

Submission status: Wrong Answer

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()
  
a=a[a.index(1):]

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

Submission status: Wrong Answer

Solution code:
N = int(input())
A = input().split()
A = [int(s) for s in A]
count = 0
if 1 not in A:
    print('-1')
else:
    for i in range(N):
        if count in A:
            count = count + 1
        else:
            break
print(N - count)

Submission status: Wrong Answer

Solution code:
a = int(input())
b = input().split()
l = []

b = list(map(int, b))

for i in range(1,a):

    if i in b:
        c = b.index(i)
        l.append(b[c])
        del b[0:c]

    else:
        continue

print(a - len(l))

Submission status: Wrong Answer

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

target_number=1
i=0

while len(numbers)>i+1:
  if(target_number in numbers):
    del numbers[i:numbers.index(target_number)]
    i+=1
    target_number+=1
  else:
    del numbers[i:]
    
  """
  if(numbers[i]!=target_number):
    numbers.pop(target_number-1)
    L-=1
  elif(numbers[i]==target_number):
    i+=1
    target_number+=1
  """

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

Submission status: Wrong Answer

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

N = int(input())
s1 = [int(x) for x in input().split()]
Ans = set()

for i in range(1, N+1):
    for k in range(i-1,N):
        if i == s1[k]:
            Ans.add(i)
            break
    if len(Ans) != i:
        break
        
if len(Ans) == 0:
    print(-1)
elif s1 ==[1]*N:
    print(1)
else:
    print(N-len(Ans))

Submission status: Wrong Answer

Solution code:
n=int(input())
a = list(map(int, input().split()))
i = 0
crash_count = 0
while True:
  if i >= len(a)-1:
    break
  if a[i] >= a[i+1]:
    del a[i]
    crash_count+=1
    if (i>=1): i-=1
    continue
  else:
    i+=1
for offset,item in enumerate(a):
  if offset+1 != item:
    print('-1')
    exit()
print(str(crash_count))

Submission status: Wrong Answer

Solution code:
kazu=int(input())
mono=input()
n=0
cnt=1
prev=0
br=-1
for n in range(kazu):
  if mono[n]==cnt:
    br=br+n-prev
    prev=n
    cnt=cnt+1
if br!=-1:
  br=br+1
print(str(br))

Submission status: Wrong Answer

Solution code:
N = int(input())
A = input().split()
A = [int(s) for s in A]
count = 1
if 1 not in A:
    print('-1')
else:
    for i in range(N):
        if count in A:
            count = count + 1
        else:
            break
print(N - count)

Submission status: Wrong Answer

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

    S = sorted(set(A), key=A.index)

    if not S.__contains__(1):
        print(-1)
        return

    if len(A) == 1:
        print(0)
        return

    ans = N - len(S)
    i = 1
    while i < len(S):
        if S[i - 1] == i:
            i += 1
        else:
            ans += S.index(i)
            del S[i - 1 : S.index(i)]

    print(ans - 1)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
N = int(input())
l = list(map(int,input().split()))
num = len(l)
l_first = min(l)
l_min = min(l)
x = l.index(l_min)

for _ in range(N):
    l = l[x+1:]
    if l_min+1 in l:
        l_min +=1
        x = l.index(l_min)
    else:
        break

if l_first == l_min:
    print(-1)
else:
    print(num-(l_min-l_first+1))

Submission status: Wrong Answer

Solution code:
n=int(input())
a = list(map(int, input().split()))
i = 0
crash_count = 0
while True:
  if i >= len(a)-1:
    break
  if a[i] >= a[i+1]:
    del a[i]
    crash_count+=1
    if (i>=1): i-=1
    continue
  else:
    i+=1
for offset,item in enumerate(a):
  if offset+1 != item:
    print('-1')
    print(a)
    exit()
print(str(crash_count))

Submission status: Wrong Answer

Solution code:
import sys
def fastio():
    from io import StringIO
    from atexit import register
    global input
    sys.stdin = StringIO(sys.stdin.read())
    input = lambda : sys.stdin.readline().rstrip('\r\n')
    sys.stdout = StringIO()
    register(lambda : sys.__stdout__.write(sys.stdout.getvalue()))
fastio()

def debug(*var):
    print(*var, file=sys.stderr)

INF = 10**20
MOD = 10**9 + 7
I = lambda:list(map(int,input().split()))
# from math import gcd
# from collections import defaultdict as dd, Counter
# from bisect import bisect_left as bl, bisect_right as br

n, = I()
a = [i-1 for i in I()]
pos = [0]*n
for i in range(n):
    pos[a[i]] = i
ans = 1
for i in range(1, n):
    if pos[i] > pos[i-1]:
        ans += 1
    else:
        break
if 0 in a:
    print(n - ans)
else:
    print(-1)

Submission status: Wrong Answer

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

prev_num = 0
target = 1
ans = -1

for i in range(N):
    if a[i] == target:
        num = i - prev_num - 1
        if ans == -1:
            ans = i
        elif ans >= 0:
            ans += num
        prev_num = i
        target += 1

ans += N - prev_num - 1

print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
kazu=int(input())
mono=input()
n=0
cnt=1
prev=0
br=-1
for n in range(kazu):
  if mono[n]==cnt:
    br=br+n-prev
    prev=n
    cnt=cnt+1
print(str(br))

Submission status: Wrong Answer

Solution code:
num = int(input())
s = input().split()
s = [int(i) for i in s]

count = 0

for i in range(num):
    if s[i]==1:
        f_point=i
        count=1
    for j in range(i+1,num):
        if s[j]==count+1:
            count = count+1
            
print(len(s)-count)

Submission status: Wrong Answer

Solution code:
max = int(input())
num = input().split(" ")
st = 0
c_st = st
i = 0

while c_st < max - 1:
  try:
    i += 1
    b_st = st
    st = num[st:].index(str(i))
    c_st = b_st + st
  except:
    print(-1)
    exit()

ans = max - i
print(ans)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))
total_breaks = 0
position = 0
for i in range(1, n + 1):
    try:
        next_position = a.index(i)
        if next_position < position:
            total_breaks = total_breaks + n - position
            break
        total_breaks = total_breaks + next_position - position
        position = next_position + 1
    except ValueError:
        if position == 0:
            total_breaks = -1
            break
        else:
            total_breaks = total_breaks + n - position
            break
print(total_breaks)

Submission status: Wrong Answer

Solution code:
import sys
def fastio():
    from io import StringIO
    from atexit import register
    global input
    sys.stdin = StringIO(sys.stdin.read())
    input = lambda : sys.stdin.readline().rstrip('\r\n')
    sys.stdout = StringIO()
    register(lambda : sys.__stdout__.write(sys.stdout.getvalue()))
fastio()

def debug(*var):
    print(*var, file=sys.stderr)

INF = 10**20
MOD = 10**9 + 7
I = lambda:list(map(int,input().split()))
# from math import gcd
# from collections import defaultdict as dd, Counter
# from bisect import bisect_left as bl, bisect_right as br

n, = I()
a = [i-1 for i in I()]
pos = [0]*n
for i in range(n):
    pos[a[i]] = i
ans = 1
for i in range(1, n):
    if pos[i] > pos[i-1]:
        ans += 1
    else:
        break
if 0 in a:
    print(ans)
else:
    print(-1)

Submission status: Wrong Answer

Solution code:
import sys

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

if 1 in a == False:
    print(-1)
    sys.exit()

res = 0
j = 1

for i in a:
    if i != j:
        res += 1
    else:
        j += 1

print(res)

Submission status: Wrong Answer

Solution code:
N=int(input())
a_list=list(map(int,input().split()))
counter=0
if 1 not in a_list:
    print(-1)
else:
    for i in range(N):
        if 1+i in a_list:
            del_n=a_list.index(1+i)
            del a_list[0:del_n]
            counter+=1
    print(N-counter)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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
            if s > n-1:
                cnt = -1
                return cnt
    return cnt

res = ers(a,cnt,s)

print(str(res))

Submission status: Wrong Answer

Solution code:
n = int(input())
a = [int(x) for x in input().split()]
a_index = a.index
c = 0
if 1 not in a:
    print(-1)
    exit()
for nn in range(1, n + 1):
    if nn in a:
        if a_index(nn) == nn -1:
            if a == range(1, nn + 1):
                break
            else:
                continue
        else:
            c += len(a[nn - 1:a_index(nn)])
            del a[nn - 1:a_index(nn)]
    else:
        c +=  len(a[nn:])
        break
print(c)

Submission status: Wrong Answer

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

N = int(input())
s1 = [int(x) for x in input().split()]
Ans = set()

for i in range(1, N+1):
    for k in range(i-1,N):
        if i == s1[k]:
            Ans.add(i)
            break
    if len(Ans) != i:
        break
if len(Ans) == 0:
    print(-1)
else:
    print(N-len(Ans))

Submission status: Wrong Answer

Solution code:
n=int(input())
a = list(map(int, input().split()))
i = 0
crash_count = 0
while True:
  if i >= len(a)-2:
    break
  if a[i] >= a[i+1]:
    del a[i]
    crash_count+=1
    if (i>=1): i-=1
    continue
  else:
    i+=1
for offset,item in enumerate(a):
  if offset+1 != item:
    print('-1')
    exit()
if crash_count >= n:
  print('-1')
  exit()
else:
  print(str(crash_count))

Submission status: Wrong Answer

Solution code:
import sys

N = int(input())
A = input().split(" ")

for i in range(N):
    if A[i] == "1":
        break
    elif i == N:
        print("-1")
        sys.exit()

counter = "1"
ans = 0
for i in range(N):
    if A[i] != counter:
        ans +=1
    else:
        counter = str(int(counter) + 1)

print(str(ans))

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int,input().split()))
aa = a
idx = []

for i in range(n):
	if i in a :
		a = a[a.index(i):]
		idx.append(a[0])

for j in range(len(idx) - 1):
	if idx[j] + 1 != idx[j + 1] :
		idx = idx[:j]
		break

ans = list(range(1, len(idx) + 1))

if ans != idx :
	print(-1)
elif not idx :
	print(0)
else :
	print(len(aa) - len(idx))

Submission status: Wrong Answer

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

c = [0] * n
j = 0
if 1 not in s:
    print(-1)
elif len(s) == 1:
    print(0)
else:
    for i in range(1,n+1):
        if i in s:
            c[i] = s.index(i)
            if c[i] == i-1:
                break
            del s[j:c[i]]
            j += 1
        elif i not in s:
            break
    print(n - len(s))

Submission status: Wrong Answer

Solution code:
N = int(input())
A = [int(x) for x in input().split()]

S=0
ans =-1

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

print(ans)

Submission status: Wrong Answer

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

count = 0

for i in range(1, 200000):
    if i in a_list:
        target_index = a_list.index(i)
        count = i
        if i == len(a_list)-1:
            break
        a_list = a_list[target_index+1:]
    else:
        break

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

Submission status: Wrong Answer

Solution code:
str = [input() for i in range(2)]
num = int(str[0])-1
A = [int(i) for i in str[1].split(" ")]
count = 0
i = 0
while i <= num:
    while A[i] != i+1:
        if i >= num:
            break
        A.pop(i)
        num -= 1
        count += 1
    i += 1

if A[0] != 1:
    count = -1
print(count)

Submission status: Wrong Answer

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

c = [0] * n
j = 0
if 1 not in s:
    print(-1)
elif len(s) == 1:
    print(0)
else:
    for i in range(1,n+1):
        if i in s:
            c[i] = s.index(i)
            if c[i] == i-1:
                break
            del s[j:c[i]]
            j += 1
        elif i not in s:
            break
print(n - len(s))

Submission status: Wrong Answer

Solution code:
N = int(input())
a = list(map(int, input().split()))
K = a[-1]
cnt = 1

for i in range(N-1):
  if a[i] == cnt:
    cnt += 1

if cnt == K:
  print(N-K)
else:
  print(-1)

Submission status: Wrong Answer

Solution code:
def input():
    import sys
    return sys.stdin.readline().rstrip()
  
def main():
  N = int(input())
  a = list(map(int, input().split()))
  index = 0
  break_ = 0
  #ans = []
  while True:
    if index == len(a) or break_ == N - 1:
      break
    elif index + 1 != a[index]: #一致しなかったら壊す
      a.pop(index)
      break_ += 1
    else: #一致したら次
      #ans.append(index + 1)
      index += 1
  print(index)
  print(break_)
  #print([i + 1 for i in range(N - break_)])
  #print(ans)
  #print(a)
  if index + break_ != N:
    if N == 1 and a[0] == 1:
      print(0)
    else:
      print(-1)
  else:
    print(break_)
    
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n=int(input())
li = list(map(int,input().split()))
count = 0
ind = 0
if(n>=len(li)):
	try:
		while True:
			if(li[ind] == ind+1):
				if(ind==len(li)-1):
					break
				ind+=1
			else:
				count+=1
				li.pop(ind)
	except:
		print(-1)
	else:
		print(count)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N = int(input())
brick_nums = input().split()

broken = 0
X = 0

for i in range(N):
    for j in range(X,N):
        if str(i+1) != "".join(brick_nums[j]):
            broken += 1
        else:
            X = j+1
            break
        
if broken >= N:
    print(-1)
else:
    print(broken)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

prev_num = 0
target = 1
ans = -1

for i in range(N):
    if a[i] == target:
        num = i - prev_num - 1
        if ans == -1:
            ans = i
        elif ans >= 0:
            ans += num
        prev_num = i
        target += 1

print(ans)

Submission status: Wrong Answer

Solution code:
import sys
def fastio():
    from io import StringIO
    from atexit import register
    global input
    sys.stdin = StringIO(sys.stdin.read())
    input = lambda : sys.stdin.readline().rstrip('\r\n')
    sys.stdout = StringIO()
    register(lambda : sys.__stdout__.write(sys.stdout.getvalue()))
fastio()

def debug(*var):
    print(*var, file=sys.stderr)

INF = 10**20
MOD = 10**9 + 7
I = lambda:list(map(int,input().split()))
# from math import gcd
# from collections import defaultdict as dd, Counter
# from bisect import bisect_left as bl, bisect_right as br

n, = I()
a = I()
pos = [0]*n
for i in range(n):
    pos[a[i]-1] = i
ans = 1
for i in range(1, n):
    if pos[i] > pos[i-1]:
        ans += 1
    else:
        break
if 0 in a:
    print(ans)
else:
    print(-1)

Submission status: Wrong Answer

Solution code:
N = int(input())
a = list(map(int, input().split()))
a = a[::-1]
temp = 0
iti = N
while iti > 0:
    temp += 1
    try:
        iti = a[:iti].index(temp)
    except ValueError:
        print(-1)
        exit()
print(N-temp)

Submission status: Wrong Answer

Solution code:
# abc148_d.py
from sys import stdin
N = int(stdin.readline().rstrip())
a = [int(x) for x in stdin.readline().rstrip().split()]
#print(a)
cnt = 0
idx = 1
flg = 1
for i in a:
	if i == idx:
		idx = idx + 1
		flg = 0
	else:
		cnt = cnt + 1
		flg = 1

if flg == 1:
	print(-1)
else:
	print(cnt)

Submission status: Wrong Answer

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

prev_num = 0
target = 1
ans = -1

for i in range(N):
    if a[i] == target:
        num = abs(i - prev_num - 1)
        if ans == -1:
            ans = i
        elif ans >= 0:
            ans += num
        prev_num = i
        target += 1

print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n = int(input())
bricks = [int(a) for a in input().split()]

result = 0
numbers = []
for i in range(1, n + 1):
  loop = len(bricks)
  for j in range(loop):
    if bricks[0] == i:
      result = result + j
      numbers.append(bricks[0])
      bricks.pop(0)
      break
    else:
      bricks.pop(0)

if len(numbers) == 0:
  print(-1)
else:
  print(result)

Submission status: Wrong Answer

Solution code:
n = int(input())
ab = input().split()
ab = [int(i) for i in ab]
f_i = 0

if ab[0] == 1:
    print(0)
    exit()
else:
    for i in range(1, n + 1):
        for ii, v in enumerate(ab):
            if v == i:
                f_i += ii
                ab = ab[ii + 1::]
                break
if f_i == 0:
    print(-1)
else:
    print(f_i)