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:
n,k=map(int,input().split())
s=input()
d=[]
f=True

if s[0]=="0":
    d.append(0)
    f=False

seq=1
for i in range(1,n):
    if s[i]=="0" and not f:
        seq+=1
    elif s[i]=="0" and f:
        f=False
        d.append(seq)
        seq=1
    elif s[i]=="1" and f:
        seq+=1
    else:
        f=True
        d.append(seq)
        seq=1
    if i==n-1:
      d.append(seq)
if s[-1]=="0":
    d.append(0)
l=len(d)
res=[0]*(l+1)
res[0]=0
for i in range(1,l+1):
    res[i]=res[i-1]+d[i-1]
ans=0
for i in range(1,n):
    if i%2==1:
        continue
    if 2*k+i>l:
      break
    a=res[2*k+i]-res[i-2]
    if a>ans:
        ans=a
print(ans)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
s=input()
from itertools import groupby
a=[len(list(j)) for i,j in groupby(s)]
if s[0]==0:
  a=[0]+a
if s[n-1]==0:
  a=a+[0]
w=2*k+1
if len(a)<=w:
  print(n)
  exit()
m=sum(a[:w])
x=m
for i in range(2,len(a)-2*k,2):
  m=m-a[i-1]-a[i-2]+a[i+k+1]+a[i+k+2]
  x=max(x,m)
print(x)

Submission status: Wrong Answer

Solution code:
from collections import deque
import numpy as np

N,K = (int(x) for x in input().split())
S = input()
count = []
d = deque(list(S))
now = d.popleft()
cum = 1
for i in range(len(S)-1):
    popped = d.popleft()
    if popped == now:
        cum += 1
    else:
        count.append(cum)
        now = popped
        cum = 1
count.append(cum)
cs = np.array(count).cumsum()
block = len(count)
if S[0] == '0':
    zeros = int(np.ceil(block/2))
    if K >= zeros:
        print(len(S))
    else:
        m = cs[K*2-1]
        if S[-1] == '1':
            for i in range(1,zeros-K+1):
                m = max(m,cs[2*i+2*K-1]-cs[2*i])
        else:
            for i in range(1,zeros-K):
                m = max(m,cs[2*i+2*K-1]-cs[2*i])
            m = max(m,cs[-1]-cs[-2*K-1])
        print(m)
else:
    zeros = int(np.floor(block/2))
    if K >= zeros:
        print(len(S))
    else:
        m = cs[2*K]
        if S[-1] == '1':
            for i in range(1,zeros-K+1):
                m = max(m,cs[2*i+2*K]-cs[2*i-1])
        else:
            for i in range(1,zeros-K):
                m = max(m,cs[2*i+2*K]-cs[2*i-1])
            m = max(m,cs[-1]-cs[-2*K-1])
        print(m)

Submission status: Wrong Answer

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

zero_list = []
for i in range(N):
    if S[i] == '0':
        if i > 0 and S[i - 1] == '0':
            zero_list[-1][0] += 1
            zero_list[-1][2] += 1
        else:
            zero_list.append([1, i, i])
            
if len(zero_list) <= K:
    ans = N
else:
    zero_c_max = sum(zero_list[:K][0])
    zero_c = sum(zero_list[:K][0])
    r = 0
    l = zero_list[K][1] - 1
    for j in range(1, len(zero_list) - K + 1):
        zero_c = zero_c - zero_list[j - 1][0] + zero_list[j + K - 1][0]
        if zero_c > zero_c_max:
            zero_c_max = zero_c
            r = zero_list[j - 1][2] + 1
            if j + K < len(zero_list): 
                l = zero_list[j + K][1] - 1
            else:
                l = N - 1
    
    ans = (l - r) + 1
    
print(ans)

Submission status: Wrong Answer

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

zero_list = []
for i in range(N):
    if S[i] == '0':
        if i > 0 and S[i - 1] == '0':
            zero_list[-1][0] += 1
            zero_list[-1][2] += 1
        else:
            zero_list.append([1, i, i])

if len(zero_list) <= K:
    ans = N
else:
    zero_c_max = sum(zero_list[:K][0])
    zero_c = sum(zero_list[:K][0])
    r = 0
    l = zero_list[K][1] - 1
    for j in range(1, len(zero_list) - K + 1):
        zero_c = zero_c - zero_list[j - 1][0] + zero_list[j + K - 1][0]
        if zero_c > zero_c_max:
            zero_c_max = zero_c
            r = zero_list[j - 1][2] + 1
            if j + K < len(zero_list): 
                l = zero_list[j + K][1] - 1
            else:
                l = N - 1
    
    ans = (l - r) + 1
    
print(ans)

Submission status: Wrong Answer

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

ns = []
now_num = s[0]
cnt = 0
for i in range(n):
    if now_num == s[i]:
        cnt += 1
    else:
        ns.append((now_num, cnt))
        now_num = s[i]
        cnt = 1
ns.append((now_num, cnt))
len_ns = len(ns)
pre = [0] * (len_ns + 1)
for i in range(len_ns):
    pre[i + 1] = pre[i] + ns[i][1]
print(ns)
print(pre)

ans = 1
for i in range(len_ns - 2*k + 1):
    temp = pre[i + 2*k] - pre[i]
    if i > 0 and ns[i - 1][0] == '1':temp += ns[i - 1][1]
    if i + 2*k < len_ns and ns[i + 2*k][0] == '1':temp += ns[i + 2*k][1]
    if temp > ans:ans = temp
print(ans if len_ns - 2*k + 1 > 0 else len(s))

Submission status: Wrong Answer

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

ns = []
now_num = s[0]
cnt = 0
for i in range(n):
    if now_num == s[i]:
        cnt += 1
    else:
        ns.append((now_num, cnt))
        now_num = s[i]
        cnt = 1
ns.append((now_num, cnt))
#print(ns)
len_ns = len(ns)
pre = [0] * (len_ns + 1)
for i in range(len_ns):
    pre[i + 1] = pre[i] + ns[i][1]
#print(pre)

ans = 1
for i in range(len_ns - 2*k + 1):
    temp = pre[i + 2*k] - pre[i]
    if ns[i - 1][0] == '1':temp += ns[i - 1][1]
    if i + 2*k < len_ns and ns[i + 2*k - 1][0] == '0':temp += ns[i + 2*k][1]
    if temp > ans:ans = temp
print(ans if len_ns - 2*k + 1 > 0 else len(s))

Submission status: Wrong Answer

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

ns = []
now_num = s[0]
cnt = 0
for i in range(n):
    if now_num == s[i]:
        cnt += 1
    else:
        ns.append((now_num, cnt))
        now_num = s[i]
        cnt = 1
ns.append((now_num, cnt))
#print(ns)
len_ns = len(ns)
pre = [0] * (len_ns + 1)
for i in range(len_ns):
    pre[i + 1] = pre[i] + ns[i][1]
#print(pre)

ans = 1
for i in range(len_ns - 2*k + 1):
    temp = pre[i + 2*k] - pre[i]
    if ns[i - 1][0] == '1':temp += ns[i - 1][1]
    elif i + 2*k != len_ns and ns[i + 2*k - 1][0] == '0':temp += ns[i + 2*k][1]
    if temp > ans:ans = temp
print(ans if len_ns - 2*k + 1 > 0 else len(s))

Submission status: Wrong Answer

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

ns = []
now_num = s[0]
cnt = 0
for i in range(n):
    if now_num == s[i]:
        cnt += 1
    else:
        ns.append((now_num, cnt))
        now_num = s[i]
        cnt = 1
ns.append((now_num, cnt))
#print(ns)
len_ns = len(ns)
pre = [0] * (len_ns + 1)
for i in range(len_ns):
    pre[i + 1] = pre[i] + ns[i][1]
#print(pre)

ans = 1
for i in range(len_ns - 2*k + 1):
    temp = pre[i + 2*k] - pre[i]
    if ns[i - 1][0] == '1':temp += ns[i - 1][1]
    elif i + 2*k != len_ns and ns[i + 2*k][0] == '0':temp += ns[i + 2*k][1]
    if temp > ans:ans = temp
print(ans if len_ns - 2*k + 1 > 0 else len(s))

Submission status: Wrong Answer

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

ns = []
now_num = s[0]
cnt = 0
for i in range(n):
    if now_num == s[i]:
        cnt += 1
    else:
        ns.append((now_num, cnt))
        now_num = s[i]
        cnt = 1
ns.append((now_num, cnt))
#print(ns)
len_ns = len(ns)
pre = [0] * (len_ns + 1)
for i in range(len_ns):
    pre[i + 1] = pre[i] + ns[i][1]
#print(pre)

ans = 1
for i in range(len_ns - 2*k + 1):
    temp = pre[i + 2*k] - pre[i]
    if ns[i - 1][0] == '1':temp += ns[i - 1][1]
    if temp > ans:ans = temp
print(ans if len_ns - 2*k + 1 > 0 else len(s))

Submission status: Wrong Answer

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

ns = []
now_num = s[0]
cnt = 0
for i in range(n):
    if now_num == s[i]:
        cnt += 1
    else:
        ns.append((now_num, cnt))
        now_num = s[i]
        cnt = 1
ns.append((now_num, cnt))
#print(ns)
len_ns = len(ns)
pre = [0] * (len_ns + 1)
for i in range(len_ns):
    pre[i + 1] = pre[i] + ns[i][1]
#print(pre)

ans = 1
for i in range(len_ns - 2*k + 1):
    temp = pre[i + 2*k] - pre[i]
    if ns[i - 1][0] == '1':temp += ns[i - 1][1]
    if temp > ans:ans = temp
print(ans)

Submission status: Wrong Answer

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

ns = []
now_num = s[0]
cnt = 0
for i in range(n):
    if now_num == s[i]:
        cnt += 1
    else:
        ns.append((now_num, cnt))
        now_num = s[i]
        cnt = 1
ns.append((now_num, cnt))
#print(ns)
len_ns = len(ns)
pre = [0] * (len_ns + 1)
for i in range(len_ns):
    pre[i + 1] = pre[i] + ns[i][1]
#print(pre)

ans = 1
for i in range(len_ns - 2*k):
    temp = pre[i + 2*k] - pre[i]
    if ns[i - 1][0] == '1':temp += ns[i - 1][1]
    if temp > ans:ans = temp
print(ans)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
s = input()
hand_list = []
for i in range(n):
    if i == 0:
        hand_list.append(i)
    else:
        if s[i] != s[i - 1]:
            hand_list.append(i)
hand_list.append(n)

ans_list = []
if len(hand_list) > 2 * k:
    for i in range(len(hand_list) - 2 * k - 1):
        if s[hand_list[i]] == '1':
            ans = hand_list[i + 2 * k + 1] - hand_list[i]
        else:
            ans = hand_list[i + 2 * k] - hand_list[i]
        ans_list.append(ans)
else:
    ans_list.append(n)
print(max(ans_list))

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
s = input()
hand_list = []
for i in range(n):
    if i == 0:
        hand_list.append(i)
    else:
        if s[i] != s[i - 1]:
            hand_list.append(i)
hand_list.append(n)

ans_list = []
if len(hand_list) > 2 * k:
    for i in range(len(hand_list) - 2 * k):
        if s[i] == '1':
            if i + 2 * k + 1 < len(hand_list):
                ans = hand_list[i + 2 * k + 1] - hand_list[i]
        else:
            if i == 0:
                ans = hand_list[i + 2 * k] - hand_list[i]
            else:
                ans = hand_list[i + 2 * k] - hand_list[i - 1]
        ans_list.append(ans)
else:
    ans_list.append(n)
print(max(ans_list))

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
s = input()
hand_list = []
for i in range(n):
    if i == 0:
        hand_list.append(i)
    else:
        if s[i] != s[i - 1]:
            hand_list.append(i)
hand_list.append(n)

ans_list = []
if len(hand_list) > 2 * k:
    for i in range(len(hand_list) - 2 * k):
        if s[i] == '1':
            if i + 2 * k + 1 < len(hand_list):
                ans = hand_list[i + 2 * k + 1] - hand_list[i]
        else:
            ans = hand_list[i + 2 * k] - hand_list[i]
        ans_list.append(ans)
else:
    ans_list.append(n)
print(max(ans_list))

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
s = input()
hand_list = []
for i in range(n):
    if i == 0:
        hand_list.append(i)
    else:
        if s[i] != s[i - 1]:
            hand_list.append(i)
hand_list.append(n)

ans_list = []
if len(hand_list) > 2 * k:
    for i in range(len(hand_list) - 2 * k):
        if s[i] == '1':
            if i + 2 * k + 1 < len(hand_list):
                ans = hand_list[i + 2 * k + 1] - hand_list[i]
        else:
            ans = hand_list[i + 2 * k] - hand_list[i]
            print(ans)
        ans_list.append(ans)
else:
    ans_list.append(n)
print(max(ans_list))

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
s = input()
hand_list = []
for i in range(n):
    if i == 0:
        hand_list.append(i)
    else:
        if s[i] != s[i - 1]:
            hand_list.append(i)
hand_list.append(n)

ans_list = []
if len(hand_list) > 2 * k:
    for i in range(len(hand_list) - 2 * k - 1):
        if s[i] == '1':
            ans = hand_list[i + 2 * k + 1] - hand_list[i]
        else:
            ans = hand_list[i + 2 * k] - hand_list[i]
        ans_list.append(ans)
else:
    ans_list.append(n)
print(max(ans_list))

Submission status: Wrong Answer

Solution code:
from collections import deque
from heapq import heapify,heappop,heappush,heappushpop
from copy import copy,deepcopy
from itertools import product,permutations,combinations,combinations_with_replacement
from collections import defaultdict,Counter
from bisect import bisect_left,bisect_right
# from math import gcd,ceil,floor,factorial
# from fractions import gcd
from functools import reduce
from pprint import pprint

INF = float("inf")

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

def mylistinput(n):
    return [ list(myinput()) for _ in range(n) ]

def mycol(data,col):
    return [ row[col] for row in data ]

def mysort(data,col,reverse_flag):
    data.sort(key=lambda x:x[col],reverse=reverse_flag)
    return data

def mymax(data):
    M = -1*float("inf")
    for i in range(len(data)):
        m = max(data[i])
        M = max(M,m)
    return M

def mymin(data):
    m = float("inf")
    for i in range(len(data)):
        M = min(data[i])
        m = min(m,M)
    return m

def mycount(ls,x):
    # lsはソート済みであること
    l = bisect_left(ls,x)
    r = bisect_right(ls,x)
    return (r-l)

def myoutput(ls,space=True):
    if space:
        if len(ls)==0:
            print(" ")
        elif type(ls[0])==str:
            print(" ".join(ls))
        elif type(ls[0])==int:
            print(" ".join(map(str,ls)))
        else:
            print("Output Error")
    else:
        if len(ls)==0:
            print("")
        elif type(ls[0])==str:
            print("".join(ls))
        elif type(ls[0])==int:
            print("".join(map(str,ls)))
        else:
            print("Output Error")

n,k = myinput()
S = input()

a = []
z = []
b = -1
for i in range(n):
    t = int(S[i])
    if i==0:
        if t==1:
            a.append(i)
        else:
            z.append(i)
        b = t
    else:
        if t==b:
            pass
        else:
            if t==1:
                a.append(i)
            else:
                z.append(i)
            b = t
# print(a)
# print(z)

if len(a)==0:
    print(n)
    exit()

if len(z)==0:
    print(n)
    exit()

ans = 1
for i in range(len(a)-k):
    l = a[i]
    r = a[i+k]
    ind = bisect_left(z,r)
    if ind >= len(z):
        rr = n-1
    else:
        rr = z[ind] - 1
    length = rr - l + 1
    # print(i,length)
    ans = max( ans, length )
# print(ans)
# k -= 1
for i in range(len(z)-k):
    l = z[i]
    r = z[i+k]
    rr = r - 1
    # ind = bisect_left(a,r)
    # if ind >= len(a):
    #     rr = n-1
    # else:
    #     rr = a[ind]
    length = rr - l + 1
    ans = max( ans, length )
print(ans)

Submission status: Wrong Answer

Solution code:
from collections import deque
from heapq import heapify,heappop,heappush,heappushpop
from copy import copy,deepcopy
from itertools import product,permutations,combinations,combinations_with_replacement
from collections import defaultdict,Counter
from bisect import bisect_left,bisect_right
# from math import gcd,ceil,floor,factorial
# from fractions import gcd
from functools import reduce
from pprint import pprint

INF = float("inf")

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

def mylistinput(n):
    return [ list(myinput()) for _ in range(n) ]

def mycol(data,col):
    return [ row[col] for row in data ]

def mysort(data,col,reverse_flag):
    data.sort(key=lambda x:x[col],reverse=reverse_flag)
    return data

def mymax(data):
    M = -1*float("inf")
    for i in range(len(data)):
        m = max(data[i])
        M = max(M,m)
    return M

def mymin(data):
    m = float("inf")
    for i in range(len(data)):
        M = min(data[i])
        m = min(m,M)
    return m

def mycount(ls,x):
    # lsはソート済みであること
    l = bisect_left(ls,x)
    r = bisect_right(ls,x)
    return (r-l)

def myoutput(ls,space=True):
    if space:
        if len(ls)==0:
            print(" ")
        elif type(ls[0])==str:
            print(" ".join(ls))
        elif type(ls[0])==int:
            print(" ".join(map(str,ls)))
        else:
            print("Output Error")
    else:
        if len(ls)==0:
            print("")
        elif type(ls[0])==str:
            print("".join(ls))
        elif type(ls[0])==int:
            print("".join(map(str,ls)))
        else:
            print("Output Error")

n,k = myinput()
S = input()

a = []
z = []
b = -1
for i in range(n):
    t = int(S[i])
    if i==0:
        if t==1:
            a.append(i)
        else:
            z.append(i)
        b = t
    else:
        if t==b:
            pass
        else:
            if t==1:
                a.append(i)
            else:
                z.append(i)
            b = t
# print(a)
# print(z)

ans = 1
for i in range(len(a)-k):
    l = a[i]
    r = a[i+k]
    ind = bisect_left(z,r)
    if ind >= len(z):
        rr = n-1
    else:
        rr = z[ind] - 1
    length = rr - l + 1
    # print(i,length)
    ans = max( ans, length )
# print(ans)
# k -= 1
for i in range(len(z)-k):
    l = z[i]
    r = z[i+k]
    rr = r - 1
    # ind = bisect_left(a,r)
    # if ind >= len(a):
    #     rr = n-1
    # else:
    #     rr = a[ind]
    length = rr - l + 1
    ans = max( ans, length )
print(ans)

Submission status: Wrong Answer

Solution code:
from collections import deque
from heapq import heapify,heappop,heappush,heappushpop
from copy import copy,deepcopy
from itertools import product,permutations,combinations,combinations_with_replacement
from collections import defaultdict,Counter
from bisect import bisect_left,bisect_right
# from math import gcd,ceil,floor,factorial
# from fractions import gcd
from functools import reduce
from pprint import pprint

INF = float("inf")

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

def mylistinput(n):
    return [ list(myinput()) for _ in range(n) ]

def mycol(data,col):
    return [ row[col] for row in data ]

def mysort(data,col,reverse_flag):
    data.sort(key=lambda x:x[col],reverse=reverse_flag)
    return data

def mymax(data):
    M = -1*float("inf")
    for i in range(len(data)):
        m = max(data[i])
        M = max(M,m)
    return M

def mymin(data):
    m = float("inf")
    for i in range(len(data)):
        M = min(data[i])
        m = min(m,M)
    return m

def mycount(ls,x):
    # lsはソート済みであること
    l = bisect_left(ls,x)
    r = bisect_right(ls,x)
    return (r-l)

def myoutput(ls,space=True):
    if space:
        if len(ls)==0:
            print(" ")
        elif type(ls[0])==str:
            print(" ".join(ls))
        elif type(ls[0])==int:
            print(" ".join(map(str,ls)))
        else:
            print("Output Error")
    else:
        if len(ls)==0:
            print("")
        elif type(ls[0])==str:
            print("".join(ls))
        elif type(ls[0])==int:
            print("".join(map(str,ls)))
        else:
            print("Output Error")

n,k = myinput()
S = input()

a = []
z = []
b = -1
for i in range(n):
    t = int(S[i])
    if i==0:
        if t==1:
            a.append(i)
        else:
            z.append(i)
        b = t
    else:
        if t==b:
            pass
        else:
            if t==1:
                a.append(i)
            else:
                z.append(i)
            b = t
# print(a)
# print(z)

ans = 1
for i in range(len(a)-k):
    l = a[i]
    r = a[i+k]
    ind = bisect_left(z,r)
    if ind >= len(z):
        rr = n-1
    else:
        rr = z[ind]
    length = rr - l
    ans = max( ans, length )
# print(ans)
# k -= 1
for i in range(len(z)-k):
    l = z[i]
    r = z[i+k]
    rr = r - 1
    # ind = bisect_left(a,r)
    # if ind >= len(a):
    #     rr = n-1
    # else:
    #     rr = a[ind]
    length = rr - l + 1
    ans = max( ans, length )
print(ans)

Submission status: Wrong Answer

Solution code:
from collections import deque
from heapq import heapify,heappop,heappush,heappushpop
from copy import copy,deepcopy
from itertools import product,permutations,combinations,combinations_with_replacement
from collections import defaultdict,Counter
from bisect import bisect_left,bisect_right
# from math import gcd,ceil,floor,factorial
# from fractions import gcd
from functools import reduce
from pprint import pprint

INF = float("inf")

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

def mylistinput(n):
    return [ list(myinput()) for _ in range(n) ]

def mycol(data,col):
    return [ row[col] for row in data ]

def mysort(data,col,reverse_flag):
    data.sort(key=lambda x:x[col],reverse=reverse_flag)
    return data

def mymax(data):
    M = -1*float("inf")
    for i in range(len(data)):
        m = max(data[i])
        M = max(M,m)
    return M

def mymin(data):
    m = float("inf")
    for i in range(len(data)):
        M = min(data[i])
        m = min(m,M)
    return m

def mycount(ls,x):
    # lsはソート済みであること
    l = bisect_left(ls,x)
    r = bisect_right(ls,x)
    return (r-l)

def myoutput(ls,space=True):
    if space:
        if len(ls)==0:
            print(" ")
        elif type(ls[0])==str:
            print(" ".join(ls))
        elif type(ls[0])==int:
            print(" ".join(map(str,ls)))
        else:
            print("Output Error")
    else:
        if len(ls)==0:
            print("")
        elif type(ls[0])==str:
            print("".join(ls))
        elif type(ls[0])==int:
            print("".join(map(str,ls)))
        else:
            print("Output Error")

n,k = myinput()
S = input()

a = []
z = []
b = -1
for i in range(n):
    t = int(S[i])
    if i==0:
        if t==1:
            a.append(i)
        else:
            z.append(i)
        b = t
    else:
        if t==b:
            pass
        else:
            if t==1:
                a.append(i)
            else:
                z.append(i)
            b = t
# print(a)
# print(z)

ans = 1
for i in range(len(a)-k):
    l = a[i]
    r = a[i+k]
    ind = bisect_left(z,r)
    if ind >= len(z):
        rr = n-1
    else:
        rr = z[ind]
    length = rr - l + 1
    ans = max( ans, length )
# print(ans)
k -= 1
for i in range(len(z)-k):
    l = z[i]
    r = z[i+k]
    ind = bisect_left(a,r)
    if ind >= len(a):
        rr = n-1
    else:
        rr = a[ind]
    length = rr - l + 1
    ans = max( ans, length )
print(ans)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
S=input()+"*"

A=[0] if S[0]=="1" else [0,0]
cnt,num=0,S[0]
for i in range(n+1):
    if num==S[i]:
        cnt +=1
    else:
        A.append(cnt)
        cnt=1
        num=S[i]

for i in range(1,len(A)):
    A[i]=A[i]+A[i-1]
ans=0
for i in range(0,len(A),2):
    try:ans=max(ans,A[i+k*2+1]-A[i])
    except:break
print(ans)

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
S = input()
row = []
count = 1
flag = S[0]
for i in range(1,N):
    if S[i] != flag:
        row.append([count,flag])
        count = 1
        flag = S[i]
    else:
        count += 1
row.append([count,flag])
inst = 0
ans,temp = 0,0
left,right = 0,0
for i in range(len(row)):
    if row[i][1] == "1":
        temp += row[i][0]
    else:
        inst += 1
        if inst <= K:
            temp += row[i][0]
        else:
            temp -= row[left][0] + row[left+1][0]
            temp += row[i][0]
            left += 2
    ans = max(ans,temp)
if row[0][1] == "0" and inst > K:
    temp += row[left-1][0]
    ans = max(ans,temp)
print(ans)

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
S = input()
row = []
count = 1
flag = S[0]
for i in range(1,N):
    if S[i] != flag:
        row.append([count,flag])
        count = 1
        flag = S[i]
    else:
        count += 1
row.append([count,flag])
inst = 0
ans,temp = 0,0
left,right = 0,0
for i in range(len(row)):
    if row[i][1] == "1":
        temp += row[i][0]
    else:
        if inst < K:
            temp += row[i][0]
            inst += 1
        else:
            temp -= row[left][0] + row[left+1][0]
            temp += row[i][0]
            left += 2
    ans = max(ans,temp)
if row[0][1] == "0" and inst > K:
    temp += row[left-1][0]
    ans = max(ans,temp)
print(ans)

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
S = input()
row = []
count = 1
flag = S[0]
for i in range(1,N):
    if S[i] != flag:
        row.append([count,flag])
        count = 1
        flag = S[i]
    else:
        count += 1
row.append([count,flag])
inst = 0
ans,temp = 0,0
left,right = 0,0
for i in range(len(row)):
    if row[i][1] == "1":
        temp += row[i][0]
    else:
        if inst < K:
            temp += row[i][0]
            inst += 1
        else:
            temp -= row[left][0] + row[left+1][0]
            temp += row[i][0]
            left += 2
    ans = max(ans,temp)
if row[0][1] == "0":
    temp += row[left-1][0]
    ans = max(ans,temp)
print(ans)

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
S = input()
row = []
count = 1
flag = S[0]
for i in range(1,N):
    if S[i] != flag:
        row.append([count,flag])
        count = 1
        flag = S[i]
    else:
        count += 1
row.append([count,flag])
print(row)
inst = 0
ans,temp = 0,0
left,right = 0,0
for i in range(len(row)):
    if row[i][1] == "1":
        temp += row[i][0]
    else:
        if inst < K:
            temp += row[i][0]
            inst += 1
        else:
            temp -= row[left][0] + row[left+1][0]
            temp += row[i][0]
            left += 2
    ans = max(ans,temp)
if row[0][1] == "0":
    temp += row[left-1][0]
    ans = max(ans,temp)
print(ans)

Submission status: Wrong Answer

Solution code:
def ranlength(A):
    num = list(A)
    count = 1
    result = ''
    table = []
    for i in range(len(num)):
        try:
            if num[i] == num[i+1]:
               count = count + 1
            else:
                result += str(num[i]) + str(count)
                table.append(count)
                count = 1
        except:
            result += str(num[i]) + str(count)
            table.append(count)
    return(table)
    
N,K=map(int,input().split())
S=input()

RL=ranlength(S)
#print(RL)

#しゃくとり法
ans1,ans2=0,0
if int(S[0])==1:
    cnt1=RL[0]
    ans1=RL[0]
    for i in range(len(RL)):
        if i%2==0 and i>=2:
            cnt1+=RL[i]
            cnt1+=RL[i-1]
            if i>=K*2+2:
                cnt1-=RL[i-(K*2+1)]
                cnt1-=RL[i-(K*2+2)]
            ans1=max(ans1,cnt1)
            #print(cnt1)
    #print(ans1)
elif int(S[0])==0:
    if len(RL)==1:
        ans2=RL[0]
    else:
        cnt2=RL[0]+RL[1]
        ans2=RL[0]+RL[1]
    for i in range(len(RL)):
        if i%2==1 and i>=3:
            cnt2+=RL[i]
            cnt2+=RL[i-1]
            if i==K*2+1:
                cnt2-=RL[0]
            elif i>=K*2+3:
                cnt2-=RL[i-(K*2+1)]
                cnt2-=RL[i-(K*2+2)]
            ans2=max(ans2,cnt2)
            #print(cnt2)            

RL=RL[::-1]
ans3,ans4=0,0
if int(S[0])==1:
    cnt1=RL[0]
    ans1=RL[0]
    for i in range(len(RL)):
        if i%2==0 and i>=2:
            cnt1+=RL[i]
            cnt1+=RL[i-1]
            if i>=K*2+2:
                cnt1-=RL[i-(K*2+1)]
                cnt1-=RL[i-(K*2+2)]
            ans3=max(ans3,cnt1)
            #print(cnt1)
    #print(ans3)
elif int(S[0])==0:
    if len(RL)==1:
        ans4=RL[0]
    else:
        cnt2=RL[0]+RL[1]
        ans2=RL[0]+RL[1]
    for i in range(len(RL)):
        if i%2==1 and i>=3:
            cnt2+=RL[i]
            cnt2+=RL[i-1]
            if i==K*2+1:
                cnt2-=RL[0]
            elif i>=K*2+3:
                cnt2-=RL[i-(K*2+1)]
                cnt2-=RL[i-(K*2+2)]
            ans4=max(ans4,cnt2)
            #print(cnt2)            

print(max([ans1,ans2,ans3,ans4]))

Submission status: Wrong Answer

Solution code:
def ranlength(A):
    num = list(A)
    count = 1
    result = ''
    table = []
    for i in range(len(num)):
        try:
            if num[i] == num[i+1]:
               count = count + 1
            else:
                result += str(num[i]) + str(count)
                table.append(count)
                count = 1
        except:
            result += str(num[i]) + str(count)
            table.append(count)
    return(table)
    
N,K=map(int,input().split())
S=input()

RL=ranlength(S)
#print(RL)

#しゃくとり法
if int(S[0])==1:
    cnt1=RL[0]
    ans1=RL[0]
    for i in range(len(RL)):
        if i%2==0 and i>=2:
            cnt1+=RL[i]
            cnt1+=RL[i-1]
            if i>=K*2+2:
                cnt1-=RL[i-(K*2+1)]
                cnt1-=RL[i-(K*2+2)]
            ans1=max(ans1,cnt1)
            #print(cnt1)
    print(ans1)
elif int(S[0])==0:
    if len(RL)==1:
        ans2=RL[0]
    else:
        cnt2=RL[0]+RL[1]
        ans2=RL[0]+RL[1]
    for i in range(len(RL)):
        if i%2==1 and i>=3:
            cnt2+=RL[i]
            cnt2+=RL[i-1]
            if i==K*2+1:
                cnt2-=RL[0]
            elif i>=K*2+3:
                cnt2-=RL[i-(K*2+1)]
                cnt2-=RL[i-(K*2+2)]
            ans2=max(ans2,cnt2)
            #print(cnt2)
    print(ans2)

Submission status: Wrong Answer

Solution code:
import sys
from itertools import accumulate
def input(): return sys.stdin.readline().strip()


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

    """
    反転させる区間は独立であるとしてよい。実際反転させる２区間が左図のように
    オーバーラップしていたとすると、これは右図のように反転させるのと同値だからである：
               _______________                             _______  
        ______|_______        |     --->    ______        |       |
    ___|______|_______|_______|_____    ___|______|_______|_______|_____ 
    
    そうすると安直には0がなるべく長く連続している区間に反転をかけたくなるが、
    長い順に反転をかけた場合1が連続する保証がないのでダメ。

    そこで1の連続する箇所を固定したうえで、そこから0の連続する区間に順番に反転をかけることを考える。
    当然愚直にやるとTLEなので、累積和で実装する
    """

    A = []
    char = S[0]
    cnt = 1
    for i in range(1, N):
        if S[i] == char:
            cnt += 1
        else:
            A.append(cnt)
            char = S[i]
            cnt = 1
    A.append(cnt)
    #print(A)

    B = list(accumulate(A))
    l = len(A)
    if S[0] == '0':
        ans = B[min(K * 2 - 1, l - 1)]
        for i in range(1, l - K * 2 + 1):
            ans = max(ans, B[i + K * 2 - 1] - B[i - 1])
    else:
        ans = B[min(K * 2, l - 1)]
        for i in range(1, l - K * 2):
            ans = max(ans, B[i + K * 2] - B[i - 1])
    print(ans)
       


if __name__ == "__main__":
    main()