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 = input()
sl1 = []
sl0 = []
cnt1 = 0
cnt0 = 0
for i in range(n):
    if s[i]=="0" and cnt1:
        sl1.append(cnt1)
        cnt1 = 0
    elif s[i]=="1" and cnt0:
        sl0.append(cnt0)
        cnt0 = 0
    if s[i]=="1": cnt1 += 1
    elif s[i]=="0": cnt0 += 1
if cnt1: sl1.append(cnt1)
if cnt0: sl0.append(cnt0)
if len(sl0)==k:
    ans = n
elif s[0]=="1" and s[-1]=="1":
    ans = sum(sl1[0:k+1]) + sum(sl0[0:k])
    ans_ = ans
    for i in range(k, len(sl0)):
        ans_ += sl1[i+1]+sl0[i]-sl1[i-k]-sl0[i-k]
        ans = max(ans, ans_)
elif s[0]=="0" and s[-1]=="0":
    ans_ = sum(sl1[0:k+1]) + sum(sl0[1:k+1])
    ans = max(sum(sl1[0:k]) + sum(sl0[0:k]), ans_)
    for i in range(k+1, len(sl1)):
        ans_ += sl1[i]+sl0[i]-sl1[i-k]-sl0[i-k+1]
        ans = max(ans, ans_)
    ans = max(ans, ans_+sl0[-1]-sl1[len(sl1)-k]-sl0[len(sl0)-k])
elif s[0]=="1" and s[-1]=="0":
    ans = sum(sl1[0:k+1]) + sum(sl0[0:k])
    ans_ = ans
    for i in range(k, len(sl1)-1):
        ans_ += sl1[i+1]+sl0[i]-sl1[i-k]-sl0[i-k]
        ans = max(ans, ans_)
    ans = max(ans, ans_+sl0[-1]-sl1[len(sl1)-k-1]-sl0[len(sl0)-k-1])
elif s[0]=="0" and s[-1]=="1":
    ans_ = sum(sl1[0:k+1]) + sum(sl0[1:k+1])
    ans = max(sum(sl1[0:k]) + sum(sl0[0:k]), ans_)
    for i in range(k+1, len(sl1)):
        ans_ += sl1[i]+sl0[i]-sl1[i-k]-sl0[i-k+1]
        ans = max(ans, ans_)        
print(ans)

Submission status: Runtime Error

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

zero = []
one = []
string = s[0]
if string == '0':
	flag = True
else:
	flag = False
count = 0
left = 0
right = 0
num = 0
for i in range(n):
	if s[i] == string:
		count += 1
	else:
		if string == '0':
			num = count
			string = '1'
		else:
			one.append(count)
			if num != 0:
				zero.append([num,left,count])
			string = '0'
			left = count
			num = 1
		count = 1

if string == '0':
	zero.append([num,left,0])
else:
	zero.append([num,left,count])
	one.append(count)
#print(zero)
#print(one)

res = 0
for i in range(k):
	res += zero[i][0] + zero[i][1]
res += zero[i][2]
#print(res)
ans = res
for i in range(len(zero)-k):
	res -= zero[i][0] + zero[i][1]
	#print(res)
	res += zero[i+k][0] + zero[i+k][2]
	#print(res)
	ans = max(ans,res)
print(ans)

Submission status: Runtime Error

Solution code:
# coding: utf-8
import sys
import itertools

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

# 0か1の連続している人数のリスト、累積和
streak = [0, 0]
N, K = lr()
S = sr() + '2'
cur = 1
for i in range(N):
    if S[i] != S[i+1]:
        streak.append(cur)
        cur = 1
    else:
        cur += 1

streak_cum = list(itertools.accumulate(streak))
streak_cum.extend([streak_cum[-1], streak_cum[-1]])
if S[0] == '0':
    start = 0
else:
    start = 1
answer = 0

for i in range(start, len(streak_cum), 2):
    result = streak_cum[i+2*K+1] - streak_cum[i]
    if result > answer:
        answer = result

print(answer)
# 52

Submission status: Runtime Error

Solution code:
def main():
    n, k = map(int, input().split())
    s = input()
    sub = []
    count = 1
    for i in range(1, n):
        if s[i] == s[i-1]:
            count += 1
        else:
            sub.append(count)
            count = 1
    sub.append(count)
    
    key = s[0]
    
    if key:
        if 2*k+1 >= len(sub):
            a = n
        else:
            a = sum(sub[:(2*k+1)])
            key = 0
    else:
        if 2*k >= len(sub):
            a = n
        else:
            a = sum(sub[:(2*k)])
            key = 0
    ans = a
    flag = False
    for i in range(len(sub)):
        a -= sub[i]
        if key:
            if i+2*k == len(sub)-1:
                a += sub[i+2*k]
                flag = True
            key = 0
        else:
            if i+2*k == len(sub)-1:
                a += sub[i+2*k]
                flag = True
            else:
                a += sub[i+2*k]+sub[i+1+2*k]
            if i+2*k == len(sub)-2:
                flag = True
            key = 1
            
        if a > ans:
            ans = a
        if flag:
            break
    
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())
s = str(input())
head = s[0]

counts = []
_hd = head
_cnt = 0
for _s in s:
  if _s == _hd:
    _cnt += 1
  else:
    counts.append(_cnt)
    _hd = _s
    _cnt = 1
else:
  counts.append(_cnt)

acc = [0]
for c in counts:
  acc.append(acc[-1] + c)

if len(counts) <= 2*k:
  ans = len(s)
else:
  if head == '1':
    for i in range(2*k+1, len(counts)+1, 2):
      p = acc[i] - acc[i - (2*k+1)]
      ans = max(ans, p)
  else:
    for i in range(2*k, len(counts)+1, 2):
      p = acc[i] - acc[max(0, i-(2*k+1))]
      ans = max(ans, p)

print(ans)

Submission status: Runtime Error

Solution code:
n,k = map(int,input().split())
s = input()
x = len(s)
lst = [0]
ans = 0
for i in range(1,x):
    if s[i-1] != s[i]:
        lst.append(i)
lst.append(x)
if s[0] == "1" and s[x-1] == "0":
    #s.reverse()
if s[0] == "0" and s[x-1] == "1":
    if 2*k >= len(lst):
        ans = x
    else:
        i = 1
        while i+2*k+1 <= len(lst)-1:
            ##ans = max(ans,lst[i+2*k+1]-lst[i])
            i += 2
        #ans = max(ans,lst[2*k])
if s[0] == "0" and s[x-1] == "0":
    if 2*k >= len(lst):
        ans = x
    else:
        i = 1
        while i+2*k+1 <= len(lst)-1:
            ##ans = max(ans,lst[i+2*k+1]-lst[i])
            i += 2
        #ans = max(ans,lst[2*k])
        #ans = max(ans,x-lst[len(lst)-2*k-1])
if s[0] == "1" and s[x-1] == "1":
    if 2*k+1 >= len(lst):
        ans = x
    else:
        i = 0
        while i+2*k+1 <= len(lst)-1:
            #ans = max(ans,lst[i+2*k+1]-lst[i])
            i += 2
print(ans)

Submission status: Runtime Error

Solution code:
n,k = map(int,input().split())
s = input()
x = len(s)
lst = [0]
ans = 0
for i in range(1,x):
    if s[i-1] != s[i]:
        lst.append(i)
lst.append(x)
if s[0] == "1" and s[x-1] == "0":
    s.reverse()
if s[0] == "0" and s[x-1] == "1":
    if 2*k >= len(lst):
        ans = x
    else:
        i = 1
        while i+2*k+1 <= len(lst)-1:
            ##ans = max(ans,lst[i+2*k+1]-lst[i])
            i += 2
        #ans = max(ans,lst[2*k])
if s[0] == "0" and s[x-1] == "0":
    if 2*k >= len(lst):
        ans = x
    else:
        i = 1
        while i+2*k+1 <= len(lst)-1:
            ##ans = max(ans,lst[i+2*k+1]-lst[i])
            i += 2
        #ans = max(ans,lst[2*k])
        #ans = max(ans,x-lst[len(lst)-2*k-1])
if s[0] == "1" and s[x-1] == "1":
    if 2*k+1 >= len(lst):
        ans = x
    else:
        i = 0
        while i+2*k+1 <= len(lst)-1:
            #ans = max(ans,lst[i+2*k+1]-lst[i])
            i += 2
print(ans)

Submission status: Runtime Error

Solution code:
n,k = map(int,input().split())
s = input()
x = len(s)
lst = [0]
ans = 0
for i in range(1,x):
    if s[i-1] != s[i]:
        lst.append(i)
lst.append(x)
if s[0] == "1" and s[x-1] == "0":
    s.reverse()
if s[0] == "0" and s[x-1] == "1":
    if 2*k >= len(lst):
        ans = x
    else:
        i = 1
        while i+2*k+1 <= len(lst)-1:
            ans = max(ans,lst[i+2*k+1]-lst[i])
            i += 2
        #ans = max(ans,lst[2*k])
if s[0] == "0" and s[x-1] == "0":
    if 2*k >= len(lst):
        ans = x
    else:
        i = 1
        while i+2*k+1 <= len(lst)-1:
            ans = max(ans,lst[i+2*k+1]-lst[i])
            i += 2
        #ans = max(ans,lst[2*k])
        #ans = max(ans,x-lst[len(lst)-2*k-1])
if s[0] == "1" and s[x-1] == "1":
    if 2*k+1 >= len(lst):
        ans = x
    else:
        i = 0
        while i+2*k+1 <= len(lst)-1:
            #ans = max(ans,lst[i+2*k+1]-lst[i])
            i += 2
print(ans)

Submission status: Runtime Error

Solution code:
n,k = map(int,input().split())
s = input()
x = len(s)
lst = [0]
ans = 0
for i in range(1,x):
    if s[i-1] != s[i]:
        lst.append(i)
lst.append(x)
if s[0] == "1" and s[x-1] == "0":
    s.reverse()
if s[0] == "0" and s[x-1] == "1":
    if 2*k >= len(lst):
        ans = x
    else:
        i = 1
        while i+2*k+1 <= len(lst)-1:
            ans = max(ans,lst[i+2*k+1]-lst[i])
            i += 2
        ans = max(ans,lst[2*k])
if s[0] == "0" and s[x-1] == "0":
    if 2*k >= len(lst):
        ans = x
    else:
        i = 1
        while i+2*k+1 <= len(lst)-1:
            ans = max(ans,lst[i+2*k+1]-lst[i])
            i += 2
        ans = max(ans,lst[2*k])
        ans = max(ans,x-lst[len(lst)-2*k-1])
if s[0] == "1" and s[x-1] == "1":
    if 2*k+1 >= len(lst):
        ans = x
    else:
        i = 0
        while i+2*k+1 <= len(lst)-1:
            ans = max(ans,lst[i+2*k+1]-lst[i])
            i += 2
print(ans)

Submission status: Runtime Error

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

now=S[0]
cnt=1
A=[] #個数配列
for i in range(n-1):
    if S[i+1]==now:
        cnt+=1
    else:
        A.append(cnt)
        cnt=1
        now=S[i+1]
if cnt>0:
    A.append(cnt)
l=len(A)
    
if S[0]=="0":
    if l<=2*k:
        ans=n
    elif 2*k+1<=l<=2*k+2:
        ans=max(sum(A[:2*k]),sum(A[1:]))
    else:
        ans=max(sum(A[:2*k]),sum(A[1:2*k+2]))
        cnt=ans
        for i in range((l-2*k-2)//2):
            cnt-=A[2*i+2]+A[2*i+3]
            cnt+=A[2*k+2*i+2]+A[2*k+2*i+3]
            ans=max(cnt,ans)
        if l%2!=0:
            ans=max(A[l-2*k:],ans)
else:
    if l<=2*k+1:
        ans=n
    else:
        ans=sum(A[:2*k+1])
        cnt=ans
        for i in range((l-2*k-1)//2):
            cnt-=A[2*i]+A[2*i+1]
            cnt+=A[2*k+2*i+1]+A[2*k+2*i+2]
            ans=max(cnt,ans)
        if l%2!=1:
            ans=max(A[l-2*k-1:],ans)
print(ans)

Submission status: Runtime Error

Solution code:
import sys
input = lambda: sys.stdin.readline().rstrip()
from itertools import accumulate


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

S = input()

if len(S) == 1:
    print(1)
    exit()

cum_sum = [0]

tmp_S = S

if S[-1] == '0':
    tmp_S += '1'
else:
    tmp_S += '0'

cnt = 1
for i in range(len(tmp_S)-1):
    if tmp_S[i] == tmp_S[i+1]:
        cnt += 1
    else:
        cum_sum.append(cnt)
        cnt = 1

cum_sum = list(accumulate(cum_sum))

ans = 0
ran = K+K
if S[0] == '0':
    for i in range(0, len(cum_sum), 2):
        if i+ran < len(cum_sum):
            ans = max(ans, cum_sum[i+ran] - cum_sum[i])
else:
    for i in range(0, len(cum_sum), 2):
        if i+ran < len(cum_sum):
            ans = max(ans, cum_sum[i+ran+1] - cum_sum[i])

print(ans)

Submission status: Runtime Error

Solution code:
import sys
input = lambda: sys.stdin.readline().rstrip()
from itertools import accumulate


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

S = input()

if len(S) == 1:
    print(1)
    exit()

cum_sum = [0]

tmp_S = S

if S[-1] == '0':
    tmp_S += '1'
else:
    tmp_S += '0'

cnt = 1
for i in range(len(tmp_S)-1):
    if tmp_S[i] == tmp_S[i+1]:
        cnt += 1
    else:
        cum_sum.append(cnt)
        cnt = 1

print(*cum_sum)
cum_sum = list(accumulate(cum_sum))
print(*cum_sum)

ans = 0
ran = K+K
if S[0] == '0':
    for i in range(0, len(cum_sum), 2):
        if i+ran < len(cum_sum):
            ans = max(ans, cum_sum[i+ran] - cum_sum[i])
else:
    for i in range(0, len(cum_sum), 2):
        if i+ran < len(cum_sum):
            ans = max(ans, cum_sum[i+ran+1] - cum_sum[i])

print(ans)

Submission status: Runtime Error

Solution code:
import sys
input = lambda: sys.stdin.readline().rstrip()
from itertools import accumulate


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

S = input()

if len(S) == 1:
    print('1')
    exit()

cum_sum = [0]

tmp_S = S

if S[-1] == '0':
    tmp_S += '1'
else:
    tmp_S += '0'

cnt = 1
for i in range(len(tmp_S)-1):
    if tmp_S[i] == tmp_S[i+1]:
        cnt += 1
    else:
        cum_sum.append(cnt)
        cnt = 1

print(*cum_sum)
cum_sum = list(accumulate(cum_sum))
print(*cum_sum)

ans = 0
ran = K+K
if S[0] == '0':
    for i in range(0, len(cum_sum), 2):
        if i+ran < len(cum_sum):
            ans = max(ans, cum_sum[i+ran] - cum_sum[i])
else:
    for i in range(0, len(cum_sum), 2):
        if i+ran < len(cum_sum):
            ans = max(ans, cum_sum[i+ran+1] - cum_sum[i])

print(ans)

Submission status: Runtime Error

Solution code:
from itertools import groupby
n, k = map(int, input().split())
S = input().strip()
L = [len(list(g)) for v, g in groupby(S)]
A = [sum(L[:2*k+1])]
for i in range(0, len(L)-2*k-1, 2):
    A += A[-1]-L[i]-L[i+1]+L[i+2*k+1]+L[i+2*k+2],
print(max(A))

Submission status: Runtime Error

Solution code:
import numpy as np
n,k = map(int,input().split())
s = list(input())
memory = s[0]
count = 1
lst = np.array([0])
M = 0
for i in range(1,n):
    if s[i] == memory:
        count += 1
    else:
        lst = np.append(lst,count)
        memory = s[i]
        count = 1
lst = np.append(lst,count)
cs = lst.cumsum()
if s[0] == '1':
    if 2*k+3 >= len(cs):
        print(cs[-1])
        exit()
    else:
        i = 0
        while i+2*k+2 <= len(cs):           
            M = max(M,cs[i+2*k+1]-cs[i])
            i += 2
        print(M)
        exit()
if s[0] == '0':
    if 2*k+2 >= len(cs):
        print(cs[-1])
        exit()
    else:
        i = 1
        while i+2*k+2 <= len(cs):
            M = max(M,cs[i+2*k+1]-cs[i])
            i += 2
        M = max(M,cs[2*k]-cs[0])
        print(M)
        exit()

Submission status: Runtime Error

Solution code:
import bisect
# from collections import Counter, defaultdict, deque, namedtuple
# import copy
# from heapq import heappush, heappop, heapify
# from fractions import gcd
import itertools
# from operator import attrgetter, itemgetter
# import math

import sys

# import numpy as np

ipti = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)

def main():
    n, k = list(map(int,ipti().split()))
    s = input()

    lg = [0] * (n+3)

    s_first = s[0]
    s_last =s[-1]

    idx = 0
    if s_first == "0":
        lg[0] = 0
        idx = 1

    s_groupby = itertools.groupby(s)

    for _, group in s_groupby:
        lg[idx] = len(list(group))
        idx += 1

    if s_last == "0":
        idx += 1

    rn = 2*k + 1
    if rn >= idx:
        print(n)
        sys.exit()

    temp = sum(lg[0:rn])
    ans = temp

    for i in range(2, idx, 2):
        temp -= (lg[i-2] + lg[i-1])
        temp += (lg[i-2+rn] + lg[i-1+rn])
        ans = max(ans, temp)

    print(ans)

if __name__ == '__main__':
    main()

Submission status: Runtime Error

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

count = 1
Nums_count = []
now = s[0]
start = 0
for i in range(1,n):
    if s[i] == str(now):
        count += 1
    else:
        Nums_count.append([start, count])
        now = s[i]
        start = i
        count = 1
    if i == n-1:
        Nums_count.append([start, count])
if len(s) == 1:
    Nums_count.append([0,1])

Nums_count_rui = [[-1, 0]]
for i in range(len(Nums_count)):
    Nums_count_rui.append([Nums_count[i][0],Nums_count[i][1]+Nums_count_rui[-1][1]])

# print(Nums_count_rui)
ans = 0
m = len(Nums_count_rui)

if s[0] == '0' and m <= 2*k:
    ans = n
elif s[0] == '1' and m <= 2*k+1:
    ans = n

for i in range(1,m+1):
    if s[Nums_count_rui[i][0]] == '0':
        if i+2*k-1 >= m:
            tot = Nums_count_rui[-1][1] - Nums_count_rui[i-1][1]
        else:
            tot = Nums_count_rui[i+2*k-1][1] - Nums_count_rui[i-1][1]
    else:
        if i+2*k >= m:
            tot = Nums_count_rui[-1][1] - Nums_count_rui[i-1][1]
        else:
            tot = Nums_count_rui[i+2*k][1] - Nums_count_rui[i-1][1]
    ans = max(tot, ans)
print(ans)

Submission status: Runtime Error

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

count = 1
Nums_count = []
now = s[0]
for i in range(1,n):
    if s[i] == str(now):
        count += 1
    else:
        now = s[i]
        Nums_count.append(count)
        count = 1
    if i == n-1:
        Nums_count.append(count)
if len(s) == 1:
    Nums_count.append(1)

Nums_count_rui = [0]
for i in range(len(Nums_count)):
    Nums_count_rui.append(Nums_count[i]+Nums_count_rui[-1])

ans = 0
m = len(Nums_count_rui)

if s[0] == '0' and m <= 2*k:
    ans = n
elif s[0] == '1' and m <= 2*k+1:
    ans = n

for i in range(n-2*k-1):
    if s[i] == '0':
        if i+2*k >= m:
            tot = Nums_count_rui[-1] - Nums_count_rui[i]
        else:
            tot = Nums_count_rui[i+2*k] - Nums_count_rui[i]
    elif s[i] == '1':
        if i+2*k+1 >= m:
            tot = Nums_count_rui[-1] - Nums_count_rui[i]
        else:
            tot = Nums_count_rui[i+2*k+1] - Nums_count_rui[i]
    ans = max(tot, ans)
print(ans)

Submission status: Runtime Error

Solution code:
S = input()

n = len(S)
cnt = 0

for i in range(n):
  if int(S[i])%2 == i%2:
    cnt += 1
    
print(cnt)

Submission status: Runtime Error

Solution code:
#!/usr/bin/python3
# -*- coding:utf-8 -*-

def main():
  n, k = map(int, input().split())
  s = input().strip()
  sz, ez = [], []
  i = 0
  while i < n:
    if s[i] == '0':
      sz.append(i)
      while i < n and s[i] == '0': i += 1
      ez.append(i)
    else:
      i += 1
  
  if k >= len(sz):
    print(len(s))
    return 
  max = -1
  for i in range(k+1):
    if i==0:
      score = sz[i+k] - 0
    elif i==k:
      score = n - ez[i-1]
    else:
      score = sz[i+k] - ez[i-1]
    if max < score:
      max = score
  print(max)
      

if __name__=='__main__':
  main()

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
S = input()
l = [1]
for i in range(N-1):
  if S[i] == S[i+1]:
    l[-1] += 1
  else:
    l.append(1)
  if S[0] == 0:
    l = [0] + l
  if S[-1] == 0:
    l += [0]
#print(l)
n = len(l)
ans = sum(l[:min(K*2+1, n+1)])
i = 0
M = ans
while True:
  if K*2 + i+1 >= n:
    break
  M = M + l[K*2+1 + i] + l[K*2+1 + i+1] - l[i] -l[i+1]
  #print(ans, M)
  ans = max(ans, M)
  i += 2
print(ans)

Submission status: Runtime Error

Solution code:
from collections import deque
N,K=map(int,input().split())
S=input()
C=[]
prv=1
cnt=0
for c in S:
    if prv != c:
        C.append(cnt)
        cnt=1
    else:
        cnt+=1
    prv=c
if cnt>0:
    C.append(cnt)
if prv==0:
    C.append(0)
M=len(C)//2
que=deque([C[0]])
res=ans=C[0]
for i in range(M):
    if len(que)==2*K+1:
        res-=que.popleft()+que.popleft()
    que.append(C[2*i+1])
    que.append(C[2*i+2])
    res+=C[2*i+1]+C[2*i+2]
    ans=max(res,ans)
print(ans)

Submission status: Runtime Error

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

T='0'+S+'0'

ans,cnt=0,0
for i,(a,b) in enumerate(zip(S,S[1:])):
  if a=='0' and b=='1':
    cnt+=1
  elif cnt==k and a=='1' and b=='0':
    ans=max(ans,i+1)
    break
else:
  print(n)
  exit()
  
S,cnt=[::-1],0
for i,(a,b) in enumerate(zip(S,S[1:])):
  if a=='0' and b=='1':
    cnt+=1
  elif cnt==k and a=='1' and b=='0':
    ans=max(ans,i+1)
    break
else:
  print(n)
  exit()
  
x,y=[],[]
for i,(a,b) in enumerate(zip(T,T[1:])):
  if a==b: continue
  if a=='0':
    x.append(i)
  else:
    y.append(i)
for a,b in zip(x,y[k:]):
  ans=max(ans,b-a)
print(ans)

Submission status: Runtime Error

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

def runLength(s):
  if len(s) == 0: return []
  if len(s) == 1: return [(s, 1)]
  res = []
  lc = 1
  for i in range(1, len(s)):
    if s[i-1] == s[i]:
      lc += 1
    else:
      res.append((s[i-1], lc))
      lc = 1
  res.append((s[i], lc))
  return res

S = runLength(input())
accsum = [0] * (1 + (S[0][0] == "0"))
for i in range(len(S)):
  accsum.append(accsum[-1] + S[i][1])
if S[-1][0] == "0":
  accsum.append(accsum[-1])
print(max([accsum[i] - accsum[i-2*K-1] for i in range(2*K+1, len(accsum), 2)]) if N >= 2*K else N)

Submission status: Runtime Error

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

parity = []
runlength = []

prev = S[0]
run = 0
for c in S:
    if c != prev:
        parity.append(bool(int(prev)))
        runlength.append(run)
        run = 0
    prev = c
    run += 1
parity.append(bool(int(prev)))
runlength.append(run)

if parity.count(False) <= K:
    print(N)
    quit()

argmax = 0
maximum = 0

L = len(parity)
for i in range(L):
    total = sum(runlength[max(0, i - 1): min(L, i + 2)])
    if not parity[i] and total > maximum:
        argmax = i
        maximum = total

while True:
    parity[argmax] = True
    runlength[argmax] = maximum
    del parity[argmax + 1]
    del parity[argmax - 1]
    del runlength[argmax + 1]
    del runlength[argmax - 1]

    K -= 1
    if not K:
        print(maximum)
        break

    L = len(parity)
    maximum = 0
    for i in [argmax - 2, argmax]:
        total = sum(runlength[max(0, i - 1): min(L, i + 2)])
        if total > maximum:
            argmax = i
            maximum = total

Submission status: Runtime Error

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

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

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

if k<=n/2:
  ans=sum(num_cnt[:2*k])

  tmp=len(num_cnt)
  if tmp%2==0:
    limit=tmp//2-k
  else:
    num_cnt.append(0)
    limit=tmp//2-k+1
  
  #print(ans)
  #print(num_cnt)
  tmp=num_cnt[2*k]+num_cnt[2*k+1]-num_cnt[0]
  if tmp>0:
    ans+=tmp
  
  for i in range(1,limit):
    tmp=num_cnt[2*(i+k)]+num_cnt[2*(i+k)+1]-num_cnt[2*i]-num_cnt[2*i+1]
    #print(tmp)
    if tmp>0:
      ans+=tmp
else:
  ans=n
print(ans)

Submission status: Runtime Error

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

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

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

if k<=n/2:
  ans=sum(num_cnt[:2*k])

  tmp=len(num_cnt)
  if tmp%2==0:
    limit=tmp//2-k-1
  else:
    num_cnt.append(0)
    limit=tmp//2-k
  
  #print(ans)
  #print(num_cnt)
  if k/2<=n:
    tmp=num_cnt[2*k]+num_cnt[2*k+1]-num_cnt[0]
    if tmp>0:
      ans+=tmp
  
  for i in range(1,limit):
    tmp=num_cnt[2*(i+k)]+num_cnt[2*(i+k)+1]-num_cnt[2*i]-num_cnt[2*i+1]
    #print(tmp)
    if tmp>0:
      ans+=tmp
else:
  ans=n
print(ans)

Submission status: Runtime Error

Solution code:
import sys, bisect, math, itertools, string, queue, copy
# import numpy as np
# import scipy
from collections import Counter,defaultdict,deque
from itertools import permutations, combinations
from heapq import heappop, heappush
# input = sys.stdin.readline
sys.setrecursionlimit(10**8)
mod = 10**9+7
def inp(): return int(input()) # n=1
def inpm(): return map(int,input().split()) # x=1,y=2
def inpl(): return list(map(int, input().split())) # a=[1,2,3,4,5,...,n]
def inpls(): return list(input().split())  # a=['1','2','3',...,'n']
def inplm(n): return list(int(input()) for _ in range(n)) # x=[] 複数行
def inplL(n): return [list(input()) for _ in range(n)]
def inplT(n): return [tuple(input()) for _ in range(n)]
def inpll(n): return [list(map(int, input().split())) for _ in range(n)] # [[1,1,1,1],[2,2,2,2],[3,3,3,3]] 
def inplls(n): return sorted([list(map(int, input().split())) for _ in range(n)]) # [[1,1,1,1],[2,2,2,2],[3,3,3,3]] 
def graph():
    n=inp()
    g=[[] for _ in range(n)]
    for i in range(n):
        a = inp()
        a -= 1
        g[i].append(a)
        g[a].append(i)
    return n,g

def main():
    n,k = inpm()
    s = list(input())
    t = [0]
    for i in range(n-1):
        if s[i] != s[i+1]:
            t.append(i+1)
    a = len(t)
    for i in range(2*a+4):
        t.append(n)
    ans = 0
    if s[0]=='1':
        for i in range(a):
            if 2*k+2*i+1 >= len(t):
                break
            ans = max(ans,t[2*k+2*i+1]-t[2*i])
    else:
        ans = t[2*k]
        for i in range(a):
            if 2*k+2*i+2 >= len(t):
                break
            ans = max(ans,t[2*k+2*i+2]-t[2*i+1])
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
import sys, bisect, math, itertools, string, queue, copy
# import numpy as np
# import scipy
from collections import Counter,defaultdict,deque
from itertools import permutations, combinations
from heapq import heappop, heappush
# input = sys.stdin.readline
sys.setrecursionlimit(10**8)
mod = 10**9+7
def inp(): return int(input()) # n=1
def inpm(): return map(int,input().split()) # x=1,y=2
def inpl(): return list(map(int, input().split())) # a=[1,2,3,4,5,...,n]
def inpls(): return list(input().split())  # a=['1','2','3',...,'n']
def inplm(n): return list(int(input()) for _ in range(n)) # x=[] 複数行
def inplL(n): return [list(input()) for _ in range(n)]
def inplT(n): return [tuple(input()) for _ in range(n)]
def inpll(n): return [list(map(int, input().split())) for _ in range(n)] # [[1,1,1,1],[2,2,2,2],[3,3,3,3]] 
def inplls(n): return sorted([list(map(int, input().split())) for _ in range(n)]) # [[1,1,1,1],[2,2,2,2],[3,3,3,3]] 
def graph():
    n=inp()
    g=[[] for _ in range(n)]
    for i in range(n):
        a = inp()
        a -= 1
        g[i].append(a)
        g[a].append(i)
    return n,g

def main():
    n,k = inpm()
    s = list(input())
    t = [0]
    for i in range(n-1):
        if s[i] != s[i+1]:
            t.append(i+1)
    a = len(t)
    for i in range(2*a+4):
        t.append(n)
    ans = 0
    if s[0]=='1':
        for i in range(a):
            ans = max(ans,t[2*k+2*i+1]-t[2*i])
    else:
        ans = t[2*k]
        for i in range(a):
            ans = max(ans,t[2*k+2*i+2]-t[2*i+1])
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
# ABC124 D - Handstand
from math import ceil
n,k=map(int,input().split())
s=input()

l=[]
tmp=''
t_cnt=0
for i,ss in enumerate(s):
    if i==0:
        tmp=ss
        t_cnt+=1
        if len(s)==1:
            l.append(t_cnt)
            break
        continue
    if i==n-1:
        if ss==tmp:
            t_cnt+=1
            l.append(t_cnt)
        else:
            t_cnt=1
            l.append(t_cnt)
        continue
    if ss==tmp:
        t_cnt+=1
    else:
        l.append(t_cnt)
        t_cnt=1
        tmp=ss

# print(l)
l+=[0]
ans=0
if s[0]=='0':
    for i in range(0,ceil(n/2),2):
        if i==0:
            ans=sum(l[:2*k-1])
            continue
        ans=max(ans,ans+l[i+1]-l[i-1])
else:
    for i in range(1,ceil(n/2)+1,2):
        if i==1:
            ans=sum(l[:2*k+1])
            continue
        ans=max(ans,ans+l[i+1]-l[i-1])
    
print(ans)

Submission status: Runtime Error

Solution code:
N,K=map(int,input().split())
S=input()
if N==1:
    print(1)
    exit()

lst0=[]

lst1=[]
if S[0]!="1":
    lst1.append([0,0])
flag=0
for i in range(N):
    if flag==0 and S[i]=="1":
        flag=1
        st=i
    elif flag and S[i]=="0":
        lst1.append([st,i-1])
        flag=0
if S[-1]!="1":
    lst1.append([N-1,N-1])

if flag:
    lst1.append([st,N-1])
flag=0
for i in range(N):
    if flag==0 and S[i]=="0":
        flag=1
        st=i
    elif flag and S[i]=="1":
        lst0.append([st,i-1])
        flag=0
if flag:
    lst0.append([st,N-1])

if K>=len(lst1):
    print(N)
else:
    value=0
    for i in range(len(lst1)-K):
        a=lst1[i+K][1]-lst1[i][0] +1
        if a>value:
            value=a
print(value)

Submission status: Runtime Error

Solution code:
from itertools import groupby
from numpy import cumsum


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

if len(set(S)) == 1:
    print(N)
    exit()

# ランレングス圧縮
G = []
for k, v in groupby(S):
    G.append(len(list(v)))
# 1 0 1 0 1...の配列が欲しいので、
# 先頭、末尾が0ならダミーの要素をそれぞれ追加する
if S[0] == '0':
    G.insert(0, 0)
if S[-1] == '0':
    G.append(0)

cum = [0]
cum.extend(list(cumsum(G)))
cum.append(0)

ans = 0
gc = len(G)
w = min(gc, 3+(K-1)*2)
for i in range(0, gc, 2):
    ans = max(ans, cum[i+w]-cum[i])
print(ans)

Submission status: Runtime Error

Solution code:
from itertools import groupby
from numpy import cumsum


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

if len(set(S)) == 1:
    print(N)
    exit()

# ランレングス圧縮
G = []
for k, v in groupby(S):
    G.append(len(list(v)))
# 1 0 1 0 1...の配列が欲しいので、
# 先頭、末尾が0ならダミーの要素をそれぞれ追加する
if S[0] == '0':
    G.insert(0, 0)
if S[-1] == '0':
    G.append(0)

cum = [0]
cum.extend(list(cumsum(G)))
cum.append(0)

ans = 0
gc = len(G)
w = min(gc, 3+(K-1)*2)
for i in range(0, w, 2):
    ans = max(ans, cum[i+w]-cum[i])
print(ans)

Submission status: Runtime Error

Solution code:
from sys import exit
def groupby(a):
    a2=[[a[0],1]]
    for i in range(1,len(a)):
        if a2[-1][0]==a[i]:
            a2[-1][1]+=1
        else:
            a2.append([a[i],1])
    return a2

n,k=map(int,input().split())
s=list(input())
x=groupby(s)
l=len(x)
y=[x[i][1] for i in range(l)]

if x[0][0]=="0":
    su=sum(y[:2*k])
    ans=su
    y.pop(0)
    l-=1
else:
    ans=0
su=sum(y[:2*k+1])
ans=max(ans,su)
l_sub=(l-(2*k+1))//2
if l_sub<=0:
    print(ans)
    exit()
for i in range(l_sub):
    su-=y[2*i]
    su-=y[2*i+1]
    su+=y[2*k+1+(2*i)]
    su+=y[2*k+1+(2*i+1)]
    ans=max(su,ans)
if 2*k+1+2*l_sub<l:
    r
    su-=y[2*l_sub]
    su+=y[2*k+1+2*l_sub]
    ans=max(su,ans)
print(ans)

Submission status: Runtime Error

Solution code:
from sys import exit
def groupby(a):
    a2=[[a[0],1]]
    for i in range(1,len(a)):
        if a2[-1][0]==a[i]:
            a2[-1][1]+=1
        else:
            a2.append([a[i],1])
    return a2

n,k=map(int,input().split())
s=list(input())
x=groupby(s)
l=len(x)
y=[x[i][1] for i in range(l)]

if x[0][0]=="0":
    r
    su=sum(y[:2*k])
    ans=su
    y.pop(0)
    l-=1
else:
    ans=0
su=sum(y[:2*k+1])
ans=max(ans,su)
l_sub=(l-(2*k+1))//2
if l_sub<=0:
    print(ans)
    exit()
for i in range(l_sub):
    su-=y[2*i]
    su-=y[2*i+1]
    su+=y[2*k+1+(2*i)]
    su+=y[2*k+1+(2*i+1)]
    ans=max(su,ans)
if 2*k+1+2*l_sub<l:
    su-=y[2*l_sub]
    su+=y[2*k+1+2*l_sub]
    ans=max(su,ans)
print(ans)