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(map(int,input().strip()))
K = min(N,K)
cur = S[0]
tmp = [] if cur == 1 else [0]
count = 1
_append = tmp.append
def ngram(x,n):
  return [x[i:i+n] for i in range(0,len(x) - n + 1)]

for s in S[1:]:
  if s != cur:
    _append(count)
    cur = s
    count = 0
  count += 1

_append(count)
if cur == 0:
  _append(0)
  
if len(tmp) == 1:
  print(tmp[0])
else:
  odd = tmp[::2]
  even = tmp[1::2]
  print(max([sum(a) + sum(b) for a,b in zip(ngram(odd,K+1),ngram(even,K))]))

Submission status: Runtime Error

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

l = list(map(int,input()))
ll = []
p = l[0]
co = 1
ppp = 0
su = 0
lll = []
for i in range(n-1):
    if p == l[i+1]:
        co += 1
    else:
        if p == 0:
            ppp += 1
        ll.append([p,co])
        p = l[i+1]
        co = 1
if p == 0:
    ppp += 1
ll.append([p,co])
co = 0

if len(ll) == 1:
    print(n)
    exit(0)
if ppp <= k:
    print(n)
    exit(0)

if ll[0][0] == 0:
    if ll[-1][0] == 0:
        for i in range(2*k):
            co += ll[i][1]
        lll.append(co)
        co -= ll[0][1]
        for i in range(ppp-k):
            if ppp-k-1 == i and i == 0:
                co += ll[-1][1]
                lll.append(co)
            elif ppp-k-1 == i:
                co += ll[-1][1] - (ll[i*2+1][1] + ll[i*2+2][1])
                lll.append(co)
            elif i == 0:
                co += ll[i*2+1+2*k][1] + ll[i*2+2+2*k][1]
                lll.append(co)
            else:
                co += ll[i*2+1+2*k][1] + ll[i*2+2+2*k][1] - (ll[i*2+1][1] + ll[i*2+2][1])
                lll.append(co)
    else:
        for i in range(2*k):
            co += ll[i][1]
        lll.append(co)
        co -= ll[0][1]
        for i in range(ppp-k):
            if i == 0:
                co += ll[i*2+1+2*k][1] + ll[i*2+2+2*k][1]
                lll.append(co)
            else:
                co += ll[i*2+1+2*k][1] + ll[i*2+2+2*k][1] - (ll[i*2+1][1] + ll[i*2+2][1])
                lll.append(co)
else:
    if ll[-1][0] == 1:
        for i in range(2*k+1):
            co += ll[i][1]
        lll.append(co)
        co -= ll[0][1] + ll[1][1]
        for i in range(ppp-k):
            if i == 0:
                co += ll[i*2+1+2*k][1] + ll[i*2+2+2*k][1]
                lll.append(co)
            else:
                co += ll[i*2+1+2*k][1] + ll[i*2+2+2*k][1] - (ll[i*2][1] + ll[i*2+1][1])
                lll.append(co)
    else:
        for i in range(2*k+1):
            co += ll[i][1]
        lll.append(co)
        co -= ll[0][1] + ll[1][1]
        for i in range(ppp-k):
            if ppp-k-1 == i and i == 0:
                co += ll[-1][1]
                lll.append(co)
            elif ppp-k-1 == i:
                co += ll[-1][1] - (ll[i*2][1] + ll[i*2+1][1])
                lll.append(co)
            elif i == 0:
                co += ll[i*2+1+2*k][1] + ll[i*2+2+2*k][1]
                lll.append(co)
            else:
                co += ll[i*2+1+2*k][1] + ll[i*2+2+2*k][1] - (ll[i*2][1] + ll[i*2+1][1])
                lll.append(co)
            print(co)
print(max(lll))

Submission status: Runtime Error

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

arr = []
cnt = 0
comp = S[0]
for s in S:
    if s==comp:
        cnt += 1
    else:
        if comp=='0':
            arr.append(-cnt)
        else:
            arr.append(cnt)
        cnt = 1
        comp = s
if comp=='0':
    arr.append(-cnt)
else:
    arr.append(cnt)

def iter(A, cnt):
    if cnt>=K or len(A)<2:
        return max(A)
    elif len(A)==2:
        return abs(A[0]) + abs(A[1])
    m = -A[0]+A[1]
    args = [-1]
    for i in range(len(A)-2):
        s3 = A[i] - A[i+1] + A[i+2]
        if m<s3:
            m = s3
            args = [i]
        elif m==s3:
            args.append(i)
    s3 = A[-2]-A[-1]
    if m<s3:
        m = s3
        args = [len(A)-1]
    elif m==s3:
        args.append(len(A)-1)
    arg = args[0]
    if arg==-1:
        B = [-A[0]+A[1]] + A[2:]
    elif arg==len(A)-1:
        B = A[:-2] + [A[-2]-A[-1]]
    else:
        B = A[:arg] + [A[arg] - A[arg+1] + A[arg+2]] + A[arg+3:]
    return iter(B, cnt+1)


print(iter(arr, 0))

Submission status: Runtime Error

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

one_list = []
zero_list = []

cur_char = S[0]
char_num = 0
for i in range(N):
    if S[i] == cur_char:
        char_num += 1
    else:
        if cur_char == '0':
            zero_list.append(char_num)
        else:
            one_list.append(char_num)
        cur_char = S[i]
        char_num = 1
if cur_char == '0':
    zero_list.append(char_num)
else:
    one_list.append(char_num)

len_zero_list = len(zero_list)
len_one_list = len(one_list)
# print(len_one_list)
# print(len_zero_list)

cont_length_list = []

if K >= len(zero_list):
    ans = N
else:
    if S[0] == '1':
        tmp_length = sum(one_list[:K+1])+sum(zero_list[:K])
        cont_length_list.append(tmp_length)
        for i in range(len_zero_list-K):
            if K+1+i<len_one_list:
                tmp_length += one_list[K+1+i]+zero_list[K+i]
            else:
                tmp_length += zero_list[K+i]
            tmp_length -= one_list[i]+zero_list[i]
            cont_length_list.append(tmp_length)
        # print(cont_length_list)
    else:
        tmp_length = sum(one_list[:K])+sum(zero_list[:K])
        cont_length_list.append(tmp_length)
        if K < len_zero_list:
            tmp_length += zero_list[K]+one_list[K]
            tmp_length -= zero_list[0]
            cont_length_list.append(tmp_length)
            for i in range(1, len_zero_list-K):
                if K+i<len_one_list:
                    tmp_length += one_list[K+i]+zero_list[K+i]
                else:
                    tmp_length += zero_list[K+i]
                tmp_length -= one_list[i-1]+zero_list[i]
                cont_length_list.append(tmp_length)
        # print(cont_length_list)
    # print(cont_length_list)
    ans = max(cont_length_list)

print(ans)

Submission status: Runtime Error

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

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

def array_count(arr):
    arr_target = [[]]
    cnt = 1
    for i in range(1,len(arr)):
        if arr[i] == arr[i-1]:
            cnt += 1
        else:
            arr_target.append([arr[i-1],cnt])
            cnt = 1
    arr_target.append([arr[len(arr)-1],cnt])
    del arr_target[0]
    return arr_target

ss = array_count(s)

if ss[0][0] == 0:
    ss = [[1,0]] + ss

if ss[-1][0] == 1:
    ss.append([0,0])

ss_sum = []
temp = 0
for i in range(len(ss)):
    temp += ss[i][1]
    ss_sum.append(temp)

ans = -1
for i in range(len(ss_sum)):
    temp = 0
    if i % 2 == 0:
        if i + k * 2 < len(ss_sum):
            temp = ss_sum[i+k*2] - ss_sum[i]
    else:
        if i + k * 2 + 1 < len(ss_sum):
            temp = ss_sum[i+k*2+1] -  ss_sum[i]
    ans = max(temp,ans)

if ans == -1
    ans = ss_sum[-1]

print(ans)

Submission status: Runtime Error

Solution code:
import sys

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


tmp1 = s[0]
count = 1
data = []
zeros = 0

for i in range(1,len(s)):
    tmp2 = s[i]
    if tmp1 != tmp2:
        data.append((tmp1, count))
        if tmp1 == '0':
            zeros += 1
        tmp1 = tmp2
        count = 1
    else:
        count += 1

data.append((tmp1,count))
if tmp1 == '0':
    zeros += 1

ans = 0

if k >= zeros:
    ans = len(s)

elif s[0] == '0' and s[-1] == '0':
    anstmp = 0
    for i in range(2*k):
        anstmp += data[i][1]
    
    # print('初期',anstmp)
    ans = max(ans, anstmp)

    i = 0
    anstmp -= data[2*i][1]
    anstmp += data[2*k + 2*i][1]
    anstmp += data[2*k + 2*i + 1][1]
    # print('次',anstmp)
    ans = max(ans, anstmp)

    for i in range(1,zeros - k - 1):
        anstmp -= data[2*i + 1][1]
        anstmp -= data[2*i + 2][1]
        anstmp += data[2*k + 2*i][1]
        anstmp += data[2*k + 2*i + 1][1]

        # print('次',anstmp)
        ans = max(ans, anstmp)

    i = (zeros - k - 1)
    anstmp -= data[2*i + 1][1]
    anstmp -= data[2*i + 2][1]
    anstmp += data[2*k + 2*i][1]

    # print('次',anstmp)
    ans = max(ans, anstmp)


if s[0] == '1' and s[-1] == '0':
    anstmp = 0
    for i in range(2*k + 1):
        anstmp += data[i][1]
    
    # print('初期',anstmp)
    ans = max(ans, anstmp)

    for i in range(zeros - k - 1):
        anstmp -= data[2*i][1]
        anstmp -= data[2*i + 1][1]
        anstmp += data[2*k + 2*i + 1][1]
        anstmp += data[2*k + 2*i + 2][1]
    
        # print('次',anstmp)
        ans = max(ans, anstmp)

    i = (zeros - k - 1)
    anstmp -= data[2*i][1]
    anstmp -= data[2*i + 1][1]
    anstmp += data[2*k + 2*i][1]
 
    # print('次',anstmp)
    ans = max(ans, anstmp)


if s[0] == '1' and s[-1] == '1':
    anstmp = 0
    for i in range(2*k + 1):
        anstmp += data[i][1]

    ans = max(ans, anstmp)
    
    for i in range(zeros - k):
        anstmp -= data[2*i][1]
        anstmp -= data[2*i + 1][1]
        anstmp += data[2*k + 2*i + 1][1]
        anstmp += data[2*k + 2*i + 2][1]

        ans = max(ans, anstmp)

if s[0] == '0' and s[-1] == '1':
    anstmp = 0
    for i in range(2*k):
        anstmp += data[i][1]
    
    ans = max(ans, anstmp)

    i = 0
    anstmp -= data[2*i + 1][1]
    anstmp += data[2*k + 2*i][1]
    anstmp += data[2*k + 2*i + 1][1]

    ans = max(ans, anstmp)
    
    for i in range(1, zeros - k):
        anstmp -= data[2*i][1]
        anstmp -= data[2*i + 1][1]
        anstmp += data[2*k + 2*i][1]
        anstmp += data[2*k + 2*i + 1][1]

        ans = max(ans, anstmp)

print(ans)

Submission status: Runtime Error

Solution code:
N,K=map(int,input().split())
S=input()
N=int(len(S))
s=list(S)
c=[]
sum=0
cnt1=0
cnt0=0
if s[0]=="0":
  d=K*2
else:
  d=K*2+1
for i in range(N):
  if s[i]=="1":
    if cnt0!=0:
      c.append(cnt0)
    cnt1=cnt1+1
    cnt0=0
  else:
    if cnt1!=0:
      c.append(cnt1)
    cnt0=cnt0+1
    cnt1=0
if cnt0!=0:
  c.append(cnt0)
if cnt1!=0:
  c.append(cnt1)
C=int(len(c))
for i in range(d):
  sum=sum+c[i]
big=sum
for i in range(C-d):
  sum=sum+c[i+d]-c[i]
  if big<=sum:
    big=sum
print(big)

Submission status: Runtime Error

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

is1 = False
firstIs1 = False

if S[0:1] == '1' :
    is1 = True
    firstIs1 = True

l0 = []
l1 = []
cnt = 1
isDeli = False

for i in range(0, len(S)) :
    if i == len(S) - 1 or S[i] != S[i+1] :
        if is1 == False :
            l0.append(cnt)
        else :
            l1.append(cnt)
        if i != len(S) - 1 :
            if S[i + 1] == '1' :
                is1 = True
            else :
                is1 = False
        cnt = 1
    else :
        cnt += 1

ans = 0

for i in range(min(len(l0), len(l1)) - min(K, len(l0)) + 1) :
    sum = 0
    for j in range(i, i + min(K, len(l0))) :
        sum += l0[j] + l1[j]
    if i != 0 and firstIs1 == False:
        sum += l1[i - 1]
    if i != min(len(l0), len(l1)) - K - 1 and firstIs1 == True:
        sum += l1[i + 1]
    ans = max(ans, sum)

if min(len(l0), len(l1)) == 0 :
    if K > 0 or S == '1':
        ans = 1
    else :
        ans = 0


print(ans)




#print(l0)
#print(l1)

"""
for i in range(1, len(S)) :
    if isDeli = True :

        if S[i] == '0' :
            is1 = False
        else :
            is1 = True

        cnt = 1
    if S[i-1] == S[i] :
        cnt += 1
    else :
        if is1 == True :
            l1 =
"""

Submission status: Runtime Error

Solution code:
import numpy as np
s=input().split()
k=int(s[1])
ini=list(input())
n=len(ini)

a1 = np.array([])
b = 0
c = 0
for i in range(n):
    if ini[i] == "0":
        if b > 0:
            a1=np.append(a1,b)
            b = 0
        c = c+1
    if ini[i] == "1":
        if c > 0:
            a1=np.append(a1, c)
            c = 0
        b = b+1
    if i == n-1:
        if b > 0:
            a1=np.append(a1,b)
            b = 0
        if c > 0:
            a1=np.append(a1, c)
            c = 0
            
elem=2*k+1
m=np.size(a1)
cands=np.array([])
if ini[0] == "1":
    for i in range((m-elem+1)//2+2):
        cand = np.sum(a1[2*i:2*i+elem])
        cands=np.append(cands,cand)
if ini[0] == "0":
    for i in range((m-elem+1)//2+2):
        cand = np.sum(a1[2*i+1:2*i+1+elem])
        cands=np.append(cands,cand)
        
print(int(np.max(cands)))

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
S = input()
S = "0" + S + "0"
lis = []
for i in range(1, N + 2):
  if S[i - 1] == "0" and S[i] == "1":
    lis.append([i])
  if S[i - 1] == "1" and S[i] == "0":
    lis[-1].append(i - 1)
leng = len(lis)
ans = 0
if S[1] == "1" and S[N] == "1":
  if K >= leng - 1:
    ans = N
  else:
    for i in range(leng):
      if i + K < leng:
        newan = lis[i + K][1] - lis[i][0] + 1
        ans = max(ans, newan)
  print(ans)
elif S[1] == "1" and S[N] == "0":
  if K >= leng:
    ans = N
  else:
    for i in range(leng):
      if i + K < leng:
        newan = lis[i + K][1] - lis[i][0] + 1
        ans = max(ans, newan)
  print(ans)
elif S[1] == "0" and S[N] == "1":
  if K >= leng:
    ans = N
  else:
    for i in range(leng):
      if i + K < leng:
        newan = lis[i + K][1] - lis[i][0] + 1
        ans = max(ans, newan)
  ans = max(ans, lis[K - 1][1])
  print(ans)
elif S[1] == "0" and S[N] == "0":
  if K - 1 >= leng:
    ans = N
  else:
    for i in range(leng):
      if i + K < leng:
        newan = lis[i + K][1] - lis[i][0] + 1
        ans = max(ans, newan)
  ans = max(ans, lis[K - 1][1])
  ans = max(ans, N - lis[leng - K][0] + 1)
  print(ans)

Submission status: Runtime Error

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

def maxdpt(depth, slist):
    # すべて1の場合は終了
    if slist.count('1') == len(slist): 
        return len(slist)
    
    # 深さ最大の場合は終了
    if depth+1>k:
        return max(list(map(len, slist.split('0'))))

    max_len=0
    str_len=0
    for i,c in enumerate(slist):   # 0の部分文字列を取得
        if c=='1':
            continue
        # 0の部分文字列を仮に変えてみる
        for j in range(i+1,len(slist)):
            #次の1まで
            if slist[j]=='1':
                break
        s_tmp=list(slist)
        for index in range(i,j):
            s_tmp[index]='1'
        s_tmp = "".join(s_tmp)
        max_len=max(max_len,maxdpt(depth+1,s_tmp))
        i=j
    return max_len

print(maxdpt(0,s_base))

Submission status: Runtime Error

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

    candi = 0
    ansli = []
    p = 0
    for i in range(len(nums)):
        if nums[i][0] == "0":
            p += 1
    k = min(k, p)

    i1 = 0
    j1 = 0
    while i1 <= k and j1 < len(nums):
        if i1 == k and nums[j1][0] == "0":
            pass
        else:
            candi += nums[j1][1]
        if nums[j1][0] == "0":
            i1 += 1
        j1 += 1
    ansli.append(candi)

    a = 0
    b = j1 - 1
    while b < len(nums):
        for i in range(2):
            if nums[a + i][0] == "1":
                candi -= nums[a + i][1]
            elif nums[a + i][0] == "0":
                candi -= nums[a + i][1]
                a += i + 1
                break
        for i in range(2):
            if b + i >= len(nums):
                ansli.append(candi)
                b += 1
                break
            else:
                if nums[b + i][0] == "1":
                    candi += nums[b + i][1]
                    b += i + 1
                    ansli.append(candi)
                    break
                elif nums[b + i][0] == "0":
                    candi += nums[b + i][1]

    print(max(ansli))

Submission status: Runtime Error

Solution code:
import numpy as np

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

Ils=[]
Cls=[]
isRe = False

count = 0
id = 0;
for i in range(N):
    if s[i] == "0":
        if isRe:
            count+= 1
        else:
            count = 1
            id = i
        isRe = True
    else:
        if isRe:
            Ils.append(id)
            Cls.append(count)
        else:
            count = 0;
        isRe = False

if isRe:
    Ils.append(i)
    Cls.append(count)

index = np.argmax(Cls)

for k in range(K):
    index = np.argmax(Cls)
    if len(Cls) <= 1:
        break

    if index == 0:
        if Ils[index] != 0:
            if Ils[index] < Ils[index+1] - Ils[index] -1:
                Cls[index] = Cls[index+1] + Ils[index+1] - Ils[index] -1
                Cls.pop(index+1)
                Ils.pop(index+1)
            else:
                Cls[index] = Ils[index]
                Ils[index] = 0
        else:
            Cls[index] = Cls[index+1] + Ils[index+1] - Ils[index] -1
            Cls.pop(index+1)
            Ils.pop(index+1)
    elif index == len(Cls)-1:
        #if Ils[index] != N-1:
            if Ils[index] - Ils[index-1] -1 < N - Ils[index] - Cls[index]:
                Cls[index] = N - Ils[index]
            else:
                Cls[index-1] = Cls[index-1] + Ils[index] - Ils[index-1] -1
                Cls.pop(index)
                Ils.pop(index)
    else:
        if Ils[index] - Ils[index-1] -1 < Ils[index+1] - Ils[index] -1:
            Cls[index] = Cls[index+1] + Ils[index+1] - Ils[index] -1
            Cls.pop(index+1)
            Ils.pop(index+1)
        else:
            Cls[index-1] = Cls[index-1] + Ils[index] - Ils[index-1] -1
            Cls.pop(index)
            Ils.pop(index)

if len(Cls) <= 0:
    print(N)
else:
    print(max(Cls))

Submission status: Runtime Error

Solution code:
import sys
stdin = sys.stdin 
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline()
N,K=na();S=ns()
l=[]
c1=0
c0=0
x0=0
first=True
if(S[0]=="1"):
    c1=1
else:
    c0=1
for i in range(1,N):
    if(S[i]=="1"):
        if(S[i]!=S[i-1]):
            l.append(c0)
            c0=0
        c1+=1
    else:
        if(S[i]!=S[i-1]):
            x0+=1
            l.append(c1)
            c1=0
        c0+=1
if(c1):
    l.append(c1)
else:
    x0+=1
    l.append(c0)
#print(l)
#print(x0)
if(x0<=K):
    print(N)
    exit()
else:
    res=0
    if(int(S[0])>0):
        if(int(S[-1])>0):
            right = 0
            left = 2*K
            while(True):
                if(left>=len(l)-1):
                    print(res)
                    exit()
                res=max(sum(l[right:left+1]),res)
                #print(res,right,left)
                right+=2
                left+=2
        else:
            right = 0
            left = 2*K
            #print("llll")
            res=max(sum(l[len(l)-(1+(K-1)*2)-1:]),res)
            #print(len(l)-(1+(K-1)*2))
            while(True):
                if(left>=len(l)-3):
                    print(res)
                    exit()
                res=max(sum(l[right:left+1]),res)
                #print(res,right,left)
                right+=2
                left+=2
    else:
        if(int(S[-1])>0):
            res=max(res,sum(l[0:2*K]))
            right = 1
            left = 2*K+1
            while(True):
                if(left>=len(l)-1):
                    print(res)
                    exit()
                res=max(sum(l[right:left+1]),res)
                #print(res,right,left)
                right+=2
                left+=2
        else:
            right = 0
            left = 2*K
            #最後
            res=max(sum(l[len(l)-(1+(K-1)*2)-1:]),res)
            #最初
            res=max(res,sum(l[0:2*K]))
            right = 1
            left = 2*K+1
            while(True):
                if(left>=len(l)-3):
                    print(res)
                    exit()
                res=max(sum(l[right:left+1]),res)
                #print(res,right,left)
                right+=2
                left+=2

Submission status: Runtime Error

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

L = [1]

for i in range(1, N):
	if S[i] == S[i - 1]:
		L[-1] += 1
	else:
		L.append(1)

n = len(L)
if S[0] == "1":
	if n // 2 <= K:
		print(N)
	else:
		if S[-1] == "1":
			x = L[0]
			for i in range(K):
				x += L[2 * i + 1] + L[2 * i + 2]
			val = x
			for i in range(int(n // 2) - K):
				val += L[2 * (i + K) + 1] + L[2 * (i + K) + 2] - L[2 * i] - L[2 * i + 1]
				if val > x:
					x = val
			print(x)
		else:
			x = L[0]
			for i in range(K):
				x += L[2 * i + 1] + L[2 * i + 2]
			val = x
			for i in range(int(n // 2) - K - 1):
				val += L[2 * (i + K) + 1] + L[2 * (i + K) + 2] - L[2 * i] - L[2 * i + 1]
				if val > x:
					x = val
			val += L[-1] - L[-2 * K - 1] - L[-2 * K - 2]
			if val > x:
				x = val
			print(x)			

else:
	if (n + 1) // 2 <= K:
		print(N)
	else:
		if S[-1] == "1":
			LL = [L[-1 - i] for i in range(n)]
			x = LL[0]
			for i in range(K):
				x += LL[2 * i + 1] + LL[2 * i + 2]
			val = x
			for i in range(int(n // 2) - K - 1):
				val += LL[2 * (i + K) + 1] + LL[2 * (i + K) + 2] - LL[2 * i] - LL[2 * i + 1]
				if val > x:
					x = val
			print(x)
		else:
			LL = L[1:]
			x = LL[0]
			for i in range(K):
				x += LL[2 * i + 1] + LL[2 * i + 2]
			val = x
			for i in range(int((n - 1) // 2) - K - 1):
				val += LL[2 * (i + K) + 1] + LL[2 * (i + K) + 2] - LL[2 * i] - LL[2 * i + 1]
				if val > x:
					x = val
			val += L[-1] - LL[-2 * K - 1] - LL[-2 * K - 2]
			if val > x:
				x = val

			LL = [L[-2 - i] for i in range(n - 1)]
			y = LL[0]
			for i in range(K):
				y += LL[2 * i + 1] + LL[2 * i + 2]
			val = y
			for i in range(int((n - 1) // 2) - K - 1):
				val += LL[2 * (i + K) + 1] + LL[2 * (i + K) + 2] - LL[2 * i] - LL[2 * i + 1]
				if val > y:
					y = val
			val += L[0] - LL[-2 * K - 1] - LL[-2 * K - 2]
			if val > y:
				y = val
			print(max(x, y))

Submission status: Runtime Error

Solution code:
N, K = [int(x) for x in input().split()]
S = [int(x) for x in list(str(input()))]

S_list = []

s_b = S[0]
cnt = 1
for i in range(1,N):
  s_n = S[i]
  if s_n == s_b:
    cnt += 1
  else:
    S_list.append(cnt)
    cnt = 1
  s_b = s_n

S_list.append(cnt)

if len(S_list) - 2 * K <= 0:
  print(len(S))

cand_list = []
if S[0] == 1:
  for idx in range(0, len(S_list)-K, 2):
    print(idx)
    cand_list.append(sum(S_list[idx:idx+2*K+1]))

if S[0] == 0:
  for idx in range(0, len(S_list)-K, 2):
    cand_list.append(sum(S_list[idx:idx+2*K]))
    
print(max(cand_list))

Submission status: Runtime Error

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

zero_ind = [m.start() for m in re.finditer('0+', S)]
one_ind = [m.start() for m in re.finditer('1+', S)]

lz = len(zero_ind)

if lz <= K:
    print(N)
else:
    if zero_ind[0] != 0:
        zero_ind = [0] + zero_ind
    else:
        one_ind = [0] + one_ind

    count = 0

    for k in range(lz -1- K):
        count = max(count, zero_ind[k+K] - one_ind[k])
        
    if S[-1] == "0":
        count = max(count, N - one_ind[-K])
    else:
        count = max(count, N - one_ind[-K-1])
    
    print(count)

Submission status: Runtime Error

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

tmpu = []

count = 0
onecount = 0
for i in S:
    if i == '0':
        count += 1
    if i == '1':
        tmpu.append(count)
        count=0
        onecount += 1

tmpu=sorted(tmpu, reverse=True)
for n in range(K):
    onecount += tmpu[n]

print(onecount)

Submission status: Runtime Error

Solution code:
N, K = list(map(int, input().split()))
S = input()
ct = 1
li0 = []
li1 = []
cap = S[0]
su = 0
if N != 1:
    for i in range(N - 1):
        if S[i + 1] == S[i]:
            ct += 1
        else:
            if S[i] == '0':
                li0.append(ct)
            else:
                li1.append(ct)
            ct = 1
    if S[i+1] == '0':
        li0.append(ct)
    else:
        li1.append(ct)
    for i in range(len(li0) - K + 1):
        num = sum(li0[i:i + K])
        if su < num:
            su = num
            idx = i
    print(max((su + sum(li1[idx:idx + K+1])), max(li1)))
else:
    print(1)

Submission status: Runtime Error

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

data = []
cnt = 0
curr = '1'
for i in range(len(s)):
    if s[i] != curr:
        data.append(cnt)
        curr = s[i]
        cnt = 1
    else:
        cnt += 1
data.append(cnt)
data.append(0)

current_sum = sum(data[0:0 + 2*k+1])
ret = current_sum
for start in range(2, len(data), 2):
    if (start + 2*k-1) < len(data): 
        diff = -data[start-2]-data[start-1]+data[start + 2*k]+data[start + 2*k-1]
        current_sum += diff

    #     print(start, val)
        if current_sum > ret:
            ret = current_sum
    else:
        break
print(ret)

Submission status: Runtime Error

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

s = list(str(input()))

first_index=0
last_index=0

res = 0
for f in range (first_index, n+1):
  for l in range (last_index, n+1):
    if f < l:
      continue
    check = 0
    for i in range(f, l+1):
      if s[i] == 0:
        check += 1
    if check <= k:
      if l-f > res:
        res = l-f

print(res)

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
a = input()
aa = a + "9"
b = []
c = 1
for i in range(len(a)):
    if aa[i] == aa[i + 1]:
        c += 1
    else:
        b.append(c)
        c = 1
d = 0
e = 0
ans = 0
if a[0] == "1" and a[-1] == "1":
    for i in range(len(b) - 2 * K):
        for j in range(2 * K + 1):
            d += b[i + j]
        ans = max(ans, d)
        d = 0
    print(ans)
elif a[0] == "1" and a[-1] == "0":
    for i in range(len(b) - 2 * K - 1):
        for j in range(2 * K + 1):
            d += b[i + j]
        ans = max(ans, d)
        d = 0
    for i in range(len(b) - 2 * K + 1, len(b)):
        d += b[i]
    print(max(ans, d))
elif a[0] == "0" and a[-1] == "1":
    for i in range(1, len(b) - 2 * K):
        for j in range(2 * K + 1):
            d += b[i + j]
        ans = max(ans, d)
        d = 0
    for i in range(2 * K):
        d += b[i]
    print(max(ans, d))
elif a[0] == "0" and a[-1] == "0":
    for i in range(1, len(b) - 2 * K - 1):
        for j in range(2 * K + 1):
            d += b[i + j]
        ans = max(ans, d)
        d = 0
    for i in range(2 * K):
        d += b[i]
    for i in range(len(b) - 2 * K + 1, len(b)):
        e += b[i]
    print(max(ans, d, e))

Submission status: Runtime Error

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

zero_ind = [m.start() for m in re.finditer('0+', S)]
one_ind = [m.start() for m in re.finditer('1+', S)]

lz = len(zero_ind)

if lz <= K:
    print(N)
else:
    if zero_ind[0] != 0:
        zero_ind = [0] + zero_ind
    else:
        one_ind = [0] + one_ind

    count = 0

    for k in range(lz - K):
        count = max(count, zero_ind[k+K] - one_ind[k])
        
    if S[-1] == "0":
        count = max(count, N - one_ind[-K])
    else:
        count = max(count, N - one_ind[-K-1])
    
    print(count)

Submission status: Runtime Error

Solution code:
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])+sum(stand[i:i+K])
			if tmp > max_:
				max_ = tmp
		print(max_)

Submission status: Runtime Error

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

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

Submission status: Runtime Error

Solution code:
import re

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

def popcnt(n):
  return bin(n).count("1")

for _ in range(K):
  best_score = max([len(v) for v in re.findall(r'1+', S)])
  best_xor = 0
  for xor in range(2 ** len(S)):
    if len(re.split('1+', bin(xor))) != 2:
      continue
    
    res = intS ^ xor
    # score = popcnt(res)
    try:
      score = max([len(v) for v in re.findall(r'1+', bin(res))])
    except:
      score = 0
    
    if score > best_score:
      best_score = score
      best_xor = xor
      # print(xor, bin(xor), res, bin(res), _popcnt)
        
  intS = intS ^ best_xor
    
print(max([len(v) for v in re.findall(r'1+', bin(intS))]))

Submission status: Runtime Error

Solution code:
l,k=map(int,input().split())
s=input()
import re
c=lambda i,n:[len(x.group()) for x in re.finditer(str(n)+'+',i)]
l1=c(s,1)
l0=c(s,0)
f=max(l1)
for i in range(len(l0)-k+1):
  if s[0]=='1':
    p=l1[i:i+k+1]
  else:
    p=l1[i:i+k]
  m=sum(l0[i:i+k])+sum(p)
  if f<m:f=m
print(f)