Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Time Limit Exceeded):
from functools import reduce


def gcd(v1, v2):
    v3 = v1 % v2
    if v3 == 0:
        return v2
    return gcd(v2, v3)


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

    max_gcd = max(reduce(gcd, A[1:]), reduce(gcd, A[:N-1]))

    for i in range(1, N - 1):
        v1 = reduce(gcd, A[:i])
        v2 = reduce(gcd, A[i + 1:])

        #print(A[:i], A[i+1:])

        max_gcd = max(max_gcd, gcd(v1, v2))

    #print(reduce(gcd, A))

    print(max_gcd)


if __name__ == "__main__":
    main()


Fixed submission:
from functools import reduce


def gcd(v1, v2):
    v3 = v1 % v2
    if v3 == 0:
        return v2
    return gcd(v2, v3)


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

    max_gcd = max(reduce(gcd, A[1:]), reduce(gcd, A[:N-1]))

    left = [1] * N
    left[0] = A[0]
    for i in range(1, N):
        left[i] = gcd(A[i], left[i - 1])

    right = [1] * N
    right[N-1] = A[N-1]
    for i in range(N - 2, -1, - 1):
        right[i] = gcd(A[i], right[i + 1])

    # print(left)
    # print(right)

    max_gcd = max(right[1], left[N-2])
    for i in range(1, N - 1):
        max_gcd = max(max_gcd, gcd(left[i - 1], right[i + 1]))

    print(max_gcd)


if __name__ == "__main__":
    main()
Buggy python submission (verdict: Runtime Error):
H, W = map(int, input().split())
h, w = map(int, input().split())

printf((H - h) * (W - w))

Fixed submission:
H, W = map(int, input().split())
h, w = map(int, input().split())

print((H - h) * (W - w))
Buggy python submission (verdict: Runtime Error):
n, k = map(int, input().split())
h = list(map(int, input().split()))

dp = [0] * n

for i, hi in enumerate(h):
    s = max(i-k,0)
    dp[i] = min(dpj + abs(hi - hj) for dpj, hj in zip(dp, h):
    
print(dp[-1])


Fixed submission:
n, k = map(int, input().split())
h = list(map(int, input().split()))

dp = [0] * n

for i in range(1, n):
    hi = h[i]
    s = max(i-k,0)
    dp[i] = min(dpj + abs(hi - hj) for dpj, hj in zip(dp[s:i], h[s:i]))
    
print(dp[-1])

Buggy python submission (verdict: Time Limit Exceeded):
n, k = map(int, input().split())
h = list(map(int, input().split()))

dp = [0] * n

for i in range(1, n):
    hi = h[i]
    dp[i] = min(dp[j] + abs(hi - h[j]) for j in range(max(i - k, 0), i))

print(dp[n-1])


Fixed submission:
n, k = map(int, input().split())
h = list(map(int, input().split()))

dp = [0] * n

for i in range(1, n):
    hi = h[i]
    s = max(i-k,0)
    dp[i] = min(dpj + abs(hi - hj) for dpj, hj in zip(dp[s:i], h[s:i]))
    
print(dp[-1])

Buggy python submission (verdict: Time Limit Exceeded):

n, k = map(int, input().split())
h = list(map(int, input().split()))

INF = 10 ** 20
dp = [INF] * n
dp[0] = 0

for i in range(1, n):
    hi = h[i]
    for j in range(max(i - k, 0), i):
        dp[i] = min(dp[i], dp[j] + abs(hi - h[j]))

print(dp[n-1])


Fixed submission:
n, k = map(int, input().split())
h = list(map(int, input().split()))

dp = [0] * n

for i in range(1, n):
    hi = h[i]
    s = max(i-k,0)
    dp[i] = min(dpj + abs(hi - hj) for dpj, hj in zip(dp[s:i], h[s:i]))
    
print(dp[-1])

Buggy python submission (verdict: Time Limit Exceeded):
n, k = [int(v) for v in input().split()]
h = [int(v) for v in input().split()]

INF = 10 ** 15
dp = [INF] * n
dp[0] = 0

for i in range(n - 1):
    for j in range(i + 1, min(i + k+1, n)):
        dp[j] = min(dp[j], dp[i]+abs(h[i] - h[j]))

print(dp[n - 1])


Fixed submission:
n, k = map(int, input().split())
h = list(map(int, input().split()))

dp = [0] * n

for i in range(1, n):
    hi = h[i]
    s = max(i-k,0)
    dp[i] = min(dpj + abs(hi - hj) for dpj, hj in zip(dp[s:i], h[s:i]))
    
print(dp[-1])
Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline

n, w = map(int, input().split())

dp = [0] * (w + 1)

for _ in range(n):
    wi, vi = map(int, input().split())

    for wj in range(w, wi - 1, -1):
        nv = dp[wj - wi] + vi
        if dp[wj] < nv:
            dp[wj] = nv

print(max(dp))


Fixed submission:
import sys
input = sys.stdin.readline

def main():
    n, w = map(int, input().split())

    dp = [0] * (w + 1)

    for _ in range(n):
        wi, vi = map(int, input().split())

        for wj in range(w, wi - 1, -1):
            nv = dp[wj - wi] + vi
            if dp[wj] < nv:
                dp[wj] = nv

    print(max(dp))

main()

Buggy python submission (verdict: Time Limit Exceeded):
n, w = map(int, input().split())

dp = [0] * (w + 1)

for _ in range(n):
    wi, vi = map(int, input().split())

    for wj in range(w, wi - 1, -1):
        nv = dp[wj - wi] + vi
        if dp[wj] < nv:
            dp[wj] = nv

print(max(dp))


Fixed submission:
import sys
input = sys.stdin.readline

def main():
    n, w = map(int, input().split())

    dp = [0] * (w + 1)

    for _ in range(n):
        wi, vi = map(int, input().split())

        for wj in range(w, wi - 1, -1):
            nv = dp[wj - wi] + vi
            if dp[wj] < nv:
                dp[wj] = nv

    print(max(dp))

main()
Buggy python submission (verdict: Runtime Error):
def main():

    n, w = map(int, input().split())

    INF = 10 ** 20
    dp = [INF] * (n * 1000)
    dp[0] = 0

    max_vi = 0
    for _ in range(n):
        wi, vi = map(int, input().split())
        max_vi += vi

        for vj in range(max_vi + 1, vi - 1, -1):
            nw = dp[vj - vi] + wi
            if dp[vj] > nw:
                dp[vj] = nw

    for i in range(max_vi, -1, -1):
        if dp[i] <= w:
            print(i)
            return

main()


Fixed submission:
def main():

    n, w = map(int, input().split())

    INF = 10 ** 20
    dp = [INF] * (n * 100001)
    dp[0] = 0

    max_vi = 0
    for _ in range(n):
        wi, vi = map(int, input().split())
        max_vi += vi

        for vj in range(max_vi + 1, vi - 1, -1):
            nw = dp[vj - vi] + wi
            if dp[vj] > nw:
                dp[vj] = nw

    for i in range(max_vi, -1, -1):
        if dp[i] <= w:
            print(i)
            return

main()

Buggy python submission (verdict: Runtime Error):
def main():

    n, w = map(int, input().split())

    INF = 10 * 20
    dp = [INF] * (n * 1000)
    dp[0] = 0

    max_vi = 0
    for _ in range(n):
        wi, vi = map(int, input().split())
        max_vi += vi

        for vj in range(max_vi + 1, vi - 1, -1):
            nw = dp[vj - vi] + wi
            if dp[vj] > nw:
                dp[vj] = nw

    for i in range(max_vi, -1, -1):
        if dp[i] <= w:
            print(i)
            return

main()


Fixed submission:
def main():

    n, w = map(int, input().split())

    INF = 10 ** 20
    dp = [INF] * (n * 100001)
    dp[0] = 0

    max_vi = 0
    for _ in range(n):
        wi, vi = map(int, input().split())
        max_vi += vi

        for vj in range(max_vi + 1, vi - 1, -1):
            nw = dp[vj - vi] + wi
            if dp[vj] > nw:
                dp[vj] = nw

    for i in range(max_vi, -1, -1):
        if dp[i] <= w:
            print(i)
            return

main()
Buggy python submission (verdict: Runtime Error):
def main():
    n, m = map(int, input().split())

    net = [[] for _ in range(n)]
    for _ in range(m):
        x, y = map(int, input().split())
        x -= 1
        y -= 1
        net[x].append(y)

    def longest(i, memo):
        if memo[i] >= 0:
            return memo[i]
        if len(net[i]) == 0:
            l = 0
        else:
            l = max(longest(j, memo) + 1 for j in net[i])
        memo[i] = l
        return l

    memo = [-1] * n
    l = max(longest(i, memo) for i in range(n))
    print(l)


main()


Fixed submission:
import sys
sys.setrecursionlimit(100000)


def main():
    n, m = map(int, input().split())

    net = [[] for _ in range(n)]
    for _ in range(m):
        x, y = map(int, input().split())
        x -= 1
        y -= 1
        net[x].append(y)

    def longest(i, memo):
        if memo[i] >= 0:
            return memo[i]
        if len(net[i]) == 0:
            l = 0
        else:
            l = max(longest(j, memo) + 1 for j in net[i])
        memo[i] = l
        return l

    memo = [-1] * n
    l = max(longest(i, memo) for i in range(n))
    print(l)


main()
Buggy python submission (verdict: Runtime Error):
def main():
	N, K = [int(v) for v in input().split()]
    
    H = sorted([-int(input() for _ in range(N)])
    
    print(-H[0] + H[K - 1])
    
main()



Fixed submission:
def main():
    N, K = [int(v) for v in input().split()]
    H = sorted([-int(input()) for _ in range(N)])

    min_diff = 10 ** 10
    for i in range(K - 1, N):
        min_diff = min(min_diff, -H[i - K + 1] + H[i])
    print(min_diff)

main()
Buggy python submission (verdict: Runtime Error):
if n != 0:
    p = list(map(int, input().split()))
 
result = x
if n != 0:
    for i in range(0, 1000):
        if not (x - i) in p:
            print(x - i)
            break
        if not (x + i) in p:
            print(x + i)
            break
else:
    print(x)
    

Fixed submission:
X,N= map(int,input().split())
p = list(map(int,input().split()))
for i in range(100):
  if X-i not in p:
    print(X-i)
    exit()
  if X+i not in p:
    print(X+i)
    exit()
Buggy python submission (verdict: Runtime Error):
a, v = map(int, input().split())
b, w = map(int, input().split())
t = int(input())

p=abs(a-b)
u = v-w
if d-u*t<=0:
    print("YES")

else:
    print('NO')

Fixed submission:
a, v = map(int, input().split())
b, w = map(int, input().split())
t = int(input())
d=abs(a-b)
u = v-w
if d-u*t<=0:
    print("YES")

else:
    print('NO')
Buggy python submission (verdict: Time Limit Exceeded):
import functools
import operator
N=int(input())
A=[int(x) for x in input().split()]
result = functools.reduce(operator.mul, A)
if result>10**18:
    print(-1)
else:
    print(result)

Fixed submission:
N=int(input())
A=[int(x) for x in input().split()]
A.sort(reverse=True)
result=1
if 0 in A:
    print(0)

else:
    for i in A:
        result*=i
        if result>10**18:
            print(-1)
            break
    else:
        print(result)

Buggy python submission (verdict: Time Limit Exceeded):
N=int(input())
A=[int(x) for x in input().split()]
ans=1
for i in A:
    ans*=i
if ans>10**18:
    print(-1)

else:
    print(ans)

Fixed submission:
N=int(input())
A=[int(x) for x in input().split()]
A.sort(reverse=True)
result=1
if 0 in A:
    print(0)

else:
    for i in A:
        result*=i
        if result>10**18:
            print(-1)
            break
    else:
        print(result)
Buggy python submission (verdict: Time Limit Exceeded):
N=int(input())
A=list(map(int,input().split()))
ans=[]
for i in range(len(A)):
    if A[i] in ans:
        print("NO")
        break
    else:
        ans.append(A[i])
else:
    print("YES")

Fixed submission:
N=int(input())
A=list(map(int,input().split()))
if len(set(A))==len(A):
    print('YES')
else:
    print('NO')
Buggy python submission (verdict: Runtime Error):
H,W,N=map(int,input().split())
K=max(H,W)
print(-(-N//K))

Fixed submission:
H=int(input())
W=int(input())
N=int(input())
K=max(H,W)
print(-(-N//K))
Buggy python submission (verdict: Runtime Error):
A,B=map(int,input().split())
import fraction
print(int((A*B)/fraction.gcd(A,B)))

Fixed submission:
A,B=map(int,input().split())
import fractions
print(int((A*B)/fractions.gcd(A,B)))

Buggy python submission (verdict: Runtime Error):
A,B=map(int,input().split())
import math
print(int((A*B)/math.gcd(A,B)))

Fixed submission:
A,B=map(int,input().split())
import fractions
print(int((A*B)/fractions.gcd(A,B)))
Buggy python submission (verdict: Runtime Error):
N=int(input())
X=N/1.08
if int(X*1.08)!=N:
    print(":(")
elif:
    ((int(X)+1)*1.08)==N:
        print(int(X)+1)
else:
    print(int(X))

Fixed submission:
N=int(input())
X=int(N/1.08)
C=[X-1,X,X+1]
for c in C:
    d=int(c*1.08)
    if d==N:
        print(c)
        break
else:
    print(":(")

Buggy python submission (verdict: Runtime Error):
N=int(input())
X=N/1.08
if X==int(X):
    print(int(X))
elif int(X*1.08)!=N:
    print(":(")
elif:
    ((int(X)+1)*1.08)==N:
        print(int(X)+1)
else:
    print(int(X))

Fixed submission:
N=int(input())
X=int(N/1.08)
C=[X-1,X,X+1]
for c in C:
    d=int(c*1.08)
    if d==N:
        print(c)
        break
else:
    print(":(")
Buggy python submission (verdict: Runtime Error):
S,T=map(str,input().split())
count=0
for i in range(3):
    if S[i]==T[i]:
        count+=1
print(count)

Fixed submission:
S=str(input())
T=str(input())
count=0
for i in range(3):
    if S[i]==T[i]:
        count+=1
print(count)

Buggy python submission (verdict: Runtime Error):
S,T=map(str,input().split())
count=0
for i in range(len(S)):
    if S[i]==T[i]:
        count+=1
print(count)

Fixed submission:
S=str(input())
T=str(input())
count=0
for i in range(3):
    if S[i]==T[i]:
        count+=1
print(count)
Buggy python submission (verdict: Runtime Error):
N = input()
for i in range(10):
  cnt += i * N.count(str(i))
print("Yes" if cnt % 9 == 0 else "No")

Fixed submission:
N=int(input())
if N%9==0:
  print('Yes')
else:
  print('No')
Buggy python submission (verdict: Time Limit Exceeded):
import math
K = int(input())
ans = 0
for i in range(1, K + 1):
  for j in range(1, K + 1):
    for k in range(1, K + 1):
      ans += math.gcd(i, math.gcd(j, k))
print(ans)

Fixed submission:
def gcd(a, b):
  if b == 0:
    return a
  else:
    return gcd(b, a % b)
import collections
K = int(input())
ans = 0
cnt = collections.defaultdict(int)
# count the number of combination of a, b
for a in range(1, K + 1):
  for b in range(1, K + 1):
    cnt[gcd(a, b)] += 1
for c in range(1, K + 1):
  for gcd_of_ab in cnt.keys():
    ans += gcd(gcd_of_ab, c) * cnt[gcd_of_ab]
print(ans)
Buggy python submission (verdict: Runtime Error):
A, B = map(int, input().split())
_MAX_PRICE = 1001
for n in range(1, _MAX_PRICE):
  if int(n * 0.08) == A and int(n * 0.1) == B:
    print(n)
    exit()
print(-1)c

Fixed submission:
A, B = map(int, input().split())
_MAX_PRICE = 1001
for n in range(1, _MAX_PRICE):
  if int(n * 0.08) == A and int(n * 0.1) == B:
    print(n)
    exit()
print(-1)
Buggy python submission (verdict: Runtime Error):
N,M=map(int,input().split())
SC=[input().split() for _ in range(M)]
ans=["0"]*N
for s,c in SC:
  if ans[int(s)-1]!="0" and ans[int(s)-1]!=c:
    print(-1)
    exit()
  if N>1 and s=="1" and c=="0":
    print(-1)
    exit()
  ans[int(s)-1]=c
if ans[0]=="0" and N>1:
  ans[0]=1
print("".join(ans))

Fixed submission:
N, M = map(int, input().split())
SC = [input().split() for _ in range(M)]
ans = ["0"] * N
for s, c in SC:
  # check duplicated s
  if ans[int(s) - 1] != "0" and ans[int(s) - 1] != c:
    print(-1)
    exit()
  # check impossible case
  if N > 1 and s == "1" and c == "0":
    print(-1)
    exit()
  ans[int(s) - 1] = c
# replace head
if ans[0] == "0" and N > 1:
  ans[0] = "1"
print("".join(ans))

Buggy python submission (verdict: Runtime Error):
N,M=map(int,input().split())
SC=[input().split() for _ in range(M)]
ans=["0"]*N
for s,c in SC:
  if ans[int(s)-1]!="0" and ans[int(s)-1]!=c:
    print(-1)
    exit()
  if N>1 and s=="1" and c=="0":
    print(-1)
    exit()
  ans[int(s)-1]=c
if ans[0]=="0" and N>1:
  ans[0]=1
print("".join(ans))

Fixed submission:
N, M = map(int, input().split())
SC = [input().split() for _ in range(M)]
ans = ["0"] * N
for s, c in SC:
  # check duplicated s
  if ans[int(s) - 1] != "0" and ans[int(s) - 1] != c:
    print(-1)
    exit()
  # check impossible case
  if N > 1 and s == "1" and c == "0":
    print(-1)
    exit()
  ans[int(s) - 1] = c
# replace head
if ans[0] == "0" and N > 1:
  ans[0] = "1"
print("".join(ans))
Buggy python submission (verdict: Time Limit Exceeded):
N, K = map(int, input().split())
P = list(map(int, input().split()))
exp, max_exp = 0, 0
def calc_exp(p):
  # calculate expectation
  dice = list(range(1, p + 1))
  exp = float(sum(dice) / p)
  return exp
for i in range(N - K + 1):
  k, exp = 0, 0
  while k <= K - 1:
    exp += calc_exp(P[i + k])
    k += 1
  max_exp = max(max_exp, exp)
print(max_exp)

Fixed submission:
from itertools import accumulate
N, K = map(int, input().split())
P = list(map(int, input().split()))
acc = [0] + list(accumulate(P))
print((max(b - a for a, b in zip(acc, acc[K:])) + K) / 2)

Buggy python submission (verdict: Time Limit Exceeded):
N, K = map(int, input().split())
P = list(map(int, input().split()))
exp, max_exp = 0, 0
def calc_exp(p):
  # calculate expectation
  dice = []
  for a in range(p):
    dice.append(a + 1)
  exp = float(sum(dice) / p)
  return exp
for i in range(N - K + 1):
  k, exp = 0, 0
  while k <= K - 1:
    exp += calc_exp(P[i + k])
    k += 1
  max_exp = max(max_exp, exp)
print(max_exp)

Fixed submission:
from itertools import accumulate
N, K = map(int, input().split())
P = list(map(int, input().split()))
acc = [0] + list(accumulate(P))
print((max(b - a for a, b in zip(acc, acc[K:])) + K) / 2)
Buggy python submission (verdict: Runtime Error):
import sys
N, M, *PS = sys.stdin.read().strip().split()
N = int(N)
ac = [0] * N
wa = [0] * N
for p, s in zip(*[iter(PS)] * 2):
  p = int(p) - 1
  if s == "AC":
    ac[p] = 1
  elif ac[p] == 0:
    wa[p] += 1
penalties = sum(w * a else 0 for a, w in zip(ac, wa))
print(sum(ac), penalties)

Fixed submission:
N, M = map(int, input().split())
PS = [list(map(str, input().split())) for _ in range(M)]
ac = [False] * N # index means p
wa = [0] * N # index means p
wa_cnt = 0
for ps in PS:
  p = int(ps[0]) - 1 # index starts from 0
  s = ps[1]
  if not ac[p] and s == "AC":
    wa_cnt += wa[p]
    ac[p] = True
  elif s == "WA":
    wa[p] += 1
print(ac.count(True), wa_cnt)

Buggy python submission (verdict: Runtime Error):
import sys
N, M, *PS = sys.stdin.read().strip().split()
N = int(N)
ac = [0] * N
wa = [0] * N
for p, s in zip(*[iter(PS)] * 2):
  p = int(p) -1
  if s == "AC":
    ac[p] = 1
  elif ac[p] == 0:
    wa[p] += 1
penalties = sum([w or a for a, w in zip(ac, wa)])
print("{} {}".format(sum(ac),penalties)

Fixed submission:
N, M = map(int, input().split())
PS = [list(map(str, input().split())) for _ in range(M)]
ac = [False] * N # index means p
wa = [0] * N # index means p
wa_cnt = 0
for ps in PS:
  p = int(ps[0]) - 1 # index starts from 0
  s = ps[1]
  if not ac[p] and s == "AC":
    wa_cnt += wa[p]
    ac[p] = True
  elif s == "WA":
    wa[p] += 1
print(ac.count(True), wa_cnt)
Buggy python submission (verdict: Time Limit Exceeded):
A, B, K = map(int, input().split())
if A + B == K:
  print("0 0")
  exit()
while True:
  if A >= K:
    t1 = K
    K -= K
    A -= t1
  elif A > 0:
    A -= 1
    K -= 1
  elif B >= K:
    t2 = K
    K -= K
    B -= t2
  elif B > 0:
    B -= 1
    K -= 1
  if K == 0:
    break
print(str(A) + " " + str(B))

Fixed submission:
A, B, K = map(int, input().split())
if A >= K:
  print(A - K, B)
elif B >= (K - A):
  print(0, B - (K - A))
else:
  print(0, 0)

Buggy python submission (verdict: Time Limit Exceeded):
A, B, K = map(int, input().split())
if A + B == K:
  print("0 0")
  exit()
while (K > 0):
  if A >= K:
    t1 = K
    K -= A
    A -= t1
  elif A > 0:
    A -= 1
    K -= 1
  elif B >= K:
    t2 = K
    K -= B
    B -= t2
  elif B > 0:
    B -= 1
    K -= 1
print(str(A) + " " + str(B))

Fixed submission:
A, B, K = map(int, input().split())
if A >= K:
  print(A - K, B)
elif B >= (K - A):
  print(0, B - (K - A))
else:
  print(0, 0)

Buggy python submission (verdict: Time Limit Exceeded):
A, B, K = map(int, input().split())
if A + B == K:
  print("0 0")
  exit()
while (K > 0):
  if A > 0:
    A -= 1
  elif B > 0:
    B -= 1
  K -= 1
print(str(A) + " " + str(B))

Fixed submission:
A, B, K = map(int, input().split())
if A >= K:
  print(A - K, B)
elif B >= (K - A):
  print(0, B - (K - A))
else:
  print(0, 0)

Buggy python submission (verdict: Time Limit Exceeded):
A, B, K = map(int, input().split())
while (K > 0):
  if A > 0:
    A -= 1
  elif B > 0:
    B -= 1
  K -= 1
print(str(A) + " " + str(B))

Fixed submission:
A, B, K = map(int, input().split())
if A >= K:
  print(A - K, B)
elif B >= (K - A):
  print(0, B - (K - A))
else:
  print(0, 0)
Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
A = list(map(int, input().split()))
# chech with no break
finish = False
def check(A, cnt=0):
  N = len(A)
  #print(A)
  for i, a in enumerate(A):
    if a != N and i != N - 1:
      continue
    if a == N and i != N - 1:
      return False
    if A[N - 1] == N:
      #print("debug")
      if cnt == 0:
        print(0)
      return True
  return False
finish = check(A)
cnt = 0
while finish is False:
  # Miss break from the large number
  #m = max(A)
  #i = A.index(m)
  i = 0
  if finish is False and len(A) > 1:
    A.pop(i) # remove
    cnt += 1
    finish = check(A, cnt)
  else:
    break
  i += 1
if finish is True and cnt != 0:
  print(cnt)
if finish is False:
  print(-1)


Fixed submission:
N = int(input())
A = list(map(int, input().split()))
cnt, i = 0, 1
for a in A:
  if a == i:
    i += 1
  else:
    cnt += 1
print(cnt if cnt != N else -1)

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
A = list(map(int, input().split()))
# chech with no break
finish = False
def check(A, cnt=0):
  N = len(A)
  #print(A)
  for i, a in enumerate(A):
    if a != N and i != N - 1:
      continue
    if a == N and i != N - 1:
      return False
    if A[N - 1] == N:
      #print("debug")
      if cnt == 0:
        print(0)
      return True
  return False
finish = check(A)
cnt = 0
while finish is False:
  # Miss break from the large number
  #m = max(A)
  #i = A.index(m)
  i = 0
  if len(A) > 1:
    A.pop(i) # remove
    cnt += 1
    finish = check(A, cnt)
  else:
    break
  i += 1
if finish is True and cnt != 0:
  print(cnt)
if finish is False:
  print(-1)


Fixed submission:
N = int(input())
A = list(map(int, input().split()))
cnt, i = 0, 1
for a in A:
  if a == i:
    i += 1
  else:
    cnt += 1
print(cnt if cnt != N else -1)