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
import numpy as np
import heapq

reatInt = lambda: int(sys.stdin.readline())
reatIntN = lambda: [int(v) for v in sys.stdin.readline().split(' ')]


n, k = reatIntN()
ss = sys.stdin.readline().strip()


class Node:
    def __init__(self, p, n, v):
        self.prev = p
        self.next = n
        self.value = v


r = Node(None, None, None)
r.next = r
r.prev = r

def append(n, value):
    nn = Node(n, n.next, value)
    n.next.prev = nn
    n.next = nn

def remove(n):
    pn = n.prev
    nn = n.next
    nn.prev = pn
    pn.next = nn
    n.next = None
    n.prev = None
    n.value = None


i = 0
while i < len(ss):
    b = i
    c = ss[i]
    while (i < len(ss)) and (c == ss[i]):
        i += 1
    append(r, (int(c), b, i))


def _calc(p):
    d = 0
    if (p is not r) and (p.value[0] == 0):
        lh = p.value[1]
        rh = p.value[2]
        if p.next is not r:
            lh = min(lh, p.next.value[1])
        if p.prev is not r:
            rh = max(rh, p.prev.value[2])
        d = rh - lh
    return d
 


p = r.next
pq = []
while p is not r:
    d = _calc(p)
    if 0 < d:
        heapq.heappush(pq, (-d, p.value[2], p))
    p = p.next


for i in range(k):
    if len(pq) == 0:
        break
    _, _, p = heapq.heappop(pq)
    if (p.value is not None) and (p.value[0] == 0):
        lh = p.value[1]
        rh = p.value[2]

        if p.next is not r:
            lh = min(lh, p.next.value[1])
            remove(p.next)
        if p.prev is not r:
            rh = max(rh, p.prev.value[2])
            remove(p.prev)
        p.value = (1, lh, rh)

        dp = _calc(p.prev)
        if 0 <= dp:
            heapq.heappush(pq, (-dp, p.prev.value[2], p.prev))
        dn = _calc(p.next)
        if 0 <= dn:
            heapq.heappush(pq, (-dn, p.next.value[2], p.next))

ret = 0
p = r.next
while p is not r:
    if p.value[0] == 1:
        ret = max(ret, p.value[2] - p.value[1])
    p = p.next
print(ret)

Submission status: Runtime Error

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

import numpy as np
line = np.zeros([2*n,],dtype=np.int)
counter1 = 0
counter0 = 0
ind = 0
for i in range(n):
    c = int(s[i])
    if ind%2 ==0:
        if c == 1:
            counter1 += 1
        else:
            counter0 += 1
            if ind == 0:
                line[ind] = counter1
            else:
                line[ind] = line[ind-1]+counter1
            counter1 = 0
            ind+=1
    elif ind%2 ==1:
        if c == 0:
            counter0 += 1
        else:
            counter1 += 1
            line[ind] = line[ind-1] + counter0
            counter0 = 0
            ind+=1

if ind%2 ==0:
        if ind == 0:
                line[ind] = counter1
        else:
                line[ind] = line[ind-1]+counter1
        ind+=1
elif ind%2 ==1:
        line[ind] = line[ind-1] + counter0
        ind+=1
        

line[1:ind+1]  = line[:ind]   
line[0] = 0 
line = line[:ind+1]
#print(line)

if (len(line)//2)<=k:
    print(n)
else:
    maxchain = 0
    for i in range(0,len(line)-(2*k+1)+1,2):
        chain = line[i+2*k+1]-line[i]
        #print(chain)
        if chain>maxchain:
            maxchain=chain
    print(maxchain)

Submission status: Runtime Error

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

pos = 0
i_s = [pos]
continue_s = S[0]
while True:
    pos += 1
    if pos == N :
        break
    s = S[pos]
    if continue_s == s:
        continue
    else:
        continue_s = s
        i_s.append(pos)


i_length = len(i_s) - 1
X = []
rev = K * 2
for n,i in enumerate(i_s):
    if S[i] == "1":
        if i_length - n >= rev-1:
            if i_length - n == rev-1:
                X.append(N-i)
            elif i_length - n == rev:
                X.append(N-i)
            else:
                X.append(i_s[n+rev+1]-i)

print(max(X))

Submission status: Runtime Error

Solution code:
#https://atcoder.jp/contests/abc124/tasks/abc124_d
#TLE

NK = [int(i) for i in input().split()]
Sarray = [int(x) * 2 - 1 for x in list(str(input()))]
N = NK[0]
K = NK[1]
'''
Sarray = list(S)

for i in range(len(Sarray)):
    Sarray[i] = int(Sarray[i])
    if Sarray[i] == 0:
        Sarray[i] = -1


for i in range(len(Sarray)):
    Sarray[i] = Sarray[i] * 2 - 1
'''

mode = Sarray[0]
a = [0]

for i in range(len(Sarray)):
    c = (len(a) - 1) % 2
    b = (-1)** c * mode
    if Sarray[i] != b:
        a.append(0)
    a[len(a) - 1] += 1

b = []
for i in range(0,len(a) - 1,2):
    b.append(a[i] + a[i + 1])
if len(a) % 2 == 1:
    b.append(a[len(a) - 1])

#print(b)

c = []
for i in range(0,len(b) - K + 1):
    d = 0
    for j in range(i,i + K):
        #print(i,j,b[j])
        d += b[j]
    if i == len(b) - K and len(a) % 2 == 1 and len(b) > 1:
        d += b[len(b) - 1]
    c.append(d)

print(max(c))
'''
adjust = 0
if mode == 1:
    adjust = 1

tmp = 0
count = []

for l in range(adjust,len(a) + 1,2):
    u = l
    v = l + 2 * K - 1
    if l > 0:
        u = l - 1
    if max(a[u,v]) * 
    for i in range(u, v + 1):
        if i < len(a):
            tmp += a[i]
        else:
            break

    count.append(tmp)
    tmp = 0

print(max(count))
'''

Submission status: Runtime Error

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

## 連続するものをlistとして保持する
import re

candidates = []
candidates_idx = []
for j in range(N-1):
  if S[j] != S[j+1]:
    candidates_idx.append(j)
for u in range(len(candidates_idx)):
  if u == 0:
    candidates.append(S[:candidates_idx[u]+1])
  else:
    candidates.append(S[candidates_idx[u-1]+1:candidates_idx[u]+1])
candidates.append(S[candidates_idx[u]+1:])

#print(candidates)
## いい具合にnumpyで操作
import numpy as np
count_0 = np.zeros((len(candidates)))
count_1 = np.zeros((len(candidates)))

for i,candidate in enumerate(candidates):
  if int(candidate[0]) == 0:
    count_0[i] += len(candidate)
  else:
    count_1[i] += len(candidate)
    
## 最小の試行回数
min_Try = len(np.where(count_0 != 0)[0])

change_ = np.where(count_0 != 0)[0]
## 最小の試行回数以下の時どうするかが厳しいな...
## 連続しないので、とりあえず大きい順に入れていけばオッケー
## 個数分だけ全部足して、indexで見たときに一番大きいやつが正解！！
if min_Try <= K :
  print(N)
else:
  count_ = count_0 + count_1
  #print(count_)
  change_list = []
  for k in range(min_Try-K+1):
    c__ = change_[k:k+K]
    s = max(0,c__[0]-1)
    e = min(len(candidates)-1,c__[-1]+1)
    #print(count_[s:e+1])
    change_list.append(np.sum(count_[s:e+1]))
  print(int(max(change_list)))

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
S = list(input())
S = [int(S[i]) for i in range(N)]

def return_ans(S_initial, S):
    ans = 0
    block_size_list = []
    tmp_block_size = 1
    for i in range(1, N):
        if S[i - 1] == S[i]:
            tmp_block_size += 1

        else:
            block_size_list.append(tmp_block_size)
            tmp_block_size = 1
        if i == N - 1:
            block_size_list.append(tmp_block_size)

    block_size_list.append(0)

    times = len(block_size_list) - K * 2

    block_size_list.append(0)
    if K * 2 >= len(block_size_list):
        ans = len(S)
    else:
        if S_initial == 0:
            ans = max([sum(block_size_list[i:i + K * 2 + i % 2]) for i in range(times - 1)])

        else:
            ans = max([sum(block_size_list[i:i + K * 2 + (i + 1) % 2]) for i in range(times)])

    return ans

print(return_ans(S[0], S))

Submission status: Runtime Error

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

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

print(ans)

Submission status: Runtime Error

Solution code:
def main():
  from itertools import groupby

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

  num = []
  cot = []
  for k, n in groupby(S):
    num.append(k)
    cot.append(len(list(n)))

  if num[-1] == "0":
    cot += [0]

  X = [0]*(len(cot)+1)
  for i in range(len(cot)):
    X[i+1] = X[i]+cot[i]

  ans = 0
  for i in range(len(X)):
    x = K*2
    if num[i] == "1":
      x += 1
    try:
      ans = max(ans, X[i+x]-X[i])
    except:
      break
  if ans == 0:
    ans = len(S)
  print(ans)

if __name__ == '__main__':
  main()

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
S = list(input())
S = [int(S[i]) for i in range(N)]

def return_ans(S_initial, S):
    ans = 0
    block_size_list = []
    tmp_block_size = 1
    for i in range(1, N):
        if S[i - 1] == S[i]:
            tmp_block_size += 1

        else:
            block_size_list.append(tmp_block_size)
            tmp_block_size = 1
        if i == N - 1:
            block_size_list.append(tmp_block_size)

    block_size_list.append(0)

    times = len(block_size_list) - K * 2

    block_size_list.append(0)
    if K * 2 >= len(block_size_list):
        ans = len(S)
    else:
        if S_initial == 0:
            ans = max([sum(block_size_list[i:i + K * 2 + i % 2]) for i in range(times)])

        else:
            ans = max([sum(block_size_list[i:i + K * 2 + (i + 1) % 2]) for i in range(times)])

    return ans

print(return_ans(S[0], S))

Submission status: Runtime Error

Solution code:
a[i]=1
    flag = 1
  else:
    flag = 0
  a[i]+=a[i-1]

r=0
ret=0
count=0
for l in range(N):
  while 1:
    r=max(l+1,r)
    if r==N:
      break
    if l==0:
      count = a[r]
    else:
      count = a[r]-a[l-1]
    if count <=K:
      ret=max(ret,r-l+1)
      r+=1
    else:
      break
print(ret)

Submission status: Runtime Error

Solution code:
import sys
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7

N, K = LIST()
S = input()

# stand = []
# reverse = []
# state = None
# count = 0
# tmp = None
# for i in S:
# 	if i == "0":
# 		state = "stand"
# 	else:
# 		state = "reverse"
# 	if tmp == "stand" and state == "reverse":
# 		stand.append(count)
# 		count = 1
# 	elif tmp == "reverse" and state == "stand":
# 		reverse.append(count)
# 		count = 1
# 	else: # 状態が前と同じ
# 		count += 1
# 	tmp = state
# if state == "stand":
# 	stand.append(count)
# else:
# 	reverse.append(count)

# if "0" not in S:
# 	print(len(S))
# else:
# 	if K >= len(stand): # 全員逆立ちにできる
# 		print(len(S))
# 	else:
# 		if len(stand) > len(reverse):
# 			reverse.append(0)
# 			reverse.insert(0, 0)
# 		if len(stand) == len(reverse) and S[0] == "0":
# 			reverse.insert(0, 0)
# 		if len(stand) == len(reverse) and S[0] == "1":
# 			reverse.append(0)
# 		# print(stand)
# 		# print(reverse)
# 		max_ = 0
# 		for i in range(max(1, 1+len(reverse)-(K+1))):
# 			# print(reverse[i:i+K+1],stand[i:i+K])
# 			tmp = sum(reverse[i:i+K+1]+stand[i:i+K])
# 			if tmp > max_:
# 				max_ = tmp
# 		print(max_)

indexes = []
tmp = ""
for i in range(len(S)):
	if S[i] == "0":
		state = "stand"
	else:
		state = "reverse"
	if tmp != state:
		indexes.append(i)
	else: # 状態が前と同じ
		pass
	tmp = state
indexes.append(N)
ans = []
# print(indexes)
for i in range(len(indexes)):
	# print(i)
	if S[indexes[i]] == "0":
		if i+2*K >= len(indexes):
			continue
		ans.append(indexes[i+2*K]-indexes[i])
	else:
		if i+2*K+1 >= len(indexes):
			continue
		ans.append(indexes[i+2*K+1]-indexes[i])
if ans == []:
	print(len(S))
else:
	print(max(ans))

Submission status: Runtime Error

Solution code:
import sys
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7

N, K = LIST()
S = input()

# stand = []
# reverse = []
# state = None
# count = 0
# tmp = None
# for i in S:
# 	if i == "0":
# 		state = "stand"
# 	else:
# 		state = "reverse"
# 	if tmp == "stand" and state == "reverse":
# 		stand.append(count)
# 		count = 1
# 	elif tmp == "reverse" and state == "stand":
# 		reverse.append(count)
# 		count = 1
# 	else: # 状態が前と同じ
# 		count += 1
# 	tmp = state
# if state == "stand":
# 	stand.append(count)
# else:
# 	reverse.append(count)

# if "0" not in S:
# 	print(len(S))
# else:
# 	if K >= len(stand): # 全員逆立ちにできる
# 		print(len(S))
# 	else:
# 		if len(stand) > len(reverse):
# 			reverse.append(0)
# 			reverse.insert(0, 0)
# 		if len(stand) == len(reverse) and S[0] == "0":
# 			reverse.insert(0, 0)
# 		if len(stand) == len(reverse) and S[0] == "1":
# 			reverse.append(0)
# 		# print(stand)
# 		# print(reverse)
# 		max_ = 0
# 		for i in range(max(1, 1+len(reverse)-(K+1))):
# 			# print(reverse[i:i+K+1],stand[i:i+K])
# 			tmp = sum(reverse[i:i+K+1]+stand[i:i+K])
# 			if tmp > max_:
# 				max_ = tmp
# 		print(max_)

indexes = []
tmp = ""
for i in range(len(S)):
	if S[i] == "0":
		state = "stand"
	else:
		state = "reverse"
	if tmp != state:
		indexes.append(i)
	else: # 状態が前と同じ
		pass
	tmp = state
indexes.append(N)
ans = []
# print(indexes)
for i in range(len(indexes)):
	# print(i)
	if S[indexes[i]] == "0":
		if i+2*K >= len(indexes):
			pass
		ans.append(indexes[i+2*K]-indexes[i])
	else:
		if i+2*K+1 >= len(indexes):
			pass
		ans.append(indexes[i+2*K+1]-indexes[i])
if ans == []:
	print(len(S))
else:
	print(max(ans))

Submission status: Runtime Error

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

l = list(map(int,input()))
ll = []
p = l[0]
co = 1
ppp = 0
su = 0
lll = []
for i in range(n-1):
    if p == l[i+1]:
        co += 1
    else:
        if p == 0:
            ppp += 1
        ll.append([p,co])
        p = l[i+1]
        co = 1
if p == 0:
    ppp += 1
ll.append([p,co])
co = 0
if ll[0][0] == 0:
    ll.insert(0,[1,0])
if ll[−1][0] == 0:
    ll.append([1,0])
if len(ll) == 1:
    print(n)
    exit(0)
if ppp <= k:
    print(n)
    exit(0)
co = 0
for i in range(2*k+1):
    co += ll[i][1]
lll.append(co)
co -= ll[0][1] + ll[1][1]
for i in range(ppp-k):
    if i == 0:
        co += ll[i*2+1+2*k][1] + ll[i*2+2+2*k][1]
        lll.append(co)
    else:
        co += ll[i*2+1+2*k][1] + ll[i*2+2+2*k][1] - (ll[i*2][1] + ll[i*2+1][1])
        lll.append(co)
print(max(lll))

Submission status: Runtime Error

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

l = list(map(int,input()))
ll = []
p = l[0]
co = 1
ppp = 0
su = 0
lll = []
for i in range(n-1):
    if p == l[i+1]:
        co += 1
    else:
        if p == 0:
            ppp += 1
        ll.append([p,co])
        p = l[i+1]
        co = 1
if p == 0:
    ppp += 1
ll.append([p,co])
co = 0
if ll[0][0] == 0:
    ll.insert(0,[1,0])
if ll[−1][0] == 0:
    ll.insert(0,[1,0])
if len(ll) == 1:
    print(n)
    exit(0)
if ppp <= k:
    print(n)
    exit(0)
co = 0
for i in range(2*k+1):
    co += ll[i][1]
lll.append(co)
co -= ll[0][1] + ll[1][1]
for i in range(ppp-k):
    if i == 0:
        co += ll[i*2+1+2*k][1] + ll[i*2+2+2*k][1]
        lll.append(co)
    else:
        co += ll[i*2+1+2*k][1] + ll[i*2+2+2*k][1] - (ll[i*2][1] + ll[i*2+1][1])
        lll.append(co)
print(max(lll))

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
s=input()+"2"
counter=1
s2=[]
s3=[]
for i in range(n):
    if s[i]==s[i+1]:
        counter+=1
    else:
        s2.append(counter)
        counter=1
if s[0]=="0":
    s2.insert(0,0)
if s[-2]=="0":
    s2.append(0)
z=int((len(s2)-1)/2)
t=len(s2)-2*k
for i in range(t):
    s3.append(sum(s2[i:i+2*k+1]))
print(max(s3[0::2]))

Submission status: Runtime Error

Solution code:
import bisect;n,k=map(int,input().split());s=input();f=[0]*n;f[0]=1-int(s[0])
for i in range(1,n):f[i]=(1-int(s[i]))*int(s[i-1])
for i in range(n-1):f[i+1]+=f[i];a=0
for l in range(n):r=bisect.bisect_right(f,k+f[l]*int(s[l]));a=max(a,r-l)
print(a)

Submission status: Runtime Error

Solution code:

from itertools import groupby
import sys
stdin = sys.stdin
 
sys.setrecursionlimit(10**5) 
 
def ii(): return int(stdin.readline())
def li(): return map(int, stdin.readline().split())
N, K = tuple(li())
S = stdin.readline()
# use window
l, r = 0, 0
cnt = 0
r_iter = groupby(S)
l_iter = groupby(S)
r_next = next(r_iter)
l_next = next(l_iter)
while r < N:
    # print(l, r)
    if K > 0:
        if r_next[0] == '1':
            r += len(list(r_next[1]))
            r_next = next(r_iter)
            cnt = max(cnt, r - l)
            K -= 1
        elif r_next[0] == "0":
            pass
        r += len(list(r_next[1]))
        r_next = next(r_iter)
    elif K == 0 and r_next[0] == "1":
        r += len(list(r_next[1]))
        r_next = next(r_iter)
    else:
        if l_next[0] == "0":
            K += 1
        l += len(list(l_next[1]))
        l_next = next(l_iter)
if K > 0:
    cnt = max(cnt, r-l)
print(cnt)

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
s=input()+"2"
counter=1
s2=[]
s3=[]
for i in range(n):
    if s[i]==s[i+1]:
        counter+=1
    else:
        s2.append(counter)
        counter=1
if s[0]=="0":
    s2.insert(0,0)
if s[-2]=="0":
    s2.append(0)
z=int((len(s2)-1)/2)
t=len(s2)-2*k
for i in range(t):
    s3.append(sum(s2[i:i+2*k+1]))
print(max(s3))

Submission status: Runtime Error

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

s = input()

a = []
flag = True
count = 0
for i in range(len(s) - 1):
    if i == 0 and s[i] == '1':
        a.append(0)

    if flag:
        if s[i] == s[i+1]:
            count += 1
        else:
            a.append(count+1)
            flag = False
            count = 0
    else:
        if s[i] == s[i+1]:
            count += 1
        else:
            a.append(count+1)
            flag = True
            count = 0

a.append(count+1)

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

m = max(sum(a[:k*2]), sum(a[-1:-k*2-1:-1]))

sum_ = a[1]

for i in range(3, len(a), 2):
    sum_ += a[i-1] + a[i]

    if i - k * 2 - 1 >= 0:
        sum_ -= a[i-k*2-1]
    if i - k * 2 - 2 >= 0:
        sum_ -= a[i-k*2-2]

    m = max(m, sum_)

print(m)

Submission status: Runtime Error

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

l = list(map(int,input()))
ll = []
p = l[0]
co = 1
ppp = 0
su = 0
lll = []
for i in range(n-1):
    if p == l[i+1]:
        co += 1
    else:
        if p == 0:
            ppp += 1
        ll.append([p,co])
        p = l[i+1]
        co = 1
if p == 0:
    ppp += 1
ll.append([p,co])
co = 0
if ll[0][0] == 0:
    ll.insert([1,0],0)
if len(ll) == 1:
    print(n)
    exit(0)
if ppp <= k:
    print(n)
    exit(0)
    if ll[-1][0] == 1:
        for i in range(2*k+1):
            co += ll[i][1]
        lll.append(co)
        co -= ll[0][1] + ll[1][1]
        for i in range(ppp-k):
            if i == 0:
                co += ll[i*2+1+2*k][1] + ll[i*2+2+2*k][1]
                lll.append(co)
            else:
                co += ll[i*2+1+2*k][1] + ll[i*2+2+2*k][1] - (ll[i*2][1] + ll[i*2+1][1])
                lll.append(co)
    else:
        for i in range(2*k+1):
            co += ll[i][1]
        lll.append(co)
        co -= ll[0][1] + ll[1][1]
        for i in range(ppp-k):
            if ppp-k-1 == i and i == 0:
                co += ll[-1][1]
                lll.append(co)
            elif ppp-k-1 == i:
                co += ll[-1][1] - (ll[i*2][1] + ll[i*2+1][1])
                lll.append(co)
            elif i == 0:
                co += ll[i*2+1+2*k][1] + ll[i*2+2+2*k][1]
                lll.append(co)
            else:
                co += ll[i*2+1+2*k][1] + ll[i*2+2+2*k][1] - (ll[i*2][1] + ll[i*2+1][1])
                lll.append(co)
print(max(lll))

Submission status: Runtime Error

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

s = input()

a = []
flag = True
count = 0
for i in range(len(s) - 1):
    if i == 0 and s[i] == '1':
        a.append(0)

    if flag:
        if s[i] == s[i+1]:
            count += 1
        else:
            a.append(count+1)
            flag = False
            count = 0
    else:
        if s[i] == s[i+1]:
            count += 1
        else:
            a.append(count+1)
            flag = True
            count = 0

a.append(count+1)

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

m = max(sum(a[:k*2]), sum(a[-1:-k*2-1:-1]))

sum_ = a[1]

for i in range(3, len(a), 2):
    sum_ += a[i-1] + a[i]

    if i - k * 2 - 1 >= 0:
        sum_ -= a[i-k*2-2] + a[i-k*2-1]

    m = max(m, sum_)

print(m)

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
S = input()
l = [0]

for i in range(1, N):
    if S[i] != S[i-1]:
        l.append(i)
l.append(N)

mxcnt = 0
if len(l) <= 2*K+1:
    mxcnt = N
else:
    for i in range(len(l)-2*K):
        if S[i] == '0':
            x = l[i+2*K] - l[i]
            mxcnt = max(mxcnt, x)
        else:
            x = l[i+2*K+1] - l[i]
        mxcnt = max(mxcnt, x)

print(mxcnt)

Submission status: Runtime Error

Solution code:
import numpy as np
import re
N, K = map(int, input().split())
i = input()
s = ''.join(map(str, list(i)))
c = np.array(list(map(len, re.findall('0*', s))))
index = np.where(c>0)[0]+1
if len(index) > 0:
  index[0] = 1
if len(index) > 1:
  index[len(index)-1] = N

if len(index) == 0:
  print(1)
else:
  print(np.max(index[K:] - index[:-K]))

Submission status: Runtime Error

Solution code:
import bisect
n,k=map(int,input().split())
s=input()
f=[0 for i in range(n)]
f[0]=1-int(s[0])
for i in range(1,n):f[i]=(1-int(s[i]))*(s[i-1]^s[i])
for i in range(n-1):f[i+1]+=f[i]
ans=0
for l in range(n):
    r=bisect.bisect_right(f,k+f[l]*int(s[l]))
    ans=max(ans,r-l)
print(ans)

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
S = list(map(int, input()))
 
Z = [j for j, s in enumerate(S) if (j > 0 and s == 0 and S[j] != S[j-1]) or (j == 0 and s == 0)] + [N]
Y = [j for j, s in enumerate(S) if (j > 0 and s == 1 and S[j] != S[j-1]) or (j == 0 and s == 1)]
result = 0
if len(Z) <= K:
    print(N)
else:
    for i in range(len(Z)-K):
        if i == 0:
            result = Z[i+K]
        else:
            result = max(result, Z[i+K] - Y[i])
    print(result)

Submission status: Runtime Error

Solution code:
N,K = map(int,input().split())
S = input()
L = [0]
for i in range(N):
    if S[i] == '0'and a != S[i]:
        L.append(i)
    a = S[i]
L.append(N)
for i in range(len(L)-(K+1)):
    ans = (L[i+(K+1)]-1)-(L[i])
if N == 1:
    ans = 1
elif N == 2 or S == '010':
    ans = 2
elif N == 3:
    ans = 3
    
print(ans)

Submission status: Runtime Error

Solution code:
N,K = map(int,input().split())
S = input()
L = [0]
for i in range(N):
    if S[i] == '0'and a != S[i]:
        L.append(i)
    a = S[i]
L.append(N)
for i in range(len(L)-3):
    ans = (L[i+3]-1)-(L[i])
if N == 1:
    ans = 1
elif N == 2 or S == '010':
    ans = 2
elif N == 3:
    ans = 3
    
print(ans)

Submission status: Runtime Error

Solution code:
def main():
    N, K = map(int, input().split())
    S = [int(i) for i in input()]

    pre = 1
    zeroCounter = -1
    conOneConter = 0
    conOneList = [0]
    for s in S:
        # 1 -> 1
        if pre == 1 and s == 1:
            conOneConter += 1
        # 1 -> 0
        elif pre == 1 and s == 0:
            # 0を発見したので, カウンターを増やす
            zeroCounter += 1
            # これまでの"1"の個数を記録する
            if len(conOneList) <= zeroCounter:
                conOneList.append(0)

            for i in range(zeroCounter - (K - 1), zeroCounter + 1):
                if i < 0:
                    continue
                conOneList[i] += conOneConter
            # この"0"を1としてカウントする
            conOneConter = 1
            pre = 0
        # 0 -> 0
        elif pre == 0 and s == 0:
            conOneConter += 1
        # 0 -> 1
        elif pre == 0 and s == 1:
            # ここまでの"0"の個数を追加する
            conOneList[zeroCounter] += conOneConter
            # 前の値にも, 今回のzeroConterにおける"0"or"1"の個数を追加
            if zeroCounter >= K - 1:
                for i in range(zeroCounter - (K - 1), zeroCounter):
                    conOneList[i] += conOneConter
            pre = 1
            # この"1"をカウントしておく
            conOneConter = 1

    index = (zeroCounter - 1) if pre == 0 else zeroCounter
    index = index if index > 0 else 0
    for i in range(index - (K - 1), index + 1):
        conOneList[i] += conOneConter
    print(max(conOneList))


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
N,K = list(map(int,input().split()))
A = input()
A = list(A)
A = list(map(int,A))
count =0
def serch (x,A,K,count1,count0):
    if x<0 or len(A)<=x:
        return count1
    if A[x] == 0:
        if A[x-1]==0 and x!=0:
            count1 +=1
            count1 = serch(x+1,A,K,count1,count0)
        elif count0==K:
            return count1
        elif A[x-1]!=0:
            count0 +=1
            count1 +=1
            count1 = serch(x+1,A,K,count1,count0)
    else:
        count1 +=1
        count1 = serch(x+1,A,K,count1,count0)
    return count1
for i in range(len(A)):
    count1 =0
    count0 =0
    count1 = serch(i,A,K,count1,count0)
    count = max(count1,count)
print(count)

Submission status: Runtime Error

Solution code:
N,K=map(int,input())
S=input()
flag = false
a=[0]*N
if S[0]=='0':
  a[0]=1
  flag = true

for i in range(1,N):
  if S[i]=='0':
    if not flag:
      a[i]=1
    flag = true
  else:
    flag = false
  a[i]=a[i-1]

r=0
ret=0
count=0
for l in range(N):
  while true:
    if l==0:
      count = a[r]
    else:
      count = a[r]-a[l-1]
    if count <=K:
      ret=max(ret,count)
      r++
    else break
    if r==N:
      break
print(ret)

Submission status: Runtime Error

Solution code:
n, k = map(int,input().split())
s = input()
l1 = [i for i in range(len(s)) if '1' in s[i]]
l2 = [l1[i+1] - l1[i] for i in range(len(l1)-1)]
l2.insert(0, l1[0]+1)
if s[-1] == "0":
    l2.append(len(s) -l1[-1]-1)
ho = 1
while 1:
    if ho >= len(l2):
        break
    if l2[ho] == 1:
        l2[ho-1] += l2.pop(ho)
    ho += 1
tmp = sum(l2[:min(len(l2),k)])
m = tmp
flag = 0
if len(l2) <= k:
    print(m)
else:
    for i in range(len(l2) - k):
        tmp = tmp - l2[i] + l2[i+k]
        if tmp >= m:
            m = tmp
            if flag != 1:
                flag =1
    if flag == 1:
        print(m+1)
    else:
        print(m)

Submission status: Runtime Error

Solution code:
n,k=map(int, input().split())
c=list(input())
place=[-1]
dist=[0]*n
for i in range(len(c)-1):
    if c[i]!=c[i+1]:
        place.append(i)
place.append(n-1)
for i in range(len(place)-2*k-2):
    dist[i]=place[i+2*k+2]-place[i]
print(max(max((dist))-1),n)

Submission status: Runtime Error

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

x = '01'+x+'10'
N2 = N[1]*2 + 1

l = [i-3 for i in range(3,len(x)) if x[i-2:i] in ['01','10']]
ll = [l[i+N2]-l[i] for i in range(len(l)-N2)]

if len(ll) == 0:
    print(l[len(l)-1])
else:
    print(max(ll))

Submission status: Runtime Error

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

a = []
flag = -1
for i in range(len(S)):
    if flag == int(S[i]):
        a[len(a) - 1] += 1
    else:
        flag = int(S[i])
        a.append(1)
if S[0] == "0":
    zerostart_firstsum = sum(a[:K*2])
    a = a[1:]
for _ in range(K):
    a.append(0)
mae = 0
ato = K * 2
tmp = sum(a[:K*2 + 1])
max_tmp = tmp
while (True):
    if a[ato + 1] == 0:
        break
    tmp = tmp + a[ato + 1] + a[ato + 2] - a[mae] - a[mae + 1]
    if max_tmp < tmp:
        max_tmp = tmp
    mae += 2
    ato += 2

if S[0] == "0":
    print(max(zerostart_firstsum, max_tmp))
else:
    print(max_tmp)

Submission status: Runtime Error

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

ans = -1    # 十分に小さい値
if (S[0] == '0'):
    stand = 1
else:
    stand = 0

L = 0
R = 0

# 尺取り法
while (R < N):
    # 右端を進めることが可能（進めたときの積がK以下）なら、右端を進める
    if (stand <= K):
        ans = max(ans, R - L + 1)
        if (S[R] == '1' and S[R + 1] == '0'):
            stand += 1
        R += 1
    # 進められない場合、左端と右端が同じなら両者を進める
    # 左端が右端を追い越さないための処理
    elif (L == R):
        L += 1
        R += 1
    # それ以外は左端のみを進める
    else:
        if (S[L] == '0' and S[L + 1] == '1'):
            stand -= 1
        L += 1

Submission status: Runtime Error

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

Submission status: Runtime Error

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

ans = -1    # 十分に小さい値
if (S[0] == '0'):
    stand = 1
else:
    stand = 0

l = 0
r = 0

# 尺取り法
while (r < N):
    # 右端を進めることが可能（進めたときの積がK以下）なら、右端を進める
    if (stand <= K):
        ans = max(ans, r - l + 1)
        if (S[r] == '1' and S[r + 1] == '0'):
            stand += 1
        r += 1
    # 進められない場合、左端と右端が同じなら両者を進める
    # 左端が右端を追い越さないための処理
    elif (l == r):
        l += 1
        r += 1
    # それ以外は左端のみを進める
    else:
        if S[l] == '0' and if S[l + 1] == '1':
            stand -= 1
        l += 1