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())
s = list(input())
a = []
switch = 0
tmp = 0
for i in range(n):
    if switch == 0:
        if s[i] == "1":
            tmp += 1
        else:
            a.append(tmp)
            tmp = 1
            switch = 1
    else:
        if s[i] == "0":
            tmp += 1
        else:
            a.append(tmp)
            tmp = 1
            switch = 0
a.append(tmp)

tmp = 0
if k*2 <= len(a):
    tmp = k
else:
    if len(a)%2 == 0:
        tmp = len(a)/2
    else:
        tmp = len(a)//2 


ans = sum(a[0:tmp*2+1])
tmpans = sum(a[0:tmp*2+1])
i = 2
while i+tmp*2<=len(a):
    tmpans -= a[i-2]+a[i-1]
    if i+tmp*2 == len(a):
        tmpans += a[i+tmp*2-1]
    else:
        tmpans += a[i+tmp*2-1]+a[i+tmp*2] 
    ans = max(ans,tmpans)
    i+=2
print(ans)

Submission status: Runtime Error

Solution code:
import re
n,k=map(int, input().split())
s=input()
i=[[m.start(),m.end()] for m in re.finditer("1(?=0)|0(?=1)",s)]
i.insert(0,[0,0]);i.append([len(s)-1,0])
ss=[i[j+1][0]-i[j][1]+1 for j in range(len(i)-1)]
if s[0]=="0": ss.insert(0,0);ss.append(0)
res=[sum(ss[2*m:2*(m+k)+1]) for m in range(max((len(ss)-1)//2+1-k,1)]
print(max(res))

Submission status: Runtime Error

Solution code:
import re
n,k=map(int, input().split())
s=input()
i=[[m.start(),m.end()] for m in re.finditer("1(?=0)|0(?=1)",s)]
i.insert(0,[0,0]);i.append([len(s)-1,0])
ss=[i[j+1][0]-i[j][1]+1 for j in range(len(i)-1)]
is1ststr1 = True if s[0]=="1" else False
if not is1ststr1: ss.insert(0,0);ss.append(0)
res=[sum(ss[2*m:2*(m+k)+1]) for m in range(len(ss)//2)]
print(max(res))

Submission status: Runtime Error

Solution code:
import re

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

def reMax(str, max, reg):
  target = ""
  kouho = re.findall(reg, str)
  for i, k in enumerate(kouho):
    l = len(k)
    if max < l:
      max = l
      target = k
  return max, target

def reverse(str, max, depth):
  if depth <= 0:
    return str
  else:
    max, target = reMax(str, max, '1+0+1+')
    str = str.replace(target,  "1" * max, 1)
    return reverse(str, max, depth-1)

S = reverse(S, 0, K)
max, target = reMax(S, 0, '1+')
print(max)

Submission status: Runtime Error

Solution code:
N, M = map(int, input().split())
in_str = input()
_list = []

for i, x in enumerate(in_str):
  if i == 0:
    l_x = 1
    pre_x = x
    if x == '1':
      st_num = 1
  else:
    if pre_x == x:
      l_x += 1
    else:
      _list.append(l_x)
      pre_x = x
      l_x = 1
_list.append(l_x)

L = len(_list)

_list2 = []
for j in range(L//2-M+1):
  if st_num == 1:
    _list2.append(sum(_list[2*j:2*j+2*M+1]))
  if st_num == 0:
    if j == 0:
      _list2.append(sum(_list[0:2*M]))
    else:
      _list2.append(sum(_list[2*j-1:2*j+2*M]))
if not _list2 == []:
  print(max(_list2))
if L == 1:
  print(max(_list))

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())
people = str(input())
max = 0

for i in range(n):
    l = i
    numk = k
    lenof0 = 0
    current = 0
    while 1:
        while people[l] == '0':
            lenof0 += 1
            l += 1
            if l == n-1:
                break
        if lenof0 != 0:
            numk -=1
            current += lenof0
            lenof0 = 0
        if l == n-1:
            if people[l] == '1':
                current += 1
            if max < current:
                max = current
            break
        while people[l] == '1':
            current += 1
            l += 1
            if l == n-1:
                if people[l] == '1':
                    current += 1
                break
        if numk == 0 or l == n-1:
            if max < current:
                max = current
            break
print(max)

Submission status: Runtime Error

Solution code:
import sys
def main():
  if 'get_ipython' not in globals():  
    # For Subsession
    N,K = map(int, input().split())
    S = sys.stdin.readline
  else:
    # For Test
    # N = 5
    # K = 1
    # S = "00010"
    N = 14
    K = 2
    S = "11101010110011"
    # N = 1
    # K = 1
    # S = "1"
    
  SC = []
  i = 0
  if S[0] == "0":SC.append(0)
  while i < N :
    if S[i] == "0":
      j = S.find("1",i)
    else:
      j = S.find("0",i)
    if j == -1: j = N 
    cnt = j - i
    i = j
    SC.append(cnt)
    
  if 'get_ipython' in globals(): print(SC)
    
  i = 0
  lenSC = len(SC) - 1
  ans = 0
  while i <= lenSC:
    cnt = 0
    k = i + K * 2
    if k > lenSC:
      k = lenSC
    k += 1
    cnt = sum(SC[i:k])
    if ans < cnt:
      ans = cnt
    i += 2

  print(ans)

main()

Submission status: Runtime Error

Solution code:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, m, n) for (int i = m; i < n; ++i)

int main() {
  int N, K;
  string S;
  cin >> N >> K >> S;
  S.push_back('2');
  vector<int> A;
  if(S[0] == '0') A.push_back(0);
  int cnt = 1;
  rep(i, 1, S.size()) {
    if(S[i] == S[i-1]) cnt++;
    else {
      A.push_back(cnt);
      cnt = 1;
    }
  }
  if(S.back() == '0') A.push_back(0);
  int ans = 0;
  int r = 0;
  int sum = 0;
  for(int l = 0; l < A.size(); l++){
    while(r < A.size() && r - l < 2 * K + 1){
      sum += A[r];
      r++;
    }
    if(l % 2 == 0) ans = max(ans, sum);
    if(l == r) r++;
    else sum -= A[l];
  }
  cout << ans << endl;
  return 0;
}

Submission status: Runtime Error

Solution code:
import numpy as np

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

memo = np.zeros(10**5+1, dtype = np.int64)
W = min(2*K, 10**5)

current = 1 # 最初は1が0個ある
p = 0

for x in S:
  if x == current:
    memo[x,p] += 1
  else:
    current = 1 - current
    p += 1

ans = memo[:W+1].sum()
x = ans
for r in range(2,10**5+1,2):
  if r+W > 10**5:
    break
  x = x-memo[r-2]-memo[r-1]+memo[r+W]+memo[r+W-1]
  if x > ans:
    ans = x

print(ans)

Submission status: Runtime Error

Solution code:
n = int(input())
list =list(input())
m = ""
j = int(input())
for i in range(n):
  if list[i] == list[j]:
    m += list[j]
  else:
    m += "*"
print(m)

Submission status: Runtime Error

Solution code:
def listCount(Se):
  slist = []
  cnt0 = 0
  cnt1 = 0
  for i in range(len(Se)):
    if Se[i] == '1':
      cnt1 = cnt1 + 1
      if cnt0 != 0:
        slist.append(cnt0)
        cnt0 = 0
    elif Se[i] == '0':
      cnt0 = cnt0 + 1
      if cnt1 != 0:
        slist.append(cnt1)
      cnt1 = 0
  slist.append(cnt1)
  return slist

def listSum(slist, K):
  Ke = 2 * K + 1
  snum = len(slist)
  val = 0
  for j in range(Ke):
    val = val + slist[j]
  
  max1val = val
  max1idx = 0
  max2val = val
  max2idx = 0
  max2val = val
  max2idx = 0

  vallist = [val]
  for i in range(2, snum-(Ke)+1, 2):
    for j in range(K):
      val = val - slist[i-j-1] + slist[i+Ke-j-1]
    if val > max1val:
      max3val = max2val
      max3idx = max2idx
      max2val = max1val
      max2idx = max1idx
      max1val = val
      max1idx = i
    vallist.append(val)

  return vallist, max1idx, max1val, max2val

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

isF0 = False
isE0 = False
Se = S
Ne = N
if Se[Ne-1] == '0':
  Se = Se + '1'
  Ne = Ne + 1
  isE0 = True
if Se[0] == '0':
  Se = '1' + Se
  Ne = Ne + 1
  isF0 = True

slist = listCount(Se)

vallist, max1idx, max1val, max2val = listSum(slist, K)

if max1idx != 0 and max1idx != len(slist) - (2 * K + 1):
  maxval = max1val
elif max1val-1 > max2val:
  maxval = max1val-1
else:
  maxval = max2val

print(maxval)

Submission status: Runtime Error

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

S=list(s)
a=[]
b=[]
w=[]
if s[0]=='0':
    a.append(0)
    m=1
u=0
for i in range(1,n):
    if S[i]=='0':
        u=0
        if S[i-1]==S[i]:
            m=m+1
            if i+1==n:
                continue
            elif S[i+1]=='1':
                b.append(m)
            else:
                continue

        else:
            a.append(i)
            m=1
            if S[i+1]=='1':
                b.append(m)
            else:
                continue
    else:
        u=u+1
        w.append(u)

w.sort(key=None,reverse=True)

if len(a)==0:
    print(w[0])
elif len(a)==1:
    if k>0:
        print(n)

else:
    q=len(a)-k+1
    v=[]
    v.append(a[k-1])
    v.append(len(s)-a[len(a)-3]-b[len(a)-3])
    for i in range(0,q-2):
        r=a[i+k]-a[i+k-3]-b[i+k-3]-1
        v.append(r)

    v.sort()
    print(v[q-1])

Submission status: Runtime Error

Solution code:
n,k = map(int,input().split())
s = list(input())
if s[0] == "1":
    cnt_list = []
else:
    cnt_list = [0]
cnt = 1
for i in range(n-1):
    if s[i] == s[i+1]:
        cnt += 1
    else:
        cnt_list.append(cnt)
        cnt = 1
cnt_list.append(cnt)
if s[-1] == '0':
    cnt_list.append(0)
#print(cnt_list)
sum_list = [0]
for i in range(len(cnt_list)):
    sum_list.append(sum_list[i] + cnt_list[i])
left = 0
right = max(len(sum_list)-2*k, 1)
ans = 0
for i in range(0,right,2):
    ans = max(ans,sum_list[i+2*k+1]-sum_list[i])
print(ans)

Submission status: Runtime Error

Solution code:
n,k = map(int,input().split())
s = list(input())
if s[0] == "1":
    cnt_list = []
else:
    cnt_list = [0]
cnt = 1
for i in range(n-1):
    if s[i] == s[i+1]:
        cnt += 1
    else:
        cnt_list.append(cnt)
        cnt = 1
cnt_list.append(cnt)
if s[-1] == '0':
    cnt_list.append(0)
print(cnt_list)
sum_list = [0]
for i in range(len(cnt_list)):
    sum_list.append(sum_list[i] + cnt_list[i])
left = 0
right = max(len(sum_list)-2*k, 1)
ans = 0
for i in range(0,right,2):
    ans = max(ans,sum_list[i+2*k+1]-sum_list[i])
print(ans)

Submission status: Runtime Error

Solution code:
n,k = map(int,input().split())
s = list(input())
if s[0] == "1":
    cnt_list = []
else:
    cnt_list = [0]
cnt = 1
for i in range(n-1):
    if s[i] == s[i+1]:
        cnt += 1
    else:
        cnt_list.append(cnt)
        cnt = 1
cnt_list.append(cnt)

sum_list = []
for i in range(0,len(cnt_list)-2*k):
    sum_list.append(sum(cnt_list[i:i+2*k+1]))
print(max(sum_list))

Submission status: Runtime Error

Solution code:
N,K =list(map(int,input().split(" ")))
S = input()
S = list(map(int,list(S)))
cnts = []
cnt = 0
check = 1
for s in S:
  if s == check:
    cnt += 1
  else:
    cnts.append(cnt)
    cnt = 1
    check = 1 - check
cnts.append(cnt)

if S[0] == 0:
    cnts.insert(0,0)

# print(cnts)

limit = len(cnts)
size = 2 * K + 1
right  = min(size,limit) 
left = 0
tmp = sum(cnts[:right])
ans = sum(cnts[:right])

while True:
    if right == limit:
        break

    tmp += cnts[right]
    tmp -= cnts[left]
    if tmp > ans:
        ans = tmp
    right += 2
    left += 2
    # print("-"*30)
    # print(ans)
    # print(right)
    # print("-"*30)
    if right == limit:
        break

print(ans)

Submission status: Runtime Error

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

now = 1
cnt = 0
x = []

for i in range(n):
    if sss[i] == str(now):
        cnt += 1
    else:
        x.append(cnt)
        now = 1 - now
        cnt = 1
if cnt != 0:
    x.append(cnt)
if len(x) % 2 == 0:
    x.append(0)

Add = 2 * k + 1
ans = 0

for i in range(len(x))[::2]:
    tmp = 0
    left = i
    right = min(i + Add, len(x))
    for j in range(left, right + 1):
        tmp += x[j]
    ans = max(tmp, ans)

print(ans)

Submission status: Runtime Error

Solution code:
from collections import deque

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

count = 0
flag = True

la = deque([])
for c in S:
    if int(flag) == c:
        count += 1
    else:
        la.append(count)
        count = 1
        flag = not flag

la.append(count)

ltmp = deque([])
sum_la = 0
for _ in range((K-1)*2 + 1):
    tmp = la.popleft()
    sum_la += tmp
    ltmp.append(tmp)

last_a = [sum_la]
while len(la) > 0 :
    tmp = la.popleft()
    sum_la += tmp
    ltmp.append(tmp)
    if len(la) > 0:
        tmp = la.popleft()
        sum_la += tmp
        ltmp.append(tmp)

    last_a.append(sum_la)

    if len(ltmp) > 0:
        tmp = ltmp.popleft()
        sum_la -= tmp
    if len(ltmp) > 0:
        tmp = ltmp.popleft()
        sum_la -= tmp
    last_a.append(sum_la)

print(max(last_a))

Submission status: Runtime Error

Solution code:
from collections import deque

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

count = 0
flag = True

la = deque([])
for c in S:
    if int(flag) == c:
        count += 1
    else:
        la.append(count)
        count = 1
        flag = not flag

la.append(count)

print(la)

ltmp = deque([])
sum_la = 0
for _ in range((K-1)*2 + 1):
    tmp = la.popleft()
    sum_la += tmp
    ltmp.append(tmp)

last_a = [sum_la]
while len(la) > 0 :
    tmp = la.popleft()
    sum_la += tmp
    ltmp.append(tmp)
    if len(la) > 0:
        tmp = la.popleft()
        sum_la += tmp
        ltmp.append(tmp)

    last_a.append(sum_la)

    if len(ltmp) > 0:
        tmp = ltmp.popleft()
        sum_la -= tmp
    if len(ltmp) > 0:
        tmp = ltmp.popleft()
        sum_la -= tmp
    last_a.append(sum_la)

# for i in range(K, len(l) - K, 2):
#     last_a.append(sum(l[i-K:i+K+1]))

# last_b = []
# K = K-1
# for i in range(K, len(l) - K, 2):
#     last_b.append(sum(l[i-K:i+K+1]))

print(max(last_a))
# for i in range(len(l)):
#     l =

Submission status: Runtime Error

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

from sys import stdin



############################################

#from sys import stdin
import numpy as np

# read data for n sequences.
n = stdin.readline().split()
N = int(n[0])
K = int(n[1])

data = stdin.readline().split()
data = str(data)[1:-1][1:-1]

# 1の数の要素にエンコード
# [1の数,次の1までの0の数]


list2=[]
if N == 1:
    print(N)
else:
    data2 = []
    count1=0
    count0=0
    for a in data:
        a = int(a)
        if count0 == 0:
            if a == 1:
                count1+=1
            else:
                count0+=1
        else:
            if a == 0:
                count0+=1
            else:
                data2.append([count1, count0])
                count1=1
                count0=0
    data2.append([count1, count0])
    
    if len(data2)==1:
        print(np.sum(data2))
    else: 
        for i,d in enumerate(data2[:-(K)]):
            sum2=0
            point=i
            for k in range(K+1):
                if k==K:
                    sum2+=(data2[point+k][0])
                else:
                    sum2+=(data2[point+k][0]+data2[point+k][1])
            list2.append(sum2)
        print(max(list2))

# 隣あうK+1コの要素で最大の部分を選ぶ。

Submission status: Runtime Error

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

from sys import stdin

############################################

#from sys import stdin
import numpy as np

# read data for n sequences.
n = stdin.readline().split()
N = int(n[0])
K = int(n[1])

data = stdin.readline().split()
data = str(data)[1:-1][1:-1]

# 1の数の要素にエンコード
# [1の数,次の1までの0の数]


list2=[]
if N == 1:
    print(N)
else:
    data2 = []
    count1=0
    count0=0
    for a in data:
        a = int(a)
        if count0 == 0:
            if a == 1:
                count1+=1
            else:
                count0+=1
        else:
            if a == 0:
                count0+=1
            else:
                data2.append([count1, count0])
                count1=1
                count0=0
    if not count1==1 and not count0==0:
        data2.append([count1, count0])
    
    if len(data2)==1:
        print(data2[0]+data[1])
    else: 
        for i,d in enumerate(data2[:-(K)]):
            sum2=0
            point=i
            for k in range(K+1):
                if k==K:
                    sum2+=(data2[point+k][0])
                else:
                    sum2+=(data2[point+k][0]+data2[point+k][1])
            list2.append(sum2)
        print(max(list2))

# 隣あうK+1コの要素で最大の部分を選ぶ。

Submission status: Runtime Error

Solution code:
import sys

n, k=[int(x) for x in input().split()]
s=input()
l=[]
a='1'
for i in range(len(s)):
  if a != s[i]:
    l.append(i)
    a = s[i]
l.append(len(s))
if s[-1]=='0':
  l.append(0)
if len(l) <= 2*k+1:
  print(len(s))
  sys.exit()
m=0
ans=0
while m+ 2*k < len(s)-1:
  b = l[m+2*k]-l[m]
  if b>ans:
    ans=b
  m+=2
print(ans)

Submission status: Runtime Error

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

from sys import stdin



############################################

#from sys import stdin
import numpy as np

# read data for n sequences.
n = stdin.readline().split()
N = int(n[0])
K = int(n[1])

data = stdin.readline().split()
data = str(data)[1:-1][1:-1]

# 1の数の要素にエンコード
# [1の数,次の1までの0の数]


list2=[]
if N == 1:
    print(N)
else:
    data2 = []
    count1=0
    count0=0
    for a in data:
        a = int(a)
        if count0 == 0:
            if a == 1:
                count1+=1
            else:
                count0+=1
        else:
            if a == 0:
                count0+=1
            else:
                data2.append([count1, count0])
                count1=1
                count0=0
    data2.append([count1, count0])
    
    if len(data2)==1:
        print(data2[0]+data[1])
    else: 
        for i,d in enumerate(data2[:-(K)]):
            sum2=0
            point=i
            for k in range(K+1):
        #        print(k)
                if k==K:
                    sum2+=(data2[point+k][0])
        #            try:
        #                sum+=(data2[point+k+1][0])
        #            except:
        #                x=1
                else:
                    sum2+=(data2[point+k][0]+data2[point+k][1])
            list2.append(sum2)
        print(max(list2))

# 隣あうK+1コの要素で最大の部分を選ぶ。

Submission status: Runtime Error

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

from sys import stdin


############################################

#from sys import stdin
import numpy as np

# read data for n sequences.
n = stdin.readline().split()
N = int(n[0])
K = int(n[1])

data = stdin.readline().split()
data = str(data)[1:-1][1:-1]

# 1の数の要素にエンコード
# [1の数,次の1までの0の数]


list=[]
if N == 1:
    print(N)
else:
    data2 = []
    count1=0
    count0=0
    for a in data:
        a = int(a)
        if count0 == 0:
            if a == 1:
                count1+=1
            else:
                count0+=1
        else:
            if a == 0:
                count0+=1
            else:
                data2.append([count1, count0])
                count1=1
                count0=0
    data2.append([count1, count0])
    for i,d in enumerate(data2[:-(K)]):
        sum=0
        point=i
        for k in range(K+1):
    #        print(k)
            if k==K:
                sum+=(data2[point+k][0])
    #            try:
    #                sum+=(data2[point+k+1][0])
    #            except:
    #                x=1
            else:
                sum+=(data2[point+k][0]+data2[point+k][1])
        list.append(sum)
    print(max(list))

# 隣あうK+1コの要素で最大の部分を選ぶ。

Submission status: Runtime Error

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

cnts = []
# denote whether the first block is 0 or 1
tmp, cnt = s[0], 0
flag = int(s[0])

for i in range(n):
    if s[i] == tmp:
        cnt += 1
    else: # here tmp should be cleared
        cnts.append(cnt)
        cnt = 1
    tmp = s[i]
cnts.append(cnt)
# print(cnts)

n_zero = (len(cnts) + 1) // 2 if flag == 0 else len(cnts) // 2
# print("# of zeros block: ", n_zero)
cumsum = []
for i in range(len(cnts)):
    if i == 0:
        cumsum.append(cnts[0])
    else:
        cumsum.append(cumsum[-1] + cnts[i])

if n_zero <= k:
     ans = sum(cnts)
else:
    ans = 0
    for inx in range(flag, len(cnts), 2):
        start = max(0, inx - 2)
        end = min(inx + 2 * k -1 , len(cnts))
        tmp = cumsum[end] - cumsum[start]
        ans = max(tmp, ans)

print(ans)

Submission status: Runtime Error

Solution code:
n, k = [int(e) for e in input().strip().split(' ')]
s = input().strip()

cnts = []
# denote whether the first block is 0 or 1
tmp, cnt = s[0], 0
flag = int(s[0])

for i in range(n):
  if s[i] == tmp:
    cnt += 1
    else: # here tmp should be cleared
      cnts.append(cnt)
      cnt = 1
      tmp = s[i]
      cnts.append(cnt)
      print(cnts)

      n_zero = (len(cnts) + 1) // 2 if flag == 0 else len(cnts) // 2
      # print("# of zeros block: ", n_zero)
      if n_zero <= k:
        ans = sum(cnts)
        else:
          ans = 0
          for inx in range(flag, len(cnts), 2):
            start = max(0, inx - 1)
            end = min(inx + 2 * k, len(cnts))
            tmp = sum(cnts[start: end])
            ans = max(tmp, ans)

            print(ans)

Submission status: Runtime Error

Solution code:
# _*_ coding: utf-8 _*_
while True:
    n, k = [int(e) for e in input().strip().split(' ')]
    s = input().strip()

    cnts = []
    ans = 0
    # denote whether the first block is 0 or 1
    tmp, cnt = s[0], 0
    flag = int(s[0])

    for i in range(n):
        if s[i] == tmp:
            cnt += 1
        else: # here tmp should be cleared
            cnts.append(cnt)
            cnt = 1
        tmp = s[i]
    cnts.append(cnt)

    n_zero = (len(cnts) + 1) // 2 if flag == 0 else len(cnts) // 2

    if n_zero <= k:
         ans = sum(cnts)
    else:
        ans = 0
        for inx in range(flag, len(cnts), 2):
            start = max(0, inx - 1)
            end = min(inx + 2 * k, len(cnts))
            tmp = sum(cnts[start: end])
            if tmp > ans:
                ans = tmp

    print(ans)

Submission status: Runtime Error

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

n, k = [int(e) for e in input().strip().split(' ')]
s = input().strip()

cnts = []
ans = 0
# denote whether the first block is 0 or 1
tmp, cnt = s[0], 0
flag = s[0]

for i in range(n):
    if s[i] == tmp:
        cnt += 1
    else: # here tmp should be cleared
        cnts.append(cnt)
        cnt = 1
    tmp = s[i]
cnts.append(cnt)

n_zero = (len(cnts) + 1) // 2 if flag == 0 else len(cnts) // 2

if n_zero <= k:
     ans = sum(cnts)
else:
    ans = 0
    for inx in range(flag, len(cnts), 2):
        start = max(0, inx - 1)
        end = min(inx + 2 * k, len(cnts))
        tmp = sum(cnts[start: end])
        if tmp > ans:
            ans = tmp

print(ans)

Submission status: Runtime Error

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

n, k = [int(e) for e in input().strip().split(' ')]
s = input().strip()

cnts = []
ans = 0
# denote whether the first block is 0 or 1
tmp, cnt = s[0], 0
flag = s[0]

for i in range(n):
    if s[i] == tmp:
        cnt += 1
    else: # here tmp should be cleared
        cnts.append(cnt)
        cnt = 1
    tmp = s[i]
cnts.append(cnt)


n_zero = (len(cnts) + 1) // 2 if flag == 0 else len(cnts) // 2

if n_zero <= k:
     ans = sum(cnts)

else:
    ans = 0
    for inx in range(flag, len(cnts), 2):
        start = min(0, inx - 1)
        end = min(inx + 2 * k, len(cnts))
        tmp = sum(cnts[start: end])
        if tmp > ans:
            ans = tmp

print(ans)

Submission status: Runtime Error

Solution code:
N,K = map(int,input().split())
S = input()
if S[0] == '0':
  divide = []
else:
  divide = [0]
tmp = 1
for i in range(1,N):
  if S[i] == S[i-1]:
    tmp += 1
  else:
    divide += [tmp]
    tmp = 1
divide += [tmp]
le = len(divide)
zero = [0]*2*le + divide + [0]*2*le
su = [0]
for i in range(0,5*le):
  su += [su[-1]+zero[i]]
del su[0]
lle = len(su)
mxm = 0
for k in range(K,lle):
  if 2*k+1 > lle:
    break
  cd = su[2*k+1] - su[2*k-2*K]
  if cd > mxm:
    mxm = cd
print(mxm)

Submission status: Runtime Error

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

S += "!"
cur_char = S[0]
cur_char_count = 1
smart_S = ""
smart_count = []
for c in S[1:]:
    if cur_char == c:
        cur_char_count += 1
    else:
        smart_S += cur_char
        smart_count.append(cur_char_count)
        cur_char = c
        cur_char_count = 1

l = 0
r = 0
cnt_0 = 0
cur_sum = 0

ans_cand = []

# print("smart_S: ", smart_S)

while r == 0 or l < r:
    # print("l: {}, r: {}".format(l, r))
    while cnt_0 < K and r < len(smart_S) - 1:
        cur_sum += smart_count[r]
        if smart_S[r] == '0':
            cnt_0 += 1
        r += 1
        
    # proceed one more 
    if r < len(smart_S) and smart_S[r] == '1':
        cur_sum += smart_count[r]
        cnt_0 += 1
        r += 1

    ans_cand.append(cur_sum)

    if l < len(smart_S) and smart_S[l] == '0':
        cur_sum -= smart_count[l]
        cnt_0 -= 1
        l += 1

    else:
        if l < len(smart_S):
            cur_sum -= smart_count[l]
            cnt_0 -= 1
            l += 1
        if l < len(smart_S):
            cur_sum -= smart_count[l]
            cnt_0 -= 1
            l += 1


#print("smart_count: ", smart_count)
#print("ans_cand: ", ans_cand)
print(max(ans_cond))

Submission status: Runtime Error

Solution code:
import bisect as bs
n,k=map(int,input().split())
s=list(input())
m=0
cnt=0
ans=[]
for i in s:
  if i=='1':
    cnt+=1
    ans.insert(bs.bisect_left(ans,m),m)
  else:
    m+=1
for j in range(1,k+1):
  cnt+=ans[-k]
print(cnt)

Submission status: Runtime Error

Solution code:
import bisect as bs
n,k=map(int,input().split())
s=list(input())
m=0
cnt=0
ans=[]
for i in s:
  if i=='1':
    cnt+=1
    ans=insort(bs.bisect_left(ans,m),m)
  else:
    m+=1
for j in range(1,k+1):
  cnt+=ans[-k]
print(cnt)

Submission status: Runtime Error

Solution code:
def search_flip_points(S):
    context = S[0]
    flip_points = []
    for i, s in enumerate(S[1:]):
        if context != s:
            flip_points.append(i + 1)
        context = s
    return flip_points


def get_maximum_value(S, K):
    flip_points = search_flip_points(S)
    return flip_points[K]


if __name__ == '__main__':
    N, K = [int(x) for x in input().split(' ')]
    S = [bool(int(x)) for x in input()]

    assert len(S) == N
    assert 1 <= N <= 10**5
    assert 1 <= K <= 10**5

    current_val = 0
    flip_points = search_flip_points(S)
    flip_points = [0].extend(flip_points)
    for i in flip_points:
        if S[i]:
            val = get_maximum_value(S[i:], K)
        else:
            val = get_maximum_value(S[i:], K - 1)
        current_val = max(current_val, val)

    print(val)

Submission status: Runtime Error

Solution code:
q, k = list(map(int, input().split()))
list = [int(i) for i in input()]

# 0 ,1 のまとまりを作る
sum01 = []
pre = 1
cnt = 0
# 111010
# 001010
for i, e in enumerate(list):
    if e != pre:
        sum01.append(cnt)
        cnt = 1
        pre = e
    else:
        cnt += 1
if cnt != 0:
    sum01.append(cnt)

if len(sum01) % 2 == 1:
    sum01.append(0)

print(sum01)
Add = 2 * k + 1
answer = 0
for i in range(0, len(sum01), 2):
    temp = 0
    left = i
    right = min(i + Add, len(sum01))
    for j in range(left, right, 1):
        temp += sum01[j]
    answer = max(temp, answer)

print(answer)

'''

Submission status: Runtime Error

Solution code:
N,K=map(int,input().split())
S=input()
po=[0]
X=[]
i=0
for i in range(N-1):
	if not S[i] == S[i+1]:
		po.append(i+1)
po.append(len(S))
for i in range(len(po)):
	if 2*K+i+1>=len(po):
		X.append(po[2*K+i]-po[i])
		break
	elif S[po[i]]=='0':
		X.append(po[2*K+i]-po[i])
	else:
		X.append(po[2*K+1+i]-po[i])
print(max(X))

Submission status: Runtime Error

Solution code:
package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"strings"
)

var sc = bufio.NewScanner(os.Stdin)

func ini() {
	var buf = make([]byte, 10000)
	sc.Buffer(buf, 1000000)
}

func nextLine() string {
	sc.Scan()
	return sc.Text()
}

func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}

func main() {
	ini()
	line1 := nextLine()
	NK := strings.Split(line1, " ")
	N, _ := strconv.Atoi(NK[0])
	K, _ := strconv.Atoi(NK[1])
	S := nextLine()
	l := make([]int, 0)
	if string(S[0]) == "0" {
		l = append(l, 0)
	}
	for i := 0; i < N; {
		j := i
		for j < N && S[j] == S[i] {
			j++
		}
		l = append(l, j-i)
		i = j
	}
	if string(S[N-1]) == "0" {
		l = append(l, 0)
	}

	sums := make([]int, len(l)+1)
	for i := 0; i < len(l); i++ {
		sums[i+1] = sums[i] + l[i]
	}
	ans := 0
	for left := 0; left < len(sums); left += 2 {
		right := left + 2*K + 1
		if right >= len(sums) {
			right = len(sums) - 1
		}
		ans = max(ans, sums[right]-sums[left])
	}
	fmt.Println(ans)
}