Harryxun/GPT-100-sft-dataset huggingface dataset:
Problem p02824 (python)

Fixed submission:

N,M,V,P = map(int,input().split())
A = list(map(int,input().split()))
#%%
As = sorted(A)
border = As[-P]
upbound = N-P
lowbound = N-P
while As[upbound] == border:
    upbound += 1
    if upbound > N-1:break
while As[lowbound] == border:
    lowbound -= 1
    if lowbound < 0:break

upbound -= 1
lowbound += 1
    
def isOK(mid):
    if mid>= lowbound: return True
    if mid==N-1: return True
    if (border-As[mid])>M:
        return False
    
    S = sum(As[mid+1:upbound+1])-As[mid]*(upbound-mid)
    U = M*(N-V)
    if S>U:
        return False
    else:
        return True

def bsearch():
    ng = -1
    ok = N
    while abs(ok-ng)>1:
        mid = (ok+ng)//2
        if isOK(mid):
            ok = mid
        else :
            ng = mid
    return ok


key = bsearch()

print(N-key)

Buggy submission (verdict: Runtime Error):

N,M,V,P = map(int,input().split())
A = list(map(int,input().split()))
#%%
As = sorted(A)
border = As[-P]
upbound = N-P
lowbound = N-P
while As[upbound] == border:
    upbound += 1
while As[lowbound] == border:
    lowbound -= 1

upbound -= 1
lowbound += 1
    
def isOK(mid):
    if mid>= lowbound: return True
    if mid==N-1: return True
    if (border-As[mid])>M:
        return False
    
    S = sum(As[mid+1:upbound+1])-As[mid]*(upbound-mid)
    U = M*(N-V)
    if S>U:
        return False
    else:
        return True

def bsearch():
    ng = -1
    ok = N
    while abs(ok-ng)>1:
        mid = (ok+ng)//2
        if isOK(mid):
            ok = mid
        else :
            ng = mid
    return ok


key = bsearch()

print(N-key)
Problem p02846 (python)

Fixed submission:

T1,T2 = map(int,input().split())
A1,A2 = map(int,input().split())
B1,B2 = map(int,input().split())

d1 = A1*T1-B1*T1
d2 = A2*T2-B2*T2

import sys
if d1+d2 == 0:
    print('infinity')
    sys.exit()
elif d1*d2 > 0 or (abs(d1)-abs(d2)>0 and d1*d2<0):
    print(0)
    sys.exit()
    
else:
    start = abs(d1)
    delta = abs(d2+d1)
    ans = start//delta * 2
    if (start%delta)!=0:
        ans += 1
    print(ans)

Buggy submission (verdict: Runtime Error):

T1,T2 = map(int,input().split())
A1,A2 = map(int,input().split())
B1,B2 = map(int,input().split())

d1 = A1*T1-B1*T1
d2 = A2*T2-B2*T2

import sys
if d1+d2 == 0:
    print('infinity')
    sys.exit()
elif d1*d2 > 0 or (abs(d1)-abs(d2)>0 and d1*d2<0):
    print(0)
    sys.exit()
    
else:
    start = abs(d1)
    delta = abs(d2+d1)
    ans = start//delta * 2
    elif (start%delta)!=0:
        ans += 1
    print(ans)
Problem p02850 (python)

Fixed submission:
import sys
sys.setrecursionlimit(10**7)


N = int(input())
AB = [list(map(int,input().split())) for _ in range(N-1)]
adj = [[] for _ in range(N)]
for i in range(N-1):
    adj[AB[i][0]-1].append([AB[i][1]-1,i])
    adj[AB[i][1]-1].append([AB[i][0]-1,i])


col = [0]*(N-1)
visited = [False] * N
visited[0] = True
def dfs(v, color):
    child = adj[v]
    ctmp = 1
    for u,idx in child:
        if not(visited[u]):
            if ctmp == color:
                ctmp += 1
            col[idx] = ctmp
            visited[u] = True
            dfs(u,ctmp)
            ctmp += 1
    return 
            
    
    
    

dfs(0,0)
K = max(col)
print(K)
for k in col:
    print(k)

Buggy submission (verdict: Runtime Error):

N = int(input())
AB = [list(map(int,input().split())) for _ in range(N-1)]
adj = [[] for _ in range(N)]
for i in range(N-1):
    adj[AB[i][0]-1].append([AB[i][1]-1,i])
    adj[AB[i][1]-1].append([AB[i][0]-1,i])


col = [0]*(N-1)
visited = [False] * N
visited[0] = True
def dfs(v, color):
    child = adj[v]
    ctmp = 1
    for u,idx in child:
        if not(visited[u]):
            if ctmp == color:
                ctmp += 1
            col[idx] = ctmp
            visited[u] = True
            dfs(u,ctmp)
            ctmp += 1
    return 
            
    
    
    

dfs(0,0)
K = max(col)
print(K)
for k in col:
    print(k)
Problem p02900 (python)

Fixed submission:

A,B = map(int,input().split())
from fractions import gcd 

def make_divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)

    # divisors.sort()
    return divisors

C = gcd(A,B)
E = make_divisors(C)
E.sort()

sosuu = []
ans = 0
for e in E:
    if e == 1:continue
    for s in sosuu:
        if e%s == 0:
            break
    else:
        sosuu.append(e)

print(len(sosuu)+1)

Buggy submission (verdict: Runtime Error):
#def make_divisors(n,m):
#    divisors = []
#    for i in range(1, int(min(n,m)**0.5)+1):
#        if n % i == 0 and m%i == 0:
#            divisors.append(i)
#            if i != min(n,m) // i:
#                divisors.append(min(n,m)//i)
#
#    # divisors.sort()
#    return divisors

def make_divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
#            if i != n // i:
#                divisors.append(n//i)

    # divisors.sort()
    return divisors

C = make_divisors(A)
D = make_divisors(B)
C.sort()
D.sort()
E = []

idx1 = 0
idx2 = 0
L = min(len(C),len(D))
while idx1 < L and idx2 < L:
    if C[idx1] == D[idx2] :
        E.append(C[idx1])
        idx1 += 1
        idx2 += 1
    elif C[idx1] > D[idx2]:
        idx2 += 1
    else:
        idx1 += 1
        
        
memo = [False] * (10**6+10)
ans = 0
for e in E:
    if not(memo[e]):
        ans += 1
        j = e
        if e == 1:continue
        while j < len(memo):
            memo[j] = True
            j += e

print(ans)
Problem p02936 (python)

Fixed submission:
import sys
sys.setrecursionlimit(100000000)

import sys
input = sys.stdin.readline

N,Q = map(int,input().split())
ab = [list(map(int,input().split())) for _ in range(N-1)]
px = [list(map(int,input().split())) for _ in range(Q)]

query = [0] * N
ans = [0] * N

for p,x in px:
    query[p-1] = query[p-1] + x
    
adj_list = [[] for _ in range(N)]
for a,b in ab:
    adj_list[a-1].append(b-1)
    adj_list[b-1].append(b-1)

memo = [True] * N

def dfs(v,val):
    child = adj_list[v]
    memo[v] = False
    val += query[v]
    ans[v] = val
    for s in child:
        if memo[s]:
            dfs(s,val)
    return 
        
dfs(0,0)
print(*ans)

Buggy submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(100000000)

N,Q = map(int,input().split())
ab = [list(map(int,input().split())) for _ in range(N-1)]
px = [list(map(int,input().split())) for _ in range(Q)]

query = [[0] for _ in range(N)]
ans = [[0] for _ in range(N)]

for p,x in px:
    query[p-1] = query[p-1] + x
    
adj_list = [[] for _ in range(N)]
for a,b in ab:
    adj_list[a-1].append(b-1)
    adj_list[b-1].append(b-1)

memo = [True] * N

def dfs(v,val):
    child = adj_list[v]
    memo[v] = False
    val += query[v]
    ans[v] = val
    for s in child:
        if memo[s]:
            dfs(s,val)
        
dfs(0,0)
print(*ans)
Problem p02937 (python)

Fixed submission:

import sys
#input = sys.stdin.readline

S = list(input())
T = list(input())

alpha2num = lambda c: ord(c) - ord('a') 

alpha = [[] for _ in range(26)]
for i,s in enumerate(S):
    alpha[alpha2num(s)].append(i)

import bisect
tmp = -1
N = len(S)
base = 0
for t in T:
    a = alpha2num(t)
    if len(alpha[a]) == 0:
        print(-1)
        sys.exit()
    else:
        L = len(alpha[a])        
        num = bisect.bisect_right(alpha[a],tmp)
        
        if num == L:
            base += 1
            tmp = alpha[a][0]
        else:
            tmp = alpha[a][num]
ans = N * base + tmp + 1
print(ans)

Buggy submission (verdict: Runtime Error):

import sys
input = sys.stdin.readline

S = list(input())
T = list(input())

alpha2num = lambda c: ord(c) - ord('a') 

alpha = [[] for _ in range(26)]
for i,s in enumerate(S):
    alpha[alpha2num(s)].append(i)

import bisect
tmp = 0
N = len(S)
base = 0
for t in T:
    a = alpha2num(t)
    if len(alpha[a]) == 0:
        print(-1)
        sys.exit()
    else:
        L = len(alpha[a])        
        num = bisect.bisect_right(alpha[a],tmp)
        
        if num == L:
            base += 1
            tmp = alpha[a][0]
        else:
            tmp = alpha[a][num]
            
            
ans = N * base + tmp + 1
print(ans)
Problem p02960 (python)

Fixed submission:
S = list(input())
N = len(S)
dp = [[0] * 13 for _ in range(N+1)]
MOD = 10**9 + 7
s = S[0]
if s=='?':
    for k in range(10):
        dp[0][k] = 1
else:
    dp[0][int(s)] = 1
 

for i in range(1,N):
    s = S[i]
    if s=='?':
        for j in range(13):
            for k in range(10):
                tmp = (j*10 + k)%13
                dp[i][tmp] += dp[i-1][j]
                dp[i][tmp] %= MOD
    else:
        for j in range(13):
            tmp = (j*10 + int(s))%13
            dp[i][tmp] += dp[i-1][j]
            dp[i][tmp] %= MOD
            
print(dp[N-1][5])

Buggy submission (verdict: Runtime Error):
S = list(input())
N = len(S)
dp = [[0] * 13 for _ in range(N+1)]
MOD = 10**9 + 7
s = S[0]
if s=='?':
    for k in range(10):
        dp[0][k] = 1
else:
    dp[0][s] = 1
 

for i in range(1,N):
    s = S[i]
    for j in range(13):
        if s=='?':
            for k in range(10):
                tmp = (j*10 + k)%13
                dp[i][tmp] += dp[i-1][j]
                dp[i][tmp] %= MOD
        else:
            tmp = (j*10 + int(s))%13
            dp[i][tmp] += dp[i-1][j]
            dp[i][tmp] %= MOD
            
print(dp[N-1][5])
Problem p02972 (python)

Fixed submission:

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



NumValid = [True] * (N+1)
ans = []
NumAns = [0] * (N+1)


for i in reversed(range(1,N+1)):
    j = 2 * i
    tmp = 0
    while j < N+1:
        tmp += NumAns[j]
        j += i
        
    if tmp % 2 == 0:
        if A[i-1] == 0:
            NumAns[i] = 0
        else:
            NumAns[i] = 1
            ans.append(i)
    else:
        if A[i-1] == 0:
            NumAns[i] = 1
            ans.append(i)
        else:
            NumAns[i] = 0
        
print(len(ans))
print(' '.join(map(str,ans)))

Buggy submission (verdict: Runtime Error):

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



NumValid = [True] * (N+1)
ans = []
NumAns = [0] * (N+1)


for i in reversed(range(1,N+1)):
    j = 2 * i
    tmp = 0
    while j < N+1:
        tmp += NumAns[j]
        j += i
        
    if tmp % 2 == 0:
        if A[i] == 0:
            NumAns[i] = 0
        else:
            NumAns[i] = 1
            ans.append(i)
    else:
        if A[i] == 0:
            NumAns[i] = 1
            ans.append(i)
        else:
            NumAns[i] = 0
        
print(len(ans))
print(ans)
Problem p02973 (python)

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

N = int(input())
A = [-1*int(input()) for _ in range(N)]

maxims = [A[0]]
ans = 1
for a in A[1:]:
    if maxims[-1] > a:
        idx = bisect.bisect(maxims,a)
        maxims[idx] = a
    else:
        maxims.append(a)
        ans += 1
    
print(ans)

Buggy submission (verdict: Runtime Error):
import bisect
import sys
input = sys.stdin.readline

N = int(input())
A = [-1*int(input()) for _ in range(N)]

maxims = [A[0]]
ans = 1
minim = A[0]
for a in A[1:]:
    if maxims[-1] < a:
        idx = bisect.bisect(maxims,a)
        maxims[idx] = a
    else:
        maxims.append(a)
        ans += 1
    
print(ans)

Buggy submission (verdict: Runtime Error):

N = int(input())
A = [int(input()) for _ in range(N)]

maxims = [A[0]]
ans = 1
minim = A[0]
for a in A[1:]:
    if a > minim:
        for j,m in reversed(enumerate(maxims)):
            if a > m:
                if m == minim:
                    minim = a
                maxims[j] = a
                break
    else:
        maxims.append(a)
        ans += 1
        minim = a
print(ans)
Problem p02995 (python)

Fixed submission:
import math
from fractions import gcd


A,B,C,D = map(int,input().split())
lcm = C//gcd(C,D)*D
ans = B - B//C - B//D + B//lcm
a = A-1
ans -= a - a//C - a//D + a//lcm 

print(ans)

Buggy submission (verdict: Runtime Error):
import math
from fractions import gcd
 
def number_calc(A,B,x):
  	if A%x == 0:
      left = A//x
    else:
      left = A//x + 1
    right = B//x
    res = max(right-left+1,0)
    return res
 
A,B,C,D = map(int,input().split())
 
ans = B-A+1
ans -=number_calc(A,B,C)
ans -=number_calc(A,B,D)
lcm = (C*D)//gcd(C,D)
ans +=number_calc(A,B,lcm)
 
print(ans)

Buggy submission (verdict: Runtime Error):
import math
from fractions import gcd


A,B,C,D = map(int,input().split())
lcm = C//math.gcd(C,D)*D
ans = B - B//C - B//D + B//lcm
a = A-1
ans -= a - a//C - a//D + a//lcm 

print(ans)

Buggy submission (verdict: Runtime Error):
import math
from fractions import gcd

def number_calc(A,B,x):
    left = math.ceil(A/x)
    right = math.floor(B/x)
    res = max(right-left+1,0)
    return res

A,B,C,D = map(int,input().split())

ans = B-A+1
ans -=number_calc(A,B,C)
ans -=number_calc(A,B,D)
ans +=number_calc(A,B,(C*D)/math.gcd(C,D))

print(ans)

Buggy submission (verdict: Runtime Error):
import math
def number_calc(A,B,x):
    left = math.ceil(A/x)
    right = math.floor(B/x)
    return (right - left + 1)

A,B,C,D = map(int,input().split())

ans = B-A+1
ans -=number_calc(A,B,C)
ans -=number_calc(A,B,D)
ans +=number_calc(A,B,(C*D)/math.gcd(C,D))

print(ans)

Buggy submission (verdict: Runtime Error):
import math
A,B,C,D = map(int,input().split())


def number(A,B,x):
    left = math.ceil(A/x)
    right = math.floor(B/x)
    return (right - left + 1)
    
ans = B-A+1
ans -=number(A,B,C)
ans -=number(A,B,D)
ans +=number(A,B,(C*D)/math.gcd(C,D))

print(ans)

Buggy submission (verdict: Runtime Error):
import math
A,B,C,D = map(int,input().split())


def number(A,B,x):
    left = math.ceil(A/x)
    right = math.floor(B/x)
    return right - left + 1
    
    
    
    
ans = B-A+1
ans -=number(A,B,C)
ans -=number(A,B,D)
ans +=number(A,B,(C*D)//math.gcd(C,D))

print(ans)
Problem p03026 (python)

Fixed submission:
N = int((input()))
ab = [list(map(int,input().split())) for _ in range(N-1)]
c = list(map(int,input().split()))
c.sort(reverse = True)

al = [[] for _ in range(N+1)]
for a,b in ab:
    al[a].append(b)
    al[b].append(a)

d = [0] * (N)
d[ab[0][0]-1] = c[0]
cnt = 1
child = al[ab[0][0]]
ans = 0
Check = [True] * (N+1)
Check[ab[0][0]] = False

while child and cnt < N:
    pi = int(child.pop())
    Check[pi] = False
    d[pi-1] = (c[cnt])
    ans += c[cnt]
    for i in al[pi]:
        if Check[i]:child.append(i)
    cnt += 1
    
print(ans)
print(' '.join(map(str,d)))

Buggy submission (verdict: Runtime Error):
N = int((input()))
ab = [list(map(int,input().split())) for _ in range(N-1)]
c = list(map(int,input().split()))
c.sort(reverse = True)

al = [[] for _ in range(N+1)]
for a,b in ab:
    al[a].append(b)


d = [0] * (N)
d[ab[0][0]-1] = c[0]
cnt = 1
child = al[ab[0][0]]
ans = 0

while cnt < N:
    pi = int(child.pop())
    d[pi-1] = (c[cnt])
    ans += c[cnt]
    if al[pi]:
        child = child + al[pi]
    cnt += 1
    
print(ans)
print(' '.join(map(str,d)))
Problem p03044 (python)

Fixed submission:
N = int(input())
uvw = [list(map(int,input().split())) for _ in range(N-1)]
import sys
sys.setrecursionlimit(10000000)
#testcase = open('testcase_04','r')
#contents = testcase.read()
#contents = contents.split("\n")
#N = int(contents[0])
#uvw = [[0,0,0] for _ in range(N-1)]
#for i in range(N-1):
#    uvw[i] = [int(j) for j in contents[i+1].split()]
        


ans = [0] * N
adj_list = [[] for _ in range(N+1)]

for u,v,w in uvw:
    adj_list[u-1].append([v-1,w])
    adj_list[v-1].append([u-1,w])
    
memo = [True] * N
def dfs(s,cnt): 
    memo[s] = False
    if cnt % 2 == 1:
        ans[s] = 1
    nxt = adj_list[s]
    for v,w in nxt:
        if memo[v]:
            dfs(v,cnt+w)
    return
    
dfs(0,0)
for a in ans:
    print(a)

Buggy submission (verdict: Runtime Error):
N = int(input())
uvw = [list(map(int,input().split())) for _ in range(N-1)]
#testcase = open('testcase_04','r')
#contents = testcase.read()
#N = int(contents[0][0])
#uvw = [[int(j) for j in contents[i]]for i in range(1,N)]


ans = [0] * N
adj_list = [[] for _ in range(N+1)]

for u,v,w in uvw:
    adj_list[u-1].append([v-1,w])
    adj_list[v-1].append([u-1,w])
    
memo = [True] * N
def dfs(s,cnt): 
    memo[s] = False
    if cnt % 2 == 1:
        ans[s] = 1
    nxt = adj_list[s]
    for v,w in nxt:
        if memo[v]:
            dfs(v,cnt+w)
    return
    
dfs(0,0)
for a in ans:
    print(a)

Buggy submission (verdict: Runtime Error):

N = int(input())
uvw = [list(map(int,input().split())) for _ in range(N-1)]

ans = [0] * N
adj_list = [[] for _ in range(N)]

for u,v,w in uvw:
    adj_list[u-1].append([v-1,w])
    adj_list[v-1].append([u-1,w])
    
memo = [True] * N
def dfs(s,cnt): 
    memo[s] = False
    if cnt % 2 == 1:
        ans[s] = 1
    nxt = adj_list[s]
    for v,w in nxt:
        if memo[v]:
            dfs(v,cnt+w)
    return
    
dfs(0,0)
for a in ans:
    print(a)
Problem p03049 (python)

Fixed submission:
N= int(input())
S = [input() for _ in range(N)]

IB= [0] * (N)
LA = [0] * (N)
flag = True
Eflag = False
cntb= 0
cnta = 0
ans = 0
for i in range(N):
    if S[i][0] == 'B':
        IB[i] = 1
        cntb += 1
    if S[i][len(S[i])-1] == 'A' :
        LA[i] = 1
        cnta += 1
    if (IB[i] == 1 and LA[i] == 0) or (IB[i]==0 and LA[i] == 1):
        flag = False
    elif (IB[i] == 1 and LA[i] == 1):
        Eflag = True
    for j in range(len(S[i])-1):
        if S[i][j] == 'A' and S[i][j+1]=='B' : ans += 1
        
        
ans += min(cntb,cnta)
if Eflag and flag:    
    ans -= 1



print(ans)

Buggy submission (verdict: Runtime Error):
N= int(input())
S = [input() for _ in range(N)]

IB= [0] * (N)
LA = [0] * (N)
flag = True
Eflag = False
cntb= 0
cnta = 0
ans = 0
for i in range(N):
    if S[i][0] == 'B':
        IB[i] = 1
        cntb += 1
    if S[i][len(S[i])-1] == 'A' :
        LA[i] = 1
        cnta += 1
    if (IB[i] == 1 and LA[i] == 0) or (IB[i]==0 and LA[i] == 1):
        flag = False
    elif (IB[i] == 1 and LA[i] == 1):
        Eflag = True
    for j in range(len(S[i])):
        if S[i][j] == 'A' and S[i][j+1]=='B' : ans += 1
        
        
ans += min(cntb,cnta)
if Eflag and flag:    
    ans -= 1



print(ans)
Problem p03067 (python)

Fixed submission:
A,B,C = map(int,input().split())

if A>B:
    tmp = A
    A = B
    B = tmp
    
if C >= A and C <= B:
    print('Yes')
else:
    print('No')

Buggy submission (verdict: Runtime Error):
N = int(input())
S = input()
S = list(S)
S2 = list(S)
ans = 0
i=0
while i < N-1:
    idx = i
    if S[i] == '#' and S[i+1] == '.':
        ans += 1
        
        i += 1
        
        for j in range(1,N):
            if idx -j < 0 or idx + 1 + j > N-1 : break

            if S[idx-j] == '#' and S[idx+1+j] == '.':
                
                ans += 1
                i += 1
            else:
                break
    i += 1

print(ans)
Problem p03074 (python)

Fixed submission:
import numpy as np

N,K = map(int,input().split())
S = list(input())

State = [0] * N
counter0 = 0
counter1 = 0
flag = -1
jj = 0
for i in range(N):
    if S[i] == '0':
        if flag == 1:
            State[jj] = counter1
            jj +=1
            counter1 = 0
        counter0 += 1
        flag = 0
    if S[i] == '1':
        if flag == 0:
            State[jj] = counter0
            jj += 1
            counter0 = 0
        counter1 += 1
        flag = 1
    if i == N-1:
        State[jj] = max(counter0,counter1)
        
N2 = np.count_nonzero(State)
State = int(S[0] == '0') * [0] + State[:N2] + int(S[N-1] == '0') * [0]
N2 = len(State)
ans = 0
total = 0
K2 = 2*K+1

if K2 >= N2:
    K2 = N2

for i in range(K2):
    total += State[i]
        
ans = total
for i in range(0,N2-K2,2):
    total2 = total - State[i] - State[i+1] + State[i+K2] + State[i+1+K2]
    ans = max(ans,total2)
    total = total2
    
print(ans)

Buggy submission (verdict: Runtime Error):


N,K = map(int,input().split())
S = list(input())

State = [0] * N
counter0 = 0
counter1 = 0
flag = -1
jj = 0
for i in range(N):
    if S[i] == '0':
        if flag == 1:
            State[jj] = counter1
            jj +=1
            counter1 = 0
        counter0 += 1
        flag = 0
    if S[i] == '1':
        if flag == 0:
            State[jj] = counter0
            jj += 1
            counter0 = 0
        counter1 += 1
        flag = 1
    if i == N-1:
        State[jj] = max(counter0,counter1)
        

State = int(S[0] == '0') * [0] + State[:]
N2 = len(State)
ans = 0
total = 0
K2 = 2*K+1

if K2 >= N2:
    K2 = N2

for i in range(K2):
    total += State[i]
        
ans = total
for i in range(0,N2-K2,2):
    total2 = total - State[i] - State[i+1] + State[i+K2] + State[i+1+K2]
    ans = max(ans,total2)
    total = total2
    
print(ans)

Buggy submission (verdict: Runtime Error):
import numpy as np

N,K = map(int,input().split())
S = list(input())

State = [0] * N
counter0 = 0
counter1 = 0
flag = -1
jj = 0
for i in range(N):
    if S[i] == '0':
        if flag == 1:
            State[jj] = counter1
            jj +=1
            counter1 = 0
        counter0 += 1
        flag = 0
    if S[i] == '1':
        if flag == 0:
            State[jj] = counter0
            jj += 1
            counter0 = 0
        counter1 += 1
        flag = 1
    if i == N-1:
        State[jj] = max(counter0,counter1)
        
N2 = np.count_nonzero(State)
State = int(S[0] == '0') * [0] + State[:N2] + int(S[N-1] == '0') * [0]
N2 = len(State)
ans = 0
total = 0
K2 = 2*K+1

if K2 >= N2:
    K2 = N2

for i in range(K2):
    total += State[i]
        
ans = total
for i in range(0,N2-K2,2):
    total2 = total - State[i] - State[i+1] + State[i+K2] + State[i+1+K2]
    ans = max(ans,total2)
    total = total2
    
print(ans)
Problem p03081 (python)

Fixed submission:
# sys.stdin.readline()
import math
import sys
input = sys.stdin.readline


def decision(s,td,j):
    #index番目の文字が落ちるかどうかを判別
    #右に落ちるときは1，左に落ちるときは-1，落ちないときは0を返す
    global N,Q
    for [qi,v] in td:
        if j < 0 or j >= N:
            break
        if s[j] == qi:
            if v == 'L':
                j -= 1
            else:
                j += 1
    if j == -1:
        return -1
    elif j == N:
        return 1
    else:
        return 0
 
N,Q = map(int,input().split())
s = input()
td = [input().split() for _ in range(Q)]
    
#td[i][j]:i番目の文字(j=0)と方向(j=1)
    
    
RN = math.ceil(math.log2(N))
#2分探索
left = 0
right = N-1
#右側のボーダー
for i in range(RN):
    mid = (right+left)//2
    state = decision(s,td,mid)
    if state < 1:
        left = mid
    else:
        right = mid
    if (right - left) == 1:
        break
if right == N-1 and decision(s,td,right) == 0:
    right += 1
border_r = right
    
left = 0
right = N-1
#左側のボーダー
for i in range(RN):
    mid = ((right+left)//2)
    state = decision(s,td,mid)
    if state > -1:
        right = mid
    else:
        left = mid
    if (right - left) == 1:
        break
if left == 0 and decision(s,td,left) == 0:
    right -= 1
border_l = right
print(border_r-border_l)

Buggy submission (verdict: Runtime Error):
# sys.stdin.readline()
import sys
input = sys.stdin.readline
import math

def decision(s,td,index):
    #index番目の文字が落ちるかどうかを判別
    #右に落ちるときは1，左に落ちるときは-1，落ちないときは0を返す
    global N,Q
    qi = 0
    j = index
    for qi in range(Q):
        if j < 0 or j >= N:
            break
        if s[j] == td[qi][0]:
            if td[qi][1] == 'L':
                j -= 1
            elif td[qi][1] == 'R':
                j += 1
    if j == -1:
        return -1
    elif j == N:
        return 1
    else:
        return 0
    
 
 
N,Q = map(int,input().split())
s = input()
 
td = [0] * Q
for i in range(Q):
    td[i] = (input().split())
    
#td[i][j]:i番目の文字(j=0)と方向(j=1)
    
    
RN = math.log2(N)
#2分探索
left = 0
right = N-1
#右側のボーダー
for i in range(RN):
    mid = int((right+left)/2)
    state = decision(s,td,mid)
    if state < 1:
        left = mid
    else:
        right = mid
    if (right - left) == 1:
        break
if right == N-1 and decision(s,td,right) == 0:
    right += 1
border_r = right
    
left = 0
right = N-1
#左側のボーダー
for i in range(RN):
    mid = int((right+left)/2)
    state = decision(s,td,mid)
    if state > -1:
        right = mid
    else:
        left = mid
    if (right - left) == 1:
        break
if left == 0 and decision(s,td,left) == 0:
    right -= 1
border_l = right
print(border_r-border_l)