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()

arr = []
cnt = 0
comp = S[0]
for s in S:
    if s==comp:
        cnt += 1
    else:
        if comp=='0':
            arr.append(-cnt)
        else:
            arr.append(cnt)
        cnt = 1
        comp = s
if comp=='0':
    arr.append(-cnt)
else:
    arr.append(cnt)

def iter(A, cnt):
    if cnt>=K or len(A)<2:
        return max(A)
    elif len(A)==2:
        return abs(A[0]) + abs(A[1])
    m = -A[0]+A[1]
    arg = -1
    for i in range(len(A)-2):
        s3 = A[i] - A[i+1] + A[i+2]
        if m<s3:
            m = s3
            arg = i
    s3 = A[-2]-A[-1]
    if m<s3:
        m = s3
        arg = len(A)-1
    if arg==-1:
        B = [-A[0]+A[1]] + A[2:]
    elif arg==len(A)-1:
        B = A[:-2] + [A[-2]-A[-1]]
    else:
        B = A[:arg] + [A[arg] - A[arg+1] + A[arg+2]] + A[arg+3:]
    return iter(B, cnt+1)


print(iter(arr, 0))

Submission status: Runtime Error

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

one_list = []
zero_list = []

cur_char = S[0]
char_num = 0
for i in range(N):
    if S[i] == cur_char:
        char_num += 1
    else:
        if cur_char == '0':
            zero_list.append(char_num)
        else:
            one_list.append(char_num)
        cur_char = S[i]
        char_num = 1
if cur_char == '0':
    zero_list.append(char_num)
else:
    one_list.append(char_num)

len_zero_list = len(zero_list)
len_one_list = len(one_list)
# print(len_one_list)
# print(len_zero_list)

cont_length_list = []

if K >= len(zero_list):
    ans = N
else:
    if S[0] == '1':
        tmp_length = sum(one_list[:K+1])+sum(zero_list[:K])
        cont_length_list.append(tmp_length)
        for i in range(len_zero_list-K):
            if K+1+i<len_one_list:
                tmp_length += one_list[K+1+i]+zero_list[K+i]
            else:
                tmp_length += zero_list[K+i]
            tmp_length -= one_list[i]+zero_list[i]
            cont_length_list.append(tmp_length)
        # print(cont_length_list)
    else:
        tmp_length = sum(one_list[:K])+sum(zero_list[:K])
        cont_length_list.append(tmp_length)
        if K < len_zero_list:
            tmp_length += zero_list[K]+one_list[K]
            tmp_length -= zero_list[0]
            cont_length_list.append(tmp_length)
            for i in range(1, len_zero_list-K):
                if K+i<len_one_list and K+i < len_zero_list:
                    tmp_length += one_list[K+i]+zero_list[K+i]
                elif K+i==len_one_list:
                    tmp_length += one_list[K+i]
                else:
                    tmp_length += zero_list[K+i]
                tmp_length -= one_list[i-1]+zero_list[i]
                cont_length_list.append(tmp_length)
        # print(cont_length_list)
    # print(cont_length_list)
    ans = max(cont_length_list)

print(ans)

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() + "2"
# 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_next[0] != "2":
    # print(l, r)
    if r_next[0] == '1' and K >= 0:
        r += len(list(r_next[1]))
        r_next = next(r_iter)
        cnt = max(cnt, r - l)
        K -= 1
        continue
    elif K >=0:
        if r_next[0] == "0":
            pass
        r += len(list(r_next[1]))
        r_next = next(r_iter)
        continue
    if l_next[0] == "0":
        K += 1
    elif l_next[0] == "1":
        pass
    l += len(list(l_next[1]))
    l_next = next(l_iter)
if K > 0:
    cnt = max(cnt, r-l)
while K <= 0:
    if l_next[0] == "0":
        K += 1
    elif l_next[0] == "1":
        pass
    l += len(list(l_next[1]))
    l_next = next(l_iter)
    cnt = max(cnt, r-l)
print(cnt)

Submission status: Runtime Error

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

a0 = list(x.split("1"))
a1 = list(x.split("0"))

l0 = [len(a0[i]) for i in range(len(a0)) if a0[i]]
l1 = [len(a1[i]) for i in range(len(a1)) if a1[i]]

if x[0] == "0":
    l1.insert(0,0)
if x[len(x)-1] == "0":
    l1.append(0)

ll = [sum(l1[i:i+N[1]+1]) + sum(l0[i:i+N[1]])  for i in range(len(l1)-N[1])]

print(max(ll))

Submission status: Runtime Error

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

ms = []
a1 = ''
fptrs = []
lptrs = []

flg = 0
i = 0

for i in range(N):
	if(S[i] == '0' and flg == 0):
		fptrs.append(i)
		flg = 1
	elif(S[i]== '1' and flg == 1):
		lptrs.append(i)
		flg = 0
	else:
		pass
	i = i + 1

m = 0
fptrss = []
lptrss = []

for i in range(len(fptrs) - K + 1):
	buf_f = []
	buf_l = []
	for j in range(K):
		buf_f.append(fptrs[i+j])
		buf_l.append(lptrs[i+j])
	fptrss.append(buf_f)
	lptrss.append(buf_l)

cntbufs = []
m = 0

for fptrs,lptrs in zip(fptrss,lptrss):
	bufs = []
	cntbufs = []
	for c in S:
		bufs.append(c)
	for fptr,lptr in fptrs,lptrs:
		for i in range(fptr,lptr):
			bufs[i] = '1'
	bufs = ''.join(bufs)
	cntbuf = 0
	for i,c in enumerate(bufs):
		if(c == '1' and i != len(bufs) - 1):
			cntbuf = cntbuf + 1
		elif(c == '1' and i != len(bufs) - 1):
			cntbuf = cntbuf + 1
			cntbufs.append(cntbuf)
		else:
			cntbufs.append(cntbuf)
			cntbuf = 0
	if(m < max(cntbufs)):
		m = max(cntbufs)

if fptrs == []:
	m = N

print(m)

Submission status: Runtime Error

Solution code:
import sys
stdin = sys.stdin 
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline()
N,K=na();S=ns()
l=[]
c1=0
c0=0
x0=0
first=True
if(S[0]=="1"):
    c1=1
else:
    c0=1
for i in range(1,N):
    if(S[i]=="1"):
        if(S[i]!=S[i-1]):
            l.append(c0)
            c0=0
        c1+=1
    else:
        if(S[i]!=S[i-1]):
            x0+=1
            l.append(c1)
            c1=0
        c0+=1
if(c1):
    l.append(c1)
else:
    x0+=1
    l.append(c0)
#print(l)
#print(x0)
if(x0<=K):
    print(N)
    exit()
else:
    res=0
    if(int(S[0])>0):
        if(int(S[-1])>0):
            right = 0
            left = 2*K
            while(True):
                if(left>=len(l)-1):
                    print(res)
                    exit()
                res=max(sum(l[right:left+1]),res)
                #print(res,right,left)
                right+=2
                left+=2
        else:
            right = 0
            left = 2*K
            #print("llll")
            res=max(sum(l[len(l)-(1+(K-1)*2)-1:]),res)
            #print(len(l)-(1+(K-1)*2))
            while(True):
                if(left>=len(l)-1):
                    print(res)
                    exit()
                res=max(sum(l[right:left+1]),res)
                #print(res,right,left)
                right+=2
                left+=2
    else:
        if(int(S[-1])>0):
            res=max(res,sum(l[0:2*K]))
            right = 1
            left = 2*K+1
            while(True):
                if(left>=len(l)-1):
                    print(res)
                    exit()
                res=max(sum(l[right:left+1]),res)
                #print(res,right,left)
                right+=2
                left+=2
        else:
            right = 0
            left = 2*K
            #最後
            res=max(sum(l[len(l)-(1+(K-1)*2)-1:]),res)
            #最初
            res=max(res,sum(l[0:2*K]))
            right = 1
            left = 2*K+1
            while(True):
                if(left>=len(l)-1):
                    print(res)
                    exit()
                res=max(sum(l[right:left+1]),res)
                #print(res,right,left)
                right+=2
                left+=2

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
s=input()
s+='a'
l=[]
a=s[0]

if a=='0':
  f=0
else:
  f=1

for x in s[1:]:
  if a[0]!=x:
    l.append(len(a))
    a=x
  else:
    a+=x

if len(l)%2==0:
  pass
else:
  l=l+[0]

l2=[]

if f==1:
  pass
else:
  l2=[0]+l2

for i in range(len(l)):
  if i%2==0:
    l2.append(l[i]+l[i+1])

mx=sum(l2[:2*(k-1)+1])
a=l2[0]
for i in range(len(l2)-1):
  print(i)
  x=l2[i+k+1]
  if a<x:
    mx+=x
    mx-=a
    a=l2[i]

print(mx)

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 = 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=list(map(int, input().split()))
S_inp=str(input())

S=[]
if S_inp[0]=='0':
  S.append(0)

dmy=1
for i in range(N-1):
  if S_inp[i]==S_inp[i+1]:
    dmy+=1
  else:
    S.append(dmy)
    dmy=1
S.append(dmy)
if len(S) % 2 ==1:
  S.append(0)

#2*K+1の長さが取れる。
if len(S)==2:
  print(max(S))
else:
  T=[]
  for j in range(int(len(S)/2-K)):
    i=j*2
    T.append(sum(S[i:i+2*K+1]))
  print(max(T))

Submission status: Runtime Error

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

one = []
cnt = 0
for i, s in enumerate(S):
    if s == '1':
        cnt += 1
        if i == N - 1:
            one.append(cnt)
    elif s == '0':
        if cnt > 0:
            one.append(cnt)
            cnt = 0
            
zero = []
cnt = 0
for i, s in enumerate(S):
    if s == '0':
        cnt += 1
        if i == N - 1:
            zero.append(cnt)
    elif s == '1':
        if cnt > 0:
            zero.append(cnt)
            cnt = 0
            
if len(one) <= len(zero):
    one = [0] + one + [0]
    
for i in range(len(one) - 1):
    one[i + 1] = one[i] + one[i + 1]
one = [0] + one
    
for i in range(len(zero) - 1):
    zero[i + 1] = zero[i] + zero[i + 1]
zero = [0] + zero
    
ans = -1
for i in range(len(one) - (K + 1) + 1):
    tmp = one[i + K] - one[i - 1] + zero[i + K - 1] - zero[i - 1]
    ans = max(ans, tmp)
    # print(tmp)
    
if len(zero) - 1 <= K:
    print(len(S))
else:
    print(ans)

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:]
a.append(0)
a.append(0)
a.append(0)
a.append(0)
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 = 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:
	list_1[-1] = N-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:
import sys

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

ones_list = S.split('0')
zeros_list = S.split('1')

ones = []
if S[0] == '0':
    ones.append(0)
for k in range(len(ones_list)):
    if ones_list[k] != '':
        ones.append(len(ones_list[k]))
if S[-1] == '0':
    ones.append(0)

zeros = []
for k in range(len(zeros_list)):
    if zeros_list[k] != '':
        zeros.append(len(zeros_list[k]))

if K >= len(zeros):
    print(len(S))
    sys.exit

result = []
maximum = 0
for k in range(K):
    maximum += ones[k]
    maximum += zeros[k]
maximum += ones[K]
result.append(maximum)


for j in range(len(ones) - K - 1):
    maximum += zeros[K+j] + ones[K+1+j] - ones[j] - zeros[j]
    result.append(maximum)
print(max(result))

Submission status: Runtime Error

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

now = "0"
nl = []
for i, s in enumerate(S):
    if s != now:
        now = s
        nl.append(i)

nl_even = nl[0::2]
nl_odd = nl[1::2]
if s[-1] == "1":
    nl_odd.append(len(S))
if s[0] == "0":
    nl_odd.insert(0, 0)
if len(nl_odd)>1:
    x = nl_odd[1]
else:
    x = len(S)
y = min(k, len(nl_even)-1)
for i in range(len(nl_even)-y):
    x_ = nl_odd[i+y] - nl_even[i]
    x = max(x, x_)

print(x)

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
S = input()
L = [0]
prev = S[0]
num = 1
for s in S[1:]:
    if s == prev:
        num += 1
    else:
        L.append(num)
        num = 1
        prev = s
L.append(num)
# print(L)
for i in range(1, len(L)):
    L[i] += L[i - 1]
# print(L)
left = 0
if S[0] == "1":
    right = min(2 * K + 1, len(L) - 1)
else:
    right = min(2 * K, len(L) - 1)
num = 0
while right < len(L):
    # print("L[" + str(right) + "] - L[" + str(left) + "] = " + str(L[right] - L[left]))
    num = max(num, L[right] - L[left])
    if left == 0 and S[0] == "0":
        left += 1
    else:
        left += 2
    right += 2
if S[-1] == "0" and len(S) > 1:
    right -= 1
    # print("L[" + str(right) + "] - L[" + str(left) + "] = " + str(L[right] - L[left]))
    num = max(num, L[right] - L[left])
print(num)

Submission status: Runtime Error

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

start = 0
rl = []
nm = 0
for i in range(int(N/2)):
  t = 0
  c = 0
  k = K
  for s in S[i:]:
    if k == 0 and s == 0:
      break
    c+=1
    if(s == 0 and t == 0):
      t = 1
    elif(s == 1 and t == 1):
      k -= 1
      t = 0
  rl.append(c)
  if(i+max(rl) >= N):
    break
print(max(rl))

Submission status: Runtime Error

Solution code:
N,K = map(int,input().split())
S = '9'+input()+'9'
S = [int(s) for s in S]
a = S[0]
L = []
for s  in S:
    if s != a:
        L.append(1)
        a = s
    else:
        L.append(0)
        a = S[0]
for i in range(N):
    if N == 1:
        ans = 1
        break
    elif N == 2:
        ans = 2
        break
    if a != S[i]:
        for j in range(i,N):
            if sum(S[i:j]) > K*2:
                ans = j-i
                break

print(ans)

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
s=input()
List=[]
i=1
j=1
while i<n:
  if s[i-1]==s[i]:
    j+=1
    i+=1
    if i==n-1:
      List.append(j)
  else:
    List.append(j)
    i+=1
    j=1
if s[0]=="0":
  List.insert(0,0)
ans=max(List)
rg=2*k+1
for i in range(0,len(List)-rg+1,2):
  ans=max(ans,sum(List[i:i+rg]))
print(ans)

Submission status: Runtime Error

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

count = []
c = 0
now_one = True if S[0] == "1" else False

for i, s in enumerate(S):
    if s == "1" and now_one:
        c += 1
    elif s == "0" and now_one:
        count.append(c)
        c = 1
        now_one ^= True
    elif s == "0" and not now_one:
        c += 1
    elif s == "1" and not now_one:
        count.append(-c)
        c = 1
        now_one ^= True
    if i == len(S) - 1:
        if S[i] == "1":
            count.append(c)
        else:
            count.append(-c)

r = 0
res = 0
for i, x in enumerate(count):
    if x > 0:
        res += abs(count[i])
    elif x < 0:
        res += abs(count[i])
        K -= 1
        if K == 0 and i + 1 < len(count):
            res += abs(count[i + 1])
            r = i + 1
            break
        elif K == 0:
            r = i
            break

left = 0
tmp = res
if count[0] > 0 and r < len(count):
    for right in range(r + 1, len(count)):
        if left == right:
            break
        if count[right] < 0:
            tmp += abs(count[right])
            tmp -= abs(count[left])
            tmp -= abs(count[left + 1])
            left += 2
        elif count[right] > 0:
            tmp += abs(count[right])
        res = max(res, tmp)
elif r < len(count):
    tmp += abs(count[r + 1])
    tmp -= abs(count[left])
    res = max(res, tmp)
    for right in range(r + 2, len(count)):
        if count[right] < 0:
            tmp += abs(count[right])
            tmp -= abs(count[left])
            tmp -= abs(count[left + 1])
            left += 2
        elif count[right] > 0:
            tmp += abs(count[right])
        res = max(res, tmp)
print(res)

Submission status: Runtime Error

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

data = []
cnt = 0
curr = '1'
for i in range(len(s)):
    if s[i] != curr:
        data.append(cnt)
        curr = s[i]
        cnt = 1
    else:
        cnt += 1
data.append(cnt)
data.append(0)

current_sum = sum(data[0:0 + 2*k+1])
ret = current_sum
for start in range(2, len(data), 2):
    if (start + 2*k-1) < len(data): 
        diff = -data[start-2]-data[start-1]+data[start + 2*k]+data[start + 2*k-1]
        current_sum += diff

        if current_sum > ret:
            ret = current_sum
    else:
        break
print(ret)

Submission status: Runtime Error

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

def kazo(memo):
    zeronum = []
    zerotmp = 0
    l, r = 0, 1
    for i in range(0, N):
        if memo[i+1] > memo[i]:
            zeronum.append([zerotmp, l, r-1])
            zerotmp = 0
            l = i+1
            r = l
        else:
            zerotmp += 1
            r += 1
    return sorted(zeronum, key=lambda x: x[0])[-1]

for i in range(K):
    memo = [0]*(N+1)
    for i in range(N):
        if S[i] == '1':
            memo[i+1] = memo[i]+1
        else:
            memo[i+1] = memo[i]
    num, l, r = kazo(memo)
    for j in range(l, r+1):
        S[j] = '1'

import re

def renzoku(string):
    return len(sorted(re.findall(r'1+', string), key=lambda x: -len(x))[0])

print(renzoku(''.join(S)))

Submission status: Runtime Error

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

l=[]
state=0
one=0
zero=0
for i in range(len(s)):
    if s[i]=='0':
        if i+1 == len(s) or s[i+1]=='1':
            l.append(one)
            l.append(zero+1)
            one=0
            zero=0
        else:
            zero+=1
    elif s[i]=='1':
        one+=1
if one!=0:
    l.append(one)

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



max_0=-1
for i in range(len(l)):
    if i%2 == 0:
        continue
    if l[i] > max_0:
        max_0 = l[i]

index = 0
for i in range(len(l)):
    if l[i]==max_0:
        index = 0
        break

ans=0
ans+=l[index]
l[index-1]=l[index]+l[index+1]
del l[index]
del l[index]
for _ in range(k):
    if len(l)<3:
        break
    if l[index-2]+l[index-1]+l[index] > l[index]+l[index+1]+l[index+2]:
        ans+=l[index-2]+l[index-1]
        del l[index-2]
        del l[index-1]
    else:
        ans+=l[index+1]+l[index+2]
        del l[index+1]
        del l[index+2]

print(ans)

Submission status: Runtime Error

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

arr = []
cnt = 0
comp = S[0]
for s in S:
    if s==comp:
        cnt += 1
    else:
        if comp=='0':
            arr.append(-cnt)
        else:
            arr.append(cnt)
        cnt = 1
        comp = s
if comp=='0':
    arr.append(-cnt)
else:
    arr.append(cnt)

def iter(A, cnt):
    if cnt>=K or len(A)<2:
        return max(A)
    elif len(A)==2:
        return abs(A[0]) + abs(A[1])
    m = -A[0]+A[1]
    arg = -1
    for i in range(len(A)-2):
        s3 = A[i] - A[i+1] + A[i+2]
        if m<s3:
            m = s3
            args = i
    s3 = A[-2]-A[-1]
    if m<s3:
        m = s3
        arg = len(A)-1
    if arg==-1:
        B = [-A[0]+A[1]] + A[2:]
    elif arg==len(A)-1:
        B = A[:-2] + [A[-2]-A[-1]]
    else:
        B = A[:arg] + [A[arg] - A[arg+1] + A[arg+2]] + A[arg+3:]
    return iter(B, cnt+1)


print(iter(arr, 0))

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
S = list(input())
#print(S)
ss = [] # set of 01 sequense
s = []
s_prev = S[0]
for i in range(N):
    si = int(S[i])
    #print(s_prev, si)
    if int(si) != int(s_prev):
        #print("push")
        ss.append(s)
        s = [si]
    else:
        s.append(si)
    s_prev = si
ss.append(s)
#print(ss)
result = 0
# get max len of ss[i]
for k in range(K):
    max_ss = 0
    max_i = -1
    for i in range(len(ss)):
        if ss[i][0] == 1: continue
        if i > 0:
            if i < N - 1:
                len_ss = len(ss[i-1]) + len(ss[i]) + len(ss[i+1])
            else:
                len_ss = len(ss[i-1]) + len(ss[i])
        else:
            if i < N - 1:
                len_ss = len(ss[i]) + len(ss[i+1])
            else:
                len_ss = len(ss[i])

        #print("i", i, len_ss)
        if len_ss > max_ss:
            max_i = i
            max_ss = len_ss
    #print(max_i, max_ss)
    if max_i != -1:
        # reverse ss
        if max_i > 0:
            if max_i < N - 1:
                ss[max_i - 1] = ss[max_i - 1] + [1 for _ in range(len(ss[max_i]))] + ss[max_i + 1]
                del ss[max_i + 1]
                del ss[max_i]
            else:
                ss[max_i - 1] = ss[max_i - 1] + [1 for _ in range(len(ss[max_i]))]
                del ss[max_i]
        else:
            if max_i < N - 1:
                ss[max_i - 1] = ss[max_i - 1] + [1 for _ in range(len(ss[max_i]))]
                del ss[max_i]
            else:
                ss[max_i - 1] = ss[max_i - 1]
        #print("ss", ss)
        result = max_ss
print(result)

Submission status: Runtime Error

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

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

for i in range(1, n):
    if s[i] == '0' and s[i-1] == '1':
        zero_start.append(i)

zero_end = []
for i in range(1, n):
    if s[i-1] == '0' and s[i] == '1':
        zero_end.append(i-1)
if s[n-2] == '1' and s[n-1] == '0':
    zero_end.append(n-1)

zero_serial_len = [zero_end[i] - zero_start[i] + 1 for i in range(len(zero_end))]

start_serial_num = [[zero_start[i], zero_serial_len[i]] for i in range(len(zero_start))]
start_serial_num.sort(key = lambda x: x[1], reverse=True)

for i in range(k):
    for j in range(start_serial_num[i][1]):
        s_list = list(s)
        s_list[start_serial_num[i][0] + j] = '1'
        s = ''.join(s_list)

one_serial_length = []
count = 0
for i, si in enumerate(s):
    if i == 0 and s[i] == '1':
        count += 1
    elif s[i] == '1' and s[i-1] == '1':
        count += 1
        if i == n-1:
            one_serial_length.append(count)
    elif s[i] == '0' and s[i-1] == '1':
        one_serial_length.append(count)
        count = 0

print(max(one_serial_length))

Submission status: Runtime Error

Solution code:
n, k = map(int,input().split())
p = list(input())
a = []
ape = 1
for i in range(1,n):
     if p[i-1] != p[i]:
          a.append(ape)
          ape = 1
     else:
          ape += 1
a.append(ape)

if (p[0] == '1') or (len(a) == 1):
     kaisi = 0
else:
     kaisi = 1

ans = 0
anslist = []

for i in range(kaisi,min(2*k+1+kaisi,len(a))):
     ans += a[i]
anslist.append(ans)

for i in range(kaisi+2,len(a),2):
     if i+2*k <= len(a)-1:
          ans = ans-a[i-2]-a[i-1]+a[i+2*k]+a[i+2*k-1]
          anslist.append(ans)
     else:
          break
ans = 0
if kaisi != 0 and k != 0:
     for i in range(0,2*k):
          ans += a[i]
     anslist.append(ans)

print(max(anslist))

Submission status: Runtime Error

Solution code:
N, K = [int(x) for x in input().split()]
S = [int(x) for x in list(str(input()))]
 
S_list = []
 
s_b = S[0]
cnt = 1
for i in range(1,N):
  s_n = S[i]
  if s_n == s_b:
    cnt += 1
  else:
    S_list.append(cnt)
    cnt = 1
  s_b = s_n
  
S_list.append(cnt)
 
if len(S_list) - 2 * K <= 0:
  print(len(S))
  
cand_list = []
if S[0] == 1:
  for idx in range(0, len(S_list)-K, 2):
    cand_list.append(sum(S_list[idx:idx+2*K+1]))

if S[0] == 0:
  for idx in range(0, len(S_list)-K, 2):
    cand_list.append(sum(S_list[idx:idx+2*K]))
    
print(max(cand_list))

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:]
a.append(0)
a.append(0)
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()
a=[0]
b=[0]
if s[0]=="0":
    a.append(1)
for i in range(1,n):
    if s[i]=="0" and s[i-1]=="1":
        a.append(i+1)
a.append(n+1)
if len(a)-2>k:
    q=len(a)-k-1
max=0
for i in range(q):
    if max<a[k+1+i]-a[i]-1:
        max=a[k+1+i]-a[i]-1
else:max=n
if s[0]=="1":
    b.append(1)
for i in range(1,n):
    if s[i]=="1" and s[i-1]=="0":
        b.append(i+1)
b.append(n+1)
if len(b)-2>k:
    q=len(b)-k-1
min=0
for i in range(q):
    if min<b[k+1+i]-b[i]-1:
        min=b[k+1+i]-b[i]-1
else:min=n
if max<min:
    print(min)
else:
    print(max)

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(10**7)
input = sys.stdin.readline
INF = float('inf')
MOD = 10**9+7
dy = [0,1,0,-1]
dx = [1,0,-1,0]
ddy = [0,1,1,1,0,-1,-1,-1]
ddx = [1,1,0,-1,-1,-1,0,1]

n,k = map(int,input().split())
S = input()+'!'

get = [0]
tmp = 1
for i in range(1,n+1):
    if S[i] == S[i-1]:
        tmp += 1
    else:
        get.append(tmp)
        tmp = 1

get = get + [0]

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

res = 0
if S[0] == '0':
    for i in range(len(get)-2*k):
        if i%2 == 0:
            ans = get[i+2*k]-get[i]
        else:
            ans = get[min(i+2*k+1,len(get))]-get[i]
        if res <= ans:
            res = ans
else:
    for i in range(len(get)-2*k):
        if i%2 == 0:
            ans = get[min(len(get),i+2*k+1)]-get[i]
        else:
            ans = get[i+2*k]-get[i]
        if res <= ans:
            res = ans

print(res)

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
S = input()
L = [0]
prev = S[0]
num = 1
for s in S[1:]:
    if s == prev:
        num += 1
    else:
        L.append(num)
        num = 1
        prev = s
L.append(num)
# print(L)
for i in range(1, len(L)):
    L[i] += L[i - 1]
# print(L)
left = 0
if S[0] == "1":
    right = min(2 * K + 1, len(L) - 1)
else:
    right = min(2 * K, len(L) - 1)
num = 0
while right < len(L):
    # print("L[" + str(right) + "] - L[" + str(left) + "] = " + str(L[right] - L[left]))
    num = max(num, L[right] - L[left])
    if left == 0 and S[0] == "0":
        left += 1
    else:
        left += 2
    right += 2
if S[-1] == "0":
    right -= 1
    # print("L[" + str(right) + "] - L[" + str(left) + "] = " + str(L[right] - L[left]))
    num = max(num, L[right] - L[left])
print(num)

Submission status: Runtime Error

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

if s[0]=='0':
    flag=0
else:
    flag=1

l=[]
i=0
x=0
while (i<n):
    if flag==0:
        if s[i]=='0':
            x+=1
        else:
            l.append(x)
            x=1
            flag=1
    else:
        if s[i]=='1':
            x+=1
        else:
            l.append(x)
            x=1
            flag=0
    i+=1
l.append(x)

m=len(l)
t=2*k+1
if s[0]=='1':
    if m<=t:
        ans=n
    else:
        ans=0
        for i in range(t):
            ans+=l[i]
        if m%2==1:
            x=ans
            for i in range(0,m-t,2):
                x+=(l[i+t+1]+l[i+t]-l[i+1]-l[i])
                if ans<x:
                    ans=x
        else:
            x=ans
            for i in range(0,m-t-1,2):
                x+=(l[i+t+1]+l[i+t]-l[i+1]-l[i])
                if ans<x:
                    ans=x
            y=0
            for i in range(m-2*k,m):
                y+=l[i]
            if ans<y:
                ans=y
else:
    if m<=2*k:
        ans=n
    else:
        ans=0
        for i in range(2*k):
            ans+=l[i]
        if m%2==0:
            x=ans
            for i in range(0,m-t,2):
                x+=(l[i+t+1]+l[i+t]-l[i+1]-l[i])
                if ans<x:
                    ans=x
        else:
            x=ans
            for i in range(0,m-t-1,2):
                x+=(l[i+t+1]+l[i+t]-l[i+1]-l[i])
                if ans<x:
                    ans=x
            y=0
            for i in range(m-2*k,m):
                y+=l[i]
            if ans<y:
                ans=y
print(ans)

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)
print(max([sum(l[j:j+k*k+1]) for j in range(len(l)-k*k)]))

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())
s = input()
c = 0
w = '1'
res, ind = [], []
for i in range(len(s)):
    if int(s[i]) == 1:
        res.append(c)
        ind.append(i)
        w = '1'
        continue
    else:
        if w == '0':
            c += 1
            continue
        else:
            w = '0'
            c = 1
M = max(res)
I = res.index(M)

mini = ind[I]-int(M)-1
maxi = ind[I]

print('mini:',mini,'maxi:',maxi)

c1 = 0
for i in range(mini,-1,-1):
    print('mini', i, s[i])
    if s[i] == 1:
        c1 += 1
    else:
        break

c2 = 0
for i in range(maxi, len(s)):
    print('maxi', i)
    if s[i] == '1':
        c2 += 1
    else:
        break

print('c1',c,'c2',c2)

print(c1+c2+c)