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: Runtime Error

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

def findmax(s,k):
    if len(s) == 1:
        return 1
    begin = s[0]
    l = [0]
    cur = 0
    for i in range(len(s)):
        if s[i]==begin:
            l[cur]+=1
        else:
            if begin == '0' : begin ='1'
            else: begin = '0'
            cur+=1
            l.append(1)
    r = 0
    if s[0] == '0':
        l = [0] + l
        r = 2*k
    else:
        r = 2*k+1
    sumup = [0]
    for i in range(len(l)):
        sumup.append(sumup[i]+l[i])
    return (max([sumup[i+r]-sumup[i] for i in range(0,len(sumup)-r,2)]))
print (findmax(s,k))

Submission status: Runtime Error

Solution code:
from itertools import accumulate

n, k = list(map(int,input().split(" ")))
s = input()


def solve():
    cnt=0
    l=[]
    prev = "1"

    for i in s:
        if prev ==i:
            cnt+=1
        else:
            l.append(cnt)
            cnt = 1
        prev=i
    l.append(cnt)
    if prev=="0":
        l.append("0")

    w = 2*k+1

    if len(l)<=w:
        return n

    acc = [0]+list(accumulate(l))
    ans = 0
    for i in range(0, len(l)-w+1,2):
        ans = max(ans,acc[i+w]-acc[i])
    return ans
print(solve())

Submission status: Runtime Error

Solution code:
    n,k=map(int, input().split())
    s=input()
    
    ruiwa = [0]

    for i in range(1,len(s)):
        if s[i] != s[i-1]:
            ruiwa.append(i)
    ruiwa.append(n)

    if s[0] == "1":
        ans = ruiwa[1]
        if ((len(ruiwa) - 1) // 2) <= k:
            ans = n
        else:
            for i in range(((len(ruiwa)-1)//2)-k+1):
                ans = max(ans,ruiwa[min(2*k+1+2*i,len(ruiwa)-1)] - ruiwa[2*i])
    else:
        ans = 0
        if len(ruiwa) - 1 - (len(ruiwa) - 1) // 2 <= k:
            ans = n
        else:
            for i in range(len(ruiwa) - 1 - (len(ruiwa)-1)//2 - k + 1):
                ans = max(ans, ruiwa[min(2*k+2*i, len(ruiwa)-1)] - ruiwa[max(0,2*i-1)])

    print(ans)

Submission status: Runtime Error

Solution code:
from itertools import accumulate

n,k = list(map(int,input().split(" ")))
s = input()

def solve():
    cnt=0
    l=[]
    prev = "1"
    for i in s:
        if prev ==i:
            cnt += 1
        else:
            l.append(cnt)
            cnt = 1
        prev =i
    l.append(cnt)
    if prev=="0":
        l.append("0")

    w = 2 * k + 1

    if len(l) <=w:
        return n

    acc=[0]+list(accumulate(l))
    ans = 0
    for i in range(0,len(l) - w+ 1,2):
        ans = max(ans, acc[i+w] - acc[i])

    return ans
print(solve())

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
import re
import itertools

N = int(input())
K = int(input())
S = str(input())

pattern = r"0+"
iterator = re.finditer(pattern ,S)
l = [match.span() for match in iterator]
count = len(l)

if count <= K:
    ans = N
else:
    ans = 0
    for k in range(K,-1,-1):
        for v in itertools.permutations(l, k):
            s = S
            for i in range(k):
                add = "1"*(v[i][1]-v[i][0])
                s = s[:v[i][0]] + add + s[v[i][1]:]
                splt = re.split(pattern, s)
                a = [len(splt[i]) for i in range(len(splt))]
                if max(a) >= ans:
                    ans = max(a)
print(ans)

Submission status: Runtime Error

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

def findmax(s,k):
    if len(s) == 1:
        return 1
    begin = s[0]
    l = [0]
    cur = 0
    for i in range(len(s)):
        if s[i]==begin:
            l[cur]+=1
        else:
            if begin == '0' : begin ='1'
            else: begin = '0'
            cur+=1
            l.append(1)
    r = 0
    if s[0] == '0':
        l = [0] + l
        r = 2*k
    else:
        r = 2*k+1
    sumup = [0]
    for i in range(len(l)):
        sumup.append(sumup[i]+l[i])
    return (max([sumup[i+r]-sumup[i] for i in range(len(sumup)-r)]))
print (findmax(s,k))

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import re
import itertools

N = int(input())
K = int(input())
S = str(input())

count = 0
pattern = r"0+"
iterator = re.finditer(pattern ,S)
l = [match.span() for match in iterator]
count = len(l)

if count <= K:
    ans = N
else:
    ans = 0
    for k in range(K,-1,-1):
        for v in itertools.permutations(l, k):
            s = S
            for i in range(k):
                add = "1"*(v[i][1]-v[i][0])
                s = s[:v[i][0]] + add + s[v[i][1]:]
                pattern = r"0+"
                splt = re.split(pattern, s)
                a = [len(splt[i]) for i in range(len(splt))]
                if max(a) >= ans:
                    ans = max(a)
print(ans)

Submission status: Runtime Error

Solution code:
#input
N, K = map(int, input().split())#0~K-1回目と考える。
s = input() #0~N-1人目と考える。


#output

first_pos = int(s[0:1]) #0人目が○なら1, ×なら0

nowpos = first_pos #今チェックしている立ち方
nowposcount = 0 #1にすると0人目を考えるときにバグる
Accum = [0] * (N+1) #累積和 取り出し方は[まとまり個数目] 要素数はまとまりの個数+1
nowRuisekiCount = 1 #何番目のまとまりを見ているか 0~まとまり個数-1
zeroRenzoku = 1

for i in range(len(s)): #0~N-1人目につき、全てをチェックして累積和のリストを作る
    if int(s[i:i+1]) == nowpos: #今チェックしている立ち方と同じ(左の人と同じ)なら
        nowposcount += 1 #左のまとまりの個数を1増やす
        Accum[nowRuisekiCount] = Accum[nowRuisekiCount-1] + nowposcount #まとまりの数字数を入れる
    else:
        nowpos = abs(nowpos - 1)
        nowposcount = 1 #nowposcountは1に戻る
        nowRuisekiCount += 1 #次のまとまりに移ったことになる
        Accum[nowRuisekiCount] = Accum[nowRuisekiCount-1] + nowposcount #まとまりの文字数を入れる(1を入れる)

matomariCount = nowRuisekiCount #まとまりの数

#累積和を出す
if (first_pos == 1) & (matomariCount <= (K*2+1)): #まとまり数がK*2+1以下なら、全部逆立ちさせられる
    print(len(s))
elif (first_pos == 0) & (matomariCount <= (K*2)): #最初が0だと同様にK*2以下なら
    print(len(s))
else:
    #一番最初の合計文字数
    if first_pos == 1:
        maxaccum = Accum[(K*2+1)]
    else: #最初のまとまりが0なら
        maxaccum = Accum[(K*2)]

    #2個目のまとまりからも同様に見ていく
    for i in range(2, matomariCount+1, 1):
        if abs((first_pos - ((i-1)%2))) == 0: #これが0なら左端が0なので右端を落とす
            if i+(K*2)-1 > matomariCount:
                maxaccum = max(maxaccum, Accum[matomariCount]-Accum[i-1])
            maxaccum = max(maxaccum, Accum[i+(K*2)-1]-Accum[i-1])
        else:
            if i+(K*2) > matomariCount:
                maxaccum = max(maxaccum, Accum[matomariCount]-Accum[i-1])
            maxaccum = max(maxaccum, Accum[i+(K*2)]-Accum[i-1])
    print(maxaccum)

Submission status: Runtime Error

Solution code:
#input
N, K = map(int, input().split())#0~K-1回目と考える。
s = input() #0~N-1人目と考える。


#output

first_pos = int(s[0:1]) #0人目が○なら1, ×なら0

nowpos = first_pos #今チェックしている立ち方
nowposcount = 0 #1にすると0人目を考えるときにバグる
Accum = [0] * (N+1) #累積和 取り出し方は[まとまり個数目] 要素数はまとまりの個数+1
nowRuisekiCount = 1 #何番目のまとまりを見ているか 0~まとまり個数-1
zeroRenzoku = 1

for i in range(len(s)): #0~N-1人目につき、全てをチェックして累積和のリストを作る
    if int(s[i:i+1]) == nowpos: #今チェックしている立ち方と同じ(左の人と同じ)なら
        nowposcount += 1 #左のまとまりの個数を1増やす
        Accum[nowRuisekiCount] = Accum[nowRuisekiCount-1] + nowposcount #まとまりの数字数を入れる
    else:
        nowpos = abs(nowpos - 1)
        nowposcount = 1 #nowposcountは1に戻る
        nowRuisekiCount += 1 #次のまとまりに移ったことになる
        Accum[nowRuisekiCount] = Accum[nowRuisekiCount-1] + nowposcount #まとまりの文字数を入れる(1を入れる)

matomariCount = nowRuisekiCount #まとまりの数

#累積和を出す
if (first_pos == 1) & (matomariCount <= (K*2+1)): #まとまり数がK*2+1以下なら、全部逆立ちさせられる
    print(len(s))
elif (first_pos == 0) & (matomariCount <= (K*2)): #最初が0だと同様にK*2以下なら
    print(len(s))
else:
    #一番最初の合計文字数
    if first_pos == 1:
        maxaccum = Accum[(K*2+1)]
    else: #最初のまとまりが0なら
        maxaccum = Accum[(K*2)]

    #2個目のまとまりからも同様に見ていく
    for i in range(2, matomariCount, 1):
        if abs((first_pos - ((i-1)%2))) == 0: #これが0なら左端が0なので右端を落とす
            if i+(K*2)-1 > matomariCount:
                maxaccum = max(maxaccum, Accum[matomariCount]-Accum[i-1])
            maxaccum = max(maxaccum, Accum[i+(K*2)-1]-Accum[i-1])
        else:
            if i+(K*2) > matomariCount:
                maxaccum = max(maxaccum, Accum[matomariCount]-Accum[i-1])
            maxaccum = max(maxaccum, Accum[i+(K*2)]-Accum[i-1])
    print(maxaccum)

Submission status: Runtime Error

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

L = 2*K + 1
i = [0] + [i + 1 for i in range(N-1) if S[i] != S[i+1]] + [N]*L 
# 切り替わる場所を記録
# 便宜上N+1 * K を後ろにつける
S = [0] + S

X = [1 for _ in range(len(i)+1)]
#print(i)
for k in range(len(i)-L):
    #print(i[k])
    if S[i[k]] == 0:
        X[i[k]] = i[k + 2*K] - i[k]
    else:
        X[i[k]] = i[k + 2*K + 1] - i[k]

print(max(X))

Submission status: Runtime Error

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

L = 2*K + 1
i = [0] + [i + 1 for i in range(N-1) if S[i] != S[i+1]] + [N]*L 
# 切り替わる場所を記録
# 便宜上N+1 * K を後ろにつける
S = [0] + S

X = [1 for _ in range(len(i))]
print(i)
for k in range(len(i)-L):
    #print(i[k])
    if S[i[k]] == 0:
        X[i[k]] = i[k + 2*K] - i[k]
    else:
        X[i[k]] = i[k + 2*K + 1] - i[k]

print(max(X))

Submission status: Runtime Error

Solution code:
from itertools import accumulate

n,k = list(map(int,input().split(" ")))
s = input()

def solve():
    cnt=0
    l=[]
    prev = "1"
    for i in s:
        if prev ==i:
            cnt += 1
        else:
            l.append(cnt)
            cnt = 1
        prev =i
    l.append(cnt)
    if prev=="0":
        l.append("0")

    w = 2 * k + 1

    if len(l) <=w:
        return n

    acc=[0]+list(accumulate(l))
    ans = 0
    for i in range(0,len(l) - w+ 1,2):
        ans = max(ans, acc[i+w] - acc[i])

    return ans
print(solve())

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())
S = input()
start =[]
end = []
if S[0] == "0":
    start.appned(0)
else:
    end.append(0)
for i in range(n):
    if S[i:i+2] == "10":
        start.append(i+1)
    elif S[i:i+2] == "01":
        end.append(i+1)
if len(start) <= k:
    print(len(S))
else:
    start.append(len(S))
    ans = []
    for i in range(len(start) - k):
        ans.append(start[i + k] - end[i])
    print(max(ans))

Submission status: Runtime Error

Solution code:
from itertools import accumulate

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

def solve():
    cnt=0
    l=[]
    prev = "1"
    for i in S:
        if prev ==i:
            cnt += 1
        else:
            l.append(cnt)
            cnt = 1
        prev =i
    l.append(cnt)
    if prev=="0":
        l.append("0")
    
    w = 2 * K + 1
    
    if len(l) <=w:
        return N
    
    acc=[0]+list(accumulate(l))
    ans = 0
    for i in range(0,len(l) - w+ 1,2):
        ans = max(ans, acc[w+i] - acc[i])

    return ans
print(solve())

Submission status: Runtime Error

Solution code:
from itertools import accumulate

N,K=list(map(int,input().split(" ")))
S=input()
def solve():
    cnt=0
    l=[]
    prev = "1"
    for i in S:
        if prev ==i:
            cnt+=1
        else:
            l.append(cnt)
            cnt = 1
        prev =i
    l.append(cnt)
    if prev=="0":
        l.append("0")
    w = 2*K+1
    if len(l) <=w:
        return N
    acc=[0]+list(accumulate(l))
    ans = 0
    for i in range(0,len(l)-w+1,2):
        ans = max(ans,acc[w+i]-acc[i])

    return ans
print(solve())

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())
s = input()
memo = []
c = 0
ps = ''
lens = len(s) - 1
b = 0
if s[0] == '1':
    b = 1

for i in range(lens + 1):
    if s[i] != ps:
        memo.append(c)
        c = 1
        ps = s[i]
        if i == lens:
            memo.append(1)
        continue
    if i == lens:
        memo.append(c+1)
    c += 1
    ps = s[i]

if  len(memo) < k * 2 + b + 1:
    print(sum(memo))
else:
    sumi = 0
    sumi = sum(memo[: k * 2 + b + 1])
    temp = [sumi]
    memo.append(0)
    # print(memo)
    # print(len(memo))
    for i in range(0, len(memo) - 2 * k  - 1, 2):
        sumi = sumi - memo[i] - memo[i + 1] + memo[i + 2 * k  + 1] + memo[i + 2 * k + 2]
        temp.append(sumi)
    # print(temp)
    print(max(temp))

Submission status: Runtime Error

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

L = 2*K + 1
i = [i + 1 for i in range(N-1) if S[i] != S[i+1]] + [N]*L 
# 切り替わる場所を記録
# 便宜上N+1 * K を後ろにつける

X = [1 for _ in range(len(i))]
#print(i)
for k in range(len(i)-L):
    if S[i[k]] == 0:
        X[i[k]] = i[k + 2*K] - i[k]
    else:
        X[i[k]] = i[k + 2*K + 1] - i[k]

print(max(X))

Submission status: Runtime Error

Solution code:
n,k = map(int,input().split())
s = input()
c = [int(s[i]) for i in range(n)]
ids = [0]
for i in range(1,n):
  if c[i] != c[i-1]:
    ids.append(i)
ids.append(n)
l = len(ids)
result = []
for i in range(l-1):
  if c[ids[i]] == 0:
    if i + 2*k < l:
      result.append(ids[i+2*k] - ids[i])
    else:
      result.append(n - ids[i])
  else:
    if i + 2*k < l:
      result.append(ids[i+2*k+1] - ids[i])
    else:
      result.append(n - ids[i])
print(max(result))

Submission status: Runtime Error

Solution code:
N, K = map(int,input().split())
S = input()
L = [0]
nowL = 0
if S[0] == '0':
    L = [0,0]
for i in range(N-1):
    nowL += 1
    if S[i] != S[i+1]:
        L.append(nowL)
# 終端処理
nowL += 1
L.append(nowL)

ans = 0
for j in range(len(L)):
    if 2*j+2*K+1 >= len(L):
        ans = max(ans,L[-1]-L[-4])
        if ans == 0:
            ans = L[-1]
        break
    ans = max(ans,L[2*j+2*K+1]-L[2*j])

print(ans)

Submission status: Runtime Error

Solution code:
N, K = map(int,input().split())
S = input()
L = [0]
nowL = 0
if S[0] == '0':
    L = [0,0]
for i in range(N-1):
    nowL += 1
    if S[i] != S[i+1]:
        L.append(nowL)
# 終端処理
nowL += 1
L.append(nowL)

print(L)

ans = 0
for j in range(len(L)):
    if 2*j+2*K+1 >= len(L):
        ans = max(ans,L[-1]-L[-4])
        if ans == 0:
            ans = L[-1]
        break
    ans = max(ans,L[2*j+2*K+1]-L[2*j])

print(ans)

Submission status: Runtime Error

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

sl = []
c = 1
if (s[0] == "1"):
    sl.append(0)
for i in range(n):
    if (i != 0):
        if (s[i] == s[i-1]):
            c += 1
        else:
            sl.append(c)
            c = 1
sl.append(c)

ruisekiwa = [0]
sums = []
for i in range(len(sl)):
    ruisekiwa.append(ruisekiwa[i] + sl[i])
# print(sl)
# print(ruisekiwa)
for i in range(len(sl)):
    if(k % 2 == 1 and i % 2 == 0 or k % 2 == 0 and i % 2 == 1):
        left = i-k
        if (left < 0):
            left = 0
        right = i + k+1
        if (right >= len(ruisekiwa)):
            right = len(ruisekiwa)-1
        # print(ruisekiwa[right], "-", ruisekiwa[left],
        #      "=", ruisekiwa[right]-ruisekiwa[left])
        sums.append(ruisekiwa[right]-ruisekiwa[left])
print(max(sums))

Submission status: Runtime Error

Solution code:
n, k = list(map(int, input().split()))
s = input()
now = 1
cnt = 0
Nums = []
for i in range(len(s)):
    if s[i] == str(now):
        cnt += 1
    else:
        Nums.append(cnt)
        now = 1 - now
        cnt = 1
if cnt != 0:
    Nums.append(cnt)
if len(Nums) % 2 == 0:
    Nums.append(0)

ans = 0
Add = 2 * k + 1

sum = [0] * (len(Nums) + 1)

for i in range(len(sum)):
    sum[i + 1] = sum[i] + Nums[i]

for i in range(len(Nums))[::2]:
    left = i
    right = min(i + Add, len(Nums))
    tmp = sum[right] - sum[left]
    ans = max(tmp, ans)
    
print(ans)

Submission status: Runtime Error

Solution code:
n, k = list(map(int, input().split()))
s = input()
now = 1
cnt = 0
Nums = []
for i in range(len(s)):
    if s[i] == str(now):
        cnt += 1
    else:
        Nums.append(cnt)
        now = 1 - now
        cnt = 1
if cnt != 0:
    Nums.append(cnt)
if len(Nums) % 2 == 0:
    Nums.append(0)

ans = 0
Add = 2 * k + 1

sum = [0] * (len(Nums) + 1)

for i in range(len(sum)):
    sum[i + 1] = sum[i] + Nums[i]

for i in range(len(Nums))[::2]:
    left = i
    right = min(i + Add, len(Nums))
    tmp = sum[left] - sum[right]
    ans = max(tmp, ans)
    
print(ans)

Submission status: Runtime Error

Solution code:
#include <bits/stdc++.h>
using namespace std;


int main(){
	int N, K;
	cin >> N >> K;
	string S;
	cin >> S;
	int max = 0;
	vector<int> fc(N);
	int tmp = 0;
	for(int i = 0; i < N; i++){
		if(S[i] != (char)('0'+tmp%2))
			tmp++;
		fc.at(tmp)++;
	}
	for(int i = 0; i+K*2 < fc.size(); i++){
		int sum = 0;
		for(int j = 0; j < 2*K; j++)
			sum += fc.at(i+j);
		if(max < sum)
			max = sum;
	}

	cout << max << endl;
}

Submission status: Runtime Error

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

c1=[]
c0=[]

if s[0]==1:
	c1.append(0)
else:
	c0.append(0)

for i in range(len(s)-1):
	if s[i] != s[i+1]:
		if s[i+1] == 0:
			c1.append(i)
			c0.append(i+1) 
		else:
			c1.append(i+1)
			c0.append(i) 

if s[n-1]==1:
	c1.append(n-1)
else:
	c0.append(n-1)

#print(c1)
res = []
for i in range(1,int((len(c1)+1)/2)):
	ct = copy.deepcopy(c1)
	for j in range(k):
		if i*2 >= len(ct):
			break
		else:
			del ct[i*2-1]
			del ct[i*2-1]
	#print(ct)
	mx=0
	for l in range(int((len(ct)+1)/2)):
		ck = ct[2*l+1]-ct[2*l] + 1
		#print(ck)
		if  ck >= mx:
			mx = ck
	res.append(mx)

print(max(res))

Submission status: Runtime Error

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

s0 = s[0]
a = [0]

s = s.replace('10', '1,0')
s = s.replace('01', '0,1')
s = s.split(',')
for i in s:
    a.append(a[-1] + len(i))

if len(a) == 2:
    print(a[1])
    exit()
ans = []
if s0 == '0':
    for i in range(2 * k,len(a)):
        ans.append(a[i] - a[i - (2 * k)])
else:
    for i in range(2 * k + 1,len(a)):
        ans.append(a[i] - a[i - (2 * k + 1)])

print(max(ans))

Submission status: Runtime Error

Solution code:
import sys,re,copy
input = sys.stdin.readline

N,K=[int(x) for x in input().strip().split()]

f=1
cnt=0
S=[]
r=0
for s in input().strip():
    s=int(s)
    if s==f:
        cnt+=1
    else:
        r+=cnt
        S.append(r)
        cnt=1
        f=1-f
else:
    r+=cnt
    S.append(r)
if len(S)%2==0:
    S.append(r)

Max=0
i=0
if len(S)==1:
    Max=S[0]
else:
    while True:
        r=i+2*K
        cnt=S[min(r,len(S)-1)]-S[max(i-1,0)]
        if cnt > Max:
            Max = cnt
        i+=2

print(Max)

Submission status: Runtime Error

Solution code:
import sys
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7

N, K = LIST()
S = input()

# stand = []
# reverse = []
# state = None
# count = 0
# tmp = None
# for i in S:
# 	if i == "0":
# 		state = "stand"
# 	else:
# 		state = "reverse"
# 	if tmp == "stand" and state == "reverse":
# 		stand.append(count)
# 		count = 1
# 	elif tmp == "reverse" and state == "stand":
# 		reverse.append(count)
# 		count = 1
# 	else: # 状態が前と同じ
# 		count += 1
# 	tmp = state
# if state == "stand":
# 	stand.append(count)
# else:
# 	reverse.append(count)

# if "0" not in S:
# 	print(len(S))
# else:
# 	if K >= len(stand): # 全員逆立ちにできる
# 		print(len(S))
# 	else:
# 		if len(stand) > len(reverse):
# 			reverse.append(0)
# 			reverse.insert(0, 0)
# 		if len(stand) == len(reverse) and S[0] == "0":
# 			reverse.insert(0, 0)
# 		if len(stand) == len(reverse) and S[0] == "1":
# 			reverse.append(0)
# 		# print(stand)
# 		# print(reverse)
# 		max_ = 0
# 		for i in range(max(1, 1+len(reverse)-(K+1))):
# 			# print(reverse[i:i+K+1],stand[i:i+K])
# 			tmp = sum(reverse[i:i+K+1]+stand[i:i+K])
# 			if tmp > max_:
# 				max_ = tmp
# 		print(max_)
indexes = [0]
if s[0] == 0:
    indexes.append(0)
for i in range(N-1):
	if S[i] != S[i+1]:
		indexes.append(i+1)
indexes.append(N)
if S[N-1] == 0:
    indexes.append(N)
ans = []
# print(indexes)
r = len(indexes)//2 -1
if K >= r:
	print(N)
else:
	print(max(indexes[2*(i+K)+1]-indexes[2*i] for i in range(r-K+1)))

Submission status: Runtime Error

Solution code:
S = S + 'E'   # 終端処理
ans = -1    # 答えの初期値（十分に小さい値）

# 立った人の連続の数
if (S[0] == '0'):
    stand = 1
else:
    stand = 0

L = 0
R = 0

# 尺取り法
while (R < N):
    # 右端を進めることが可能（立った人の連続の数がK以下）なら、右端を進める
    if (stand <= K):
        # 立った人の連続の数が増えるなら1足す
        if (S[R] == '1' and S[R + 1] == '0'):
            stand += 1
        # 答えの更新
        ans = max(ans, R - L + 1)
        R += 1
    # 進められない場合、左端と右端が同じなら両者を進める
    # 左端が右端を追い越さないための処理
    """
    """
    elif (L == R):
        L += 1
        R += 1
    # それ以外は左端のみを進める
    else:
        # 立った人の連続の数が減るなら1減らす
        if (S[L] == '0' and S[L + 1] == '1'):
            stand -= 1
        L += 1
        # 左端のみを進めた場合は答えを更新し得ない
    

print(ans)

Submission status: Runtime Error

Solution code:
S = S + 'E'   # 終端処理
ans = -1    # 答えの初期値（十分に小さい値）

# 立った人の連続の数
if (S[0] == '0'):
    stand = 1
else:
    stand = 0

L = 0
R = 0

# 尺取り法
while (R < N):
    # 右端を進めることが可能（立った人の連続の数がK以下）なら、右端を進める
    if (stand <= K):
        # 立った人の連続の数が増えるなら1足す
        if (S[R] == '1' and S[R + 1] == '0'):
            stand += 1
        # 答えの更新
        ans = max(ans, R - L + 1)
        R += 1
    # 進められない場合、左端と右端が同じなら両者を進める
    # 左端が右端を追い越さないための処理
    """
    elif (L == R):
        L += 1
        R += 1
    # それ以外は左端のみを進める
    else:
        # 立った人の連続の数が減るなら1減らす
        if (S[L] == '0' and S[L + 1] == '1'):
            stand -= 1
        L += 1
        # 左端のみを進めた場合は答えを更新し得ない
    """

print(ans)

Submission status: Runtime Error

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

STAND = '0'
REVERSE = '1'
NONE = '2'

prev = NONE
size = 0
groups = []
for s in S:
    if s == prev:
        size += 1
    else:
        if prev == STAND:
            groups.append(-size)
        elif prev == REVERSE:
            groups.append(size)
        prev = s
        size = 1
if prev == STAND:
    groups.append(-size)
elif prev == REVERSE:
    groups.append(size)
max_size = 2 * K
ans = 0
cul_sum = []
total = 0
for g in groups:
    total += g if g > 0 else -g
    cul_sum.append(total)
if groups[0] < 0:
    ans = cul_sum[min(max_size-1, len(cul_sum))]
    offset = 1
else:
    ans = cul_sum[max_size]
    offset = 2
for i in range(offset, len(cul_sum)-max_size, 2):
    ans = max(ans, cul_sum[i+max_size]-cul_sum[i-1])
print(ans)

Submission status: Runtime Error

Solution code:
import itertools


def count_maxone(binstr):
    return max([len(l) for l in binstr.split('0') if len(l)])


n, k = map(int, input().split())
s = input().rstrip()
ones = count_maxone(s)

pos = []
pre = '1'

for i in range(n):
    if s[i] == '0' and pre == '1':
        pos.append(i)
    pre = s[i]

for i in range(1, k + 1):
    combs = itertools.combinations(pos, i)
    for cmb in combs:
        slist = list(s)
        for c in cmb:
            zc = s.find('1', c)
            if zc == -1:
                zc = c + 1
            for t in range(c, zc):
                slist[t] = '1'
        ones = max(ones, count_maxone(''.join(slist)))

print(ones)

Submission status: Runtime Error

Solution code:
import sys
N, K = map(int, input().split())
s = input()
  
group = []
sign = s[0]
curr = 0
for i in range(N):
  curr += 1
  if s[i] != sign: 
    group += [curr-1]
    curr = 1
    sign = s[i]
else:group += [curr]

if s[0] == '0' :
  if s[N-1] == '0':
    m = (len(group)+1)//2
    if K >= m:
      print(N)
      sys.exit()
    score = [sum(group[:2*K])]
    for i in range(m-K):
      score += [score[-1] - group[2*i]  - group[2*i+1] + group[2*i+2*K+1]+group[2*i+2*K]]
  else :
    m = (len(group)+1)//2
    if K >= m:
      print(N)
      sys.exit()
    score = [sum(group[:2*K])]
    for i in range(m-K):
      score += [score[-1] - group[2*i] - group[2*i+1] + group[2*i+2*K+1]+group[2*i+2*K]]
    score[-1] += group[-1]
else:
  m = len(group)//2
  group += [0]
  if K >= m:
    print(N)
    sys.exit()
  score = [sum(group[:2*K+1])]
  for i in range(m-K):
    score += [score[-1] - group[2*i] - group[2*i+1] + group[2*i+2*K+1]+group[2*i+2*K +2]]

print(max(score))

Submission status: Runtime Error

Solution code:
import sys
N, K = map(int, input().split())
s = input()
  
group = []
sign = s[0]
curr = 0
for i in range(N):
  curr += 1
  if s[i] != sign: 
    group += [curr-1]
    curr = 1
    sign = s[i]
else:group += [curr]

if s[0] == '0' :
  if s[N-1] == '0':
    m = (len(group)+1)//2
    if K >= m:
      print(N)
      sys.exit()
    score = [sum(group[:2*K])]
    for i in range(m-K):
      score += [score[-1] - group[2*i] + group[2*i+2*K]]
  else :
    0/0
    m = (len(group)+1)//2
    if K >= m:
      print(N)
      sys.exit()
    score = [sum(group[:2*K])]
    for i in range(m-K):
      score += [score[-1] - group[2*i] + group[2*i+2*K]]
    score[-1] += group[-1]
else:
  m = len(group)//2
  group += [0]
  if K >= m:
    print(N)
    sys.exit()
  score = [sum(group[:2*K+1])]
  for i in range(m-K):
    score += [score[-1] - group[2*i] - group[2*i+1] + group[2*i+2*K+1]+group[2*i+2*K +2]]

print(max(score))

Submission status: Runtime Error

Solution code:
import sys
N, K = map(int, input().split())
s = input()
  
group = []
sign = s[0]
curr = 0
for i in range(N):
  curr += 1
  if s[i] != sign: 
    group += [curr-1]
    curr = 1
    sign = s[i]
else:group += [curr]

if s[0] == '0' :
  if s[N-1] == '0':
    m = (len(group)+1)//2
    if K >= m:
      print(N)
      sys.exit()
    score = [sum(group[:2*K])]
    for i in range(m-K):
      score += [score[-1] - group[2*i] + group[2*i+2*K]]
  else :
    m = (len(group)+1)//2
    if K >= m:
      print(N)
      sys.exit()
    score = [sum(group[:2*K])]
    for i in range(m-K):
      score += [score[-1] - group[2*i] + group[2*i+2*K]]
    score[-1] += group[-1]
else:
  0/0
  m = len(group)//2
  group += [0]
  if K >= m:
    print(N)
    sys.exit()
  score = [sum(group[:2*K+1])]
  for i in range(m-K):
    score += [score[-1] - group[2*i] - group[2*i+1] + group[2*i+2*K+1]+group[2*i+2*K +2]]

print(max(score))

Submission status: Runtime Error

Solution code:
aa

Submission status: Runtime Error

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

Z = [j for j, s in enumerate(S) if (j > 0 and s == 0 and S[j] != S[j-1]) or (j == 0 and s == 0)] + [N]
Y = [j for j, s in enumerate(S) if (j > 0 and s == 1 and S[j] != S[j-1]) or (j == 0 and s == 1)]
result = 0
if len(Z) <= K:
  print(N)
  else:
    for i in range(len(Z)-K):
      if i == 0:
        result = Z[i+K]
        else:
          result = max(result, Z[i+K] - Y[i if Y[0] < Z[0] else i-1])
          print(result)

Submission status: Runtime Error

Solution code:
    N, K = map(int, input().split())
    S = list(map(int, input()))
     
    Z = [j for j, s in enumerate(S) if (j > 0 and s == 0 and S[j] != S[j-1]) or (j == 0 and s == 0)] + [N]
    Y = [j for j, s in enumerate(S) if (j > 0 and s == 1 and S[j] != S[j-1]) or (j == 0 and s == 1)]
    result = 0
    if len(Z) <= K:
        print(N)
    else:
        for i in range(len(Z)-K):
            if i == 0:
                result = Z[i+K]
            else:
                result = max(result, Z[i+K] - Y[i if Y[0] < Z[0] else i-1])
        print(result)

Submission status: Runtime Error

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

l = len(s)
mx = 0 

def count_consecutive(s):
    return max(list(map(lambda x: len(x), s.split('0'))))

def dfs(s, k, st):
    global mx
    if k == 0: return count_consecutive(s)
    for i in range(st, l): 
        if s[i] == '0':
            count = 0 
            while i+count < l and s[i+count] == '0':
                count += 1
            mx = max(dfs(s[:i] + '1'*count + s[i+count:], k-1, i+count), mx) 
    return max(count_consecutive(s), mx)                                                                       

print(dfs(s, k, 0))

Submission status: Runtime Error

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

cnt = 1
cntlist = []
for i in range(1, N):
    if s[i - 1] != s[i]:
        cntlist.append(cnt)
        cnt = 1
    else:
        cnt += 1

cntlist.append(cnt)
if s[0] == "0":
    cntlist = [0] + cntlist

ans = 0
for i in range(2 * K + 1):
    if i >= len(cntlist):
        break
    ans += cntlist[i]
anslist = [ans]
s = 0
g = 2 * K + 1
while g < len(cntlist):
    ans -= cntlist[s]
    ans -= cntlist[s + 1]
    s += 2
    ans += cntlist[g]
    g += 1

    ans += cntlist[g]
    if g >= len(cntlist):
        break
    g += 1
    anslist.append(ans)

print(max(anslist))

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
s = input()
  
group = []
sign = s[0]
curr = 0
for i in range(N):
  curr += 1
  if s[i] != sign: 
    group += [curr-1]
    curr = 1
    sign = s[i]
else:group += [curr]

if s[0] == '0':
  score = [sum(group[:2*K])]
  m = (len(group)+1)//2
  for i in range(m-K):
    score += [score[-1] - group[2*i] + group[2*i+2*K]]
else:
  score = [sum(group[:2*K+1])]
  m = len(group)//2
  for i in range(m-K):
    score += [score[-1] - group[2*i] - group[2*i+1] + group[2*i+2*K+1]+group[2*i+2*K +2]]

print(max(score))