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:
import sys

N, K = [int(x) for x in input().split()] # N:Sの長さ K:操作を行える回数の最大値
S = [int(x) for x in list(input())] # S:0と1から成る列
index = [0] # index:Sにおいて0と1が切り替わる部分の添字を集めたリスト
length = [0] # length:1が連続して並んでいる長さの最大値の候補を集めたリスト
zeros = 0 # zeros:Sにおいて0が連続して並んでいる箇所がいくつあるか

for i in range(0, len(S) - 1): # indexの要素を取得
    if S[i] != S[i + 1]: # 0と1の変わり目を探索
        index.append(i + 1) 

if S[0] == 0: # Sが0から始まる場合
    
    for i in range(len(index)): # zerosの値を取得
        if len(index) % 2 == 0: # indexの長さが偶数である場合
            zeros = int(len(index) / 2)
        else: # indexの長さが奇数である場合
            zeros = int((len(index) + 1) / 2) 
            
    for i in range(len(index)): # max(length)の初期値を取得
        if S[len(S) - 1] == 0: # Sが0で終わっている場合
            if i % 2 == 1:
                length.append(index[i + 1] - index[i])
        else: # Sが1で終わっている場合
            if (i % 2 ==1 and (i + 1) < len(index)):
                length.append(index[i + 1] - index[i])
            elif (i % 2 ==1 and (i + 1) == len(index)): # 最後の1の連続を処理
                length.append((len(S) - 1) - index[i] + 1)

    for i in range(len(index)): # Sのindex[i]番目を左端としたときの1の連続回数の最大値をそれぞれ求める
        if K >= zeros: # Kがzeros以上なら0の連続を全て1に置き換えて終了
            print(len(S))
            sys.exit()
        
        elif i + (2 * K) <= len(index): # 添字が飛び出ないように注意（飛び出るようなやつはそもそも最大値の候補にならない）
            if i % 2 == 0: # 左端が0の場合
                length.append(index[(i + (2 * K) - 1) + 1] - index[i])
            else: # 左端が1の場合
                if i + (2 * K) < (len(index) - 1):
                    length.append(index[i + (2 * K) + 1] - index[i])
                elif i + (2 * K) == (len(index) - 1):
                    length.append((len(S) - 1) - index[i] + 1)
                elif i + (2 * K) == len(index):
                    length.append(len(S) - index[i])
                
                
    print(max(length))
    sys.exit()

else: # Sが1から始まる場合
    
    for i in range(len(index)): # zerosの値を取得
        if len(index) % 2 == 0: # indexの長さが偶数である場合
            zeros = int(len(index) / 2)
        else: # indexの長さが奇数である場合
            zeros = int((len(index) - 1) / 2) 
            
    for i in range(len(index)): # max(length)の初期値を取得
        if S[len(S) - 1] == 0: # Sが0で終わっている場合
            if i % 2 == 0:
                length.append(index[i + 1] - index[i])
        else: # Sが1で終わっている場合
            if (i % 2 ==0 and (i + 1) < len(index)):
                length.append(index[i + 1] - index[i])
            elif (i % 2 ==0 and (i + 1) == len(index)): # 最後の1の連続を処理
                length.append((len(S) - 1) - index[i] + 1)

    for i in range(len(index)): # Sのindex[i]番目を左端としたときの1の連続回数の最大値をそれぞれ求める
        if K >= zeros: # Kがzeros以上なら0の連続を全て1に置き換えて終了
            print(len(S))
            sys.exit()
        
        elif i + (2 * K) <= (len(index) - 1): # 添字が飛び出ないように注意（飛び出るようなやつはそもそも最大値の候補にならない）
            if i % 2 == 0: # 左端が1の場合
                if i + (2 * K) < (len(index) - 1):
                    length.append(index[i + (2 * K) + 1] - index[i])
                elif i + (2 * K) == (len(index) - 1):
                    length.append((len(S) - 1) - index[i] + 1)
                
            else: # 左端が0の場合
                length.append(index[(i + (2 * K) - 1) + 1] - index[i])

                
    print(max(length))
    sys.exit()

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 len(s0) <= K:
        return N

    if S[0] == '0':
        s1.insert(0, 0)
    if S[-1] == '0':
        s1.append(0)

    q = deque()
    q.append(s1.pop())
    q.append(s1.pop())
    q.append(s1.pop())
    ans = 0
    tmp = sum(q)
    while s1 or s0:
        if len(q) >= 2*K + 1:
            tmp -= q.popleft()
            tmp -= q.popleft()
        if s0:
            q.append(s0.pop())
            tmp += q[-1]
        if s1:
            q.append(s1.pop())
            tmp += q[-1]
        ans = max(ans, tmp)

    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,input().split())
S = input()
lenth = 0
AllLen =[]
preS0,preS1 = S.split('1'),S.split('0')
S0 = [i for i in preS0 if i != '']
S1 = [i for i in preS1 if i != '']
num0 = k
if len(S0) <= k:
	print(len(S))
	exit() 
if S[0] == '0':
	J = k
else:J = k + 1
FirstLen = sum([len(S0[i]) for i in range(k)]) + sum([len(S1[j]) for j in range(J)])
AllLen.append(FirstLen)
CheckLen = FirstLen
if S[0] == '1' and S[-1] == '1':
    for i in range(len(S0) - k):
	    CheckLen = CheckLen - len(S0[i]) - len(S1[i]) + len(S0[i + k]) + len(S1[i + k + 1])
	    AllLen.append(CheckLen)
if S[0] == '0' and S[-1] == '1':
	CheckLen = CheckLen - len(S0[0]) + len(S0[k]) + len(S1[k])
	AllLen.append(CheckLen)
	for i in range(len(S0) - k - 1):
		CheckLen = CheckLen - len(S0[i + 1]) - len(S1[i + 1]) + len(S0[i + k + 1]) + len(S1[i + k + 2])
		AllLen.append(CheckLen)
if S[0] == '1' and S[-1] == '0':
	for i in range(len(S0) - k - 1):
		CheckLen = CheckLen - len(S0[i]) - len(S1[i]) + len(S0[i + k]) + len(S1[i + k + 1])
		AllLen.append(CheckLen)
	CheckLen = CheckLen - len(S0[len(S0) - k - 1]) - len(S1[len(S0) - k - 1]) + len(S0[len(S0) - 1])
	AllLen.append(CheckLen)
if S[0] == '0' and S[-1] == '0':
	if len(S1) == 1:
		print(len(max(S0)) + len(S1[0]))
		exit()
	CheckLen = CheckLen - len(S0[0]) + len(S0[k]) + len(S1[k])
	AllLen.append(CheckLen)
	for i in range(len(S0) - k - 2):
		CheckLen = CheckLen - len(S0[i + 1]) - len(S1[i + 1]) + len(S0[i + k + 1]) + len(S1[i + k + 2])
		AllLen.append(CheckLen)
	CheckLen = CheckLen - len(S0[len(S0) - k - 1]) - len(S1[len(S0) - k - 1]) + len(S0[len(S0) - 1])
	AllLen.append(CheckLen)
print(max(AllLen))

Submission status: Runtime Error

Solution code:
#とちゅうまでやった（1と0の個数を、1と0のかたまりごとにリストの要素にしたかった）
#その後1010…01で、s2[i]+s2[i+1]+…+s2[i+k]の和で一番大きいものを出力すればいいはず

n,k = [int(x) for x in input().split()]
s1 = input()
s2 = list()
for i in range(0,len(s1)):
    if s1[i]=='1':
        for j in range(i+1, len(s1)):
            if s1[j]!='1':
                s2.append(j - i)
                break
    elif s1[i]=='0':
        for j in range(i+1, len(s1)):
            if s1[j]!='0':
                s2.append(j - i)
                break
    i+=s2[len(s2)-1]+1
    print(i)
print(s2)

Submission status: Runtime Error

Solution code:
N,k = map(int,input().split())
S = input()
lenth = 0
AllLen =[]
preS0,preS1 = S.split('1'),S.split('0')
S0 = [i for i in preS0 if i != '']
S1 = [i for i in preS1 if i != '']
num0 = k
if len(S0) <= k:
	print(len(S))
if S[0] == '0':
	J = k
else:J = k + 1
FirstLen = sum([len(S0[i]) for i in range(k)]) + sum([len(S1[j]) for j in range(J)])
AllLen.append(FirstLen)
CheckLen = FirstLen
if S[0] == '1' and S[-1] == '1':
    for i in range(len(S0) - k):
	    CheckLen = CheckLen - len(S0[i]) - len(S1[i]) + len(S0[i + k]) + len(S1[i + k + 1])
	    AllLen.append(CheckLen)
if S[0] == '0' and S[-1] == '1':
	CheckLen = CheckLen - len(S0[0]) + len(S0[k]) + len(S1[k])
	AllLen.append(CheckLen)
	for i in range(len(S0) - k - 1):
		CheckLen = CheckLen - len(S0[i]) - len(S1[i]) + len(S0[i + k]) + len(S1[i + k + 1])
		AllLen.append(CheckLen)
if S[0] == '1' and S[-1] == '0':
	for i in range(len(S0) - k - 1):
		CheckLen = CheckLen - len(S0[i]) - len(S1[i]) + len(S0[i + k]) + len(S1[i + k + 1])
		AllLen.append(CheckLen)
	CheckLen = CheckLen - len(S0[len(S0) - k - 1]) - len(S1[len(S0) - k - 1]) + len(S0[len(S0) - 1])
	AllLen.append(CheckLen)
if S[0] == '0' and S[-1] == '0':
	CheckLen = CheckLen - len(S0[0]) + len(S0[k]) + len(S1[k])
	AllLen.append(CheckLen)
	for i in range(len(S0) - k - 2):
		CheckLen = CheckLen - len(S0[i]) - len(S1[i]) + len(S0[i + k]) + len(S1[i + k + 1])
		AllLen.append(CheckLen)
	CheckLen = CheckLen - len(S0[len(S0) - k - 1]) - len(S1[len(S0) - k - 1]) + len(S0[len(S0) - 1])
	AllLen.append(CheckLen)
print(max(AllLen))

Submission status: Runtime Error

Solution code:

n, k = map(int, input().split())
s = input().rstrip()
m = 0
cur_s = cur_e = 0

while True:
  while s[cur_e] == '1':
    cur_e++
  
  if k <= 0 or cur_e >= n:
    wm = cur_e - cur_s
    if wm < m:
      m = wm
      
    if cur_e >= n:
      break
 
    while s[cur_s] == '1':
      cur_s++
    while s[cur_s] == '0':
      cur_s++
    k++

  if k > 0:
    while s[cur_e] = '0':
      cur_e++
    k--
    
print(m)

Submission status: Runtime Error

Solution code:
N, k = map(int, input().split())
S = input()
ans = []
state = S[0]
for i in range(1, len(S)):
  if S[i] != state:
    ans.append(i)
    state = S[i]
ans.append(len(S))
temp = ans[2*k-1]
for i in range(1, len(ans)-2*k-1):
  dif = ans[i+2*k+1] - ans[i]
  if dif > temp:
    temp = dif
print(temp)

Submission status: Runtime Error

Solution code:
N, k = map(int, input().split())
S = input()
ans = []
state = S[0]
for i in range(1, len(S)):
  if S[i] != state:
    ans.append(i)
    state = S[i]
ans.append(len(S))
temp = ans[2*k-1]
for i in range(1, len(ans)-2*k):
  dif = ans[i+2*k] - ans[i]
  if dif > temp:
    temp = dif
print(temp)

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
S = input().replace('\n', '')


def solve(S, K):
    record = []
    temp = []
    for i in range(len(S) - 1):
        if S[i:i+2] == '01':
            temp.append(i + 1)
        elif S[i:i+2] == '10':
            if not temp: temp = [0, i]
            else: temp.append(i)
            record.append(temp)
            temp = []
    if len(temp) > 0:
        temp.append(len(S) - 1)
        record.append(temp)
    if S[0] == '0':
        record = [[0, record[0][0] - 1]] + record
    if S[-1] == '0':
        record = record + [[record[-1][-1] + 1, len(S) - 1]]
    if len(record) <= K + 1: return(len(S))
    else:
        cnt = 0
        for i in range(len(record) - K):
            cnt = max(cnt, record[i + K][1] - record[i][0] + 1)
        return(cnt)
print(solve(S, K))

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
S = input().replace('\n', '')


def solve(S, K):
    record = []
    temp = []
    for i in range(len(S) - 1):
        if S[i:i+2] == '01':
            temp.append(i + 1)
        elif S[i:i+2] == '10':
            if not temp: temp = [0, i]
            else: temp.append(i)
            record.append(temp)
            temp = []
    if len(temp) > 0:
        temp.append(len(S) - 1)
        record.append(temp)
    if S[0] == '0':
        record = [[0, record[0][0] - 1]] + record
    if S[-1] == '0':
        record = record + [[record[-1][-1] + 1, len(S) - 1]]
    print(record)
    if len(record) <= K + 1: return(len(S))
    else:
        cnt = 0
        for i in range(len(record) - K):
            cnt = max(cnt, record[i + K][1] - record[i][0] + 1)
        return(cnt)
print(solve(S, K))

Submission status: Runtime Error

Solution code:
import sys
read = sys.stdin.readline


def read_ints():
    return list(map(int, read().split()))


def read_a_int():
    return int(read())


def read_matrix(H):
    '''
    H is number of rows
    '''
    return [list(map(int, read().split())) for _ in range(H)]


def read_map(H):
    '''
    H is number of rows
    文字列で与えられた盤面を読み取る用
    '''
    return [read() for _ in range(H)]


def read_col(H, n_cols):
    '''
    H is number of rows
    n_cols is number of cols

    A列、B列が与えられるようなとき
    '''
    ret = [[] for _ in range(n_cols)]
    for _ in range(H):
        tmp = list(map(int, read().split()))
        for col in range(n_cols):
            ret[col].append(tmp[col])

    return ret


N, K = read_ints()
S = '1' + read()
sum_0 = [0]

for s_new, s_next in zip(S[:-2], S[1:-1]):
    # print(s_new, s_next)
    if s_new == '1' and s_next == '0':
        sum_0.append(sum_0[-1] + 1)
    else:
        sum_0.append(sum_0[-1])

S = S[1:-1]
# print(sum_0)

left = 0  # 左端初期値
ans = 1  # 解初期値
right = 1

# right-leftが答え
# sum_0[right]-sum0[left]がKを超えたらleftを進める
# print(sum_0)
# しゃくとり法で溶けそうでは？
for left in range(N-1):
    while (True):
        right += 1
        if sum_0[right + 1] - sum_0[left] > K:
            right -= 1
            break
        if right == N - 1:
            break
        # print(left, right, sum_0[right + 1] - sum_0[left])

    # print(left, right)
    ans = max(ans, right - left+1)  # 大きい方に更新
    # print(ans)

print(ans)

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
s=input()
lst=[1]
for i in range(1,n):
    if s[i]!=s[i-1]:
        lst.append(1)
    else:
        lst[-1]=lst[-1]+1
p=2*k
L=[0]
for i in lst:
  L.append(L[-1]+i)
l=len(L)
ans=0
if s=='1'*n:
    ans=n
else:
    if s[0]=='1':
        if l%2==0:
            q=(l-p)//2
            for i in range(q):
                ans=max(ans,L[2*i+p+1]-L[2*i])
        else:
            L.append(0)
            q=(l-p+1)//2
            for i in range(q):
                ans=max(ans,L[2*i+p+1]-L[2*i])
    else:
        L.insert(0,0)
        if l%2==1:
            L.insert(0,0)
            q=(l-p+1)//2
            for i in range(q):
                ans=max(ans,L[2*i+p+1]-L[2*i])
        else:
          L.append(0)
            q=(l-p+2)//2
            for i in range(q):
                ans=max(ans,L[2*i+p+1]-L[2*i])
print(ans)

Submission status: Runtime Error

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

now = 1
cnt = 0
li = []

# 1-0-1-0-1-0-..... を準備
for i in range(n):
    if s[i] == str(now):
        cnt += 1
    else:
        li.append(cnt)
        now = 1 - now
        cnt = 1
        
if len(li)%2 == 0:
    li.append(cnt)

ans = -1
for i in range(n):
    left = 2*i
    right = 2*i + 2*k + 1
    length = sum(li[left:right])
    ans = max(ans, length)
    
    if end >= len(li):
        break

print(ans)

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
s=input()
lst=[1]
for i in range(1,n):
    if s[i]!=s[i-1]:
        lst.append(1)
    else:
        lst[-1]=lst[-1]+1
p=2*k
L=[0]
for i in lst:
  L.append(L[-1]+i)
l=len(L)
ans=0
if s=='1'*n:
    ans=n
else:
    if s[0]=='1':
        if l%2==0:
            q=(l-p)//2
            for i in range(q):
                ans=max(ans,L[2*i+p+1]-L[2*i])
        else:
            L.append(0)
            q=(l-p+1)//2+1
            for i in range(q):
                ans=max(ans,L[2*i+p+1]-L[2*i])
    else:
        L.insert(0,0)
        if (l+1)%2==1:
            L.append(0)
            q=(l-p)//2+1
            for i in range(q):
                ans=max(ans,L[2*i+p+1]-L[2*i])
        else:
            q=(l-p)//2+1
            for i in range(q):
                ans=max(ans,L[2*i+p+1]-L[2*i])
print(ans)

Submission status: Runtime Error

Solution code:
l, accum = len(s), []
i = 0
while i < l:
    ch = s[i]
    count = 0
    while i < l and s[i] == ch:
        count += 1
        i += 1
    accum.append(count)

l = len(accum)
for i in range(1, l): accum[i] += accum[i-1]

mx = accum[min(2*k - (1-int(s[0])), l-1)]
i = 1
while l > 1:
    idx = min(2*k - (1-int(s[0])) + 2*i, l-1)
    mx = max(accum[idx] - accum[2*(i-1)+int(s[0])], mx)
    if idx >= l-1: break
    i += 1                                                                                                     
print(mx)

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
s=input()
lst=[1]
for i in range(1,n):
    if s[i]!=s[i-1]:
        lst.append(1)
    else:
        lst[-1]=lst[-1]+1
p=2*k
L=[0]
for i in lst:
  L.append(L[-1]+i)
l=len(L)
ans=0
print(lst);print(L)
if s=='1'*n:
    ans=n
else:
    if s[0]=='1':
        if l%2==0:
            q=(l-p)//2
            for i in range(q):
                ans=max(ans,L[2*i+p+1]-L[2*i])
        else:
            L.append(0)
            q=(l-p+1)//2+1
            for i in range(q):
                ans=max(ans,L[2*i+p+1]-L[2*i])
    else:
        L.insert(0,0)
        if (l+1)%2==1:
            L.append(0)
            q=(l-p)//2+1
            for i in range(q):
                ans=max(ans,L[2*i+p+1]-L[2*i])
        else:
            q=(l-p)//2
            for i in range(q):
                ans=max(ans,L[2*i+p+1]-L[2*i])
print(ans)

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
s=input()
lst=[1]
for i in range(1,n):
    if s[i]!=s[i-1]:
        lst.append(1)
    else:
        lst[-1]=lst[-1]+1
p=2*k
L=[0]
for i in lst:
  L.append(L[-1]+i)
l=len(L)
ans=0

if s=='1'*n:
    ans=n
else:
    if s[0]=='1':
        if l%2==0:
            q=(l-p)//2
            for i in range(q):
                ans=max(ans,L[2*i+p+1]-L[2*i])
        else:
            L.append(0)
            q=(l-p+1)//2+1
            for i in range(q):
                ans=max(ans,L[2*i+p+1]-L[2*i])
    else:
        L.insert(0,0)
        if (l+1)%2==1:
            L.append(0)
            q=(l-p)//2+1
            for i in range(q):
                ans=max(ans,L[2*i+p+1]-L[2*i])
        else:
            q=(l-p+1)//2
            for i in range(q):
                ans=max(ans,L[2*i+p+1]-L[2*i])
print(ans)

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
s=input()
lst=[1]
for i in range(1,n):
    if s[i]!=s[i-1]:
        lst.append(1)
    else:
        lst[-1]=lst[-1]+1
p=2*k
L=[0]
for i in lst:
  L.append(L[-1]+i)
l=len(L)
ans=0
print(lst);print(L)
if s=='1'*n:
    ans=n
else:
    if s[0]=='1':
        if l%2==0:
            q=(l-p)//2
            for i in range(q):
                ans=max(ans,L[2*i+p+1]-L[2*i])
        else:
            L.append(0)
            q=(l-p+1)//2+1
            for i in range(q):
                ans=max(ans,L[2*i+p+1]-L[2*i])
    else:
        L.insert(0,0)
        if (l+1)%2==1:
            L.append(0)
            q=(l-p)//2+1
            for i in range(q):
                ans=max(ans,L[2*i+p+1]-L[2*i])
        else:
            q=(l-p+1)//2
            for i in range(q):
                ans=max(ans,L[2*i+p+1]-L[2*i])
print(ans)

Submission status: Runtime Error

Solution code:
N,K=[int(i) for i in input().split()]
S=input()
cur=0
line=[]

for i in range(len(S)):
	if i==0:
		if S[0]=="0":
			line.append(0)
		line.append(1)
	else:
		if S[i-1]!=S[i]:
			line.append(1)
		else:
			line[-1]+=1
c=0
for i in range(len(line)):

	if i==0:
		if 2*K+1<=len(line):
			s=sum(line[i:2*K+1])
			c=s
		else:
			s=sum(line[i:])
			c=s
	elif i%2==0:
		if i+2*K<=len(line):
			s=s-line[i-2]-line[i-1]+line[i+2*K-1]+line[i+2*K]
			c=max(c,s)
		else:
			c=max(c,sum(line[i:]))
			break
print(c)

Submission status: Runtime Error

Solution code:
N,K=[int(i) for i in input().split()]
S=input()
cur=0
line=[]

for i in range(len(S)):
	if i==0:
		if S[0]=="0":
			line.append(0)
		line.append(1)
	else:
		if S[i-1]!=S[i]:
			line.append(1)
		else:
			line[-1]+=1
c=0
for i in range(len(line)):

	if i==0:
		if 2*K+1<=len(line):
			s=sum(line[i:2*K+1])
			c=s
		else:
			s=sum(line[i:])
			c=S
	elif i%2==0:
		if i+2*K+1<=len(line):
			s=s-line[i-2]-line[i-1]+line[i+2*K-1]+line[i+2*K]
			c=max(c,s)
		else:
			c=max(c,sum(line[i:]))
			break
print(c)

Submission status: Runtime Error

Solution code:
n, k = [int(elem) for elem in input().split()]
s = input()
new_s = [elem for elem in s]

lst = []
str_lst = []
def build_cnt_lst(s_lst, lst, str_lst):
    if '0' in s_lst and '1' in s_lst:    
        ind_0 = s_lst.index('0')
        ind_1 = s_lst.index('1')
        if ind_0 < ind_1:
            lst.append((ind_1 - ind_0))
            str_lst.append('0')
            if ind_1 != len(s_lst):
                return build_cnt_lst(s_lst[ind_1:], lst, str_lst)
            else:
                return lst, str_lst
        else:
            lst.append((ind_0 - ind_1))
            str_lst.append('1')
            if ind_0 != len(s_lst):
                return build_cnt_lst(s_lst[ind_0:], lst, str_lst)
            else:
                return lst, str_lst
    elif '0' in s_lst and '1' not in s_lst:
        lst.append(len(s_lst))
        str_lst.append('0')
        return lst, str_lst
    elif '1' in s_lst and '0' not in s_lst:
        lst.append(len(s_lst))
        str_lst.append('1')
        return lst, str_lst
    else:
        return lst, str_lst

if '0' not in s:
    print(n)

elif k == 0:
    print(s.count('1'))
    
else:
    build_cnt_lst(new_s, lst, str_lst)
    res = []
    for i, elem in enumerate(lst):
        num = 2 * k
        if str_lst[i] == '0':
            if (i + num) <= len(s):
                res.append(sum(lst[i:i+num]))
        else:
            if (i + num + 1) <= len(s):
                res.append(sum(lst[i:i+num+1]))
    if res != []:
        print(max(res))
    else:
        print(0)

Submission status: Runtime Error

Solution code:
n, k = [int(elem) for elem in input().split()]
s = input()
new_s = [elem for elem in s]

lst = []
str_lst = []
def build_cnt_lst(s_lst, lst, str_lst):
    if '0' in s_lst and '1' in s_lst:    
        ind_0 = s_lst.index('0')
        ind_1 = s_lst.index('1')
        if ind_0 < ind_1:
            lst.append((ind_1 - ind_0))
            str_lst.append('0')
            if ind_1 != len(s_lst):
                return build_cnt_lst(s_lst[ind_1:], lst, str_lst)
            else:
                return lst, str_lst
        else:
            lst.append((ind_0 - ind_1))
            str_lst.append('1')
            if ind_0 != len(s_lst):
                return build_cnt_lst(s_lst[ind_0:], lst, str_lst)
            else:
                return lst, str_lst
    elif '0' in s_lst and '1' not in s_lst:
        lst.append(len(s_lst))
        str_lst.append('0')
        return lst, str_lst
    elif '1' in s_lst and '0' not in s_lst:
        lst.append(len(s_lst))
        str_lst.append('1')
        return lst, str_lst
    else:
        return lst, str_lst

if '0' not in s:
    print(n)

elif k == 0:
    print(s.count('1'))
    
else:
    build_cnt_lst(new_s, lst, str_lst)
    res = []
    for i, elem in enumerate(lst):
        num = 2 * k
        if str_lst[i] == '0':
            if (i + num) <= len(s):
                res.append(sum(lst[i:i+num]))
        else:
            if (i + num + 1) <= len(s):
                res.append(sum(lst[i:i+num+1]))

    print(max(res))

Submission status: Runtime Error

Solution code:
n, k = [int(elem) for elem in input().split()]
s = input()
new_s = [elem for elem in s]

lst = []
str_lst = []
def build_cnt_lst(s_lst, lst, str_lst):
    if '0' in s_lst and '1' in s_lst:    
        ind_0 = s_lst.index('0')
        ind_1 = s_lst.index('1')
        if ind_0 < ind_1:
            lst.append((ind_1 - ind_0))
            str_lst.append('0')
            if ind_1 != len(s_lst):
                return build_cnt_lst(s_lst[ind_1:], lst, str_lst)
            else:
                return lst, str_lst
        else:
            lst.append((ind_0 - ind_1))
            str_lst.append('1')
            if ind_0 != len(s_lst):
                return build_cnt_lst(s_lst[ind_0:], lst, str_lst)
            else:
                return lst, str_lst
    elif '0' in s_lst and '1' not in s_lst:
        lst.append(len(s_lst))
        str_lst.append('0')
        return lst, str_lst
    elif '1' in s_lst and '0' not in s_lst:
        lst.append(len(s_lst))
        str_lst.append('1')
        return lst, str_lst
    else:
        return lst, str_lst

if '0' not in s:
    print(n)

elif k == 0:
    print(s.count('1'))
    
else:
    build_cnt_lst(new_s, lst, str_lst)
    res = []
    for i, elem in enumerate(lst):
        num = 2 * k
        if str_lst[i] == '0':
            if (i + num) <= len(lst):
                res.append(sum(lst[i:i+num]))
        else:
            if (i + num + 1) <= len(lst):
                res.append(sum(lst[i:i+num+1]))

    print(max(res))

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 = [0]
 
s_b = S[0]
for idx in range(1,N):
  s_n = S[idx]
  if s_n != s_b:
    S_list.append(idx)
  s_b = s_n

S_list.append(N)

cand_list = []
idx = 0

if 2*K+1 < len(S_list):
  print(N)
  exit()

while idx+2*K+1 < len(S_list):
	cand_list.append(S_list[idx+2*K+1]-S_list[idx])
	idx += 2
    
print(max(cand_list))

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 = [0]
 
s_b = S[0]
for idx in range(1,N):
  s_n = S[idx]
  if s_n != s_b:
    S_list.append(idx)
  s_b = s_n

S_list.append(N)

cand_list = []
idx = 0

if 2*K+1 < len(S_list)
	print(N)
  	exit()

while idx+2*K+1 < len(S_list):
	cand_list.append(S_list[idx+2*K+1]-S_list[idx])
	idx += 2
    
print(max(cand_list))

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 = [0]
 
s_b = S[0]
for idx in range(1,N):
  s_n = S[idx]
  if s_n != s_b:
    S_list.append(idx)
  s_b = s_n

S_list.append(N)

cand_list = []
idx = 0
while idx+2*K+1 < len(S_list):
	cand_list.append(S_list[idx+2*K+1]-S_list[idx])
	idx += 2
    
print(max(cand_list))

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sat Apr 13 09:53:12 2019

@author: Owner
"""

import collections
import scipy.misc
import sys
import numpy as np
import math
from operator import itemgetter
import itertools
import copy
import bisect

#素因数を並べる
def prime_decomposition(n):
  i = 2
  table = []
  while i * i <= n:
    while n % i == 0:
      n /= i
      table.append(int(i))
    i += 1
  if n > 1:
    table.append(int(n))
  return table   
# 桁数を吐く
def digit(i):
    if i > 0:
        return digit(i//10) + [i%10]
    else:
        return []
    
def getNearestValueIndex(list, num):
    """
    概要: リストからある値に最も近い値のインデックスを取得する関数
    @param list: データ配列
    @param num: 対象値
    @return 対象値に最も近い値
    """

    # リスト要素と対象値の差分を計算し最小値のインデックスを取得
    idx = np.abs(np.asarray(list) - num).argmin()
    return idx

def find_index(l, x, default=False):
    if x in l:
        return l.index(x)
    else:
        return default


"""
N, X = map(int, input().split())

x = [0]*N
x[:] = map(int, input().split())

P = [0]*N
Y = [0]*N
for n in range(N):
    P[n], Y[n] = map(int, input().split())

    
all(nstr.count(c) for c in '753')

# 複数配列を並び替え
ABT = zip(A, B, totAB)
result = 0
# itemgetterには何番目の配列をキーにしたいか渡します
sorted(ABT,key=itemgetter(2))
A, B, totAB = zip(*ABT)
A.sort(reverse=True)

# 2進数のbit判定
(x >> i) & 1

# dp最小化問題
dp = [np.inf]*N
for n in range(N):
    if n == 0:
        dp[n] = 0
    else:
        for k in range(1,K+1):
            if n-k >= 0:
                dp[n] = min(dp[n], dp[n-k] + abs(h[n]-h[n-k]))
            else:
                break

"""

N, K = map(int, input().split())
S = input()
def main():
    now = 1
    cnt = 0
    nums = []
    for i in range(N):
        if S[i] == str(now):
            cnt += 1
        else:
            nums.append(cnt);
            now = 1-now
            cnt = 1
    if cnt != 0:
        nums.append(cnt)
    if len(nums)%2 ==0:
        nums.append(0)
    add = 2*K + 1
    res = 0
    # ループの外にleft , right
    left = 0
    right = 0
    tmp= 0 # [left, right) のsum
    for i in range(0, len(nums), 2):
        # 次のleft, right を計算
        nextLeft = i
        nextRight = min(i+add, len(nums.size()))
        # 左端を計算
        while (nextLeft > left):
            tmp -= nums[left]
            left += 1
        
        # 右端を計算
        while (nextRight > right):
            tmp += nums[right]
            right +=1
        res = max(tmp, res)
    print(res)
 
if __name__ == '__main__':
    main()

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)
        
r=len(I)

for _ in range(10**5):
        I.append(n)

X=[]

for i in range(r):
    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)
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)
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)
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)