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

Fixed submission:
h, w = map(int,input().split())
if h == 1 or w == 1:
    print(1)
elif h%2 == 1 and w%2 == 1:
    print((h*w+1)//2)
else:
    print(h*w//2)

Buggy submission (verdict: Runtime Error):
h, w = map(int,input().split())
if h == 1 or w == 1:
    print(1)
else h%2 == 1 and w%2 == 1:
    print((h*w+1)//2)
elif:
    print(h*w//2)
Problem p02761 (python)

Fixed submission:
N, M = map(int,input().split())
num = [0,1,2,3,4,5,6,7,8,9]
ans = [str(i) for i in range(10**(N-1), 10**N)]
check = [1 for i in range(10**(N-1), 10**N)]
if N == 1:
    ans.insert(0,"0")
    check.insert(0,1)

for i in range(M):
    s, c = map(int,input().split())
    s -= 1
    for idx, j in enumerate(ans):
        if j[s] != str(c):
            check[idx] = 0
for idx, i in enumerate(check):
    if i == 1:
        print(ans[idx])
        quit()
print(-1)

Buggy submission (verdict: Runtime Error):
N, M = map(int,input().split())
num = [0,1,2,3,4,5,6,7,8,9]
ans = [str(i) for i in range(10**(N-1), 10**N)]
check = [1 for i in range(10**(N-1), 10**N)]
if N == 1:
    ans.insert(0,0)
    check.insert(0,1)

for i in range(M):
    s, c = map(int,input().split())
    s -= 1
    for idx, j in enumerate(ans):
        if j[s] != str(c):
            check[idx] = 0
for idx, i in enumerate(check):
    if i == 1:
        print(ans[idx])
        quit()
print(-1)
Problem p02888 (python)

Fixed submission:
import bisect
n = int(input())
l = list(map(int, input().split()))
l.sort()
ans = 0
for i in range(n):
    for j in range(i+1, n):
        ans += bisect.bisect_left(l, l[i] + l[j]) - j - 1
print(ans)

Buggy submission (verdict: Runtime Error):
import numpy as np
import bisect
n = int(input())
l = np.array(list(map(int, input().split())))
l = np.sort(l)
l_unique, l_uniue_index = np.unique(l, return_counts = True)
m = len(l_unique)
ans = 0

ans += len(np.where(l_uniue_index >= 3)[0])
print(ans)

for a in l_unique[np.where(l_uniue_index >= 2)[0]]:
    ans += bisect.bisect_left(l_unique, a * 2) - 1
print(ans)    
    
for i in range(m):
    for j in range(i+1, m):
        ans += bisect.bisect_left(l_unique[j+1:], l_unique[i] + l_unique[j])
print(ans)
Problem p02900 (python)

Fixed submission:
def factorization(n):
    arr = []
    temp = n
    for i in range(2, int(-(-n**0.5//1))+1):
        if temp%i==0:
            cnt=0
            while temp%i==0:
                cnt+=1
                temp //= i
            arr.append([i, cnt])

    if temp!=1:
        arr.append([temp, 1])

    if arr==[]:
        arr.append([n, 1])

    return arr

import fractions
a, b = map(int,input().split())

if fractions.gcd(a, b) == 1:
    print(1)
else:
    print(len(factorization(fractions.gcd(a, b))) + 1)

Buggy submission (verdict: Runtime Error):
def factorization(n):
    arr = []
    temp = n
    for i in range(2, int(-(-n**0.5//1))+1):
        if temp%i==0:
            cnt=0
            while temp%i==0:
                cnt+=1
                temp //= i
            arr.append([i, cnt])

    if temp!=1:
        arr.append([temp, 1])

    if arr==[]:
        arr.append([n, 1])

    return arr

import math
a, b = map(int,input().split())

if math.gcd(a, b) == 1:
    print(1)
else:
    print(len(factorization(math.gcd(a, b))) + 1)

Buggy submission (verdict: Runtime Error):

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)
    return divisors

import random
def is_prime(q,k=50):
    q = abs(q)
    #計算するまでもなく判定できるものははじく
    if q == 2: return True
    if q < 2 or q&1 == 0: return False

    #n-1=2^s*dとし（但しaは整数、dは奇数)、dを求める
    d = (q-1)>>1
    while d&1 == 0:
        d >>= 1
    
    #判定をk回繰り返す
    for i in range(k):
        a = random.randint(1,q-1)
        t = d
        y = pow(a,t,q)
        #[0,s-1]の範囲すべてをチェック
        while t != q-1 and y != 1 and y != q-1: 
            y = pow(y,2,q)
            t <<= 1
        if y != q-1 and t&1 == 0:
            return False
    return True

import math
a, b = map(int,input().split())
ans = 1
gcd_div = make_divisors(math.gcd(a, b))
for i in gcd_div:
    if is_prime(i) == True:
        ans += 1
print(ans)

Buggy submission (verdict: Runtime Error):
import sympy
a, b = map(int,input().split())
ans = 1
gcd_div = sympy.divisors(sympy.gcd(a, b))[:-1]
for i in gcd_div:
    if sympy.isprime(i) == True:
        ans += 1
print(ans)
Problem p02928 (python)

Fixed submission:
N, K = map(int,input().split())
An = list(map(int,input().split()))
f_count = 0
r_count = 0

for i in range(N):
    for j in range(N):
        if i < j:
            if An[i] < An[j]:
                f_count += 1
            elif An[i] > An[j]:
                r_count += 1

Ans = (f_count * K*(K-1)//2 + r_count * (K*(K+1)//2)) % (10**9+7)

print(int(Ans))

Buggy submission (verdict: Runtime Error):



def bitsum(bit,i):
    s = 0
    while i > 0:
        s += bit[i]
        i -= i&(-i)
    return s
    

def bitadd(bit,i,x):
    while i <= N:
        bit[i] += x
        i += i&(-i)
    return bit
    
#################################
import math
N, K = map(int,input().split())
a = list(map(int,input().split()))


n_ = int(math.log2(N)) + 1
bit = [0] * (2**n_)
bit.append(0)

################################
ass = 0
for j in range(N):
    ass += j-bitsum(bit,a[j])
    bit = bitadd(bit,a[j],1)

Ans = ass*(K*(K+1)/2)+(N*(N-1)/2-ass)*(K*(K-1)/2)

print(int(Ans%(10^9+7)))
Problem p02971 (python)

Fixed submission:
import sys
input = sys.stdin.buffer.readline
N = int(input())
A = [int(input()) for _ in range(N)]
B = A[:]
max1 = max(A)
A.remove(max1)
max2 = max(A)

for i in B:
    if i == max1:
        print(max2)
    else:
        print(max1)

Buggy submission (verdict: Runtime Error):
import numpy as np
n = int(input())
a = [input() for _ in range(n)]
a_sort = sorted(a)
for i in range(n):
    if i == np.argmax(a):
        print(a_sort[-2])
    else:
        print(a_sort[-1])
Problem p03161 (python)

Fixed submission:
n, k = map(int,input().split())
h = list(map(int,input().split()))
inf = float("inf")
dp = [0]+[inf]*(n-1)

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

print(dp[n-1])

Buggy submission (verdict: Runtime Error):
n, k = map(int,input().split())
h = list(map(int,input().split()))
inf = float("inf")
dp = [inf]*(n+1)
if n == 2:
    print(abs(h[1]-h[0]))
    sys.exit()

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

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

print(dp[n-1])
Problem p03166 (python)

Fixed submission:
import sys
sys.setrecursionlimit(10 ** 7)
N, M = map(int,input().split())
path = [[] for _ in range(N)]
for _ in range(M):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    path[a].append(b)

dp = [-1] * N

def chdp(x):
    if dp[x] != -1:
        return(dp[x]) 
    dp[x] = 0
    for t in path[x]:
        dp[x] = max(dp[x], chdp(t)+1)
    return(dp[x])

for i in range(N):
    chdp(i)

print(max(dp))

Buggy submission (verdict: Runtime Error):
N, M = map(int,input().split())
path = [[] for _ in range(N)]
for _ in range(M):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    path[a].append(b)

dp = [-1] * N

def chdp(x):
    if dp[x] != -1:
        return(dp[x]) 
    dp[x] = 0
    for t in path[x]:
        dp[x] = max(dp[x], chdp(t)+1)
    return(dp[x])

for i in range(N):
    chdp(i)

print(max(dp))
Problem p03659 (python)

Fixed submission:
from itertools import accumulate

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

b = list(accumulate(a))
ma = b[-1]
mi = 10**10
for sunuke in b[:N-1]:
    arai = ma - sunuke
    if abs(sunuke - arai) < mi:
        mi = abs(sunuke - arai)
print(mi)

Buggy submission (verdict: Runtime Error):
N = int(input())
a = list(map(int, input().split())) 

b = list(accumulate(a))
ma = b[-1]
mi = 10**10
for sunuke in b:
    arai = ma - sunuke
    if abs(sunuke - arai) < mi:
        mi = abs(sunuke - arai)
print(mi)

Buggy submission (verdict: Runtime Error):
N = int(input())
a = list(map(int, input().split())) 

b = list(accumulate(a))
ma = b[-1]
mi = 10**10
for sunuke in b:
    arai = ma - sunuke
    if abs(sunuke - arai) < mi:
        mi = abs(sunuke - arai)
print(mi)
Problem p02603 (python)

Fixed submission:
n = int(input())
A = list(map(int,input().split()))
l = []

m = 1000
k = 0

for i in range(n):
    if i == n-1: # 最後ながら全部売る
        m = m + A[i]*k
    elif A[i] <= m and A[i] < A[i+1]: #お金あって明日上がるなら買う
        k = m//A[i]
        m = m - A[i]*k
    elif 0 < k and A[i+1] < A[i]: #株があって明日下がるなら売る
        m = m + A[i]*k
        k = 0
    else:
        pass

print (m)

Buggy submission (verdict: Runtime Error):
n = int(input())
A = list(map(int,input().split()))

m = 1000
k = 0

for i in range(n):
    if A[i] <= m and A[i] == min(A) and i != n-1:
        # 全体で最安なら買い
        k = m//A[i]
        m = m- A[i]*k
    elif A[i] < m and A[i] < max(A[i::]) and i != n-1:
        # 今後、現在の株価より高い株価があれば買い
        k = m//A[i]
        m = m - A[i]*k
    elif 0 < k and A[i] == max(A):
        # 全体で最高なら売り
        m = m + A[i]*k
        k = 0
    elif 0 < k and A[i] != min(A[i::]):
        # 今後、現在の株価より安い株価があれば売り
        m = m + A[i]*k
        k = 0
    elif 0 < k and A[i] == max(A[i::])
        # 今後、現在より高い株価がなければ売り
        m = m + A[i]*k
        k = 0
    else:
        pass

print (m)
Problem p02756 (python)

Fixed submission:
from collections import deque
d = deque(input())
q = int(input())
reverse = False
for _ in range(q):
    query = list(input().split())
    if query[0] == '1':
        if reverse:
            reverse = False
        else:
            reverse = True
        continue
    else:
        if query[1] == '1':
            if reverse:
                d.append(query[2])
            else:
                d.appendleft(query[2])
        else:
            if reverse:
                d.appendleft(query[2])
            else:
                d.append(query[2])

if reverse:
    d = list(reversed(d))

print(''.join(map(str, d)))

Buggy submission (verdict: Runtime Error):
s = input()
q = int(input())
query_d = {}
for i in range(q):
    if i == 0:
        query_d[0] = list(input().split())
    else:
        query = list(input().split())
        if (query[0] == '1') and (query_d[i-1][0] == '1'):
            query_d.pop(i-1)
        else:
            query_d[i] = query

for query in query_d.values():
    if int(query[0]) == 1:
        s = s[::-1]
    else:
        if int(query[1]) == 1:
            s = query[2] + s
        else:
            s = s + query[2]
print (s)
Problem p02812 (python)

Fixed submission:
n = int(input())
s = str(input())
count = 0
for i in range(0,n):
    if s[i] == "A":
        if i+2 <= n-1:
            if s[i+1] == "B" and s[i+2] == "C":
                count += 1
print (count)

Buggy submission (verdict: Runtime Error):
n = int(input())
s = str(input())
count = 0
for i in range(0,n):
    if s[i] == "A":
        if i+2 <= n:
            if s[i+1] == "B" and s[i+2] == "C":
                count += 1
print (count)

Buggy submission (verdict: Runtime Error):
n = int(input())
s = str(input())
count = 0
for i in range(0,len(s)):
    if s[i] == "A":
        if i+2 <= len(s):
            if s[i+1] == "B" and s[i+2] == "C":
                count += 1
print (count)
Problem p02909 (python)

Fixed submission:
s = input()
if s[0] == "S":
    print ("Cloudy")
elif s[0] == "C":
    print ("Rainy")
else:
    print ("Sunny")

Buggy submission (verdict: Runtime Error):
s = input()
if s[0] = "S":
    print ("Cloudy")
elif s[0] = "C":
    print ("Rainy")
else:
    print ("Sunny")
Problem p03328 (python)

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

a,b = map(int,input().split())
d = b-a

def h(x:int):
    if x == 1:
        return 1
    else:
        return h(x-1)+x

print (h(d)-b)

Buggy submission (verdict: Runtime Error):
a,b = map(int,input().split())
d = b-a

def h(x:int):
    if x == 1:
        return 1
    else:
        return h(x-1)+x

print (h(d)-b)
Problem p03587 (python)

Fixed submission:
print (input().count('1'))

Buggy submission (verdict: Runtime Error):
print (input().count(1)
Problem p03657 (python)

Fixed submission:
a,b = map(int,input().split())
print ("Possible") if a%3 == 0 or b%3 == 0 or (a+b)%3 == 0 else print ("Impossible")

Buggy submission (verdict: Runtime Error):
N = int(input())
a = list(map(int,input().split()))
total = sum(a)
ans = float('inf')
x = 0
for i in range(N-1):
    x += a[i]
    ans = min(abs(total-2*x),ans)
print (ans)
Problem p03773 (python)

Fixed submission:
a,b = map(int,input().split())
print (a+b) if a+b < 24 else print (a+b-24)

Buggy submission (verdict: Runtime Error):
a,b = map(int,input().split())
print (a+b) if a+b >= 24 print (a+b-24)
Problem p03853 (python)

Fixed submission:
h,w = map(int,input().split())
pic = []
for _ in range(h):
    pic.append(input())
for line in pic:
    print (line)
    print (line)

Buggy submission (verdict: Runtime Error):
h,w = map(int,input().split())
for _ in range(w):
    s = input()
    print (s)
    print (s)
Problem p03125 (python)

Fixed submission:
[a, b] = map(int, input().split())
print(a + b if b % a == 0 else b - a)

Buggy submission (verdict: Runtime Error):
[a, b] = read().split().map(int)
print(a + b if b % a == 0 else b - a)
Problem p02548 (python)

Fixed submission:
N = int(input())

table = [0] * (N+5)

for i in range(1, N+3):
    for j in range(i, N+2, i):
        table[j] += 1
ans = 0
for i in range(N):
    ans += table[i]

print(ans)

Buggy submission (verdict: Runtime Error):
    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)

    return divisors

N = int(input())

table = [0] * (N+5)

for i in range(1, N+2):
    for j in range(i, N+2, i):
        table[j] += 1
ans = 0
for i in range(N):
    ans += table[i]

print(ans)
Problem p02607 (python)

Fixed submission:
N = int(input())
A = list(map(int, input().split()))
A = [0] + A

ans = 0
for i in range(1, len(A)):
    if i % 2 == 1 and A[i] % 2 == 1:
        ans += 1
print(ans)

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

N = int(input())
A = list(map(int, input().split()))
A = [0] + A

ans = 0
for i in range(1, len(A)+1):
    if i % 2 == 1 and A[i] % 2 == 1:
        ans += 1
print(ans)
Problem p02684 (python)

Fixed submission:
import sys
input = lambda: sys.stdin.readline().rstrip()

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

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


visited = set()
visited.add(1)
lst = [1, ]

now = 1
flg = 0
for i in range(K):
    now = A[now-1]
    if now in visited:
        last = K - i
        loop_point = now
        flg = 1
        break
    visited.add(now)
    lst.append(now)
if not flg:
    print(lst[-1])
    exit()    

for i in range(len(lst)):
    if lst[i] == loop_point:
        inter = i
        loop = lst[i:]

last = (last-1) % len(loop)
print(loop[last])

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

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

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


visited = set()
visited.add(1)
lst = [1, ]

now = 1
for i in range(K):
    now = A[now-1]
    if now in visited:
        last = K - i
        loop_point = now
        break
    visited.add(now)
    lst.append(now)

for i in range(len(lst)):
    if lst[i] == loop_point:
        inter = i
        loop = lst[i:]
        flg = 1

if not flg:
    print(lst[-1])
    exit()        

last = (last-1) % len(loop)
print(loop[last])
Problem p02797 (python)

Fixed submission:
N, K, S = map(int, input().split())

if S != 10 **9:
    N = [S+1] * N    
else:
    N = [1] * N

for i in range(K):
        N[i] = S

print(*N)

Buggy submission (verdict: Runtime Error):
N, K, S = map(int, input().split())

N = [1] * N

if S % 2 == 0:
    for i in range(K+1):
        N[i] = S // 2
else:
    for i in range(K):
        N[i] = S

print(*N)

Buggy submission (verdict: Runtime Error):
N, K, S = map(int, input().split())

array = [0] * N
if S % 2 == 0:
    for i in range(K+1):
        array[i] = S//2
    print(*array)
else:
    for i in range(0, K, 2):
        array[i] = (S-1) //2
        array[i+1] = (S+1)  // 2
    print(*array)

Buggy submission (verdict: Runtime Error):
N, K, S = map(int, input().split())

array = [0] * N
if S % 2 == 0:
    for i in range(K+1):
        array[i] = S//2
    print(*array)
else:
    for i in range(0, K+1, 2):
        array[i] = (S-1) //2
        array[i+1] = (S+1)  // 2
    print(*array)
Problem p02814 (python)

Fixed submission:
import fractions
from functools import reduce

N, M = map(int, input().split())
a = list(map(int, input().split()))

def f(x):
    res = 0
    while x % 2 == 0:
        x //= 2
        res += 1
    return res  

def lcm(x, y):
    return (x * y) // fractions.gcd(x, y)
  

for i in range(len(a)):
    if a[i] % 2 == 1:
        print(0)
        exit()
    a[i] //= 2

t = f(a[0])
for i in range(len(a)):
    if f(a[i]) != t:
        print(0)
        exit()
    a[i] >>= t #a[i] //= 2 ** t
M >>= t

l = 1
for i in range(N):
    l = lcm(l, a[i])
    if l > M:
        print(0)
        exit()

M //= l
ans = (M+1) // 2
print(ans)

Buggy submission (verdict: Runtime Error):
import fractions
from functools import reduce

N, M = map(int, input().split())

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

a_set = list(set(a))

def lcm_base(x, y):
    return (x * y) // fractions.gcd(x, y)

def lcm_list(numbers):
    return reduce(lcm_base, numbers, 1)

lcm = lcm_list(a_set)

sum = lcm / 2
ans = 0
while sum <= M:
    if sum > M:
        break
    sum += lcm
    ans += 1
print(ans)

Buggy submission (verdict: Runtime Error):
import fractions
from functools import reduce

N, M = map(int, input().split())

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

a_set = list(set(a))

def lcm_base(x, y):
    return (x * y) // fractions.gcd(x, y)

def lcm_list(numbers):
    return reduce(lcm_base, numbers, 1)

lcm = lcm_list(a_set)

sum = lcm / 2
ans = 0
while sum <= M:
    if sum > M:
        break
    sum += lcm
    ans += 1
print(ans)
Problem p02923 (python)

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

ans = 0
cnt = 0

for i in range(N-1):
    if H[i] >= H[i+1]:
        cnt += 1
    else:
        if ans < cnt:
            ans = cnt
        cnt = 0    
if ans < cnt:
    ans = cnt
print(ans)

Buggy submission (verdict: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int N;

int main() {
  cin >> N;
  int H[N];
  for (int i = 0; i < N; i++) cin >> H[i];

  int ans = 0;

  for (int i = 0; i < N-1; i++) {
    int tmp = 0;
    for (int j = i; j < N-1;) {
      if (H[j] >= H[j+1]) tmp++;
      else break;
    }
    ans = max(ans, tmp);
  }
  cout << ans << endl;
}
Problem p02936 (python)

Fixed submission:
N,Q = map(int, input().split())
to = [[] for _ in range(N+1)]
ans = [0]*N

for _ in range(N-1):
    a,b = map(int, input().split())
    a -= 1
    b -= 1
    to[a].append(b)
    to[b].append(a)

for _ in range(Q):
    pa, x = map(int, input().split())
    pa -= 1
    ans[pa] += x

q = [(0, -1)]
while(q):
    v, p = q.pop()
    for nv in to[v]:
        if nv == p: continue
        ans[nv] += ans[v]
        q.append((nv, v))

print(*ans)

Buggy submission (verdict: Runtime Error):
N,Q = map(int, input().split())
to = [[] for _ in range(N+1)]
ans = [0]*N

for _ in range(N-1):
    a,b = map(int, input().split())
    a -= 1
    b -= 1
    to[a].append(b)
    to[b].append(a)

for _ in range(Q):
    pa, x = map(int, input().split())
    pa -= 1
    ans[pa] += x

def dfs(v, p=-1):
    for i in to[v]:
        if i == p: continue
        ans[i] += ans[v]
        dfs(i, v)
        
dfs(0)        
print(' '.join([str(x) for x in ans[0:]]))

Buggy submission (verdict: Runtime Error):
N,Q = map(int, input().split())
to = [[] for _ in range(N+1)]
ans = [0]*N

for _ in range(N-1):
    a,b = map(int, input().split())
    a -= 1
    b -= 1
    to[a].append(b)
    to[b].append(a)

for _ in range(Q):
    pa, x = map(int, input().split())
    pa -= 1
    ans[pa] += x

def dfs(v, p=-1):
    for i in to[v]:
        if i == p: continue
        ans[i] += ans[v]
        dfs(i, v)
        
dfs(0)        
print(' '.join([str(x) for x in ans[0:]]))
Problem p02982 (python)

Fixed submission:
def dist(a, b):
  res = 0
  for i in range(D):
    res += (a[i] - b[i]) ** 2
  res = res ** 0.5
  return res

N, D = map(int, input().split())
X = [list(map(int, input().split())) for i in range(N)]
ans = 0
for i in range(N - 1):
  for j in range(i +1, N):
    if dist(X[i], X[j]).is_integer():
      ans += 1
print(ans)

Buggy submission (verdict: Runtime Error):
def dist(a, b):
  res = 0
  for i in range(D):
    res += (a[i] - b[i]) ** 2
    res = res ** 0.5

N, D = map(int, input().split())
X = [list(map(int, input().split())) for i in range(N)]
ans = 0
for i in range(N - 1):
  for j in range(i +1, N):
    if dist(X[i], X[j]).is_integer():
      ans += 1
print(ans)
Problem p03074 (python)

Fixed submission:
import sys
input = lambda: sys.stdin.readline().rstrip()
from itertools import accumulate

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

S = input()


num = []

if S[0] == '0':
    num.append(0)

if S[-1] == '1':
    S += '0'
else:
    S += '1'    

cnt = 1
for i in range(len(S)-1):
    if S[i] == S[i+1]:
        cnt += 1
    else:
        num.append(cnt)
        cnt = 1 
if S[-2] == '0':
    num.append(0)

num = [0] + num
cum_sum = list(accumulate(num))

ans = 0

for i in range(0, len(cum_sum), 2):
    right = i + K * 2 + 1
    if i + K * 2 >= len(cum_sum): 
        right = len(cum_sum) - 1

    ans = max(ans, cum_sum[right] - cum_sum[i])
print(ans)

Buggy submission (verdict: Runtime Error):
import sys
input = lambda: sys.stdin.readline().rstrip()
from itertools import accumulate


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

S = input()

if len(S) == 1:
    print(1)
    exit()

cum_sum = [0]

tmp_S = S

if S[-1] == '0':
    tmp_S += '1'
else:
    tmp_S += '0'

cnt = 1
for i in range(len(tmp_S)-1):
    if tmp_S[i] == tmp_S[i+1]:
        cnt += 1
    else:
        cum_sum.append(cnt)
        cnt = 1

cum_sum = list(accumulate(cum_sum))

ans = 0
ran = K+K
if S[0] == '0':
    for i in range(0, len(cum_sum), 2):
        if i+ran < len(cum_sum):
            ans = max(ans, cum_sum[i+ran] - cum_sum[i])
else:
    for i in range(0, len(cum_sum), 2):
        if i+ran < len(cum_sum):
            ans = max(ans, cum_sum[i+ran+1] - cum_sum[i])

print(ans)

Buggy submission (verdict: Runtime Error):
import sys
input = lambda: sys.stdin.readline().rstrip()
from itertools import accumulate


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

S = input()

if len(S) == 1:
    print(1)
    exit()

cum_sum = [0]

tmp_S = S

if S[-1] == '0':
    tmp_S += '1'
else:
    tmp_S += '0'

cnt = 1
for i in range(len(tmp_S)-1):
    if tmp_S[i] == tmp_S[i+1]:
        cnt += 1
    else:
        cum_sum.append(cnt)
        cnt = 1

print(*cum_sum)
cum_sum = list(accumulate(cum_sum))
print(*cum_sum)

ans = 0
ran = K+K
if S[0] == '0':
    for i in range(0, len(cum_sum), 2):
        if i+ran < len(cum_sum):
            ans = max(ans, cum_sum[i+ran] - cum_sum[i])
else:
    for i in range(0, len(cum_sum), 2):
        if i+ran < len(cum_sum):
            ans = max(ans, cum_sum[i+ran+1] - cum_sum[i])

print(ans)

Buggy submission (verdict: Runtime Error):
import sys
input = lambda: sys.stdin.readline().rstrip()
from itertools import accumulate


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

S = input()

if len(S) == 1:
    print('1')
    exit()

cum_sum = [0]

tmp_S = S

if S[-1] == '0':
    tmp_S += '1'
else:
    tmp_S += '0'

cnt = 1
for i in range(len(tmp_S)-1):
    if tmp_S[i] == tmp_S[i+1]:
        cnt += 1
    else:
        cum_sum.append(cnt)
        cnt = 1

print(*cum_sum)
cum_sum = list(accumulate(cum_sum))
print(*cum_sum)

ans = 0
ran = K+K
if S[0] == '0':
    for i in range(0, len(cum_sum), 2):
        if i+ran < len(cum_sum):
            ans = max(ans, cum_sum[i+ran] - cum_sum[i])
else:
    for i in range(0, len(cum_sum), 2):
        if i+ran < len(cum_sum):
            ans = max(ans, cum_sum[i+ran+1] - cum_sum[i])

print(ans)