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 = [int(elem) for elem in input().split()]
s = input()
new_s = [elem for elem in s]

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

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

else:
    build_cnt_lst(new_s, lst)
    res = []
    for i, elem in enumerate(lst):
        num = 3 + 2 * (k-1)
        if (i + num) <= len(lst):
            res.append(sum(lst[i:i+num]))
    print(max(res))

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':
            L.append(L[-1]+1) 

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:
a = []

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

for i in range(k):
  a.append(0)

q = input()


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

c = 0
mx = 0

for i in range(n-1):
  c = c+1  
  if q[i] == q[i+1]:
    lalala = 1

  if q[i] = 1 & q[i+1] = 0:
    for x in range(n+1):
      a[x] = a[x]+c
    c = 0
    if mx < a[0]:
      mx = a[0]
    pop(a[0])
    
  if q[i] = 0 & q[i+1] = 1:
    for x in range(n):
      a[x] = a[x]+c
    c = 0
    a.append(0)

for x in range(n):
  a[0] = a[0]+c+1
  if mx < a[0]:
    mx = a[0]
  pop(a[0])
 

if q[n-1] = 1:
  a[0] = a[0]+c+1
  if mx < a[0]:
    mx = a[0]
  pop(a[0])


print(mx)

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

N, K = map(int, input().split())
S = input()
last_s = "0"
count = 0
A = []
for i, s in enumerate(S):
	if i == 0:
		count += 1
	else:
		if s != last_s:
			A.append(count)
			count = 0
		count += 1
	last_s = s

if count != 0:
	A.append(count)

A.append(0)
A.append(0)

val = 0
if S[0] == "0":
	sys.exit(1)
	val = sum(A[:2*K])
	currentMax = val
	old = A[0]
	new = sum(A[2*K:2*K+2])
	val += new - old
	if val > currentMax:
		currentMax = val
	
	for i in range(1, len(A)-2*K+2, 2):
		old_sum_of_two = sum(A[i:i+2])
		sum_of_two = sum(A[i+2*K:i+2*K+2])
		val += sum_of_two - old_sum_of_two
		if val > currentMax:
			currentMax = val
	print(currentMax)
else:
	val = sum(A[:2*K+1])
	currentMax = val
	for i in range(0, len(A)-2*K+2, 2):
		old_sum_of_two = sum(A[i:i+2])
		sum_of_two = sum(A[i+2*K+1:i+2*K+3])
		val += sum_of_two - old_sum_of_two
		if val > currentMax:
			currentMax = val
	print(currentMax)

Submission status: Runtime Error

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

x = '01'+x+'01'
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)]

print(max(ll))

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
*s,=input()
g=[]
t=1
for i in range(1,n):
 if s[i]==s[i-1]:t+=1
 else:g.append(t);t=1
g.append(t)
for i in range(1,len(g)):g[i]+=g[i-1]
g=[0]+g+[g[-1]]
print(max(g[i]-g[max(0,i-2*k-1)]for i in range(s[0]=='1',len(g),2))

Submission status: Runtime Error

Solution code:
import re
import numpy as np
N, K = [int(item) for item in re.split(r"\s", input())]
S = [int(item) for item in list(input())]

if sum(S) == len(S) or sum(S) == 0:
    print(len(S))
else:
    S = S + [2]

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

    count = np.diff(np.array(index)).tolist()

    # print(index)
    # print(count)

    cumsum = [0] + np.cumsum(count).tolist()
    c_len = len(cumsum)
    win = min(2 * K, c_len)
    if S[0] == 1:
        win += 1

    print(max([cumsum[i + win] - cumsum[i] for i in range(0, c_len - win, 2)]))

Submission status: Runtime Error

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

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

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

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

Submission status: Runtime Error

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

l = []
ans = 0
for i in range(len(s)-1):
    ans += 1
    if s[i+1] != s[i]:
        l.append(ans)
        ans = 0
l.append(ans+1)

k = min(len(l)-1,k)
if s[0] == '0':
    print(max([sum(l[j:j+k*k+1]) for j in range(len(l)-k*k)]))
else:
    print(max([sum(l[j:j+k*k+2]) for j in range(len(l)-k*k-1)]))

Submission status: Runtime Error

Solution code:
import numpy as np
n,k=map(int,input().split())
s=input()
A=[]

a=0
ans=0
s+="2"
for i in range(n):
    if s[i]!=s[i+1]:
        A.append(i-a)
        
        a+=i
Amax=max(A)
index=A.index(max(A))
if (len(A)-1)/2<k:
    ans=n
dp=np.zeros((n,2*k+1))
dp[0][0]=A[0]
for j in range(n-1):
    for i in range(2*k):
        dp[j+1][i+1]=max(dp[j][i]+A[i],dp[j][i])
if (len(A)-1)/2<k:
    ans=n
else:
    ans=dp[len(A)][2*k+1]
print(ans)

Submission status: Runtime Error

Solution code:
times = input().split()
N = int(times[0])
K = int(times[1])
S = input()


def one_counter(S):
    S = S.split("0")
    high = 0
    tmp = 0
    for i in S:
        tmp = i.count("1")
        if tmp > high:
            high = tmp
    return high


last_score = 0
for i in range(K):
    if S.find("0") == -1:
        last_score = len(S)
        break
    zero_counter = []
    start_index = -1
    end_index = -1
    diff = 0
    for j, value in enumerate(S):
        if value == "0" and start_index == -1:
            start_index = j
        elif value == "1" and start_index != -1:
            end_index = j
            diff = end_index - start_index
            zero_counter.append([start_index, end_index, diff])
            start_index = -1
            end_index = -1
            diff = 0
    high_score = one_counter(S)
    high_S = S
    for k in zero_counter:
        S_f = S[:k[0]]
        S_b = S[k[1]:]
        ones = "1"*k[2]
        tmp_S = S_f+ones+S_b
        score = one_counter(tmp_S)
        if score > high_score:
            high_S = tmp_S
            last_score = score
    S = high_S


print(last_score)
times = input().split()
N = int(times[0])
K = int(times[1])
S = input()


def one_counter(S):
    S = S.split("0")
    high = 0
    tmp = 0
    for i in S:
        tmp = i.count("1")
        if tmp > high:
            high = tmp
    return high


last_score = 0
for i in range(K):
    if S.find("0") == -1:
        last_score = len(S)
        break
    zero_counter = []
    start_index = -1
    end_index = -1
    diff = 0
    for j, value in enumerate(S):
        if value == "0" and start_index == -1:
            start_index = j
        elif value == "1" and start_index != -1:
            end_index = j
            diff = end_index - start_index
            zero_counter.append([start_index, end_index, diff])
            start_index = -1
            end_index = -1
            diff = 0
    high_score = one_counter(S)
    high_S = S
    for k in zero_counter:
        S_f = S[:k[0]]
        S_b = S[k[1]:]
        ones = "1"*k[2]
        tmp_S = S_f+ones+S_b
        score = one_counter(tmp_S)
        if score > high_score:
            high_S = tmp_S
            last_score = score
    S = high_S


print(last_score)

Submission status: Runtime Error

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

l = []
ans = 0
for i in range(len(s)-1):
    ans += 1
    if s[i+1] != s[i]:
        l.append(ans)
        ans = 0
l.append(ans+1)

k = min(len(l)-1,k)
if s[0] == '1' and n%2 == 0:
    print(max([sum(l[j:j+k*k+1]) for j in range(len(l)-k*k)]))
else:
    print(max([sum(l[j:j+k*k+2]) for j in range(len(l)-k*k-1)]))

Submission status: Runtime Error

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

i=0
temp=s[0]
adata=[]
bdata=[]
cont=1
while(i<=n):
	if i==n-1:
		if s[i]==temp:
			if temp=='0':
				adata.append(cont+1)
			else:
				bdata.append(cont+1)
		else:
			if temp=='0':
				adata.append(cont)
				bdata.append(1)
			else:
				bdata.append(cont)
				adata.append(1)
		break
	if i!=0:
		if s[i]==temp:
			cont+=1
		else:
			if temp=='0':
				adata.append(cont)
			else:
				bdata.append(cont)
			temp=s[i]
			cont=1
	i+=1

if len(adata)<=k:
	print(len(s))
adata.append(0)
bdata.append(0)
ans=[]
for i in range(len(adata)-k+1):
	if s[0]=='0':
		if i==0:
			ans.append(sum(adata[i:i+k])+sum(bdata[i:i+k]))
		else:
			ans.append(sum(adata[i:i+k])+sum(bdata[i-1:i+k]))
	else:
		ans.append(sum(adata[i:i+k])+sum(bdata[i:i+k+1]))
print(max(ans))

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
a=input()
l=len(a)
src=[int(a[i:i+1]) for i in range(l)]
from itertools import groupby
y =[sum(1 for e in it) for _, it in groupby(src, key=lambda x: x==1)]
m=len(y)
if m<=k+1:
  print(int(n))
else:
  c=0
  for i in range(m-k):
    d=y[i,i+k+1]
    f=sum(d)
    if f>=c:
      c=f
  print(c)

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
S = input()
S = [int(S[i:i+1]) for i in range(N)]
ns = []
c0 = 0
c1 = 0
s =  S[0]
for i in S:
    if i == 0:
        c0 += 1
        if c1 > 0:
            ns.append(c1)
            c1 = 0
    else:
        c1 += 1
        if c0 > 0:
            ns.append(c0)
            c0 = 0
msum = 0
ssum = 0
ofset = 0
if s == 0:
    while(2):
        if ofset == 0:
            for i in K * 2:
                ssum += ns[i]
                if i == len(ns):
                    print(ssum)
                    exit()
            ofset += 1
            msum  = ssum
            ssum = 0
        else:
            while(True):
                for i in K * 2 + 1:
                    ssum += ns[i + ofset]:
                        if i + ofset == len(ns):
                            if msum < ssum:
                                msum = ssum
                            print(msum)
                            exit()
                if msum < ssum:
                    msum = ssum
                ofset += 2
else:
    while(True):
            for i in K * 2 + 1:
                ssum += ns[i + ofset]:
                    if i + ofset == len(ns):
                        if msum < ssum:
                            msum = ssum
                        print(msum)
                        exit()
            if msum < ssum:
                msum = ssum
            ofset += 2

Submission status: Runtime Error

Solution code:
import re

NK = [int(i) for i in input().split()]
S = input()
answer = 0
len_zero = []
val = []
sum_zero = []
sum_zero2 = []
if(NK[0]/2 <= NK[1]):
    answer = NK[0]
else:
    len_zero = re.findall("0+",S)
    result = re.finditer("0+",S)
    for e in result:
        sum_zero.append((e.span()))

    for i in sum_zero:
        val.append(i[1] - i[0])

    for i in range(len(len_zero) - NK[1] + 1):
        sum_zero2.append(sum(val[i:i+NK[1]]))
    a = sum_zero2.index(max(sum_zero2))
    answer = sum_zero[a + NK[1] - 1][1] - sum_zero[a][0] + NK[0] - sum_zero[a + NK[1] - 1][1] + sum_zero[a][0] - sum_zero[a - 1][1]


print(answer)

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())
seq = [bool(int(c)) for c in input()]

ones = []
if seq[0] is False:
    ones.append(0)
zeros = []
state = True
cnt = 0
for i, c in enumerate(seq):
    if i == 0:
        state = c
        cnt = 1
        continue

    if c == state:
        cnt += 1
    else:
        if state:
            ones.append(cnt)
        else:
            zeros.append(cnt)
        cnt = 1
        state = c

if state is True:
    ones.append(cnt)
else:
    ones.append(0)
    zeros.append(cnt)

# print(ones)
# print(zeros)

n_zeros = len(zeros)

k = min(k, n_zeros)

cum_ones = []
for i, num in enumerate(ones):
    if i == 0:
        cum_ones.append(num)
        continue
    cum_ones.append(num + cum_ones[-1])
cum_zeros = []
for i, num in enumerate(zeros):
    if i == 0:
        cum_zeros.append(num)
        continue
    cum_zeros.append(num + cum_zeros[-1])

max_score = 0
for i in range(n_zeros - k + 1):
    score = 0
    score += (cum_ones[i+k] - cum_ones[i-1]) if i > 0 else cum_ones[i+k]
    if k > 0:
        score += (cum_zeros[i+k-1] - cum_zeros[i-1]) if i > 0 else cum_zeros[i+k]
    if max_score < score:
        max_score = score

print(max_score)

Submission status: Runtime Error

Solution code:
from sys import stdin
from collections import deque, defaultdict as ddic
from itertools import combinations, permutations, product
import bisect
import heapq

rs = lambda: stdin.readline().strip()
ri = lambda: int(rs())
rsl = lambda: stdin.readline().split()
rim = lambda: map(int, rsl())
ril = lambda: list(rim())

n, k = rim()
s = list(rs())
zeros = []
temp = []
instreak = False
for i, char in enumerate(s):
    if char == '0':
        if not instreak:
            zeros.append(i)
            instreak = True
    else:
        instreak = False

# 1111111
if not zeros:
    print(len(s))
else:
    zeros = [-1] + zeros + [len(s)]
    maximum = 0
    index = 0
    left = zeros[index]
    if k == 0:
        # just count 1
        streak, maximum = 0, 0
        for i, char in enumerate(s):
            if char == '1':
                streak += 1
            else:
                if streak > maximum:
                    maximum = streak
                streak = 0

            if i == len(s)-1 and char == '1':
                if streak > maximum:
                    maximum = streak
    else:
        if k+1 >= len(zeros):
            k = len(zeros)-2
        for i in range(k+1, len(zeros)):
            right = zeros[i]
            streak = right - 1 - left
            if streak > maximum:
                maximum = streak
            index += 1
            ######
            left = zeros[index]
            l = index+1
            while True:
                if s[l] == '0':
                    left += 1
                else:
                    break
                l +=1


    print(maximum)

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
S = input()
li = []
recent_score = 1
ans = 0

x = 0
while True:
    if S[0] == "0":
        x += 1
        S = S[1:]
    else:
        break
first = 0
for i in range(1, len(S)):
    if S[i-1] == "1" and S[i] == "0":
        if not first :
            if len(li) == K-1:
                ans = max(ans, sum(li) + recent_score + x)
        if len(li) == K:
            ans = max(ans, sum(li) + recent_score)
            li.pop(0)
    elif S[i-1] == "0" and S[i] == "1":
        li.append(recent_score)
        recent_score = 0
    recent_score += 1
if ans == 0:
    ans = len(S)
elif S[-1] == "1":
    ans =max(ans, sum(li) + recent_score)
else:
    ans = max(ans, sum(li) + recent_score)

print(ans)

Submission status: Runtime Error

Solution code:
N,K = map(int,input().split())
S= [i for i in input()]
list_1 = []
max = 0
if S[0] == '0':
	list_1.append(0)
for i in range(N-1):
	if S[i] == '1' and S[i+1] == '0':
		list_1.append(i)
if S[0] == '1':
	list_1[0] = 0
if S[N-1] == '0':
	list_1.append(N-1)
else:
	for j in range(len(S)-list_1[-1]):
		if S[j] == '0':
			list_1.append(N-1)
		else:
			list_1[-1] = len(S)-1
print(list_1)
if K>=len(list_1) or '0' not in S:
	print(len(S))
else:
	for i in range(len(list_1)-K):
		if list_1[i+K]-list_1[i]+1 > max:
			max = list_1[i+K]-list_1[i]+1
	print(max)

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())
s = input()
num_list = []
know = []

for i in range(len(s)):
    num_list.append(s[i])
    if (i == 0) or (num_list[i] != num_list[i-1]):
        know.append(1)
    elif num_list[i] == num_list[i-1]:
        know[-1] += 1
print(num_list)
print(know)

length = []
if (num_list[0] == '1') and (num_list[-1] == '1'):
    for i in range(0, int((len(know)-1)/2), 2):
        if i + (k * 2) < len(know):
            length.append(sum(know[i: i + (k * 2) + 1]))
        else:
            length.append((sum(know[i:])))
        i += 1
elif (num_list[0] == '1') and (num_list[-1] == '0'):
    for i in range(0, int(len(know)/2), 2):
        if know[i+1] == know[-1]:
            length.append(know[i] + know[i+1])
        else:
            if i + (k * 2) < len(know):
                length.append(sum(know[i: i + (k * 2) + 1]))
            else:
                length.append((sum(know[i:])))
        i += 1
elif (num_list[0] == '0') and (num_list[-1] == '1'):
    for i in range(0, int(len(know)/2), 2):
        if i == 0:
            if i + (2*k) <= len(know):
                length.append(sum(know[i: i + (2*k)]))
            else:
                length.append(sum(know[i:]))
        else:
            if i + (k * 2) < len(know):
                length.append(sum(know[i: i + (k * 2) + 1]))
            else:
                length.append((sum(know[i:])))
            i += 1
elif (num_list[0] == '0') and (num_list[-1] == '0'):
    for i in range(0, int((len(know)+1)/2), 2):
        if i == 0:
            if i == 0:
                if i + (2 * k) <= len(know):
                    length.append(sum(know[i: i + (2 * k)]))
                else:
                    length.append(sum(know[i:]))
        elif know[i+1] == know[-1]:
            length.append(know[i] + know[i+1])
        else:
            if i + (k * 2) < len(know):
                length.append(sum(know[i: i + (k * 2) + 1]))
            else:
                length.append((sum(know[i:])))
            i += 1


print(max(length))

Submission status: Runtime Error

Solution code:
l=len(a)
src=[int(a[i:i+1]) for i in range(l)]
from itertools import groupby
y =[sum(1 for e in it) for _, it in groupby(src, key=lambda x: x==1)]
m=len(y)
if m<=k+1:
  print(int(n))
else:
  c=0
  for i in range(m-k):
    d=sum (y[i,i+k+1])
    if d>=c:
      c=d
  print(c)

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())
seq = [bool(int(c)) for c in input()]

ones = []
if seq[0] is False:
    ones.append(0)
zeros = []
state = True
cnt = 0
for i, c in enumerate(seq):
    if i == 0:
        state = c
        cnt = 1
        continue

    if c == state:
        cnt += 1
    else:
        if state:
            ones.append(cnt)
        else:
            zeros.append(cnt)
        cnt = 1
        state = c

if state is True:
    ones.append(cnt)
else:
    ones.append(0)
    zeros.append(cnt)

# print(ones)
# print(zeros)

n_zeros = len(zeros)

k = min(k, n_zeros)

cum_ones = []
for i, num in enumerate(ones):
    if i == 0:
        cum_ones.append(num)
        continue
    cum_ones.append(num + cum_ones[-1])
cum_zeros = []
for i, num in enumerate(zeros):
    if i == 0:
        cum_zeros.append(num)
        continue
    cum_zeros.append(num + cum_zeros[-1])

max_score = 0
for i in range(n_zeros - k + 1):
    score = 0
    score += (cum_ones[i+k] - cum_ones[i-1]) if i > 0 else cum_ones[i+k]
    score += (cum_zeros[i+k] - cum_zeros[i-1]) if i > 0 else cum_zeros[i+k]
    if max_score < score:
        max_score = score

print(max_score)

Submission status: Runtime Error

Solution code:
import numpy as np
n,k = map(int,input().split())
s = input()
ls = []
ls = list(s)
if ls[0] == '0':
  start = 0
else:
  start = 1
a = []
po = 1
for i in range(len(ls)-1):
    if ls[i] == ls[i+1]:
      po = po + 1
      if i == len(ls)-2:
        a.append(po)
    else:
      a.append(po)
      po = 1
      if i == len(ls)-2:
        a.append(po)
if a == []:
  a.append(1)
sum = 0
max = 0
for i in range(k):
  b = []
  if start == 0:
    for j in range(int((len(a)-1)/2)+1):
      if j == 0:
        sum = a[0] + a[1]
      elif j == int((len(a)-1)/2):
        if len(a)%2 == 0:
          sum = a[2*j-1] + a[2*j] + a[2*j+1]
        else:
          sum = a[2*j-1] + a[2*j]
      else:
        sum = a[2*j-1] + a[2*j] + a[2*j+1]
      b.append(sum)
print(np.max(b))

Submission status: Runtime Error

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

S = input()
S = " ".join(S)
S = list(map(int , S.split()))
S.append(S[-1])

list = []
if S[0] == 0:
	list.append(0)

number = 0
for n in range(len(S)-1):
	if S[n] == S[n+1]:
		number += 1
	if S[n] != S[n+1]:
		number += 1
		list.append(number)
		number = 0
list.append(number)


N = 0
for n in range(len(S)- 4):
	N = max(N, list[n] + list[n+1] +  + list[n+2])

print(N)

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())
s = input()
memo = []
c = 0
ps = ''
lens = len(s) - 1
b = 0
if s[0] == '1':
    b = 1

for i in range(lens + 1):
    if s[i] != ps:
        memo.append(c)
        c = 1
        ps = s[i]
        if i == lens:
            memo.append(1)
        continue
    if i == lens:
        memo.append(c+1)
    c += 1
    ps = s[i]
sum = 0
if  len(memo) < k * 2 + b + 1:
    print(sum(memo))
else:
    sum = sum(memo[: k * 2 + b + 1])
    temp = [sum]
    memo.append(0)
    # print(memo)
    # print(len(memo))
    for i in range(0, len(memo) - 2 * k  - 1, 2):
        sum = sum - memo[i] - memo[i + 1] + memo[i + 2 * k  + 1] + memo[i + 2 * k + 2]
        temp.append(sum)
    # print(temp)
    print(max(temp))

Submission status: Runtime Error

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

L=[]

P=S[0]
cnt=1
for i in range(1,N):
    if S[i]==P:
        cnt+=1
    else:
        if P=="1":
            L.append(cnt)
            cnt=1
            P="0"
        else:
            L.append(cnt*-1)
            cnt=1
            P="1"
if P=="1":
    L.append(cnt)
else:
    L.append(cnt*-1)
if len(L)==1:
    print(L)
    exit()
X=[0 for i in range(len(L))]
L=X+L+X
#print(L)
ANS=[]
NUM=0
for i in range(1,2*K-1):
    NUM+=abs(L[i])
ANS=[NUM]
#print(ANS)

SUM=[0]
for i in range(1,len(L)):
    SUM.append(SUM[i-1]+abs(L[i]))



ans=0
if len(SUM)-len(X)<2*K:
    print(SUM[len(SUM)-1])
else:    
    for i in range(len(SUM)-len(X)):
        if L[i]<0:
            if (SUM[2*K+1+i]-SUM[i])>ans:
                ans=(SUM[2*K+1+i]-SUM[i])
        else:
            if (SUM[2*K+i]-SUM[i])>ans:
                ans=(SUM[2*K+i]-SUM[i])
            #print(i)
    print(ans)

Submission status: Runtime Error

Solution code:
import sys
from collections import Counter
input = sys.stdin.readline

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

s = [int(x) for x in list(input().rstrip())]

if(s[0] == 1):
    lis = [[1]]
else:
    lis = [[0]]


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

zero = [lis[x] for x in range(len(lis)) if x % 2 != 0]


ma = 0

for i in range(len(lis) - 2*k-2):

    wa = 0
    for j in range(2 * k+1):
        wa += len(lis[2*i + j])

    if (ma < wa):
        ma = wa

print(ma)

Submission status: Runtime Error

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

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

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

Submission status: Runtime Error

Solution code:
def cul(lst, m):
    r = 0
    for i in lst:
        r  += i if i < m else m
    return r

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

if len(s) == 1:
    print(1)

a = 1
for i in range(1, len(s)):
    if s[i-1] == s[i]:
        a += 1
    else:
        val.append(a)
        a = 1
val.append(a)
ans = []
now = int(s[0])
for i in range(len(s)-k):
    lst = val[i:i+k*2+1]
    if now == 1:
        ans.append(cul(lst, n))
    now = 1 if now == 0 else 0
print(max(ans))

Submission status: Runtime Error

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

for i in range(K):
  if "0" not in S:
    break
  for j in range(N-1):
    if j == 0:
      sw = 1
      l.append(0)
      for k in range(j,N-1-j):
        if S[k] == "0" and S[k+1] == "1":
          r.append(k+1) #r自体は1
          sw = 2
        if sw == 2 and S[k] == "1" and S[k+1] == "0":
          width.append(k-j)
          break
    if S[j] == "0" and S[j+1] == "1":
      sw = 1
      for k in range(j+1,N-1-j):
        if S[k] == "1" and S[k+1] == "0":
          l.append(k+1)
        if S[k] == "0" and S[k+1] == "1":
          r.append(k+1) #r自体は1
          sw = 2
        if sw == 2 and S[k] == "1" and S[k+1] == "0":
          width.append(k-j)
          break
  idx = width.index(max(width))
  ans = max(width)
  S = S[:l[idx]] + "1"*(r[idx]-l[idx]) + S[r[idx]:]
print(ans)

Submission status: Runtime Error

Solution code:
import numpy as np
n, k = (int(i) for i in input().split())  
s = input()

i = 0
count_list = []
starts_from = s[0]
tmp = s[0]
count = 0

while i < n:
  if s[i] == tmp: # still same 0/1
    count += 1
  else:
    count_list.append(count)
    tmp = s[i]
    count = 1
  i += 1
count_list.append(count)  # last

sum_list = []

sum_list = []
for j, count in enumerate(count_list):
  if starts_from == "0":
    if j == 0:
      sum_list.append(count_list[0] + count_list[1])
    elif j % 2 == 0:
      sum_list.append(count_list[j-1] + count_list[j] + count_list[j+1])
  else:
    if j + 1 > len(count_list):
      sum_list.append(count_list[j-1] + count_list[j] + count_list[j+1])
    else:
      sum_list.append(count_list[j-1] + count_list[j])
i = 0
sum = 0

while i < k:
  if i == 0:
    sum += max(sum_list)
    before_idx = np.argmax(sum_list)
    max_idx = np.argmax(sum_list) 
  else:
    max_idx = np.argmax(sum_list)
    if max_idx - before_idx == 1:
      if starts_from == "0":
        sum -= count_list[before_idx * 2 - 1]
      else:
        sum -= count_list[before_idx * 2]
      sum += max(sum_list)
    else:
      sum += max(sum_list)
    before_idx = np.argmax(sum_list) 
  sum_list[max_idx] = 0
  i += 1

print(sum)