Problem:


Score : 400 points

Problem Statement

N people are arranged in a row from left to right.

You are given a string S of length N consisting of 0 and 1, and a positive integer K.

The i-th person from the left is standing on feet if the i-th character of S is 0, and standing on hands if that character is 1.

You will give the following direction at most K times (possibly zero):

Direction: Choose integers l and r satisfying 1 \leq l \leq r \leq N, and flip the l-th, (l+1)-th, ..., and r-th persons. That is, for each i = l, l+1, ..., r, the i-th person from the left now stands on hands if he/she was standing on feet, and stands on feet if he/she was standing on hands.

Find the maximum possible number of consecutive people standing on hands after at most K directions.

Constraints

N is an integer satisfying 1 \leq N \leq 10^5.

K is an integer satisfying 1 \leq K \leq 10^5.

The length of the string S is N.

Each character of the string S is 0 or 1.

Input

Input is given from Standard Input in the following format:

N K
S

Output

Print the maximum possible number of consecutive people standing on hands after at most K directions.

Sample Input 1

5 1
00010

Sample Output 1

4

We can have four consecutive people standing on hands, which is the maximum result, by giving the following direction:

Give the direction with l = 1, r = 3, which flips the first, second and third persons from the left.

Sample Input 2

14 2
11101010110011

Sample Output 2

8

Sample Input 3

1 1
1

Sample Output 3

1

No directions are necessary.



Sample input:
5 1
00010


Sample output:
4

Submission status: Wrong Answer

Solution code:
from collections import deque,defaultdict,Counter
from itertools import accumulate
import bisect
from heapq import heappop,heappush
from fractions import gcd
from copy import deepcopy
import math
import queue
#import numpy as np
Mod = 1000000007




def main(): #startline--------------------------------------------
    N,K = map(int,input().split())
    S = input()
    anslist = []
    if S.count("0") == 0:
        print(len(S))
        exit()
    S += "A"
    cnt = 0
    keep = "start"
    for i in S:
        if keep == "start":
            keep = i
        elif keep == i:
            keep = i
        else:
            keep = i
            anslist.append(cnt)
        cnt += 1
    p = len(anslist)
    ans = 0
    if S[0] == "1":    
        for i in range(p-2*K-1):
            ans = max(anslist[i+2*K+1]-anslist[i],ans)
    else:
        anslist.insert(0,1)
        for i in range(p-2*K):
            ans = max(anslist[i+2*K+1]-anslist[i],ans)



if __name__ == "__main__":
    main() #endline===============================================





def sieve_of_eratosthenes(n):
    if not isinstance(n,int):
        raise TypeError("n is not int")
    if n<2:
        raise ValueError("n < 2 is not effective")
    prime = [1]*(n+1)
    for i in range(2,int(math.sqrt(n))):
        if prime[i] == 1:
            for j in range(2*i,n):
                if j%i == 0:
                    prime[j] = 0
    res = []
    for i in range(2,n):
        if prime[i] == 1:
            res.append(i)
    return res

class UnionFind:
    def __init__(self,n):
        self.parent = [i for i in range(n+1)]
        self.rank = [0 for i in range(n+1)]
    
    def findroot(self,x):
        if x == self.parent[x]:
            return x
        else:
            y = self.parent[x]
            y = self.findroot(self.parent[x])
            return y
    
    def union(self,x,y):
        px = self.findroot(x)
        py = self.findroot(y)
        if px < py:
            self.parent[py] = px
        else:
            self.parent[px] = py

    def same_group_or_no(self,x,y):
        return self.findroot(x) == self.findroot(y)

Submission status: Wrong Answer

Solution code:
# coding:utf-8
import sys
from collections import deque
sys.setrecursionlimit(2**31-1)
input = sys.stdin.readline
write = sys.stdout.write
LMIIS = lambda : list(map(int,input().split()))
II = lambda : int(input())
dbg = lambda *something : print(*something) if DEBUG is True else 0
DEBUG = False

def main():
    N,K = LMIIS()
    S = input().strip()
    nums = []
    count = 0
    conti = '0'
    for s in S:
        if s == conti:
            count += 1
        else:
            conti = s
            nums.append(count)
            count = 1
    nums.append(count)

    i = 0
    tmpsum = sum(nums[:2*K])
    max_c = tmpsum
    for i in range(2*K+1,len(nums),2):
        tmpsum = tmpsum - nums[i- 2*K -1] - nums[i- 2*K] + nums[i-1] + nums[i]
        max_c = max(max_c,tmpsum)
    
    tmpsum = sum(nums[1:2*K+2])
    max_c = max(max_c,tmpsum)
    for i in range(2*K+3,len(nums),2):
        tmpsum = tmpsum - nums[i- 2*K -2] - nums[i-2*K -1] + nums[i-1] + nums[i]
        max_c = max(max_c,tmpsum)

    print(max_c)
        


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

if s[0] == '1':
    length_01 = [0]
else:
    length_01 = [0,0]

for i in range(n-1):
    if s[i] != s[i+1]:
        length_01.append(i+1)

if s[n-1] == '1':
    length_01.append(n)

num_0 = int(len(length_01)/2)
if num_0 - 1 >= k:
    answer = 0
    for i in range(num_0 - k):
        if answer < length_01[2*(i+k)+1] - length_01[2*i]:
            answer = length_01[2*(i+k)+1] - length_01[2*i]
else:
    answer = n

print(answer)

Submission status: Wrong Answer

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

if s[0] == '1':
    length_01 = [0]
else:
    length_01 = [0,0]

for i in range(n-1):
    if s[i] != s[i+1]:
        length_01.append(i+1)

if s[n-1] == '1':
    length_01.append(n)

num_0 = int((len(length_01)+1)/2)
if num_0 - 1 >= k:
    answer = 0
    for i in range(num_0 - k):
        if answer < length_01[2*(i+k)+1] - length_01[2*i]:
            answer = length_01[2*(i+k)+1] - length_01[2*i]
else:
    answer = n

print(answer)

Submission status: Wrong Answer

Solution code:
import itertools

def getint(): return int(input())
def getints(): return list(map(int, input().split()))

n,k=getints()
s=input()
n=len(s)

rl = []
now, cnt = '0', 0

for i in range(n):
    if now != s[i]:
        rl.append(cnt)
        now = s[i]
        cnt = 1
    else:
        cnt += 1
if cnt > 0:
    rl.append(cnt)
acc=[0]*len(rl)
acc[0]=rl[0]
for i in range(1, len(rl)):
    acc[i] = acc[i-1]+rl[i]

# print(rl)
# print(acc)

res = 0
for i in range(len(acc)):
    if i % 2 == 0:
        i0 = i - 2
        j  = min(i + 2 * k - 1, len(acc) - 1)
    else:
        i0 = i - 1
        j  = min(i + 2 * k, len(acc) - 1)
    print(i,i0,j)
    tmp1 = acc[i0] if i0 >= 0      else 0
    tmp2 = acc[j]  if j < len(acc) else 0
    res = max(res, tmp2-tmp1)


print(res)

Submission status: Wrong Answer

Solution code:
import itertools

def getint(): return int(input())
def getints(): return list(map(int, input().split()))

n,k=getints()
s=input()
n=len(s)

rl = []
now, cnt = '0', 0

for i in range(n):
    if now != s[i]:
        rl.append(cnt)
        now = s[i]
        cnt = 1
    else:
        cnt += 1
if cnt > 0:
    rl.append(cnt)
acc=[0]*len(rl)
acc[0]=rl[0]
for i in range(1, len(rl)):
    acc[i] = acc[i-1]+rl[i]
res = 0
for i in range(len(acc)):
    if i % 2 == 0:
        i0 = i - 2
        j  = i + 2 * k - 1
    else:
        i0 = i - 1
        j  = i + 2 * k
    tmp1 = acc[i0] if i0 >= 0      else 0
    tmp2 = acc[j]  if j < len(acc) else 0
    res = max(res, tmp2-tmp1)


print(res)

Submission status: Wrong Answer

Solution code:
def solve(string):
    n, k, s = string.split()
    n, k = map(int, [n, k])

    def cul(s, k):
        s = "1" + s + "0"
        starts, ends = [1], []
        for i, (s0, s1) in enumerate(zip(s, s[1:])):
            if s0 == "0" and s1 == "1":
                starts.append(i + 1)
            elif s0 == "1" and s1 == "0":
                ends.append(i + 1)
        return max([e - s for s, e in zip(starts, ends[k:])]) if len(starts) > k else n

    return str(max(cul(s, k), cul(s.replace("0", "a").replace("1", "0").replace("a", "1"), k - 1)))


if __name__ == '__main__':
    print(solve('\n'.join([input(), input()])))

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
s=list(map(int,str(input())))
if s[0]==1:
  one_st=[0]+[i+1 for i in range(n-1) if s[i]==0 and s[i+1]==1]
  zero_st=[i+1 for i in range(n-1) if s[i]==1 and s[i+1]==0]
else:
  one_st=[i+1 for i in range(n-1) if s[i]==0 and s[i+1]==1]
  zero_st=[0]+[i+1 for i in range(n-1) if s[i]==1 and s[i+1]==0]
if s[n-1]==1:
  one_en=[i for i in range(n-1) if s[i]==1 and s[i+1]==0]+[n-1]
  zero_en=[i for i in range(n-1) if s[i]==0 and s[i+1]==1]
else:
  one_en=[i for i in range(n-1) if s[i]==1 and s[i+1]==0]
  zero_en=[i for i in range(n-1) if s[i]==0 and s[i+1]==1]+[n-1]
if len(zero_st) <= k:
  x=n
else:
  y=[one_en[i+k]-one_st[i]+1 for i in range(len(zero_st)-k+1) if i+k+1<=len(one_en)]
  y=y+[one_en[k-1]+1,n-one_st[len(one_st)-k-1]]
  x=max(y)
print(x)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
s=list(map(int,str(input())))
if s[0]==1:
  one_st=[0]+[i+1 for i in range(n-1) if s[i]==0 and s[i+1]==1]
  zero_st=[i+1 for i in range(n-1) if s[i]==1 and s[i+1]==0]
else:
  one_st=[i+1 for i in range(n-1) if s[i]==0 and s[i+1]==1]
  zero_st=[0]+[i+1 for i in range(n-1) if s[i]==1 and s[i+1]==0]
if s[n-1]==1:
  one_en=[i for i in range(n-1) if s[i]==1 and s[i+1]==0]+[n-1]
  zero_en=[i for i in range(n-1) if s[i]==0 and s[i+1]==1]
else:
  one_en=[i for i in range(n-1) if s[i]==1 and s[i+1]==0]
  zero_en=[i for i in range(n-1) if s[i]==0 and s[i+1]==1]+[n-1]
if len(zero_st) <= k:
  x=n
else:
  y=[one_en[i+k]-one_st[i]+1 for i in range(len(zero_st)-k+1) if i+k+1<=len(one_en)]
  y=y+[one_en[k-1]+1,n-one_st[len(zero_st)-k-1]]
  x=max(y)
print(x)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
s=list(map(int,str(input())))
if s[0]==1:
  one_st=[0]+[i+1 for i in range(n-1) if s[i]==0 and s[i+1]==1]
  zero_st=[i+1 for i in range(n-1) if s[i]==1 and s[i+1]==0]
else:
  one_st=[i+1 for i in range(n-1) if s[i]==0 and s[i+1]==1]
  zero_st=[0]+[i+1 for i in range(n-1) if s[i]==1 and s[i+1]==0]
if s[n-1]==1:
  one_en=[i for i in range(n-1) if s[i]==1 and s[i+1]==0]+[n-1]
  zero_en=[i for i in range(n-1) if s[i]==0 and s[i+1]==1]
else:
  one_en=[i for i in range(n-1) if s[i]==1 and s[i+1]==0]
  zero_en=[i for i in range(n-1) if s[i]==0 and s[i+1]==1]+[n-1]
if len(zero_st) <= k:
  x=n
else:
  y=[one_en[i+k]-one_st[i]+1 for i in range(len(zero_st)-k+1) if i+k+1<=len(one_en)]
  print(y)
  y=y+[one_en[k-1]+1,n-one_st[len(zero_st)-k-1]]
  print(y)
  x=max(y)
print(x)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
s=list(map(int,str(input())))
if s[0]==1:
  one_st=[0]+[i+1 for i in range(n-1) if s[i]==0 and s[i+1]==1]
  zero_st=[i+1 for i in range(n-1) if s[i]==1 and s[i+1]==0]
else:
  one_st=[i+1 for i in range(n-1) if s[i]==0 and s[i+1]==1]
  zero_st=[0]+[i+1 for i in range(n-1) if s[i]==1 and s[i+1]==0]
if s[n-1]==1:
  one_en=[i for i in range(n-1) if s[i]==1 and s[i+1]==0]+[n-1]
  zero_en=[i for i in range(n-1) if s[i]==0 and s[i+1]==1]
else:
  one_en=[i for i in range(n-1) if s[i]==1 and s[i+1]==0]
  zero_en=[i for i in range(n-1) if s[i]==0 and s[i+1]==1]+[n-1]
if len(zero_st) <= k:
  x=n
else:
  y=[one_en[i+k]-one_st[i]+1 for i in range(len(zero_st)-k+1) if i+k+1<=len(one_en)]
  y=y+[one_en[k-1],one_st[len(one_st)-k-1]]
  x=max(y)
print(x)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
s=list(map(int,str(input())))
if s[0]==1:
  one_st=[0]+[i+1 for i in range(n-1) if s[i]==0 and s[i+1]==1]
  zero_st=[i+1 for i in range(n-1) if s[i]==1 and s[i+1]==0]
else:
  one_st=[i+1 for i in range(n-1) if s[i]==0 and s[i+1]==1]
  zero_st=[0]+[i+1 for i in range(n-1) if s[i]==1 and s[i+1]==0]
if s[n-1]==1:
  one_en=[i for i in range(n-1) if s[i]==1 and s[i+1]==0]+[n-1]
  zero_en=[i for i in range(n-1) if s[i]==0 and s[i+1]==1]
else:
  one_en=[i for i in range(n-1) if s[i]==1 and s[i+1]==0]
  zero_en=[i for i in range(n-1) if s[i]==0 and s[i+1]==1]+[n-1]
if len(one_st) <= k:
  x=n
else:
  y=[one_en[i+k]-one_st[i]+1 for i in range(len(zero_st)-k+1) if i+k+1<=len(one_en)]
  y=y+[one_en[k-1],one_st[len(one_st)-k-1]]
  x=max(y)
print(x)

Submission status: Wrong Answer

Solution code:
def solve(string):
    n, k, s = string.split()
    n, k = map(int, [n, k])
    s = "1" + s + "0"
    starts = [1]
    ends = []
    for i, (s0, s1) in enumerate(zip(s, s[1:])):
        if s0 == "0" and s1 == "1":
            starts.append(i + 1)
        elif s0 == "1" and s1 == "0":
            ends.append(i + 1)
    return str(max([e - s for s, e in zip(starts, ends[k:])])) if len(starts) > k else str(n)


if __name__ == '__main__':
    print(solve('\n'.join([input(), input()])))

Submission status: Wrong Answer

Solution code:
n, k = (int(i) for i in input().split())
s = input()
s += "2"
l = [0]
cnt = 0
for i in range(n):
  cnt += 1
  if s[i] != s[i+1]:
    l.append(cnt)
    cnt = 0

#print(l)
l = [0] + l
l = l + [0]

from itertools import accumulate as ac
l = list(ac(l))

#print(l)
max=0
for i in range(2*k+1, len(l))[::2]:
  kukan = l[i] - l[i - (2*k+1)]

  if kukan > max:
    max = kukan

print(max)

Submission status: Wrong Answer

Solution code:
n, k = (int(i) for i in input().split())
s = input()
s += "2"
l = [0]
cnt = 0
for i in range(n):
  cnt += 1
  if s[i] != s[i+1]:
    l.append(cnt)
    cnt = 0

print(l)
l = [0] + l
l = l + [0]

from itertools import accumulate as ac
l = list(ac(l))

print(l)
max=0
for i in range(2*k+1, len(l))[::2]:
  kukan = l[i] - l[i - (2*k+1)]

  if kukan > max:
    max = kukan

print(max)

Submission status: Wrong Answer

Solution code:
n, k = (int(i) for i in input().split())
s = input()
s += "2"
l = [0]
cnt = 0
for i in range(n):
  cnt += 1
  if s[i] != s[i+1]:
    l.append(cnt)
    cnt = 0

#print(l)
from itertools import accumulate as ac
l = list(ac(l))
if s[0] == "0":
  l = [0] + l
#print(l)
max=0
for i in range(2*k+1, len(l))[::2]:
  kukan = l[i] - l[i - (2*k+1)]
  if kukan > max:
    max = kukan
print(max)

Submission status: Wrong Answer

Solution code:
n, k = (int(i) for i in input().split())
s = input()
s += "2"
l = [0]
cnt = 0
for i in range(n):
  cnt += 1
  if s[i] != s[i+1]:
    l.append(cnt)
    cnt = 0
    
if s[-1] == "0":
  l += [0]
#print(l)
from itertools import accumulate as ac
l = list(ac(l))
if s[0] == "0":
  l = [0] + l
#print(l)
max=0
for i in range(2*k+1, len(l))[::2]:
  kukan = l[i] - l[i - (2*k+1)]
  if kukan > max:
    max = kukan
print(max)

Submission status: Wrong Answer

Solution code:
n, k = (int(i) for i in input().split())
s = input()
s += "2"
l = [0]
cnt = 0
for i in range(n):
  cnt += 1
  if s[i] != s[i+1]:
    l.append(cnt)
    cnt = 0

l += [0]
#print(l)
from itertools import accumulate as ac
l = list(ac(l))
if s[0] == "0":
  l = [0] + l
#print(l)
max=0
for i in range(2*k+1, len(l))[::2]:
  kukan = l[i] - l[i - (2*k+1)]
  if kukan > max:
    max = kukan
print(max)

Submission status: Wrong Answer

Solution code:
n, k = (int(i) for i in input().split())
s = input()
s += "2"
l = [0]
cnt = 0
for i in range(n):
  cnt += 1
  if s[i] != s[i+1]:
    l.append(cnt)
    cnt = 0

l += [0]
print(l)
from itertools import accumulate as ac
l = list(ac(l))
if s[0] == "0":
  l = [0] + l
print(l)
max=0
for i in range(2*k+1, len(l))[::2]:
  kukan = l[i] - l[i - (2*k+1)]
  if kukan > max:
    max = kukan
print(max)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import sys


def get_num():
    return int(input())


def get_nums(n):
    res = [0 for _ in range(n)]
    for i in range(n):
        res[i] = int(input())
    return res


def get_numbers():
    return map(int, input().split())

n, k = get_numbers()
s = input()

rle = list()
last_c = s[0]
cnt = 1
for c in s[1:]:
    if c == last_c:
        cnt += 1
    else:
        rle.append(cnt)
        cnt = 1
    last_c = c
rle.append(cnt)
# print(rle)

if s[0] == '0':
    step = 2 * k
else:
    step = 2 * k + 1

ans = 0
for l in range(len(rle)):
    r = min(l + step, len(rle))
    ans = max(ans, sum(rle[l:r]))

print(ans)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
S = input()
a = S.split('1')
a = [len(i) for i in a if len(i) != 0]
b = S.split('0')
b = [len(i) for i in b if len(i) != 0]

c = [0]
d = 0
for i in range(len(a)+len(b)):
    if S[0] == '0':
        if i%2 == 0:
            d += a[int(i/2)]
        else:
            d += b[int(i/2)]
    else:
        if i%2 == 0:
            d += b[int(i/2)]
        else:
            d += a[int(i/2)]
    c.append(d)
mc=0
e=0
for i in range(1,len(a)+len(b)+1):
    if S[0] == '0':
        if i%2 != '0':
            if i+2*K-1 >= len(a)+len(b)+1:
                e = c[-1] - c[i-1]
            else:
                e = c[i+2*K-1] - c[i-1]
        else:
            if i+2*K >= len(a)+len(b)+1:
                e = c[-1] - c[i-1]
            else:
                e = c[i+2*K] - c[i-1]
    else:
        if i%2 != '0':
            if i+2*K >= len(a)+len(b)+1:
                e = c[-1] - c[i-1]
            else:
                e = c[i+2*K] - c[i-1]
        else:
            if i+2*K-1 >= len(a)+len(b)+1:
                e = c[-1] - c[i-1]
            else:
                e = c[i+2*K-1] - c[i-1]
            
    if mc < e:
        mc = e
print(mc)

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
S = (input())
a = S.split('1')
a = [len(i) for i in a if len(i)!=0]

b = S.split('0')
b = [len(i) for i in b if len(i)!=0]

m = 0
c = [0]
for i in range(len(a)+len(b)):
    if S[0] == '0':
        if i%2==0:
            m+=a[int(i/2)]
        else:
            m+=b[int(i/2)]
        c.append(m)
    else:
        if i%2==0:
            m+=b[int(i/2)]
        else:
            m+=a[int(i/2)]
        c.append(m)
mc=0
try:
    if S[0]=='0':
        if 1+2*K-1 >len(c) -1:
            mc = 1
    else:
        if 1+2*K > len(c) -1:
            mc =1
    for i in range(1,len(c)):
        d=0
        if S[0]=='0':
            if i%2!=0:
                d=c[i+2*K-1] - c[i-1]
            else:
                d=c[i+2*K] - c[i-1]
        else:
            if i%2==0:
                d=c[i+2*K-1] - c[i-1]
            else:
                d=c[i+2*K] - c[i-1]
        if mc < d:
            mc = d
except:        
    print(mc)
else:
   
    print(mc)

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
S = (input())
a = S.split('1')
a = [len(i) for i in a if len(i)!=0]

b = S.split('0')
b = [len(i) for i in b if len(i)!=0]
m = 0
c = [0]
for i in range(len(a)+len(b)):
    if S[0] == '0':
        if i%2==0:
            m+=a[int(i/2)]
        else:
            m+=b[int(i/2)]
        c.append(m)
    else:
        if i%2==0:
            m+=b[int(i/2)]
        else:
            m+=a[int(i/2)]
        c.append(m)
mc=0
try:
    for i in range(1,len(c)):
        d=0
        if S[0]=='0':
            if i%2!=0:
                d=c[i+2*K-1] - c[i-1]
            else:
                d=c[i+2*K] - c[i-1]
        else:
            if i%2==0:
                d=c[i+2*K-1] - c[i-1]
            else:
                d=c[i+2*K] - c[i-1]
        if mc < d:
            mc = d
except:
    if len(S)==1 and S[0]=='1':
        mc = 1
    if len(S)==1 and S[0]=='0':
        mc = 1
    print(mc)
else:
   
    print(mc)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
s = input()
#---------------------
arr = []
if(s[0] == '0'):
  arr.append(0)
for i in range(n-1):
  if(s[i] != s[i+1]):
    arr.append(i+1)
arr.append(n)
  
width = 2*k + 1
len_arr = len(arr)

if(2*k >= len_arr):
  ans = arr[-1]
else:
  ans = arr[2*k]
  left = 2
  while True:
    if(left + width > len_arr):
      break
    tmp = arr[left + width - 1] - arr[left - 1]
    ans = max(ans, tmp)
    left += 2
print(ans)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
s = input()
#---------------------
arr = []
if(s[0] == '0'):
  arr.append(0)
for i in range(n-1):
  if(s[i] != s[i+1]):
    arr.append(i+1)
arr.append(n)
  
width = 2*k + 1
len_arr = len(arr)

if(2*k > len_arr):
  ans = arr[-1]
else:
  ans = arr[2*k]
  left = 2
  while True:
    if(left + width > len_arr):
      break
    tmp = arr[left + width - 1] - arr[left - 1]
    ans = max(ans, tmp)
    left += 2
print(ans)

Submission status: Wrong Answer

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

count_list = []
count = 0

if S[0] == '0':
    check = 'b'
    for i in range(N):
        if i != N-1:
            if check == 'a' and S[i] == '1':
                count += 1
            elif check == 'a'and S[i] == '0':
                count_list.append(count)
                count = 1
                check = 'b'
            elif check == 'b' and S[i] == '1':
                count_list.append(count)
                count = 1
                check = 'a'
            elif check == 'b' and S[i] == '0':
                count += 1
        elif i == N-1:
            if check == 'a' and S[i] == '1':
                count += 1
                count_list.append(count)
            elif check == 'a'and S[i] == '0':
                count_list.append(count)
                count = 1
                check = 'b'
                count_list.append(count)
            elif check == 'b' and S[i] == '1':
                count_list.append(count)
                count = 1
                check = 'a'
                count_list.append(count)
            elif check == 'b' and S[i] == '0':
                count += 1
                count_list.append(count)


elif S[0] == '1':
    check = 'a'
    for i in range(N):
        if i != N-1:
            if check == 'a' and S[i] == '1':
                count += 1
            elif check == 'a'and S[i] == '0':
                count_list.append(count)
                count = 1
                check = 'b'
            elif check == 'b' and S[i] == '1':
                count_list.append(count)
                count = 1
                check = 'a'
            elif check == 'b' and S[i] == '0':
                count += 1
        elif i == N-1:
            if check == 'a' and S[i] == '1':
                count += 1
                count_list.append(count)
            elif check == 'a'and S[i] == '0':
                count_list.append(count)
                count = 1
                check = 'b'
                count_list.append(count)
            elif check == 'b' and S[i] == '1':
                count_list.append(count)
                count = 1
                check = 'a'
                count_list.append(count)
            elif check == 'b' and S[i] == '0':
                count += 1
                count_list.append(count)

                    



if len(count_list) == 1:
    m = 1
elif S[0] == '1' and S[-1] == '1':
    m = (len(count_list) + 1) // 2 - K  
    m = max(1, m)

elif S[0] == '1' and S[-1] == '0':
    m =  (len(count_list)) // 2 - K +1
    m = max(1, m)
    
elif S[0] == '0' and S[-1] == '1':
    m =  (len(count_list)) // 2 - K +1
    m = max(1, m)
    
elif S[0] == '0' and S[-1] == '0':
    m =  (len(count_list) - 1) // 2 - K + 2
    m = max(1, m)



ans = 0

h = len(count_list)

if S[0] == '1' and S[-1] == '1':
    for i in range(m):
        j = 2*i
        if sum(count_list[j:min(j+2*K+1, h)])  > ans :
            ans = sum(count_list[j:min(j+2*K+1, h)])


elif S[0] == '1' and S[-1] == '0':
    for i in range(m):
        j = 2*i
        if i != m-1:
            if sum(count_list[j:min(j+2*K+1,h)])  > ans :
                ans = sum(count_list[j:min(j+2*K+1,h)])  
        else:
            if sum(count_list[j:min(j+2*K, h)]) > ans:
                ans = sum(count_list[j:min(j+2*K, h)])
    
    
elif S[0] == '0' and S[-1] == '1':
    for i in range(m):
        j = 2*i
        if i != 0:
            if sum(count_list[j-1:min(j+2*K, h)])   > ans:
                ans = sum(count_list[j-1:min(j+2*K, h)])  
        else:
            if sum(count_list[j:min(j+2*K, h)]) > ans:
                ans = sum(count_list[j:min(j+2*K, h)])

    
elif S[0] == '0' and S[-1] == '0':
    for i in range(m):
        j = 2*i
        if i == 0:
            if sum(count_list[j:min(j+2*K, h)])   > ans :
                ans = sum(count_list[j:min(j+2*K, h)])  

        elif i == m-1:
            if sum(count_list[j-1:min(j+2*K, h)])  > ans :
                ans = sum(count_list[j-1:min(j+2*K, h)]) 
        else:
            if sum(count_list[j:min(j+2*K+1, h)]) > ans:
                ans = sum(count_list[j:min(j+2*K+1, h)])

print(ans)

Submission status: Wrong Answer

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

count_list = []
count = 0

if S[0] == '0':
    check = 'b'
    for i in range(N):
        if i != N-1:
            if check == 'a' and S[i] == '1':
                count += 1
            elif check == 'a'and S[i] == '0':
                count_list.append(count)
                count = 1
                check = 'b'
            elif check == 'b' and S[i] == '1':
                count_list.append(count)
                count = 1
                check = 'a'
            elif check == 'b' and S[i] == '0':
                count += 1
        elif i == N-1:
            if check == 'a' and S[i] == '1':
                count += 1
                count_list.append(count)
            elif check == 'a'and S[i] == '0':
                count_list.append(count)
                count = 1
                check = 'b'
                count_list.append(count)
            elif check == 'b' and S[i] == '1':
                count_list.append(count)
                count = 1
                check = 'a'
                count_list.append(count)
            elif check == 'b' and S[i] == '0':
                count += 1
                count_list.append(count)


elif S[0] == '1':
    check = 'a'
    for i in range(N):
        if i != N-1:
            if check == 'a' and S[i] == '1':
                count += 1
            elif check == 'a'and S[i] == '0':
                count_list.append(count)
                count = 1
                check = 'b'
            elif check == 'b' and S[i] == '1':
                count_list.append(count)
                count = 1
                check = 'a'
            elif check == 'b' and S[i] == '0':
                count += 1
        elif i == N-1:
            if check == 'a' and S[i] == '1':
                count += 1
                count_list.append(count)
            elif check == 'a'and S[i] == '0':
                count_list.append(count)
                count = 1
                check = 'b'
                count_list.append(count)
            elif check == 'b' and S[i] == '1':
                count_list.append(count)
                count = 1
                check = 'a'
                count_list.append(count)
            elif check == 'b' and S[i] == '0':
                count += 1
                count_list.append(count)

                    



if len(count_list) == 1:
    m = 1
elif S[0] == '1' and S[-1] == '1':
    m = (len(count_list) + 1) // 2 - K  
    m = max(1, m)

elif S[0] == '1' and S[-1] == '0':
    m =  (len(count_list)) // 2 - K +1
    m = max(1, m)
    
elif S[0] == '0' and S[-1] == '1':
    m =  (len(count_list)) // 2 - K +1
    m = max(1, m)
    
elif S[0] == '0' and S[-1] == '0':
    m =  (len(count_list) - 1) // 2 - K + 2
    m = max(1, m)



ans_list = [0] * m

h = len(count_list)

if S[0] == '1' and S[-1] == '1':
    for i in range(m):
        j = 2*i
        ans_list[i] = sum(count_list[j:min(j+2*K+1, h)])  


elif S[0] == '1' and S[-1] == '0':
    for i in range(m):
        j = 2*i
        if i != m-1:
            ans_list[i] = sum(count_list[j:min(j+2*K+1,h)])  
        else:
            ans_list[i] = sum(count_list[j:min(j+2*K, h)])
    
elif S[0] == '0' and S[-1] == '1':
    for i in range(m):
        j = 2*i
        if i != 0:
            ans_list[i] = sum(count_list[j-1:min(j+2*K, h)])  
        else:
            ans_list[i] = sum(count_list[j:min(j+2*K, h)])
    
elif S[0] == '0' and S[-1] == '0':
    for i in range(m):
        j = 2*i
        if i == 0:
            ans_list[i] = sum(count_list[j:min(j+2*K, h)])  
        elif i == m-1:
            ans_list[i] = sum(count_list[j-1:min(j+2*K, h)]) 
        else:
            ans_list[i] = sum(count_list[j:min(j+2*K+1, h)])

print(max(ans_list))

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
S = input()

ctr = []
cur = ''
cnt = 0
for i in range(N):
    if len(cur) == 0:
        cur = S[i]
        cnt = 1
    elif cur != S[i]:
        ctr.append(cnt)
        cur = S[i]
        cnt = 1
    else:
        cnt += 1
else:
    ctr.append(cnt)

ans = 0
for i in range(max(len(ctr), N - K)):
    ans = max(ans, sum(ctr[i:i + 2 * K + 1]))
print(ans)

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
S = list(input())
#
A = [0,0]
a = 1
bef = "0"
cou = 0
for i in range(N):
    if bef!=S[i]:
        A.append(A[a]+cou)
        a+=1
        bef = S[i]
        cou = 1
    else:
        cou += 1
A.append(A[a]+cou)
a+=1
ans = 0
for i in range(a-1):
    if 2*i+2*K+1>a:break
    ans = max(ans,A[i*2+1+2*K]-A[i*2])
print(ans)

Submission status: Wrong Answer

Solution code:
def main():
	N, K = map(int, input().split())
	S = input()

	counts = [0]
	if S[0] == "0":
		counts.append(0)
	last_char = S[0]
	for s in S:
		if s == last_char:
			counts[-1] += 1
		else:
			counts.append(1)
			last_char = s
	if S[-1] == "0":
		counts.append(0)

	max_count = counts[0]
	for n in range(0, N, 2):
		l, r = n, n + K * 2 + 1
		if N <= r:
			break
		count = sum(counts[l:r])
		max_count = max(count, max_count)
	print(max_count)

main()

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
S = input()

before = S[0]
cnt = 0
same_nums = []
for i in range(N):
  if S[i] == before:
    cnt += 1
  else:
    same_nums.append(cnt)
    before = S[i]
    cnt = 1
same_nums.append(cnt)
if S[0] == '0':
  sum_temp = sum(same_nums[:2*K])
  ans = sum_temp
  for i in range(0, K+1, 2):
    sum_temp = sum_temp - sum(same_nums[i:i+2]) + sum(same_nums[i+2*K:i+2*(K+1)])
    ans = max(ans, sum_temp)
  print(ans)
else:
  sum_temp = sum(same_nums[:2*K+1])
  ans = sum_temp
  for i in range(0, K+1, 2):
    sum_temp = sum_temp - sum(same_nums[i:i+2]) + sum(same_nums[i+2*K+1:i+2*K+3])
    ans = max(ans, sum_temp)
  print(ans)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
S = input()

before = S[0]
cnt = 0
same_nums = []
for i in range(N):
  if S[i] == before:
    cnt += 1
  else:
    same_nums.append(cnt)
    before = S[i]
    cnt = 1
same_nums.append(cnt)
if S[0] == '0':
  sum_temp = sum(same_nums[:2*K])
  ans = sum_temp
  for i in range(0, K+1, 2):
    if i + 2*K <= len(same_nums):
      sum_temp = sum_temp - sum(same_nums[i:i+2]) + sum(same_nums[i+2*K:i+2*(K+1)])
      ans = max(ans, sum_temp)
    else:
      break
  print(ans)
else:
  sum_temp = sum(same_nums[:2*K+1])
  ans = sum_temp
  for i in range(0, K+1, 2):
    if i + 2*K+1 <= len(same_nums):
      sum_temp = sum_temp - sum(same_nums[i:i+2]) + sum(same_nums[i+2*K+1:i+2*K+3])
      ans = max(ans, sum_temp)
    else:
      break
  print(ans)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
S = input() + '2'

T = []
c = 1
for i in range(1, len(S)) :
    if S[i] == S[i-1] :
        c += 1
    else :
        T.append(c)
        c = 1
        
for i in range(1, len(T)) :
    T[i] += T[i-1]

ret = 0
for i in range(0 if S[0] == '1' else 1, len(T), 2) :
    if i - 2*K - 1 < 0 :
        ret = max(ret, T[i])
    else :
        ret = max(ret, T[i] - T[i-2*K-1])
    
print(ret)