Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Time Limit Exceeded):
from itertools import combinations
from collections import defaultdict


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

dA = defaultdict(int)
for i in A:
    dA[i] += 1

dB = defaultdict(int)

for i in A:
    temp = 0
    for j in dA.keys():
        if i == j:
            a = list(range(dA[j] - 1))
            if not dB[len(a)]:
                temp += len(list(combinations(a, 2)))
                dB[len(a)] = len(list(combinations(a, 2)))
            else:
                temp += dB[len(a)]
        else:
            b = list(range(dA[j]))
            if not dB[len(b)]:
                temp += len(list(combinations(b, 2)))
                dB[len(b)] = len(list(combinations(b, 2)))
            else:
                temp += dB[len(b)]
    print(temp)

Fixed submission:
from math import factorial
from collections import defaultdict


def cmb(n, r):
    return factorial(n) // (factorial(n - r) * factorial(r))


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

dA = defaultdict(int)
for i in A:
    dA[i] += 1

sm = 0

for i in dA.values():
    if i >= 2:
        sm += cmb(i, 2)
    else:
        sm += 0

for j in A:
    print(sm - (dA[j] - 1))
Buggy python submission (verdict: Time Limit Exceeded):
from collections import defaultdict


N, K = map(int, input().split())
p = list(map(int, input().split()))
pd = defaultdict(int)

kitai = []
for i in p:
    if not pd[i]:
        ex = (1 + i) / 2
        kitai.append(ex)
    else:
        kitai.append(pd[i])

ans = 0
for i in range(N):
    if i + (K-1) >= N:
        break
    else:
        ans = max(ans, sum(kitai[i:i+K]))

print(ans)

Fixed submission:
from collections import defaultdict


N, K = map(int, input().split())
p = list(map(int, input().split()))
pd = defaultdict(int)

kitai = []
for i in p:
    if not pd[i]:
        ex = (1 + i) / 2
        kitai.append(ex)
    else:
        kitai.append(pd[i])

# 累積和
rui = []
for i, j in enumerate(kitai):
    if i == 0:
        rui.append(j)
    else:
        rui.append(rui[i-1] + j)

ans = 0
for i, j in enumerate(rui):
    if i + (K-1) >= N:
        continue
    else:
        if i == 0:
            ans = rui[i+K-1]
        else:
            ans = max(ans, rui[i+K-1] - rui[i-1])

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import defaultdict


N, K = map(int, input().split())
p = list(map(int, input().split()))
pd = defaultdict(int)

kitai = []
for i in p:
    if not pd[i]:
        dice = list(range(1, i+1))
        ex = 0
        p = 1 / len(dice)

        for x in dice:
            px = p*x
            ex += px
        pd[i] = ex
        kitai.append(ex)
    else:
        kitai.append(pd[i])

ans = 0
for i in range(N):
    if i + (K-1) >= N:
        break
    else:
        ans = max(ans, sum(kitai[i:i+K]))

print(ans)

Fixed submission:
from collections import defaultdict


N, K = map(int, input().split())
p = list(map(int, input().split()))
pd = defaultdict(int)

kitai = []
for i in p:
    if not pd[i]:
        ex = (1 + i) / 2
        kitai.append(ex)
    else:
        kitai.append(pd[i])

# 累積和
rui = []
for i, j in enumerate(kitai):
    if i == 0:
        rui.append(j)
    else:
        rui.append(rui[i-1] + j)

ans = 0
for i, j in enumerate(rui):
    if i + (K-1) >= N:
        continue
    else:
        if i == 0:
            ans = rui[i+K-1]
        else:
            ans = max(ans, rui[i+K-1] - rui[i-1])

print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
p = list(map(int, input().split()))

ans = 0
for i, j in enumerate(p):
    if i == 0:
        ans += 1
        continue
    else:
        if j < min(p[:i]):
            ans += 1
            
print(ans)

Fixed submission:
N = int(input())
p = list(map(int, input().split()))

minval = p[0]
ans = 1
for i in p:
    if i < minval:
        ans += 1
        minval = i

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
p = list(map(int, input().split()))

ans = 0
for i, j in enumerate(p):
    if i == 0:
        ans += 1
        continue
    
    for k in p[:i]:
        if j < k:
            continue
        else:
            break
    else:
        ans += 1

print(ans)

Fixed submission:
N = int(input())
p = list(map(int, input().split()))

minval = p[0]
ans = 1
for i in p:
    if i < minval:
        ans += 1
        minval = i

print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
XL = [list(map(int, input().split())) for _ in range(N)]

rXL = []
for i, j in XL:
    rXL.append([i - j, i + j])
rXL = sorted(rXL, key = lambda x: x[1])
TF = [True for _ in range(N)]

for i in range(1, N):
    for j in range(i):
        if rXL[i][0] < rXL[j][1] and rXL[i][1] > rXL[j][0] and TF[j]:
            TF[i] = False

print(sum(TF))

Fixed submission:
N = int(input())
XL = [list(map(int, input().split())) for _ in range(N)]

rXL = []
for i, j in XL:
    rXL.append([i - j, i + j])
rXL = sorted(rXL, key = lambda x: x[1])
TF = [True for _ in range(N)]

maxr = rXL[0][1]
for i in range(1, N):
    if maxr <= rXL[i][0]:
        maxr = rXL[i][1]
    else:
        TF[i] = False

print(sum(TF))
Buggy python submission (verdict: Time Limit Exceeded):
S = list(input())
ans = []
N = len(S) + 1


def search(n):
    temp = [0, 0]
    # right
    if n != N-1:
        j = n
        ret = 0
        while j < N-1 and S[j] == ">":
            ret += 1
            j += 1
        temp[1] = ret

    # left
    if n != 0:
        j = n - 1
        ret = 0
        while j >= 0 and S[j] == "<":
            ret += 1
            j -= 1
        temp[0] = ret
    return temp


for i in range(N):
    l, r = search(i)
    ans.append(max(l, r))
print(sum(ans))

Fixed submission:
S = list(input())
ans = []
N = len(S) + 1

lr = [[] for _ in range(N)]

# 左から見る
temp = 0
for i in range(N):
    if i == 0:
        lr[i].append(0)
    else:
        if S[i-1] == "<":
            temp += 1
            lr[i].append(temp)
        else:
            temp = 0
            lr[i].append(temp)

# 右から見る
temp = 0
for i in reversed(range(N)):
    if i == N - 1:
        lr[i].append(0)
    else:
        if S[i] == ">":
            temp += 1
            lr[i].append(temp)
        else:
            temp = 0
            lr[i].append(temp)

ans = []
for i in range(N):
    ans.append(max(lr[i]))
print(sum(ans))
Buggy python submission (verdict: Runtime Error):
import math

a, b, x = map(int, input().split())
maxv = a**2 * b
if x >= maxv / 2:
    t = 2*b - (2 * x) / a**2
    tan = a / t
    print(90 - math.degrees(math.atan(tan)))
else:
    t = 2*x / (a * b)
    tan = 1 / (b / t)
    print(90 - math.degrees(math.atan(tan)))

Fixed submission:
import math
import sys

a, b, x = map(int, input().split())
maxv = a**2 * b

if maxv == x:
    print(0)
    sys.exit()

if x >= maxv / 2:
    t = 2*b - (2 * x) / a**2
    tan = a / t
    print(90 - math.degrees(math.atan(tan)))
else:
    t = 2*x / (a * b)
    tan = 1 / (b / t)
    print(90 - math.degrees(math.atan(tan)))
Buggy python submission (verdict: Time Limit Exceeded):
import itertools
 
N = int(input())
L = list(map(int, input().split()))
L = sorted(L, reverse=True)
 
counter = 0
 
for i in range(0, N-2):
    temp = L[i]
    other = list(itertools.combinations(L[i+1:], 2))
    for j in range(len(other)):
        if temp < other[j][0] + other[j][1]:
            counter += 1
 
print(counter)

Fixed submission:
n = int(input())
nums = list(map(int, input().split(' ')))
nums.sort()
total = 0

for i in range(n-1, -1, -1):
    l = 0
    r = i - 1
    while l < r:
        if nums[l] + nums[r] > nums[i]:
            total += r - l
            r -= 1
        else:
            l += 1
print(total)

Buggy python submission (verdict: Time Limit Exceeded):
import itertools

N = int(input())
L = list(map(int, input().split()))
L = sorted(L, reverse=True)

counter = 0

for i in range(0, N-2):
    temp = L[i]
    other = list(itertools.combinations(L[i+1:], 2))
    for j in range(len(other)):
        if temp < other[j][0] + other[j][1] and other[j][0] < temp + other[j][1] and other[j][1] < temp + other[j][0]:
            counter += 1

print(counter)

Fixed submission:
n = int(input())
nums = list(map(int, input().split(' ')))
nums.sort()
total = 0

for i in range(n-1, -1, -1):
    l = 0
    r = i - 1
    while l < r:
        if nums[l] + nums[r] > nums[i]:
            total += r - l
            r -= 1
        else:
            l += 1
print(total)

Buggy python submission (verdict: Time Limit Exceeded):
import itertools

N = int(input())
L = list(map(int, input().split()))
L = sorted(L, reverse=True)

counter = 0

for i in range(0, N-2):
    temp = L[i]
    other = list(itertools.combinations(L[i+1:], 2))
    for j in range(len(other)):
        if temp < other[j][0] + other[j][1] and other[j][0] < temp + other[j][1] and other[j][1] < temp + other[j][0]:
            counter += 1

print(counter)

Fixed submission:
n = int(input())
nums = list(map(int, input().split(' ')))
nums.sort()
total = 0

for i in range(n-1, -1, -1):
    l = 0
    r = i - 1
    while l < r:
        if nums[l] + nums[r] > nums[i]:
            total += r - l
            r -= 1
        else:
            l += 1
print(total)
Buggy python submission (verdict: Time Limit Exceeded):
import sys
 
L, R = map(int, input().split())
t = 2019
 
if L == 0:
    print(0)
    sys.exit()
 
temp = R // t
temp2 = L // t
kouho = []
if temp != temp2:
    print(0)
    sys.exit()
else:
    for i in range(L, R+1):
        for j in range(L, R+1):
            if i != j:
                kouho.append((i * j)%t)
    kouho.sort()
    print(kouho[0])

Fixed submission:
import sys

L, R = map(int, input().split())
t = 2019

if L == 0:
    print(0)
    sys.exit()

temp = R // t
temp2 = L // t
kouho = []
if temp != temp2:
    print(0)
    sys.exit()
else:
    for i in range(L, R+1):
        for j in range(L, R+1):
            if i != j:
                kouho.append((i * j)%t)
    kouho.sort()
    print(kouho[0])
Buggy python submission (verdict: Runtime Error):
# ABC132 D Blue and Red Balls

from math import factorial

def comb(n, r):
    ''' 組み合わせ '''
    return factorial(n) // (factorial(n - r) * factorial(r))

N, K = map(int, input().split())
MOD = int(1e9)+7

for i in range(1, K+1):
    temp = comb(N-K+1, i) * comb(K-1, i-1)
    print(int(temp % MOD))

Fixed submission:
# ABC132 D Blue and Red Balls

def comb(n, r):
    if n == 0 or r == 0:
        return 1
    if n < r:
        return 0
    a = b = 1
    r = min(n-r, r)
    for i in range(n-r+1,n+1):
        a*=i
    for i in range(1,r+1):
        b*=i
    
    return a//b

N, K = map(int, input().split())
MOD = int(1e9)+7

for i in range(1, K+1):
    temp = comb(N-K+1, i) * comb(K-1, i-1)
    print(int(temp % MOD))

Buggy python submission (verdict: Runtime Error):
# ABC132 D Blue and Red Balls

from scipy.misc import comb

N, K = map(int, input().split())
MOD = int(1e9)+7

for i in range(1, K+1):
    temp = comb(N-K+1, i) * comb(K-1, i-1)
    print(int(temp % MOD))

Fixed submission:
# ABC132 D Blue and Red Balls

def comb(n, r):
    if n == 0 or r == 0:
        return 1
    if n < r:
        return 0
    a = b = 1
    r = min(n-r, r)
    for i in range(n-r+1,n+1):
        a*=i
    for i in range(1,r+1):
        b*=i
    
    return a//b

N, K = map(int, input().split())
MOD = int(1e9)+7

for i in range(1, K+1):
    temp = comb(N-K+1, i) * comb(K-1, i-1)
    print(int(temp % MOD))

Buggy python submission (verdict: Runtime Error):
# ABC132 D Blue and Red Balls

from scipy.special import comb

N, K = map(int, input().split())
MOD = int(1e9)+7

for i in range(1, K+1):
    temp = comb(N-K+1, i) * comb(K-1, i-1)
    print(int(temp % MOD))

Fixed submission:
# ABC132 D Blue and Red Balls

def comb(n, r):
    if n == 0 or r == 0:
        return 1
    if n < r:
        return 0
    a = b = 1
    r = min(n-r, r)
    for i in range(n-r+1,n+1):
        a*=i
    for i in range(1,r+1):
        b*=i
    
    return a//b

N, K = map(int, input().split())
MOD = int(1e9)+7

for i in range(1, K+1):
    temp = comb(N-K+1, i) * comb(K-1, i-1)
    print(int(temp % MOD))
Buggy python submission (verdict: Runtime Error):
N, M = map(int, input().split())
# N:スイッチの数、M：電球の数
s = [[] for _ in range(M)]
for i in range(M):
    temp = list(map(int, input().split()))
    s[i] = temp[1:]
p = list(map(int, input().split()))
ans = 0

# bit全探索
for i in range(2 ** N):
    on = [0 for _ in range(N)]
    for j in range(N): 
        if (i >> j) & 1:
            for k in range(N):
                if j+1 in s[k]:
                    on[k] += 1
    tf = []
    for on_n, p_n in zip(on, p):
        if on_n % 2 == p_n:
            tf.append(True)
        else:
            tf.append(False)
    if all(flg for flg in tf):
        ans += 1

print(ans)


Fixed submission:
# ABC128 C-Switches

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

k = []
s = []
for _ in range(m):
    ks = list(map(int, input().split()))
    k.append(ks[0])
    s.append(ks[1:])
p = list(map(int, input().split()))

ans = 0
for bits in range(2**n):
    flag = True
    for i in range(m):
        on_sum = 0
        for switch in s[i]:
            on_sum += ((bits >> (switch - 1)) & 1)
        flag = flag and ((on_sum % 2) == p[i])
    if flag:
        ans += 1
print(ans)

Buggy python submission (verdict: Runtime Error):
N, M = map(int, input().split())
# N:スイッチの数、M：電球の数
s = [[] for _ in range(N)]
for i in range(M):
    temp = list(map(int, input().split()))
    s[i] = temp[1:]
p = list(map(int, input().split()))
ans = 0

# bit全探索
for i in range(2 ** N):
    on = [0 for _ in range(N)]
    for j in range(N): 
        if (i >> j) & 1:
            for k in range(N):
                if j+1 in s[k]:
                    on[k] += 1
    tf = []
    for on_n, p_n in zip(on, p):
        if on_n % 2 == p_n:
            tf.append(True)
        else:
            tf.append(False)
    if all(flg for flg in tf):
        ans += 1

print(ans)


Fixed submission:
# ABC128 C-Switches

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

k = []
s = []
for _ in range(m):
    ks = list(map(int, input().split()))
    k.append(ks[0])
    s.append(ks[1:])
p = list(map(int, input().split()))

ans = 0
for bits in range(2**n):
    flag = True
    for i in range(m):
        on_sum = 0
        for switch in s[i]:
            on_sum += ((bits >> (switch - 1)) & 1)
        flag = flag and ((on_sum % 2) == p[i])
    if flag:
        ans += 1
print(ans)
Buggy python submission (verdict: Runtime Error):
N, M = map(int, input().split())
A = list(map(int, input().split()))
bc = [list(map(int, input().split())) for _ in range(M)]
A = sorted(A)
bc = sorted(bc, reverse=True, key=lambda x:x[1])
i = 0
for b, c in bc:
    while A[i] <= c and b > 0:
        A[i] = c
        b -= 1
        i += 1
        if i >= N:
            break
print(sum(A))

Fixed submission:
N, M = map(int, input().split())
A = list(map(int, input().split()))
bc = [list(map(int, input().split())) for _ in range(M)]
A = sorted(A)
bc = sorted(bc, reverse=True, key=lambda x:x[1])
i = 0
for b, c in bc:
    while A[i] <= c and b > 0:
        A[i] = c
        b -= 1
        i += 1
        if i >= N:
            break
    if i >= N:
        break
print(sum(A))
Buggy python submission (verdict: Time Limit Exceeded):
R, G, B, N = map(int, input().split())
ans = 0
for i in range(N // R + 1):
    for j in range((N - R*i) // G + 1):
        if (N - i*R - j*G) / B >= 0 and (N - i*R - j*G) % B == 0:
            ans += 1
            
print(ans)

Fixed submission:
R, G, B, N = map(int, input().split())
ans = 0
for i in range(N // R + 1):
    for j in range((N - R*i) // G + 1):
        if (N - i*R - j*G) % B == 0:
            ans += 1
            
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
R, G, B, N = map(int, input().split())
ans = 0
for i in range(N // R + 1):
    for j in range(N-i+1):
        if (N - i*R - j*G) / B >= 0 and (N - i*R - j*G) % B == 0:
            ans += 1
            
print(ans)

Fixed submission:
R, G, B, N = map(int, input().split())
ans = 0
for i in range(N // R + 1):
    for j in range((N - R*i) // G + 1):
        if (N - i*R - j*G) % B == 0:
            ans += 1
            
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
R, G, B, N = map(int, input().split())
ans = 0
for i in range(N+1):
    for j in range(N-i+1):
        if (N - i*R - j*G) / B >= 0 and (N - i*R - j*G) % B == 0:
            ans += 1
            
print(ans)

Fixed submission:
R, G, B, N = map(int, input().split())
ans = 0
for i in range(N // R + 1):
    for j in range((N - R*i) // G + 1):
        if (N - i*R - j*G) % B == 0:
            ans += 1
            
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
R, G, B, N = map(int, input().split())
ans = 0
for i in range(N // R + 1):
    for j in range(N // G + 1):
        if (N - i*R - j*G) / B >= 0 and (N - i*R - j*G) % B == 0:
            ans += 1
            
print(ans)

Fixed submission:
R, G, B, N = map(int, input().split())
ans = 0
for i in range(N // R + 1):
    for j in range((N - R*i) // G + 1):
        if (N - i*R - j*G) % B == 0:
            ans += 1
            
print(ans)
Buggy python submission (verdict: Runtime Error):
N = int(input())
s = [input() for _ in range(N)]

end_A = 0
start_B = 0
AB_count = 0
both = 0

for i in range(N):
    if 'AB' in s[i]:
        AB_count += s[i].count('AB')
    if s[i][0] == 'B':
        start_B += 1
    if s[i][len(s[i])-1] == 'A':
        end_A += 1
    if s[i][0] == 'B' and s[i][len(s[i])-1] == 'A':
        both += 1
start_B -= both
end_A -= both

if both == 0:
    print(AB_count + min(start_B, end_A))
elif end_A + start_B > 0:
    print(AB_count + both + min(start_B, end_A))
elif end_A + end_B == 0:
    print(AB_count + both - 1)

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

end_A = 0
start_B = 0
AB_count = 0
both = 0

for i in range(N):
    if 'AB' in s[i]:
        AB_count += s[i].count('AB')
    if s[i][0] == 'B':
        start_B += 1
    if s[i][len(s[i])-1] == 'A':
        end_A += 1
    if s[i][0] == 'B' and s[i][len(s[i])-1] == 'A':
        both += 1
start_B -= both
end_A -= both

if both == 0:
    print(AB_count + min(start_B, end_A))
elif end_A + start_B > 0:
    print(AB_count + both + min(start_B, end_A))
else:
    print(AB_count + both - 1)

Buggy python submission (verdict: Runtime Error):
N = int(input())
s = [input() for i in range(N)]

end_A = 0
start_B = 0
AB_count = 0
both = 0

for i in range(N):
    if 'AB' in s[i]:
        AB_count += s[i].count('AB')
    if s[i][0] == 'B':
        start_B += 1
    if s[i][len(s[i])-1] == 'A':
        end_A += 1
    if s[i][0] == 'B' and s[i][len(s[i])-1] == 'A':
        both += 1
start_B -= both
end_A -= both

if both == 0:
    print(AB_count + min(start_B, end_A))
elif end_A + start_B > 0:
    print(AB_count + both + min(start_B, end_A))
elif end_A + end_B == 0:
    print(both - 1)

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

end_A = 0
start_B = 0
AB_count = 0
both = 0

for i in range(N):
    if 'AB' in s[i]:
        AB_count += s[i].count('AB')
    if s[i][0] == 'B':
        start_B += 1
    if s[i][len(s[i])-1] == 'A':
        end_A += 1
    if s[i][0] == 'B' and s[i][len(s[i])-1] == 'A':
        both += 1
start_B -= both
end_A -= both

if both == 0:
    print(AB_count + min(start_B, end_A))
elif end_A + start_B > 0:
    print(AB_count + both + min(start_B, end_A))
else:
    print(AB_count + both - 1)
Buggy python submission (verdict: Runtime Error):
import math


def solute(n, a):
    gcd_left = [0]
    gcd_right = [0]
    for i in range(n):
        gcd_left.append(math.gcd(gcd_left[i], a[i]))
        gcd_right.append((math.gcd(gcd_right[i], a[-(i+1)])))
    gcd_right = gcd_right[::-1]

    ans = []
    for i in range(n):
        ans.append(math.gcd(gcd_left[i], gcd_right[i+1]))
    return max(ans)


n = int(input())
a = [int(x) for x in input().split()]
print(solute(n, a))

Fixed submission:
import fractions


def solute(n, a):
    gcd_left = [0]
    gcd_right = [0]
    for i in range(n):
        gcd_left.append(fractions.gcd(gcd_left[i], a[i]))
        gcd_right.append((fractions.gcd(gcd_right[i], a[-(i+1)])))
    gcd_right = gcd_right[::-1]

    ans = []
    for i in range(n):
        ans.append(fractions.gcd(gcd_left[i], gcd_right[i+1]))
    return max(ans)


n = int(input())
a = [int(x) for x in input().split()]
print(solute(n, a))
Buggy python submission (verdict: Runtime Error):
N = int(input())
S = str(input())
new_S = ""

for i in range(N):
    if S[i] == "#":
        S = S[i:]
        break
else:
    S = S

for i in reversed(S):
    new_S += i

for i in range(N):
    if new_S[i] == ".":
        S_new = S[i:]
        break
else:
    S_new = ""

print(min(S_new.count('#'), S_new.count('.'), len(S_new)))


Fixed submission:
N = int(input())
S = str(input())

suu = []
s_count = S.count('.')
k_count = 0
suu.append(s_count + k_count)
for i in range(N):
    if S[i] == "#":
        k_count += 1
    else:
        s_count -= 1
    suu.append(k_count + s_count)

print(min(suu))
Buggy python submission (verdict: Runtime Error):
import sys
from collections import deque
input = sys.stdin.readline

def main():
    n, k = map(int, input().split())
    s = list(input())
    
    tank = []
    cnt = 1
    for i in range(1, n):
        if s[i] != s[i-1]:
            tank.append(cnt)
            cnt = 0
        cnt += 1
        if i == n-1:
            tank.append(cnt)
            
    p = len(tank)
    
    if s[0] == '0' and (p+1)//2 <= k:
        print(n)
        exit()
    if p//2 <= k and s[0] == '1':
        print(n)
        exit()
        
    ans = 0
    res = 0
    if s[0] == '0':
        for j in range(2*k):
            res += tank[j]
        ans = res
        for j in range(2*k, p):
            if j == 2*k:
                res -= tank[0]
            elif j%2 == 0:
                res = res - tank[j-2*k] - tank[j*2*k-1]
            res += tank[j]
            ans = max(ans, res)
            
    else:
        for j in range(2*k+1):
            res += tank[j]
        ans = res
        for j in range(2*k+1, p):
            if j%2 == 1:
                res = res - tank[j-2*k-1] - tank[j-2*k]
            res += tank[j]
            ans = max(ans, res)
            
    print(ans)
    
if __name__ == '__main__':
    main()

Fixed submission:
import sys
from collections import deque
input = sys.stdin.readline


def main():
    n,k = map(int,input().split())
    s = list(input())

    #tank: '0...0'と'1...1'の塊の数リスト
    #ex. 0001011ならtank = [3,1,1,2] (0が3つ,1が1つ,0が2つ,1が2つ)
    tank = []
    cnt = 1
    for i in range(1,n):
        if s[i] != s[i-1]:
            tank.append(cnt)
            cnt = 0
        cnt += 1
        if i == n-1:
            tank.append(cnt)
    #p:tankの長さ
    p = len(tank)

    #全部1にできてしまう場合
    if s[0] == '0' and (p+1)//2 <= k:
        print(n)
        exit()
    if p//2 <= k and s[0] == '1':
        print(n)
        exit()

    
    ans = 0
    #res:tank[j]が1...1の右端の時の解
    res = 0
    if s[0] == '0':
        for j in range(2*k):
            res += tank[j]
        ans = res
        for j in range(2*k,p):
            if j == 2*k:
                res -= tank[0]
            elif j%2 == 0:
                res= res - tank[j-2*k] -tank[j-2*k-1]
            res += tank[j]
            ans = max(ans,res)

    else:
        for j in range(2*k+1):
            res += tank[j]
        ans = res
        for j in range(2*k+1,p):
            if j % 2 == 1:
                res = res- tank[j-2*k-1]-tank[j-2*k]
            res += tank[j]
            ans = max(ans,res)

    print(ans)

if __name__ == '__main__':
    main()