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:
def sub():
    N, K = [int(e) for e in input().split(" ")]
    ss = [int(e) for e in input()]
    if len(set(ss)) == 1:
        print(len(ss))
        return

    dd = [ss[i+1] - ss[i] for i in range(N-1)]
    idd = [0] + [i+1 for i, d in enumerate(dd) if d != 0]
    
    maxScore = 0
    for i in idd:
        if ss[i] == 0:
            ni = i + 2*K
            if ni < len(idd):
                score = idd[ni] - idd[i]
            else:
                score = N - idd[i]
            if score > maxScore:
                maxScore = score
        else:
            ni = i + 2*K + 1
            if ni < len(idd):
                score = idd[ni] - idd[i]
            else:
                score = N - idd[i]
            if score > maxScore:
                maxScore = score
    print(maxScore)

sub()

Submission status: Runtime Error

Solution code:
from sys import stdin

def consecutive_ranges(s, char):
    ranges = []
    i = 0
    while i < len(s):
        cur = s[i]
        if cur == char:
            start = i
            end = i
            while i < len(s) and s[i] == char:
                end = end + 1
                i = i + 1
            r = start, end 
            ranges.append(r)
        i = i + 1
    return ranges


def merge_range(r1, r2):
    s1, e1 = r1
    s2, e2 = r2
    return s1, e2


# n: sの長さ
# k: 正整数(指示を出せる回数)
# s: 長さnの文字の配列
def solve(n, k, s):
    # ゼロが連続している範囲
    ranges = consecutive_ranges(s, '0')

    # 何回指示を出すか
    num_ops = min(len(ranges), k)
    max_length = max([e - s for s, e in consecutive_ranges(s, '1')])
    for i in range(0, min(len(ranges) - num_ops + 1, len(ranges))):
        start, end = merge_range(ranges[i], ranges[i + num_ops - 1])
        s2 = s[:]
        s2[start:end] = ['1'] * (end - start)
        length = max([e - s for s, e in consecutive_ranges(s2, '1')])
        if length > max_length:
            max_length = length
    return max_length


def main():
    lines = stdin.readlines()
    n, k = lines[0].split(' ')
    s = list(lines[1].strip())
    ans = solve(n, int(k), s)
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
from sys import stdin
from itertools import combinations


def consecutive_ranges(s, char):
    ranges = []
    i = 0
    while i < len(s):
        cur = s[i]
        if cur == char:
            start = i
            length = 0
            while i < len(s) and s[i] == char:
                length = length + 1
                i = i + 1
            r = start, length
            ranges.append(r)
        i = i + 1
    return ranges


# toggle in-place
def toggle_range(s, r):
    start, length = r
    for i in range(start, start + length):
        char = '0' if s[i] == '1' else '1'
        s[i] = char


# n: sの長さ
# k: 正整数(指示を出せる回数)
# s: 長さnの文字の配列
def solve(n, k, s):
    # ゼロが連続している範囲
    ranges = consecutive_ranges(s, '0')
    
    # 何回指示を出すか
    num_ops = min(len(ranges), k)
    max_length = max([l for _, l in consecutive_ranges(s, '1')])
    for i in range(0, len(ranges) - num_ops + 1):
        # 指示を適用した後の状態
        s2 = s[:]
        for r in ranges[i : i + num_ops]: 
            toggle_range(s2, r)
        length = max([l for _, l in consecutive_ranges(s2, '1')])
        if length > max_length:
            max_length = length
    return max_length


def main():
    lines = stdin.readlines()
    n, k = lines[0].split(' ')
    s = list(lines[1].strip())
    ans = solve(n, int(k), s)
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
from sys import stdin
from itertools import combinations


def consecutive_ranges(s, char):
    ranges = []
    i = 0
    while i < len(s):
        cur = s[i]
        if cur == char:
            start = i
            length = 0
            while i < len(s) and s[i] == char:
                length = length + 1
                i = i + 1
            r = start, length
            ranges.append(r)
        i = i + 1
    return ranges

# n: sの長さ
# k: 正整数(指示を出せる回数)
# s: 長さnの文字の配列
def solve(n, k, s):
    # array of tuple indicates idx, length
    ranges = consecutive_ranges(s, '0')
    combs = list(combinations(ranges, k))
    max_len = 0
    for comb in combs:
        # toggle range in `comb`
        s2 = s[:]
        for start, length in comb:
            s2[start : start + length] = ['1'] * length
        lengths = [l for s, l in consecutive_ranges(s2, '1')]
        length = lengths[0] if len(lengths) < 2 else max(*lengths)
        if max_len < length:
            max_len = length
    if max_len == 0:
        lengths = [l for s, l in consecutive_ranges(s, '1')]
        length = lengths[0] if len(lengths) < 2 else max(*lengths)
        return length
    return max_len


def main():
    lines = stdin.readlines()
    n, k = lines[0].split(' ')
    s = list(lines[1].strip())
    ans = solve(n, int(k), s)
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
def main():
    n, k = [int(i) for i in input().split()]
    s = [int(i) for i in list(input())]
    table = []
    now = 1
    cnt = 0
    for i in range(n):
        if s[i] == now:
            cnt += 1
        else:
            table.append(cnt)
            cnt = 1
            now ^= 1 # 0　と 1 の切り替え
    if cnt != 0:
        table.append(cnt)
    if len(table)%2 == 0:
        # 奇数で終わらせれば、1 で終わったことになる
        table.append(0)
    ans = 0
    l = 0
    d = min(len(table), 2*k+1)
    r = l + d
    tmp = sum(table[l:r])
    while r <= len(table):
        ans = max(ans, tmp)
        if l < len(table):
            tmp -= table[l]
        l += 1
        if l < len(table):
            tmp -= table[l]
        l += 1
        r += 1
        if r <= len(table):
            tmp += table[r-1]
        r += 1
        if r <= len(table):
            tmp += table[r-1]
    print(ans)


if __name__ == '__main__':

Submission status: Runtime Error

Solution code:
import sys
N,K = map(int,input().split())
S = input()
i=0
while i<N and S[i] =='0':
  i += 1
left_0 = i
if i==N:
  print(N)
  sys.exit()
i=0
while S[N-1-i]=='0':
  i += 1
right_0 = i

H = [0,left_0]
count =1
for i in range(N-left_0-right_0):
  if i==N-left_0-right_0-1:
    H.append(count)
  elif S[i+left_0] == S[i+left_0+1]:
    count += 1
  else:
    H.append(count)
    count =1
H.append(right_0)
H.append(0)
new_k = 2*K+1
ans=0
LL = (len(H)-new_k)//2
if LL>=0:
  for i in range(LL+1):
    s =0
    for j in range(new_k+1):
      s+=H(i*2+new_k)
    ans = max(ans,s)
else:
  ans = N
print(ans)

Submission status: Runtime Error

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

if len(S) == 1:
    print(1)
elif len(S) == 2:
    print(2)
elif S[0] == '0':
    LA = [0]
    LB = [0]
    for i in range(1,N-1):
        if (S[i] != S[i+1]) & (S[i] == '0'):
            LA.append(i+1)
        elif (S[i] != S[i+1]) & (S[i] == '1'):
            LB.append(i)
    LB.append(N-1)
    ANS = 0
    if K >= len(LB)-1:
        print(len(S))
    else:
        for i in range(len(LA)-K):
            J = LB[i+K]-LB[i]+1
            if J > ANS:
                ANS = J
        print(ANS)
else:
    LA = [0]
    LB = []
    for i in range(0,N-1):
        if (S[i] != S[i+1]) & (S[i] == '0'):
            LA.append(i+1)
        elif (S[i] != S[i+1]) & (S[i] == '1'):
            LB.append(i)
    LB.append(N-1)\
    ANS = 0
    if K >= len(LB)-1:
        print(len(S))
    else:
        for i in range(len(LB)-K):
            J = LB[i+K]-LA[i]+1
            if J > ANS:
                ANS = J
        print(ANS)

Submission status: Runtime Error

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

if len(S) == 1:
    print(1)
elif len(S) == 2:
    print(2)
elif S[0] == '0':
    LA = [0]
    LB = [0]
    for i in range(1,N-1):
        if (S[i] != S[i+1]) & (S[i] == '0'):
            LA.append(i+1)
        elif (S[i] != S[i+1]) & (S[i] == '1'):
            LB.append(i)
    LB.append(N-1)
    ANS = 0
    if K > len(LB)-1:
        print(len(S))
    else:
        for i in range(len(LA)-K):
            J = LB[i+K]-LA[i]+1
            if J > ANS:
                ANS = J
        print(ANS)
else:
    LA = [0]
    LB = []
    for i in range(1,N-1):
        if (S[i] != S[i+1]) & (S[i] == '0'):
            LA.append(i+1)
        elif (S[i] != S[i+1]) & (S[i] == '1'):
            LB.append(i)
    LB.append(N-1)
    ANS = 0
    if K > len(LB)-1:
        print(len(S))
    else:
        for i in range(len(LA)-K):
            J = LB[i+K]-LA[i]+1
            if J > ANS:
                ANS = J
        print(ANS)

Submission status: Runtime Error

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

if len(S) == 1:
    print(1)
elif len(S) == 2:
    print(2)
elif S[0] == '0':
    LA = [0]
    LB = [0]
    for i in range(1,N-1):
        if (S[i] != S[i+1]) & (S[i] == '0'):
            LA.append(i+1)
        elif (S[i] != S[i+1]) & (S[i] == '1'):
            LB.append(i)
    LB.append(N-1)
    ANS = 0
    for i in range(len(LA)-K):
        J = LB[i+K]-LA[i]+1
        if J > ANS:
            ANS = J
    print(ANS)
else:
    LA = [0]
    LB = []
    for i in range(1,N-1):
        if (S[i] != S[i+1]) & (S[i] == '0'):
            LA.append(i+1)
        elif (S[i] != S[i+1]) & (S[i] == '1'):
            LB.append(i)
    LB.append(N-1)
    ANS = 0
    if K > len(LB)-1:
        print(len(S))
    else:
        for i in range(len(LA)-K):
            J = LB[i+K]-LA[i]+1
            if J > ANS:
                ANS = J
        print(ANS)

Submission status: Runtime Error

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

if len(S) == 1:
    print(1)
elif len(S) == 2:
    print(2)
elif S[0] == '0':
    LA = [0]
    LB = [0]
    for i in range(1,N-1):
        if (S[i] != S[i+1]) & (S[i] == '0'):
            LA.append(i+1)
        elif (S[i] != S[i+1]) & (S[i] == '1'):
            LB.append(i)
    LB.append(N-1)
    ANS = 0
    for i in range(len(LA)-K):
        J = LB[i+K]-LA[i]
        if J > ANS:
            ANS = J
    print(ANS)
else:
    LA = [0]
    LB = []
    for i in range(1,N-1):
        if (S[i] != S[i+1]) & (S[i] == '0'):
            LA.append(i+1)
        elif (S[i] != S[i+1]) & (S[i] == '1'):
            LB.append(i)
    LB.append(N-1)
    ANS = 0
    if K > len(LB)-1:
        print(len(S))
    else:
        for i in range(len(LA)-K):
            J = LB[i+K]-LA[i]+1
            if J > ANS:
                ANS = J
        print(ANS)

Submission status: Runtime Error

Solution code:
import re


n, k = map(int, input().split())
s = input()
s_re = re.split(r'(0+)', s)
srl = len(s_re)
m = [0] * srl
if (srl - 1 // 2) <= k:
    ans = n
else:
    for i, w in enumerate(s_re):
        m[i] = len(w)
    ans = [1]
    for i in range(0, srl, 2):
        ie = i + k * 2 + 1
        if ie <= srl:
            ans += [sum(m[i:ie])]
        else:
            break
print(max(ans))

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())
s = input()
s_re = re.split(r'(0+)', s)
srl = len(s_re)
m = [0] * srl
if (srl - 1 // 2) <= k:
    ans = n
else:
    for i, w in enumerate(s_re):
        m[i] = len(w)
    ans = [1]
    for i in range(0, srl, 2):
        ie = i + k * 2 + 1
        if ie <= srl:
            ans += [sum(m[i:ie])]
        else:
            break
print(max(ans))

Submission status: Runtime Error

Solution code:

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

nowOn = S[0]
count = 0
serialList = []

for i in S:

    if(nowOn == i):
        count += 1

    else:
        serialList.append(count)
        count = 1
        nowOn = i

serialList.append(count)

T = 2 * K + 1
A = 0

#serialList -> ["1", "0", "1", "0", ...]
if S[0] == "0":
    serialList.insert(0, 0)
    
#exception
# --???

maxA = 0
for i in range(T):
    A += serialList[i]
    maxA = A
    
for i in range(T, len(serialList), 2):
    A -= serialList[i - T]
    A -= serialList[i - T + 1]
    A += serialList[i]

    if(i+1 < len(serialList)):
        A += serialList[i + 1]

    if maxA < A:
        maxA = A
        
print(maxA)

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
"""
D - Handstand
https://atcoder.jp/contests/abc124/tasks/abc124_d

"""
import sys


from itertools import groupby
from collections import deque

def solve(N, K, S):
    seq = [[k, len(list(g))]  for k, g in groupby(S)]
    if len(seq) <= 2*K:
        return len(S)
    if seq[0][0] == '0':
        seq = [['1', 0]] + seq
    if len(seq) % 2 == 0:
        seq.append(['1', 0])

    seq = deque(seq)
    rem = deque()
    ans = 0
    for i in range(2*K+1):
        _, v = seq.popleft()
        ans += v
        rem.append(v)

    max_ans = ans
    while seq:
        ans -= rem.popleft()
        ans -= rem.popleft()
        _, v = seq.popleft()
        ans += v
        _, v = seq.popleft()
        ans += v
        max_ans = max(max_ans, ans)
    return max_ans


def main(args):
    N, K = map(int, input().split())
    S = input()
    ans = solve(N, K, S)
    print(ans)


if __name__ == '__main__':
    main(sys.argv[1:])

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())
s = input()
c = 1
l = []
l += [0,0] if (s[0] == '0') else [0]
for i in range(1, n):
    if s[i-1] != s[i]: l += [c+l[-1]]; c = 1; else: c +=1
l += [c+l[-1]]
if s[-1] == '0': l += [l[-1]]
ll = len(l)
pl = 2 * k + 1
if ll <= pl: print(n)
else: m = 0
   for i in range(0, ll-pl+1, 2): t = l[pl+i]-l[i]; m = t if (t > m) else m
   print(m)

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())
s = input()
c = 1
l = []

if s[0] == '0':
    l.append(0)

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

if s[0] == '0':
    l.append(0)

ll = len(l)
pl = 2 * k + 1

if ll <= pl:
    t = 0
    for i in range(ll):
        t += l[i]
    print(t)
else:
    t = 0
    for i in range(0,pl):
      t += l[i]
    m = t
    for i in range(0, ll-pl, 2):
        t += l[pl+i] + l[pl+i+1]
        t -= l[i]    + l[i+1]
        m = t if (t > m) else m
    print(m)

Submission status: Runtime Error

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

li = []

v = 1
c = 0
for it in S:
  if (v != it ):
    li.append(c)
    c=1
  else:
    c+=1
  v = it
li.append(c)
s = 0
lis = [0]*(len(li)+1)
for i in range(len(li)):
  lis[i+1]=lis[i]+li[i]
s = 0
for i in range(N):
  if (i*2>=N):
    continue
  a = min(i*2,len(li))
  b = min(i*2+2*K+1,len(li))

  ds2 = lis[b]-lis[a]
  s = max(s,ds)
print (s)

Submission status: Runtime Error

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

l=[0]
curr='1'

for c is S:
    if c !=curr:
        l.append(1)
        curr=c
    else:
        l[-1]+=1
if curr=='0':
    l.append(0)

n_group=len(1)
sum_=0
k =min(k,n_group)

for i in range(k):
    sum_+=l[2*i]+l[2*i+1]
sum_ +=l[2*k]
max_=sum_

for i in range(k,n_group):
    sum_ -=l[2*i-2*k]+l[2*i+1-2*k]
    sum_ +=l[2*i+1]+l[2*i+2]
    max_ =max(max_,sum_)

print(max_)

Submission status: Runtime Error

Solution code:

# coding: utf-8

# In[1]:

import sys
#input = sys.stdin.readline
N, K = map(int, input().split())
S = input()


# In[2]:

#print(N, K)
#print(len(S), S)


# In[4]:

tmp = S[0]
sums = [0]*(N+1)
counter = 1
gc = N
nb = 0
for i in range(1, N):
    if S[i] != tmp:
        sums[nb+1] = i
        nb += 1
        counter = 0
        tmp = S[i]
    else:
        gc -= 1
    counter += 1
    if i == N-1:
        sums[nb+1] = i+1

#print(N, gc)
sums = sums[:gc+1]
pl = [0]*gc
if gc < K*2:
    print(N)
else:
    if S[0] == "0":
        for i in range(-1, gc-K*2+1, 2):
            j = 0 if i < 0 else i
            pl[i] = sums[i+1+K*2]-sums[j]
    else:
        for i in range(0, gc-K*2+1, 2):
            j = i
            pl[i] = sums[i+1+K*2]-sums[j]
    print(max(pl))


# In[ ]:

Submission status: Runtime Error

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

longest = 0
for m in range(N):
  k = 0
  if S[m] == '1':
    for n in range(m, N-1):
      if S[n] == '0' and S[n+1] == '1':
        k += 1
        if k > K:
          break
    currentL = n-m+1
    if currentL > longest:
      longest = currentL    
  else:
    for n in range(m+1, N-1):
      if S[n] == '1' and S[n+1] == '0':
        k += 1
        if k == K:
          break
    currentL = n-m+1
    if currentL > longest:
      longest = currentL  

print(longest)

Submission status: Runtime Error

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

longest = 0
for m in range(N):
    k = 0
    if S[m] == '1':
        for n in range(m, N-1):
            if S[n] == '0' and S[n+1] == '1':
                k += 1
                if k > K:
                    break
        currentL = n-m+1
        if currentL > longest:
            longest = currentL    
    else:
        for n in range(m+1, N-1):
            if S[n] == '1' and S[n+1] == '0':
                k += 1
                if k == K:
                    break
        currentL = n-m+1
        if currentL > longest:
            longest = currentL

Submission status: Runtime Error

Solution code:
N, K = input().split()
N = int(N)
K = int(K)
S = input()
A = []
for i in range(len(S)-1):
    if (S[i]=="0" and S[i+1]=="1") or (S[i]=="1" and S[i+1]=="0"):
        A.append(i+1)
A.insert(0, 0)
A.append(N)
M = []
if S[0] == "0":
    if K > len(A)//2:
        K = len(A)//2
    M.append(A[K*2]-A[0])
    a = 1
    b = a + K*2 + 1
elif S[0] == "1":
    if K > (len(A)-1)//2:
        K = (len(A)-1)//2
    a = 0
    b = a + K*2 + 1
if S[-1] == "1":
    while b <= len(A):
        M.append(A[b]-A[a])
        a += 2
        b = a + K*2 + 1
elif S[-1] == "0":
    while b <= len(A)-1:
        M.append(A[b]-A[a])
        a += 2
        b = a + K*2 + 1
    M.append(A[-1]-A[-1-K*2])
print(max(M))

Submission status: Runtime Error

Solution code:
from sys import stdin

N = [int(x) for x in stdin.readline().rstrip().split()]
S = [int(x) for x in list(stdin.readline().rstrip())]

one = []
zero = []

c = 0

if S[0] != 1:
    one.append(0)

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

if S[-1] != 1:
    one.append(0)

ans = []

for k in range(len(one) // 2):
    ans.append(sum(one[2 * k:(2 * k) + (2 * N[1]) + 1]))

print(max(ans))

Submission status: Runtime Error

Solution code:
from sys import stdin

N = [int(x) for x in stdin.readline().rstrip().split()]
S = [int(x) for x in list(stdin.readline().rstrip())]

one = []
zero = []

c = 0

if S[0] != 1:
    one.append(0)

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

if S[-1] != 1:
    one.append(0)

ans = []

for k in range(len(one) // 2):
    ans.append(sum(one[2 * k:(2 * k) + (2 * N[1]) + 1]))

print(zero,one,ans)

print(max(ans))

Submission status: Runtime Error

Solution code:
N , K = map(int,input().split())
S = input()
L = []
flag = int(S[0])
for x in S:
    L.append(x)
cnt = 1
C = []
for i in range(len(L)):
    try:
        if L[i] == L[i+1]:
            cnt += 1
        else:
            C.append(cnt)
            cnt = 1
    except:
        pass
C.append(cnt)
ans = 0
from itertools import accumulate
accumulated =  [0]+list(accumulate(C))
ans = 0
A = 0
for i in range(len(C)):
    try:
        if (flag + i) % 2 == 0:
            K = min(K,len(C)//A+1)
            ans = accumulated[i+2*K] - accumulated[i]
        else:
            K = min(K,len(C)//2)
            ans = accumulated[i+2*K+1] - accumulated[i]
        A = max(ans,A)
    except:
        pass
print(A)

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())
s = input()
s_split_0 = s.split("1")
s_split_1 = s.split("0")

s_split_1= [len(i) for i in s_split_1 if i!=""]
s_split_0= [len(i) for i in s_split_0 if i!=""]

if s[0]=="1":
    print(max([sum(s_split_0[i:i+k])+sum(s_split_1[i:i+k+1]) for i in range(len(s_split_0)-k+1)]))
else:
    print(max([sum(s_split_0[i:i+k])+sum(s_split_1[i:i+k]) for i in range(len(s_split_1)-k+1)]))

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())
s = input()
s_split_0 = s.split("1")
s_split_1 = s.split("0")

s_split_1= [len(i) for i in s_split_1 if i!=""]
s_split_0= [len(i) for i in s_split_0 if i!=""]

if s[0]=="1":
    print(max([sum(s_split_0[i:i+k])+sum(s_split_1[i:i+k+1]) for i in range(len(s_split_0)-k+1)]))
else:
    print(max([sum(s_split_0[i:i+k+1])+sum(s_split_1[i:i+k]) for i in range(len(s_split_1)-k+1)]))

Submission status: Runtime Error

Solution code:
n,k = list(map(int, input().split()))
s = input()
if s[n-1] == '1':
	s += '0'
else:
	s += '1'
c = []

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

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

if 2*k >= 2*len(c):
	print(c[-1][1])
	quit()
else:
	p = 0
	res = 0
	if s[0][0] == '0':
		res = c[1][1] if k == 1 else c[2*k+1][1]
		p += 1
	else:
		res = c[2][1] if k == 1 else c[2*k][1]
		p += 2

	while p+2*k < len(c):
		res = max(res, c[p+2*k][1] - c[p-1][1])
		p += 2

	print(res)
	quit()

Submission status: Runtime Error

Solution code:
n,k = list(map(int, input().split()))
s = input()
s += '0' if s[n-1] == '1' else '0'
c = []

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

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

if 2*k >= 2*len(c):
	print(c[-1][1])
	quit()
else:
	p = 0
	res = 0
	if s[0][0] == '0':
		res = c[1][1] if k == 1 else c[2*k+1][1]
		p += 1
	else:
		res = c[2][1] if k == 1 else c[2*k][1]
		p += 2

	while p+2*k < len(c):
		res = max(res, c[p+2*k][1] - c[p-1][1])
		p += 2

	print(res)
	quit()

Submission status: Runtime Error

Solution code:
n,k = list(map(int, input().split()))
s = input()
s += '0' if s[n-1] == '1' else '0'
c = []

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

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

if 2*k >= 2*len(c):
  print(c[-1][1])
  quit()
else:
  p = 0
  res = 0
  if c[0][0] == '0':
    res = c[(k-1)+1][1] if k == 1 else c[k+1][1]
    p += 1
  else:
    res = c[(k-1)+2][1] if k == 1 else c[k+2][1]
    p += 2

  while p+2*k < len(c):
    res = max(res, c[p+2*k][1] - c[p-1][1])
    p += 2

  print(res)
  quit()

Submission status: Runtime Error

Solution code:
if __name__ == "__main__":

	n,k = list(map(int, input().split()))
	s = input()
	s += '0' if s[n-1] == '1' else '0'
	c = []

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

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

	if 2*k >= 2*len(c):
		print(c[-1][1])
		quit()
	else:
		p = 0
		res = 0
		if c[0][0] == '0':
			res = c[(k-1)+1][1] if k == 1 else c[k+1][1]
			p += 1
		else:
			res = c[(k-1)+2][1] if k == 1 else c[k+2][1]
			p += 2

		while p+2*k < len(c):
			res = max(res, c[p+2*k][1] - c[p-1][1])
			p += 2

		print(res)
		quit()

Submission status: Runtime Error

Solution code:
if __name__ == "__main__":

	n,k = list(map(int, input().split()))
	s = input()
	s += '0' if s[n-1] == '1' else '0'
	c = []

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

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

	if 2*k >= 2*len(c):
		print(c[-1][1])
		quit()
	else:
		p = 0
		res = 0
		if s[0][0] == '0':
			res = c[(k-1)+1][1] if k == 1 else c[k+1][1]
			p += 1
		else:
			res = c[(k-1)+2][1] if k == 1 else c[k+2][1]
			p += 2

		while p+2*k < len(c):
			res = max(res, c[p+2*k][1] - c[p-1][1])
			p += 2

		print(res)
		quit()

Submission status: Runtime Error

Solution code:
n, k = [int(i) for i in input().split()]
s = [0,1] + [int(i) for i in list(input())] + [1,0]
down = [i for i in range(n+3) if s[i] == 1 and s[i+1] == 0]
up = [i for i in range(n+3) if s[i] == 0 and s[i+1] == 1]
#print(s)
#print(down)
#print(up)
if k >= len(up):
  ans = n
else:
  if down[k] - up[0] - 1 > ans:
    ans = down[k] - up[0] - 1
  for i in range(1,len(up)-k-1):
    if down[i + k] - up[i] > ans:
      ans = down[i + k] - up[i]
  if down[len(up)-1] - up[len(up)-k -1] - 1 > ans:
    ans = down[len(up)-1] - up[len(up)-k -1] - 1
print(ans)

Submission status: Runtime Error

Solution code:
n, k = [int(i) for i in input().split()]
s = [0,1] + [int(i) for i in list(input())] + [1,0]
down = [i for i in range(n+3) if s[i] == 1 and s[i+1] == 0]
up = [i for i in range(n+3) if s[i] == 0 and s[i+1] == 1]
#print(s)
#print(down)
#print(up)
ans = 1
if down[k] - up[0] - 1 > ans:
  ans = down[k] - up[0] - 1
for i in range(1,len(up)-k-1):
  if down[i + k] - up[i] > ans:
    ans = down[i + k] - up[i]
if down[len(up)-1] - up[len(up)-k -1] - 1 > ans:
  ans = down[len(up)-1] - up[len(up)-k -1] - 1
print(ans)

Submission status: Runtime Error

Solution code:
N, K = input().split()
N = int(N)
K = int(K)
S = input()
A = []
for i in range(len(S)-1):
    if (S[i]=="0" and S[i+1]=="1") or (S[i]=="1" and S[i+1]=="0"):
        A.append(i+1)
A.insert(0, 0)
A.append(N)
if K > len(A)//2:
    K = len(A)//2
M = []
while K > 0:
    if S[0] == "0":
        M.append(A[K*2]-A[0])
        a = 1
        b = a + K*2 + 1
    elif S[0] == "1":
        a = 0
        b = a + K*2 + 1
    if S[-1] == "1":
        while b <= len(A):
            M.append(A[b]-A[a])
            a += 2
            b = a + K*2 + 1
    elif S[-1] == "0":
        while b <= len(A)-1:
            M.append(A[b]-A[a])
            a += 2
            b = a + K*2 + 1
        M.append(A[-1]-A[-1-K*2])
    K -= 1
print(max(M))

Submission status: Runtime Error

Solution code:
N, K = input().split()
N = int(N)
K = int(K)
S = input()
A = []
for i in range(len(S)-1):
    if (S[i]=="0" and S[i+1]=="1") or (S[i]=="1" and S[i+1]=="0"):
        A.append(i+1)
A.insert(0, 0)
A.append(N)
if K > len(A)//2:
    K = len(A)//2
M = []
while K > 0:
    if S[0] == "0":
        M.append(A[K*2]-A[0])
        a = 1
        b = a + K*2 + 1
    elif S[0] == "1":
        a = 0
        b = a + K*2 + 1
    if S[-1] == "1":
        while b <= len(A):
            M.append(A[b]-A[a])
            a += 2
            b = a + K*2 + 1
    elif S[-1] == "0":
        while b <= len(A)-1:
            M.append(A[b]-A[a])
            a += 2
            b = a + K*2 + 1
        M.append(A[-1]-A[-1-K*2])
    K -= 1
    print(max(M))

Submission status: Runtime Error

Solution code:
N, K = input().split()
N = int(N)
K = int(K)
S = input()
A = []
for i in range(len(S)-1):
    if (S[i]=="0" and S[i+1]=="1") or (S[i]=="1" and S[i+1]=="0"):
        A.append(i+1)
A.insert(0, 0)
A.append(N)
if len(A) == 2:
    print(len(S))
else:
    M = []
    while K > 0:
        if S[0] == "0":
            M.append(A[K*2]-A[0])
            a = 1
            b = a + K*2 + 1
        elif S[0] == "1":
            a = 0
            b = a + K*2 + 1
        if S[-1] == "1":
            while b <= len(A):
                M.append(A[b]-A[a])
                a += 2
                b = a + K*2 + 1
        elif S[-1] == "0":
            while b <= len(A)-1:
                M.append(A[b]-A[a])
                a += 2
                b = a + K*2 + 1
            M.append(A[-1]-A[-1-K*2])
        K -= 1
    print(max(M))

Submission status: Runtime Error

Solution code:
N, K = input().split()
N = int(N)
K = int(K)
S = input()
A = []
for i in range(len(S)-1):
    if (S[i]=="0" and S[i+1]=="1") or (S[i]=="1" and S[i+1]=="0"):
        A.append(i+1)
A.insert(0, 0)
A.append(N)
M = []
if S[0] == "0":
    M.append(A[K*2]-A[0])
    a = 1
    b = a + K*2 + 1
elif S[0] == "1":
    a = 0
    b = a + K*2 + 1
if S[-1] == "1":
    while b <= len(A):
        M.append(A[b]-A[a])
        a += 2
        b = a + K*2 + 1
elif S[-1] == "0":
    while b <= len(A)-1:
        M.append(A[b]-A[a])
        a += 2
        b = a + K*2 + 1
    M.append(A[-1]-A[-1-K*2])
print(max(M))

Submission status: Runtime Error

Solution code:
N, K = input().split()
N = int(N)
K = int(K)
S = input()
A = []
for i in range(len(S)-1):
    if (S[i]=="0" and S[i+1]=="1") or (S[i]=="1" and S[i+1]=="0"):
        A.append(i+1)
A.insert(0, 0)
A.append(N)
M = []
a = 0
while a <= len(A)-K*2-1:
    b = a + K*2 + 1
    M.append(A[b]-A[a])
    a += 2
print(max(M))

Submission status: Runtime Error

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

seq = []

flag = False
tmp = 0
for i in range(N-1):
    if S[i] != S[i+1]:
        tmp += 1
        seq.append(tmp)
        tmp = 0
            
    else:
        tmp += 1

res1 = []     
res2 = []

seq.append(tmp+1)

seq_sum = [0 for _ in range(len(seq)+1)]

for i in range(len(seq)):
    seq_sum[i+1] = seq_sum[i] + seq[i]

res = 1
if S[0] == '0':
    for i in range(len(seq_sum) - 2 * K - 1):
        if i%2 == 0:
            r = min(i + 2*K, len(seq_sum))
            res = max(res, seq_sum[r] - seq_sum[i]) 
        else:
            r = min(i + 2*K+1, len(seq_sum))
            res = max(res, seq_sum[r] - seq_sum[i])
        
else:
    for i in range(len(seq_sum) - 2 * K):
        if i%2 == 0:
            r = min(i + 2*K+1, len(seq_sum))
            res = max(res, seq_sum[r] - seq_sum[i])
        else:
            r = min(i + 2*K, len(seq_sum))
            res = max(res, seq_sum[r] - seq_sum[i]) 
 


print(res)