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=map(int,input().split())
a=input()
l=len(a)
src=[int(a[i:i+1]) for i in range(l)]
from itertools import groupby
y =[sum(1 for e in it) for _, it in groupby(src, key=lambda x: x==1)]
m=len(y)
if src[0]==1 and src[l-1]==1:
  if (m-1)/2<=k:
    print(int(n))
  else:
    c=0
    for i in range(m-2*k+1):
      if i%2==0:
        d=y[i:i+2*k+1]
        f=sum(d)
        if f>=c:
          c=f
if src[0]==1 and src[l-1]==0:
  if m/2<=k:
    print(int(n))
  else:
    c=0
    for i in range(m-2*k+1):
      if i%2==0:
        d=y[i:i+2*k+1]
        f=sum(d)
        if f>=c:
          c=f
if src[0]==0 and src[l-1]==1:
  if m/2<=k:
    print(int(n))
  else:
    c=0
    for i in range(m-2*k+1):
      if i%2==1:
        d=y[i:i+2*k+1]
        f=sum(d)
        if f>=c:
          c=f
if src[0]==0 and src[l-1]==0:
  if (m+1)/2<=k:
    print(int(n))
  else:
    c=0
    for i in range(m-2*k+1):
      if i%2==1:
        d=y[i:i+2*k+1]
        f=sum(d)
        if f>=c:
          c=f
print(c)

Submission status: Runtime Error

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

I=[0,]

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

if len(I)<k:
    for _ in range(10**5):
        I.append(n)

print(I)

X=[]

for i in range(len(I)):
    if s[I[i]]=='0':
        X.append(I[i+2*k]-I[i])
    else:
        X.append(I[i+2*k+1]-I[i])

print(max(X))

Submission status: Runtime Error

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

I=[0,]

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

if len(I)<k:
    I.extend([n]*(10**5))

X=[]

for i in range(len(I)):
    if s[I[i]]=='0':
        X.append(I[i+2*k]-I[i])
    else:
        X.append(I[i+2*k+1]-I[i])

print(max(X))

Submission status: Runtime Error

Solution code:
from numpy import*
from itertools import*
N, K = map(int, input().split())
S=input()
zeros=[len(list(v)) for k,v in groupby(S) if k=="0"]
ones =[len(list(v)) for k,v in groupby(S) if k=="1"]


#print(zeros,ones)

c_zero= cumsum(zeros)
c_one = cumsum(ones)

#print(c_zero,c_one)

if len(zeros) <= K:
    print(len(S))
else:
    ans = 0
    for i in range(0, len(zeros) - K):
        aaa = c_zero[i+K]-c_zero[i] + c_one[i+K+1]-c_one[i]
        ans = max(aaa, ans)
    print(ans)

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
a=input()
l=len(a)
src=[int(a[i:i+1]) for i in range(l)]
from itertools import groupby
y =[sum(1 for e in it) for _, it in groupby(src, key=lambda x: x==1)]
m=len(y)
if src[0]==1 and src[l-1]==1:
  if (m-1)/2<=k:
    print(int(n))
  else:
    c=0
    for i in range(m-2*k+1):
      if i%2==0:
        d=y[i:i+2*k+1]
        f=sum(d)
        if f>=c:
          c=f
          print(c)
if src[0]==0 and src[l-1]==1:
  if m/2<=k:
    print(int(n))
  else:
    c=0
    for i in range(m-2*k+1):
      if i%2==1:
        d=y[i:i+2*k+1]
        f=sum(d)
        if f>=c:
          c=f
          print(c)
n,k=map(int,input().split())
a=input()
l=len(a)
src=[int(a[i:i+1]) for i in range(l)]
from itertools import groupby
y =[sum(1 for e in it) for _, it in groupby(src, key=lambda x: x==1)]
m=len(y)
if src[0]==1 and src[l-1]==1:
  if (m-1)/2<=k:
    print(int(n))
  else:
    c=0
    for i in range(m-2*k+1):
      if i%2==0:
        d=y[i:i+2*k+1]
        f=sum(d)
        if f>=c:
          c=f
          print(c)
if src[0]==1 and src[l-1]==0:
  if m/2<=k:
    print(int(n))
  else:
    c=0
    for i in range(m-2*k+1):
      if i%2==0:
        d=y[i:i+2*k+1]
        f=sum(d)
        if f>=c:
          c=f
          print(c)

Submission status: Runtime Error

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

I=[0,]

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

if len(I)<k:
    I.extend([n+1]*(2*k+1))

X=[]

for i in range(len(I)):
    if s[I[i]]=='0':
        X.append(I[i+2*k]-I[i])
    else:
        X.append(I[i+2*k+1]-I[i])

print(max(X))

Submission status: Runtime Error

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

I=[0,]

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

if len(I)<k:
    I.extend([n+1]*(k-len(I)))

X=[]

for i in range(len(I)):
    if s[I[i]]=='0':
        X.append(I[i+2*k]-I[i])
    else:
        X.append(I[i+2*k+1]-I[i])

print(max(X))

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
a=input()
l=len(a)
src=[int(a[i:i+1]) for i in range(l)]
from itertools import groupby
y =[sum(1 for e in it) for _, it in groupby(src, key=lambda x: x==1)]
m=len(y)
if src[0]==1 and src[l-1]==1:
  if (m-1)/2<=k:
  print(int(n))
  else:
    c=0
    for i in range(m-2*k+1):
      if i%2==0:
        d=y[i:i+2*k+1]
        f=sum(d)
        if f>=c:
          c=f
          print(c)

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
s=list(input())
s=list(map(int,s))
stack=[]
tmp=1
flag=True
for i in range(1,len(s)):
  if s[i]!=s[i-1]:
    stack.append(tmp)
    tmp=1
  else:
    tmp+=1
stack.append(tmp)
left=[0]
right=[0]
tmp=0
if s[0]==0:
  flag=False
  if len(stack)%2==1:
    stack.append(0)
  if len(stack)//2<=k:
    print(len(s))
    exit()
else:
  if len(stack)%2==0:
    stack.append(0)
  if len(stack)//2<=k:
    print(len(s))
    exit()
for i in range(len(stack)):
  tmp+=stack[i]
  if i%2==0:
    if flag:
      right.append(tmp)
    else:
      left.append(tmp)
  else:
    if flag:
      left.append(tmp)
    else:
      right.append(tmp)
ans=0
for i in range(len(right)-k):
  if flag:
    ans=max(ans,right[i+k+1]-left[i])
  else:
    ans=max(ans,right[i+k]-left[i])
print(ans)

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
s=input()
a=[]
cnt=1
for i in range(n-1):
  if s[i]==s[i+1]:
    cnt+=1
  else:
    a.append(cnt)
    cnt=1
b=[a[0]]*len(a)
for j in range(len(a)-1):
  b[j+1]=b[j]+a[j+1]
ans=0
for l in range(len(b)-k):
  if ans<b[l+k+1]-b[l]:
    ans=b[l+k]-b[l]
print(ans)

Submission status: Runtime Error

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

using ll = long long;
using ull = unsigned long long;

const double PI = acos(-1);
const int inf = 2e9;
const ll INF = 2e18;
const ll MOD = 1e9+7;

#define sz(s) (s).size()
#define pb push_back
#define fi first
#define se second
#define REP(i,n) for (int i = 0; i<n; i++)
#define ALL(a) begin(a), end(a)

int main (void) {
    int N,K;
    cin>>N>>K;
    string S;
    cin>>S;
    vector<pair<int,int>> R;
    if (S.size() == 1) {
        cout<<1<<endl;
        return 0;
    }

    int temp = 1;
    for (int i=1;i<N;i++) {
        if (S[i] == S[i-1]) temp++;
        else {
            if (S[i] == '0') {
                R.pb(make_pair(temp,1));
            }
            else {
                R.pb(make_pair(temp,0));
            }
            temp = 1;
        }
    }
    if (S[N -1] == '1') {
        R.pb(make_pair(temp,1));
    }
    else {
        R.pb(make_pair(temp,0));
    }

    if (R[R.size()-1].se == 0) R.pb(make_pair(0,1));
    if (R[0].se == 0) R.insert(R.begin(),make_pair(0,1));
    int ans = 0;
    int M = R.size();
    int Znum = 0;

    vector<int> RS(M+5,0);
    REP(i,M) {
        RS[i + 1] = R[i].fi + RS[i];
    }
    
    int add = 2 * K + 1;
    for (int i=0;i<M;i+=2) {
        int left = i;
        int right = min(i + add, M);
        int sum = RS[right] - RS[left];
        ans = max(sum,ans);
    }

    cout<<ans<<endl;
    
    return 0;
}

Submission status: Runtime Error

Solution code:
import collections
n, k = map(int, input().split())
s = input()
nums = collections.deque()
anss = collections.deque(maxlen = 2*k +1)
ans = []
c_zero = 0
c_one = 0
if s[1] == '0':
    nums.append(0)
for i in s:
    if i == '0':
        c_zero += 1
        if c_one > 0:
            nums.append(c_one)
            c_one = 0
    elif i == '1':
        c_one += 1
        if c_zero > 0:
            nums.append(c_zero)
            c_zero = 0
if s[-1] == '1':
    nums.append(c_one)
elif s[-1] == '0':
    nums.append(c_zero)
while len(anss) < 2*k +1:
    if len(nums) == 0:
        print(n)
        break
    anss.append(nums.popleft())
if len(nums) == 0:
    print(n)
while nums:
    ans.append(sum(anss))
    if len(nums) == 0:
        break
    anss.append(nums.popleft())
    if len(nums) == 0:
        break
    anss.append(nums.popleft())
    if len(nums) == 0:
        ans.append(sum(anss))
if ans:
    print(max(ans))

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
import bisect
import heapq
import math
import random
import sys
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations
from operator import add, mul, sub

sys.setrecursionlimit(100000)


def read_int():
    return int(input())


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


def read_float():
    return float(input())


def read_float_n():
    return list(map(float, input().split()))


def read_str():
    return input().strip()


def read_str_n():
    return list(map(str, input().split()))


def error_print(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.time()
        ret = f(*args, **kwargs)
        e = time.time()

        error_print(e - s, 'sec')
        return ret

    return wrap


@mt
def slv(N, K, S):
    s1 = list(map(lambda x: len(x), filter(lambda x: x != '', S.split('0'))))
    s0 = list(map(lambda x: len(x), filter(lambda x: x != '', S.split('1'))))
    if S[0] == '0':
        s1.insert(0, 0)
    if S[-1] == '0':
        s1.append(0)
    s = []
    for i in range(len(s0)):
        s.append(s1[i])
        s.append(s0[i])
    s.extend(s1[i+1:])

    sa = [0]
    for v in s:
        sa.append(sa[-1]+v)

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

    return ans


def main():
    N, K = read_int_n()
    S = read_str()
    rS = ''.join(['0' if c == '1' else '1' for c in S])
    print(max(slv(N, K, S), slv(N, K-1, rS)))

    # N = 20
    # K = random.randint(1, 10)
    # S = ''.join(random.choices('01', k=N))
    # rS = ''.join(['0' if c == '1' else '1' for c in S])
    # print(max(slv(N, K, S), slv(N, K-1, rS)))

if __name__ == '__main__':
    main()

Submission status: Runtime Error

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

if "0" not in S:
	print len(S)
	quit()

L=[]
zero_kukan=0
for i,x in enumerate(S):
	if i==0:
		cnt=1
		if x=="0": zero_kukan+=1
	else:
		if prev==x:
			cnt+=1
		else:
			L.append(cnt)
			cnt=1
			if x=="0": zero_kukan+=1
	prev=x
else:
	L.append(cnt)

K=min(K, zero_kukan)


LSUM=[0]
for x in L:
	LSUM.append(LSUM[-1]+x)


#print "L :::: ",L
#print "LSUM  :::: ",LSUM

lsum_len=len(LSUM)

#print "lsum_len  :::: ",lsum_len

h=2*K+1

ans=0
if S[0]=="1":
	for l in range(0,lsum_len-h,2):
		r=l+h
		ans=max(ans, LSUM[r]-LSUM[l])
else:
	ans=max(ans, LSUM[2*K]-LSUM[0])
	for l in range(1,lsum_len-h,2):
		r=l+h
		ans=max(ans, LSUM[r]-LSUM[l])
	
	ans=max(ans, LSUM[lsum_len-1]-LSUM[lsum_len-1-2*K])
print ans

Submission status: Runtime Error

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

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

def dfs(s, k, st):
    global mx
    if mx == N: return mx
    if k:
        for j in range(st, len(pos)):                                                                          
            i, count = pos[j]
            mx = max(dfs(s[:i] + '1'*count + s[i+count:], k-1, j+1), mx) 
    return max(count_consecutive(s), mx) 

l, mx, pos = len(s), 0, []
i = 0 
while i < l:
    if s[i] == '0':
        count = 0 
        while i+count < l and s[i+count] == '0':
            count += 1
        pos.append([i, count])
        i += count
    else:
        i += 1
print(dfs(s, k, 0))

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
S = [int(i) for i in list(input())]
S.append(9)
z = []
o = []
tmp = 0
for i in range(N):
    tmp += 1
    if S[i] == 0 and S[i+1] != 0:
        z.append(tmp)
        tmp = 0
    elif S[i] == 1 and S[i+1] != 1:
        o.append(tmp)
        tmp = 0

ans = []
for i in range(len(o)):
    ans.append(sum(o[i:i+K+1]) + sum(z[i:i+K]))
print(max(ans))

Submission status: Runtime Error

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

if "0" not in S:
	print len(S)
	quit()

L=[]
for i,x in enumerate(S):
	if i==0:
		cnt=1
	else:
		if prev==x:
			cnt+=1
		else:
			L.append(cnt)
			cnt=1
	prev=x
else:
	L.append(cnt)	


LSUM=[0]
for x in L:
	LSUM.append(LSUM[-1]+x)


#print "L :::: ",L
#print "LSUM  :::: ",LSUM

lsum_len=len(LSUM)

#print "lsum_len  :::: ",lsum_len

h=2*K+1

ans=0
if S[0]=="1":
	for l in range(0,lsum_len-h,2):
		r=l+h
		ans=max(ans, LSUM[r]-LSUM[l])
else:
	ans=max(ans, LSUM[2*K]-LSUM[0])
	for l in range(1,lsum_len-h,2):
		r=l+h
		ans=max(ans, LSUM[r]-LSUM[l])
	
	ans=max(ans, LSUM[lsum_len-1]-LSUM[lsum_len-1-2*K])
print ans

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
S = input()
Nums = [0]
now = 1 # 今見ている数
cnt = 0 # nowがいくつ並んでいるか
for i in range(N):
    if s == str(now):
        cnt += 1
    else:
        Nums.append(cnt)
        now = 1 - now   # 0と1を切り替えるときの計算 now ^= 1
        cnt = 1
if cnt != 0:
    Nums.append(cnt)
# 1-0-1-0-1-0-1 って感じの配列が欲しい
# 1-0-1-0-1-0 みたいに0で終わっていたら適当に1つ足す
if len(Nums) % 2 == 0:
    Nums.append(0)

Add = 2 * K + 1
ans = 0
# 1-0-1... の1から始めるので、偶数番目だけ見る
for i in range(0, len(Nums), 2):
    tmp = 0
    left = i
    right = min(i + Add, len(Nums))
    for j in range(left, right):
        tmp += Nums[j]
    ans = max(tmp, ans)

print(ans)

Submission status: Runtime Error

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

import sys
input = sys.stdin.readline

def io_generator():
	return input()

#+++++++++++++++++++

def main(io):
	n,k= map(int, io().split())
	s=[int(i) for i in io()[:n]]
	init=s[0]
	ll=[]
	m=0
	pre=init
	for c in s:
		if c == pre:
			m+=1
		else:
			ll.append(m)
			m=1
			pre=c
	ll.append(m)
	
	#print(ll)
	off= 1 if init==1 else 0
	if len(ll)<=2*k + off:
		return n
	pre=sum(ll[:2*k+off])
	rr_max=pre
	for i in range(len(ll)//2 - k):
		cc=pre - ll[2*i] - ll[2*i+1] + ll[2*(k+i)+off]+ll[2*(k+i)+1+off]
		rr_max = max(rr_max, cc)
		pre=cc
	return rr_max


#++++++++++++++++++++

if __name__ == "__main__":
	io= lambda : io_generator()
	print (main(io))

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 i=0;
    int j=0;

    int ans = 0;

    while(j<n)
    {
        while(j<n && s[j]=='1')
        {
            j++;
        }
        ans = max(ans,j-i);
        while(j<n && s[j]=='0')
        {
            j++;
        }
        k--;
        if(k<0)
        {
            while(i<n && s[i]=='1')
            {
                i++;
            }
            while(i<n && s[i]=='0')
            {
                i++;
            }
        }
        ans = max(ans,j-i);
    }
    cout << ans << endl;
}

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;
    
    vector<int> arr;
    if (s[0] == '0') arr.push_back(0);
    int cnt = 1;
    for (int i = 1; i != n; ++i)
    {
        if (s[i] != s[i - 1])
        {
            arr.push_back(cnt);
            cnt = 1;
        }
        else ++cnt;
    }
    arr.push_back(cnt);
    if (s[n - 1] == '0') arr.push_back(0);
    
    int ans = 0;
    int curr = arr[0];
    int j = 1;
    for (int i = 1; i != arr.size(); i += 2)
    {
        curr += arr[i] + arr[i + 1];
        if (i - j == k << 1)
        {
            curr -= arr[j] + arr[j - 1];
            j += 2;
        }
        ans = max(ans, curr);
    }
    cout << ans << endl;
    return 0;
}

Submission status: Runtime Error

Solution code:
x,y = map(int,input().split())
li=input()
i=0
m=0
count=0
f=0
n=0
while m<y:

    while '0'*n in li:
        if '0'*n in li:
            c=n
        n+=1

    a=li.find('0'*c)
    b=a+c

    nli = [int(s) for s in li]

    while i<c:
        nli[a+i]=1
        i+=1
    li=[str(s) for s in nli]
    m+=1
    
for num in li:
    if num=='1':
        count+=1

    if num=='0':
        if f<count:
            f=count
        count=0
print(f)

Submission status: Runtime Error

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

cnt = 0
now = 1
Nums = []

for i in range(N):
    if S[i] == 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)

add = 2*K+1
ans = 0
left = right = 0
tmp = 0    # [left, right)

for i in range(0, len(Nums), 2):
    Nextleft = i
    Nextright = min(i+add, len(Nums))
    
    while Nextleft > left:
        tmp -= Nums[left]
        left += 1
    
    while Nextright > right:
        tmp += Nums[right]
        right += 1
    
    ans = max(tmp, ans)

print(ans)

Submission status: Runtime Error

Solution code:
def ret_arr(N, K, S):
    a0 = str(S).split('1')
    remove_specified_values(a0, '')
    a1 = str(S).split('0')
    remove_specified_values(a1, '')

    a = [2] * (len(a1) + len(a0))
    if int(S[0]) == 0:
        for i in range(0, len(a0)):
            if i > (len(a0) - 2) and int(S[-1]) == 0:
                a[i * 2] = a0[i]
            else:
                a[i * 2] = a0[i]
                a[i * 2 + 1] = a1[i]
    else:
        for i in range(0, len(a1)):
            if i > (len(a1) - 2) and int(S[-1]) == 1:
                a[i * 2] = a1[i]
            else:
                a[i * 2] = a1[i]
                a[i * 2 + 1] = a0[i]
    return a


def remove_specified_values(arr, value):
    while value in arr:
        arr.remove(value)


def ret_arr_len(arr):
    ret_a = []
    for i in arr:
        ret_a.append(len(i))
    return ret_a


def count(arr, K):
    k = K * 2 + 1
    c_a = []
    for i in range(0, len(arr) - (k - 1)):
        c = 0
        for _k in range(0, k):
            c = c + arr[(i + _k)]
        # print(c)
        c_a.append(c)
    try:
        m = max(c_a)
    except Exception as e:
        m = 0

    return m


def main(N, K, S):
    print('~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~')
    a = ret_arr(N, K, S)
    len_a = ret_arr_len(a)

    c_a = []
    for i in range(0, K + 1):
        c = count(len_a, i)
        print(f'{i} : {c}')
        c_a.append(c)

    return max(c_a)

Submission status: Runtime Error

Solution code:
ans=-1
l=0

r=0
R=0

q=[0]
for i in range(1,N-1):
    if S[i]!=S[i-1]:
        q.append(i)
q.append(N+1)
        
L=q[l]
R=q[r]

S[L:R]

for l in range(len(q)):
    count=0
    for r in range(l+1,len(q)):        
        L=q[l]
        R=q[r]
        print(S[L:R])
        if S[L:R][-1]=='0':
            count+=1
            if count>K:
                break
        ans=max(ans,len(S[L:R]))
        
print(ans)

Submission status: Runtime Error

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

N,K = map(int, input().split())
S = str(input())
pattern = r"0+"
iterator = re.finditer(pattern ,S)
l = [match.span() for match in iterator]
print(l)
print(len(l)-K)
# -*- coding: utf-8 -*-
import re
import itertools

N,K = map(int, input().split())
S = str(input())if len(l) <= K:
    ans = N
else:
    ans = 0
    for i in range(len(l)-K+1):
        v = l[i:i+K]
        s = S
        for j in range(K):
            add = "1"*(v[j][1]-v[j][0])
            s =s[:v[j][0]] + add + s[v[j][1]:]
        splt = re.split(pattern, s)
        print(splt)
        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=map(int,input().split())
S=input()

q=[0]
for i in range(1,N-1):
    if S[i]!=S[i-1]:
        q.append(i)

answers=[]

for i,p in enumerate(q):
    if S[p]=='1':
        if i+2*K+1>len(q)-1:
                answers.append(N-p)
        else:
            answers.append(q[min(i+2*K+1,len(q)-1)]-p)
#    else:
#        answers.append(q[min(2*K,len(q)-1)]-p+1)
        
print(max(answers))

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
S = str(input())
S_list = []
cum_list = [0]
tmp = 1
count = 0

for i in range(N):
    if i == 0:
        if int(S[i]) == tmp:
             count += 1
        else:
            S_list.append(count)
            count = 1
    else:
        if S[i] == S[i - 1]:
            count += 1
            
        else:
            S_list.append(count)
            count = 1

S_list.append(count)
if S[-1] == "0":
    S_list.append(0)


now = 0
for i in range(len(S_list)):
    now += S_list[i]
    cum_list.append(now)


answer_list = []
i = 0
if 2 * K + 1 < len(cum_list):
    while i + 2 * K + 1 < len(cum_list):
        answer_list.append(cum_list[i + 2 * K + 1] - cum_list[i])
        i += 2
else:
    answer_list.append(len(S))
answer_list = check(cum_list,K)
answer = max(answer_list)
print(answer)

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
S = str(input())
S_list = []
cum_list = [0]
tmp = 1
count = 0

for i in range(N):
    if i == 0:
        if int(S[i]) == tmp:
             count += 1
        else:
            S_list.append(count)
            count = 1
    else:
        if S[i] == S[i - 1]:
            count += 1
            
        else:
            S_list.append(count)
            count = 1

S_list.append(count)
if S[-1] == "0":
    S_list.append(0)


now = 0
for i in range(len(S_list)):
    now += S_list[i]
    cum_list.append(now)


answer_list = []
i = 0
if 2 * K + 1 < len(x):
    while i + 2 * K + 1 < len(x):
        answer_list.append(x[i + 2 * K + 1] - x[i])
        i += 2
else:
    answer_list.append(len(S))
answer_list = check(cum_list,K)
answer = max(answer_list)
print(answer)

Submission status: Runtime Error

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

q=[0]
for i in range(1,N):
    if S[i]!=S[i-1]:
        q.append(i)

answers=[]

for p in q[:(2*K+1)]:
    if S[p]==1:
        answers.append(q[min(2*K+1,N)]-p+1)
    else:
        answers.append(q[min(2*K,N)]-p+1)
        
print(max(answers))

Submission status: Runtime Error

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

q=[0]
for i in range(1,N):
    if S[i]!=S[i-1]:
        q.append(i)

answers=[]

for p in q[:(2*K+1)]:
    if S[p]==1:
        answers.append(q[min(2*K+1,N)]-p+1)
    else:
        answers.append(q[min(2*K,N)]-p+1)
        
print(max(answers)

Submission status: Runtime Error

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

q=[0]
for i in range(1,N):
    if S[i]!=S[i-1]:
        q.append(i)

answers=[]

for p in q[:(2*K+1)]:
    if S[p]==1:
        answers.append(q[min(2*K+1,N)]-p+1)
    else:
        answers.append(q[min(2*K,N)]-p+1)

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
S = str(input())
S_list = []
cum_list = [0]
tmp = 1
count = 0
if "0" not in S or "1" not in S:
    print(len(S))

else:
    for i in range(N):
        if i == 0:
            if int(S[i]) == tmp:
                count += 1
            else:
                S_list.append(count)
                count = 1
        else:
            if S[i] == S[i - 1]:
                count += 1
                if i == N - 1:
                    S_list.append(count)
            else:
                S_list.append(count)
                count = 1


    if S[-1] == "0":
        S_list.append(0)




    now = 0
    for i in range(len(S_list)):
        now += S_list[i]
        cum_list.append(now)
    def check(x,K):
        answer_list = []
        i = 0
        if i + 2 * K + 1 < len(x):
            while i + 2 * K + 1 < len(x):
                answer_list.append(x[i + 2 * K + 1] - x[i])
                i += 2
            return answer_list
        elif K > 0:
            check(x,K-1)
    answer_list = check(cum_list,K)

    answer = max(answer_list)
    print(answer)

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
S = str(input())
S_list = []
cum_list = [0]
tmp = 1
count = 0
if "0" not in S:
    print(len(S))
else:
    for i in range(N):
        if i == 0:
            if int(S[i]) == tmp:
                count += 1
            else:
                S_list.append(count)
                count = 1
        else:
            if S[i] == S[i - 1]:
                count += 1
                if i == N - 1:
                    S_list.append(count)
            else:
                S_list.append(count)
                count = 1


    if S[-1] == "0":
        S_list.append(0)




    now = 0
    for i in range(len(S_list)):
        now += S_list[i]
        cum_list.append(now)
    def check(x,K):
        answer_list = []
        i = 0
        if i + 2 * K + 1 < len(x):
            while i + 2 * K + 1 < len(x):
                answer_list.append(x[i + 2 * K + 1] - x[i])
                i += 2
            return answer_list
        elif K > 0:
            check(x,K-1)
    answer_list = check(cum_list,K)

    answer = max(answer_list)
    print(answer)

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
S = str(input())
S_list = []
cum_list = [0]
tmp = 1
count = 0
if S == "1":
    print(1)
else:
    for i in range(N):
        if i == 0:
            if int(S[i]) == tmp:
                count += 1
            else:
                S_list.append(count)
                count = 1
        else:
            if S[i] == S[i - 1]:
                count += 1
                if i == N - 1:
                    S_list.append(count)
            else:
                S_list.append(count)
                count = 1


    if S[-1] == "0":
        S_list.append(0)




    now = 0
    for i in range(len(S_list)):
        now += S_list[i]
        cum_list.append(now)
    def check(x,K):
        answer_list = []
        i = 0
        if i + 2 * K + 1 < len(x):
            while i + 2 * K + 1 < len(x):
                answer_list.append(x[i + 2 * K + 1] - x[i])
                i += 2
            return answer_list
        elif K > 0:
            check(x,K-1)
    answer_list = check(cum_list,K)

    answer = max(answer_list)
    print(answer)

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
S = str(input())
S_list = []
cum_list = [0]
tmp = 1
count = 0
for i in range(N):
    if i == 0:
        if int(S[i]) == tmp:
            count += 1
        else:
            S_list.append(count)
            count = 1
    else:
        if S[i] == S[i - 1]:
            count += 1
            if i == N - 1:
                S_list.append(count)
        else:
            S_list.append(count)
            count = 1


if S[-1] == "0":
    S_list.append(0)




now = 0
for i in range(len(S_list)):
    now += S_list[i]
    cum_list.append(now)
def check(x,K):
    answer_list = []
    i = 0
    if i + 2 * K + 1 < len(x):
        while i + 2 * K + 1 < len(x):
            answer_list.append(x[i + 2 * K + 1] - x[i])
            i += 2
        return answer_list
    elif K > 0:
        check(x,K-1)
        
    
    
answer_list = check(cum_list,K)
if answer_list == None:
    answer_list = [1]
answer = max(answer_list)
print(answer)

Submission status: Runtime Error

Solution code:
n,k = map(int,input().split())
s = input()
#入力
cnt = 0
if s[0] == '0':
    cnt += 1
for i in range(1,n):
    if s[i-1] == '1' and s[i] == '0':
        cnt += 1
##切り替わるところで＋1
if cnt <= k:
    print(n)
    #切り替わるところが動かす回数より少ない場合n(自明)
else:
    one_left,one_right = [0],[]
    #
    for i in range(1,n):
        if s[i-1] == '0' and s[i] == '1':
            one_left.append(i)
        if s[i-1] == '1' and s[i] == '0':
            one_right.append(i)
    #01 の時右に加える10の時左に加える
    #切り替えの時の配列の番号を加える
    one_right.append(n)
    #nはとりあえず右
    if s[0] == '1':
        one_right.pop(0)
    if s[-1] == '1':
        one_left.pop(-1)
    #最初の人が逆立ちしているなら右の切り替えの最初はなし
    #最後の人が逆立ちしているなら左の切り替えの最後はなし
    #print(one_right,'r')
    #print(one_left,'l')

    ans = 0
    #ans 01,10の切り替えの少ないほうでloopを回す
    for i in range(min(len(one_left),len(one_right)-k+1)):
        ans = max(ans,one_right[i+k-1]-one_left[i])
        #ans は　ある点までの個数-フラグの開始点までの個数の最大値
print(ans)

Submission status: Runtime Error

Solution code:
from itertools import*
from numpy import*
n,k=map(int,input().split())
k2=k+1
s=input()
if len(s)==1:
    if s=="1":
        print(1)
    else:
        print(0)
    exit()
p=[len(list(v))for k,v in groupby(s)if k=="0"]
q=[len(list(v))for k,v in groupby(s)if k=="1"]
if len(p)==1 and len(q)==0 and k>0:
    print(sum(p))
    exit()
elif len(q)==1 and len(p)==0:
    print(sum(q))
    exit()
if len(p)==1 and len(q)==1:
    if k>0:
        print(sum(p)+sum(q))
        exit()
    else:
        print(q[0])
        exit()

if s[0]=="1" and s[-1]=="0":
    x=[0]+p
    y=[0]+q+[0]
elif s[0]=="0" and s[-1]=="1":
    x=[0]+p+[0]
    y=[0]+q
else:
    x=[0]+p+[0]
    y=[0]+q+[0]
x1=array(x[:-k])
x2=array(x[k:])
x3=sum(x[:k])
t=x3+cumsum(x2-x1)
y1=array(y[:-k2])
y2=array(y[k2:])
y3=sum(y[:k2])
f=y3+cumsum(y2-y1)

print(max(max(t+f),x3+y3))

Submission status: Runtime Error

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

now = 1 # 今見ている数
cnt = 0 # nowがいくつ並んでいるか
nums =[]

for i in range(n):
    if s[i] == '0'+str(now):
        cnt += 1
    else:
        nums.append(cnt)
        now ^= 1    #見る値の切り替え
        cnt += 1

if cnt != 0:
    nums.append(cnt)

if len(nums) % 2 == 0:
    nums.append(0)

add = 2 * k + 1
ans = 0
left = 0
right = 0
r_l_sum = 0
for i in range(len(nums))[::2]:
    next_left = i
    next_right = min(i+add,len(nums))
    # 尺取法
    while next_left > left:
        tmp -= nums[left]
        left += 1
    while next_right > right:
        tmp += nums[right]
        right += 1
    ans = max(tmp,ans)

print(ans)