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 = input()
n_k_list = n_k.split()
K = int(n_k_list[1])
people = input()
people_list = []
now = people[0]
for i in people[1:]:
    if now[-1] == i:
        now += i
    else:
        people_list.append(now)
        now = i
people_list.append(now)

max_count = 0
change_index = None
while K:
    for index, cell in enumerate(people_list):
        if cell[0] == "0":
            if index == 0:
                this_count = cell.__len__() + people_list[index + 1].__len__()
            elif index == people_list.__len__() - 1:
                this_count = cell.__len__() + people_list[index - 1].__len__()
            else:
                this_count = cell.__len__() + people_list[index - 1].__len__() + people_list[index + 1].__len__()
            if max_count < this_count:
                max_count = this_count
                change_index = index
    if change_index is not None:
        change_char = ""
        for z in range(people_list[change_index].__len__()):
            change_char += "1"

        if change_index == 0:
            new_char = people_list[change_index + 1] + change_char
            people_list[change_index] = new_char
            del people_list[change_index + 1]
        elif change_index == people_list.__len__() - 1:
            new_char = people_list[change_index - 1] + change_char
            people_list[change_index] = new_char
            del people_list[change_index - 1]
        else:
            new_char = people_list[change_index - 1] + people_list[change_index + 1] + change_char
            people_list[change_index] = new_char
            del people_list[change_index + 1]
            del people_list[change_index - 1]
        change_index = None
        K -= 1
    else:
        break
print(max(people_list, key=len).__len__())

Submission status: Runtime Error

Solution code:
def solve():
  n,k = (int(i) for i in input().split())
  s = input()
  one = []
  zero = []  
  for i in range(n):
    if i == 0:
      if s[i] == "0":
        one.append(0)
        zero.append(1)
      else:
        one.append(1)
    
    else:     
     if s[i] == s[i-1]:
      if s[i] == "0":
        zero[-1]+= 1
      else:
        one[-1] += 1
     else:
      if s[i] == "0":
        zero.append(1)
      else:
        one.append(1)
  ans = 0 
  if len(zero) <= k:
    ans = n
  one = one + [0]*5
  zero = zero + [0]*5
  
  tmp = 0
  for i in range(k+1):
   tmp += one[i]
  for i in range(k):
   tmp += zero[i]
  ans = max(tmp,ans)
  for i in range(len(zero)-k-1):
       
       tmp -=  one[i]
       tmp +=  one[i+k+1]
       tmp -=  zero[i]
       tmp +=  zero[i+k]
       ans = max(ans,tmp)

  #print (one) 
 # print(zero)
  print(ans)
    
solve()

Submission status: Runtime Error

Solution code:
n,m=map(int,input().split())
l1=list(input())
l=[]
lm=[]
c=0
d=l1[0]
for i in l1:
  if i==d:
    c+=1
  else:
    d=i
    l.append(c)
    c=1
l.append(c)
if l1[0]=="1":
  if len(l)//2<=m:
    print(n)
  else:
    j=0
    for i in range((len(l)-2*m-1)//2+1):
      lm.append(sum(l[j:j+2*m])+l[j+2*m])
      j+=2
      if j+2*m+1>len(l):
        break
    print(max(lm))
else:
  if (len(l)+1)//2<=m:
    print(n)
  else:
    j=1
    for i in range((len(l)-2*m-2)//2+1):
      lm.append(sum(l[j:j+2*m])+l[j+2*m])
      j+=2
      if j+2*m+1>len(l):
        break
    print(max(lm))

Submission status: Runtime Error

Solution code:
from itertools import*
from numpy import*
n,k=map(int,input().split())
k2=k+1
s=input()
if len(s)==1:
    if s=="1":
        print(1)
    else:
        print(0)
    exit()
if s[0]=="1" and s[-1]=="0":
    x=[0]+[len(list(v))for k,v in groupby(s)if k=="0"]
    y=[0]+[len(list(v))for k,v in groupby(s)if k=="1"]+[0]
elif s[0]=="0" and s[-1]=="1":
    x=[0]+[len(list(v))for k,v in groupby(s)if k=="0"]+[0]
    y=[0]+[len(list(v))for k,v in groupby(s)if k=="1"]
else:
    x=[0]+[len(list(v))for k,v in groupby(s)if k=="0"]+[0]
    y=[0]+[len(list(v))for k,v in groupby(s)if k=="1"]+[0]
x1=array(x[:-k])
x2=array(x[k:])
x3=sum(x[:k])
t=x3+cumsum(x2-x1)
y1=array(y[:-k2])
y2=array(y[k2:])
y3=sum(y[:k2])
f=y3+cumsum(y2-y1)
#print(x,y)
#print(t,f)
print(max(max(t+f),x3+y3))

Submission status: Runtime Error

Solution code:
#import time

N,K = [int(x) for x in input().split()]

#t1 = time.time() 
S = input()
a= S[0]
b = int(S[0])
table = [0 for i in range(N)]
c = 0
l = 0
for s in S:
    if int(s) == b:
        c += 1
    else:
        b = not(b)
        table[l]=c
        c = 1
        l+=1

table[l]=c
#t2 = time.time() 
if a =='0':
    N = 2*K+1
    ans = sum(table[0:2*K])
    for i in range(1,l,2):
        ans = max(ans,sum(table[i:i+N]))

else:
    ans =0
    N = 2*K +1
    for i in range(0,le,2):
        ans = max(ans,sum(table[i:i+N]))

#t3 = time.time() 
#print(table)
print(ans)
#print(t2-t1)
#print(t3-t2)

Submission status: Runtime Error

Solution code:
import time
N,K = [int(x) for x in input().split()]

t1 = time.time() 
S = input()
a= S[0]
b = int(S[0])
table = [0 for i in range(N)]
c = 0
l = 0
for s in S:
    if int(s) == b:
        c += 1
    else:
        b = not(b)
        table[l]=c
        c = 1
        l+=1

table[l]=c
t2 = time.time() 
if a =='0':
    N = 2*K+1
    ans = sum(table[0:2*K])
    for i in range(1,l,2):
        ans = max(ans,sum(table[i:i+N]))

else:
    ans =0
    N = 2*K +1
    for i in range(0,le,2):
        ans = max(ans,sum(table[i:i+N]))

t3 = time.time() 
#print(table)
print(ans)

Submission status: Runtime Error

Solution code:
def count1(st):
    g = 0
    c = 0
    cont = False
    for p in st:
        if p == '1':
            if cont == False:
                c = 1
                cont = True
            else:
                c += 1
            g = max(g,c)
        else:
            cont = False
    return g
N,K = map(int,input().split())
s = input()
ls = len(s)
i = 0
il = 0
ir = 0
mg = 0
mgs = ''
if ls == 1:
    print(1)
else:
    for j in range(K):
        #print('j',j)
        while i < N:
            #print(s[i])
            if s[i] == '0':
                il = i
                while i < N:
                    if s[i] == '1':
                        ir = i - 1
                        break
                    i += 1
                #print('lr',il,ir)
                new_s = s[:il] + '1'*(ir-il+1) + s[ir+1:]
                #print(new_s)
                if mg < count1(new_s):
                    mg = count1(new_s)
                    mgs = new_s
                #print('mg',mg)
            else:
                i += 1
        s = new_s
        i = 0
    print(mg)
    #print(mg,mgs)

Submission status: Runtime Error

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

o0=S.split('0')
o1=[x for x in o0 if x]
o2=[len(x) for x in o1]

z0=S.split('1')
z1=[x for x in z0 if x]
z2=[len(x) for x in z1]

if(N==1): print(1)
else:
  anslist=[]
  if(S[0]=='0'):
    anslist.append(sum(o2[0:K])+sum(z2[0:K]))
    del z2[0]
  if(S[-1]=='0'):
    anslist.append(sum(o2[-K:])+sum(z2[-K:]))
    del z2[-1]

  for i in range(len(o2)-K):
    anslist.append(sum(o2[i:i+K+1])+sum(z2[i:i+K]))

  print(max(anslist))

Submission status: Runtime Error

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

# 奇数番目は1のカタマリの人数　偶数番目は0のカタマリの人数 両端は必ず1のカタマリの人数
now_state, now_count = '1', 0
L = []
for i in range(len(S)):
  if S[i] == now_state:
    now_count += 1
  else:
    L.append(now_count)
    # reset
    now_count = 1
    now_state = '1' if now_state == '0' else '0'
L.append(now_count)
if now_state == '0':
  L.append(0)

#print(L)
# 0の塊をj個ひっくり返す
# j==1のとき101の3個の和の最大
# j==2のとき10101の5個の和の最大
# j==jのとき101...1のj*2+1個の和の最大

def ngram(n, _list):
  return [_list[i:i+n] for i in range(len(_list)-n+1)]

now_max = 0
j = min((len(L)-1)/2, K)
tmp = ngram(j*2+1, L)
# 偶数番目だけ
tmp = [sum(tmp[i]) for i in range(len(tmp)) if i%2 == 0]
tmp = max(tmp) if len(tmp) > 0 else 0
now_max = max(tmp, now_max)
print(now_max)

Submission status: Runtime Error

Solution code:
import sys
n,k = map(int,input().split())
st = input()
s = []
for i in st:
    s.append(i)
s.append('-1')
man = 1 if s[0] == '0' else 0

check_ls = [];check_num = 0;tyokuritu = []
for i in range(len(s)):
    check_num += 1
    if s[i] != s[i+1]:
        check_ls.append(check_num)
        check_num = 0
        if s[i] == '0':
            tyokuritu.append(True)
    if s[i+1] == '-1':break
if k >= len(tyokuritu):print(len(s)-1);sys.exit()
tyokuritu.append(False)
ios_check = [0]+[check_ls[0]]*(len(check_ls))
for i in range(len(check_ls)-1):
    ios_check[i+1+1] = check_ls[i+1] + ios_check[i+1]
ans = 0
for i in range(0,10**5,2):
    ans = max(ans,ios_check[k*2+1+i+man] - ios_check[i+man])
    if k*2+1+i+man+2 >= len(check_ls):break
if man == 1:ans = max(ans,ios_check[k*2-1])
print(ans)

Submission status: Runtime Error

Solution code:
import numpy as np

N, K = map(int, input().split(" "))
S = np.array([int(i) for i in input()])
pos1 = np.linspace(0,N-1,N)[S == 1]
lst = []
st =[pos1[0]]
for i in pos1[1:]:
    if st[-1] + 1 == i:
        st.append(i)
    else:
        lst.append(st)
        st = [i]
    if i == pos1[-1]:
        lst.append(st)
lst = [[0]] + lst + [[N-1]]
time = len(lst) - K
if time <= 0:
    print(N)
else:
    print(int(max([max(lst[i+K]) - min(lst[i])+1 for i in range(time)])))

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)<3:
        return max(A)
    m = -A[0]+A[1]
    args = [-1]
    for i in range(len(A)-2):
        s3 = A[i] - A[i+1] + A[i+2]
        if m<s3:
            m = s3
            args = [i]
        elif m==s3:
            args.append(i)
    s3 = A[-2]-A[-1]
    if m<s3:
        m = s3
        args = [len(A)-1]
    elif m==s3:
        args.append(len(A)-1)
    cands = []
    for arg in args:
        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:]
        cands.append(iter(B, cnt+1))
    return max(cands)


print(iter(arr, 0))

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+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][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):
            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][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][1] + ll[i*2+2][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][1] + ll[i*2+2][1])
                lll.append(co)
            elif i == 0:
                co += ll[i*2+1+2*k][1] + ll[i*2+2+2*k][1] - (ll[i*2+1][1] + ll[i*2+2][1])
                lll.append(co)
            else:
                co += ll[i*2+1+2*k][1] + ll[i*2+2+2*k][1] - (ll[i*2+1][1] + ll[i*2+2][1])
                lll.append(co)
print(max(lll))

Submission status: Runtime Error

Solution code:
n_k = input()
n_k_list = n_k.split()
K = int(n_k_list[1])
people = input()
people_list = []
now = people[0]
for i in people[1:]:
    if now[-1] == i:
        now += i
    else:
        people_list.append(now)
        now = i
people_list.append(now)

max_count = 0
change_index = None
while K:
    for index, cell in enumerate(people_list):
        if cell[0] == "0":
            if index == 0:
                this_count = cell.__len__() + people_list[index + 1].__len__()
            elif index == people_list.__len__() - 1:
                this_count = cell.__len__() + people_list[index - 1].__len__()
            else:
                this_count = cell.__len__() + people_list[index - 1].__len__() + people_list[index + 1].__len__()
            if max_count < this_count:
                max_count = this_count
                change_index = index
    if change_index is not None:
        change_char = ""
        for z in range(people_list[change_index].__len__()):
            change_char += "1"

        if change_index == 0:
            new_char = people_list[change_index + 1] + change_char
            people_list[change_index] = new_char
            del people_list[change_index + 1]
        elif change_index == people_list.__len__() - 1:
            new_char = people_list[change_index - 1] + change_char
            people_list[change_index] = new_char
            del people_list[change_index - 1]
        else:
            new_char = people_list[change_index - 1] + people_list[change_index + 1] + change_char
            people_list[change_index] = new_char
            del people_list[change_index + 1]
            del people_list[change_index - 1]
        K -= 1
    else:
        break
print(max(people_list, key=len).__len__())

Submission status: Runtime Error

Solution code:
n, k = map(int,input().split())
s = input()
ha = []
owa = []
ju = 0
for i in range(len(s)) :
    if s[i] == "1" :
        if ju == 0 :
            ha.append(i)
            ju = 1
    else :
        if ju == 1 :
            owa.append(i - 1)
            ju = 0
if ha[0] != 0 :
    ha.insert(0,0)
    owa.insert(0,0)
owa.append(len(s)-1)
if ha[-1] != len(s) - 1 :
    ha.append(len(s) - 1)
#print(ha,owa)
dai = 0
for i in range(len(owa) - k) :
    dai = max(dai,owa[i+k] - ha[i] + 1)
if len(ha) > k :
    print(dai)
else :
    print(len(s))

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())
s = list(map(int, input()))
count = 0
ans = 0
j = []
x = []
for i in range(len(s)):
    if s[i] == 0:
        x.append(i)
    else:
        j.append(x)
        x = []
m = sorted(j)
for i in range(k):
    for i in m[-1]:
        s[i] = 1
    del m[-1]

for i in s:
    if i == 1:
        count += 1
        if ans < count:
            ans = count
    else:
        count = 0

print(ans)

Submission status: Runtime Error

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

o0=S.split('0')
o1=[x for x in o0 if x]
o2=[len(x) for x in o1]

z0=S.split('1')
z1=[x for x in z0 if x]
z2=[len(x) for x in z1]

if(N==1): print(1)
else:
  anslist=[]
  if(S[0]=='0'):
    anslist.append(sum(o2[0:K])+sum(z2[0:K]))
    del z2[0]
  if(S[-1]=='0'):
    anslist.append(sum(o2[-K:])+sum(z2[-K:]))
    del z2[-1]

  for i in range(len(o2)-K):
    anslist.append(sum(o2[i:i+K+1])+sum(z2[i:i+K]))

    print(max(anslist))

Submission status: Runtime Error

Solution code:
N, K = [int(i) for i in input().split()]
S = input()
arr = []
val = 1
temp = S[0]
if temp == '1':
    arr += [0]
for s in S[1:]:
    if s == temp:
        val+=1
    else:
        arr += [val]
        val = 1
    temp = s
if val != 0:
    arr += [val]

print(max([sum(arr[i:i+K*2+1]) if i%2!=0 else sum(arr[i:i+K*2]) for i in range(len(arr)-(K*2)+1)]))

Submission status: Runtime Error

Solution code:
import functools

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

before = s[0]
count = 1
lst = []
head = True if before == '1' else False
tail = True if s[-1] == '1' else False

for i in s[1:]:
    if i == before:
        count += 1
    else:
        lst.append(count)
        count = 1
        before = i
lst.append(count)

S = 0

lst1 = lst
if not head:
    S = sum(lst1[:2*k])
    lst1 = lst1[1:]
if not tail:
    S = max(S, sum(lst1[-2*k:]))
    lst1 = lst1[:-1]

lst1.insert(0,0)
accum_lst = list(itertools.accumulate(lst1))

#print('accum_lst :', accum_lst)
N = len(lst1)
if N <= 2*k + 1:
    S = max(S, accum_lst[-1])
else:
    K = 2*k+1
    for i in range(0, N-K, 2):
        S = max(S, accum_lst[i+K] - accum_lst[i])
        #print(S, accum_lst[i+K], accum_lst[i])

print(S)

Submission status: Runtime Error

Solution code:
from itertools import*
from numpy import*
n,k=map(int,input().split())
k2=k+1
s=input()
if len(s)==1:
    if s=="1":
        print(1)
    else:
        print(0)
    exit()
x=[0]+[len(list(v))for k,v in groupby(s)if k=="0"]+[0]
y=[0]+[len(list(v))for k,v in groupby(s)if k=="1"]+[0]
x1=array(x[:-k])
x2=array(x[k:])
x3=sum(x[:k])
t=x3+cumsum(x2-x1)
y1=array(y[:-k2])
y2=array(y[k2:])
y3=sum(y[:k2])
f=y3+cumsum(y2-y1)
#print(x,y)
#print(t,f)
print(max(max(t+f),x3+y3))

Submission status: Runtime Error

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

a = []
b = []

index = 0
length = 0
judge = "0"
for i in range(len(s)):
	if s[i] == judge:
		length += 1
	if s[i] != judge:
		if judge == "0":
			b.append(length)
		if judge == "1":
			a.append(length)
		length = 1
		judge = str((int(judge)+1)%2)
if judge == "0":
	b.append(length)
if judge == "1":
	a.append(length)


if len(a) == len(b):
	M = len(a)
	if K >= M:
		print(len(s))
		exit()
	
	ans = 0
	ans_new = 0
	for i in range(K):
		ans += a[i]+b[i]
	ans_max = ans
	for i in range(M-K):
		if i == 0:
			ans_new = ans-b[0]+b[K]+a[K]
		if i != 0:
			ans_new = ans-a[i-1]-b[i]+b[K+i]+a[K+i]
		ans_max = max(ans_max,ans_new)
		ans = ans_new
	print(ans_max)
	exit()
	
if len(a) < len(b):
	M = len(b)
	if K >= M:
		print(len(s))
		exit()
	
	ans = 0
	for i in range(K):
		ans += a[i]+b[i]
	ans_max = ans
	for i in range(M-K+1):
		if i == 0:
			if M-1 <= K:
				ans_new = ans-b[0]+b[K]
			else:
				ans_new = ans-b[0]+b[K]+a[K]
		if i == M-K:
			ans_new = ans-a[M-K-1]-b[M-K]+b[M-1]
		if i != 0 and i != M-K:
			ans_new = ans-a[i-1]-b[i]+b[K+i]+a[K+i]
		
		ans_max = max(ans_max,ans_new)
		ans = ans_new
	print(ans_max)
	exit()

Submission status: Runtime Error

Solution code:
import sys
n,k = map(int,input().split())
st = input()
s = []
for i in st:
    s.append(i)
s.append('-1')
man = 1 if s[0] == '0' else 0

check_ls = [];check_num = 0;tyokuritu = []
for i in range(len(s)):
    check_num += 1
    if s[i] != s[i+1]:
        check_ls.append(check_num)
        check_num = 0
        if s[i] == '0':
            tyokuritu.append(True)
    if s[i+1] == '-1':break
if k >= len(tyokuritu):print(len(s)-1);sys.exit()
tyokuritu.append(False)
ios_check = [0]+[check_ls[0]]*(len(check_ls))
for i in range(len(check_ls)-1):
    ios_check[i+1+1] = check_ls[i+1] + ios_check[i+1]
ans = 0
for i in range(0,10**5,2):
    ans = max(ans,ios_check[k*2+1+i+man] - ios_check[i+man])
    if 2*k-1-man+i+2 >= len(check_ls):break
if man == 1:ans = max(ans,ios_check[k*2-1])
print(ans)

Submission status: Runtime Error

Solution code:
from sys import exit
N,K = [int(n) for n in input().split()]
# N = int(input())
# a = [int(input()) for _ in range(N)]
S = str(input())
L = len(S)
if(S[0] == "0"):#偶数Indexがブリッジ
    st0 = True
else:
    st0 = False
tmp = S[0]
lis = []
maxlen = 0
for i in range(1,L):
    if(S[i]!=S[i-1]):
        lis.append(len(tmp))
        maxlen = max(maxlen,len(tmp))
        tmp =S[i]
    else:
        tmp+=S[i]
maxlen = max(maxlen,len(tmp))
lis.append(len(tmp))
# print(maxlen)
# print(lis)
# print(maxlen)
# print(st0)
ans = maxlen
# tmpans = maxlen
L = len(lis)
if(st0):#奇数Indexをうめる時！0001010100000
    l = 0
    r = 0
    tmpans = 0
    tmpk = 0#うめたかず
    for i in range(0,L,2):
        if(tmpk < K and i+1 < L):#うめられるだけうめてない
            tmpk+=1
            tmpans+=lis[i]+lis[i+1]
        elif i+1 < L:#うめきった
            ans = max(ans,tmpans)
            if(l == 0):
                tmpans-=lis[r]
                l=9
            else:
                tmpans-=lis[r]+lis[r+1]
            tmpans+=lis[i]+lis[i+1]
            # print(tmpans,i)
            r+=2
        elif i+1 == L:#0が最後
            ans = max(ans,tmpans)
            tmpans-=lis[r]+lis[r+1]
            tmpans+=lis[i]
    print(max(ans,tmpans))
else:#偶数Indexをうめる時！11101010110011
    r = 0
    tmpans = lis[0]
    tmpk = 0#うめたかず
    for i in range(1,L,2):
        if(tmpk < K and i+1 < L):#うめられるだけうめてない
            tmpk+=1
            tmpans+=lis[i]+lis[i+1]

        elif i+1 < L:#うめきった
            ans = max(ans,tmpans)
            # print(tmpans,i)
            tmpans-=lis[r]+lis[r+1]
            tmpans+=lis[i]+lis[i+1]
            r+=2
        elif i+1 == L:#0が最後
            ans = max(ans,tmpans)
            tmpans-=lis[r]+lis[r+1]
            tmpans+=lis[i]
    print(max(ans,tmpans))
# 14 2
# 11101010110011
# 10 1
# 0000000001

Submission status: Runtime Error

Solution code:
import numpy as np

n, k = [int(x) for x in input().split()]
s = input()

zero_list = np.array([len(x) for x in s.split("1") if len(x) > 0])
one_list = np.array([len(x) for x in s.split("0") if len(x) > 0])

if k >= len(zero_list):
    print(len(s))
else:
    max_num = 0

    if len(zero_list) < len(one_list):
        for i in range(len(zero_list) - k + 1):
            tmp = zero_list[i:i+k].sum() + one_list[i:i+k+1].sum()
            if tmp > max_num:
                max_num = tmp
    else:
        if len(zero_list) == len(one_list) and int(s[0]) == 1:
            zero_list = np.array([0, *zero_list])
        max_num = zero_list[:k].sum() + one_list[:k].sum()
        for i in range(len(one_list) - k):
            tmp = zero_list[i+1:i+k+1].sum() + one_list[i:i+k+1].sum()
            if tmp > max_num:
                max_num = tmp
        tmp = zero_list[-k:].sum() + one_list[-k:].sum()
        if tmp > max_num:
            max_num = tmp

    print(max_num)

Submission status: Runtime Error

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

o0=S.split('0')
o1=[x for x in o0 if x]
o2=[len(x) for x in o1]

z0=S.split('1')
z1=[x for x in z0 if x]
z2=[len(x) for x in z1]

anslist=[]
if(S[0]=='0'):
  anslist.append(sum(o2[0:K])+sum(z2[0:K]))
  del z2[0]
if(S[-1]=='0'):
  anslist.append(sum(o2[-K:])+sum(z2[-K:]))
  del z2[-1]

for i in range(len(o2)-K):
  anslist.append(sum(o2[i:i+K+1])+sum(z2[i:i+K]))

print(max(anslist))

Submission status: Runtime Error

Solution code:
N, K = map(int,input().split())
s = input()
a = [int(c) for c in s]

b = []
c = 0
d = 0
for i in a:
	if(i == 1):
		if(d>0):
			b.append(d)
			d = 0
		c = c + 1
	else:
		if(c>0):
			b.append(c)
			c = 0
		d = d + 1
b.append(max(c,d))

ans = 0
e = 0
k = 2*K+1
if(a[0] == 1):
	e  = sum(b[0:k])
	for l in range(0,len(b), 2):
		e = e - b[l] - b[l+1] + b[k] + b[k+1]
		if(ans < e):
			ans = e
else:
	e = sum(b[1:k])
	for l in range(1,len(b), 2):
		e = e - b[l] - b[l+1] + b[k] + b[k+1]
		if(ans < e):
			ans = e
	if(sum(b[0:2*K]) > ans):
		ans = sum(b[0:2*K])
if(2*K > len(b)):
	ans = sum(b)
print(ans)

Submission status: Runtime Error

Solution code:
N,K = map(int,input().split())
S = list(input())
l = []
memo = 0
for i in range(len(S)-1):
    if i==len(S)-1-memo:
        break
    if S[i] =="0" and S[i+1]=="0":
        S.pop(i)
        memo += 1
else:
    S=",".join(S)


res,tmp = 0,0
bf,mres = 0,0

for i in range(len(S)):
    res += 1
    if K < 0:
        tmp = l[-1*(K+1)]
        res -= (tmp-bf)
        bf = tmp
    if S[i] == "0":
        K -= 1
        l.append(i)
    mres = max(mres,res)
else:
    print(mres)

Submission status: Runtime Error

Solution code:
def A(S,K):
  for i,s in enumerate(S):
    if s == '0':
      if K >= 1:
        a = i
        while S[a] == '0':
          S[a] = '1'
          a += 1
        K-=1
      else:
        break
  return i

def B(S,K):
  n = 0
  for i in range(len(S)-1,0,-1):
    if S[i] == '0':
      if K >= 1:
        a = i
        while S[a] == '0':
          S[a] = '1'
          a -= 1
        K-=1
      else:
        break
    n += 1
  return n
      
      
N,K = (int(i) for i in input().split())
S = input()

if len(S) <= 1:
  if S == '1':
    print('1')
  else:
    print('0')
else:
  a = A([s for s in S],K)
  b = B([s for s in S],K)

if a > b:  
  print(a)
else:
  print(b)

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())
s = list(input())
d = dict() # d[poc.niza] = [dulj.niza, lijevo, desno]

niz = []
for i in range(len(s)):
    if len(niz) and niz[0] <= i <= niz[1]:
        continue
    if not int(s[i]):
        d[i] = [1]
        j = i + 1
        while j < len(s) and not int(s[j]):
            d[i][0] += 1
            j += 1
        niz = [i, j]
for i in d:
    j = i - 1
    d[i] += [0, 0]
    while j >= 0 and int(s[j]):
        d[i][1] += 1
        j -= 1
    j = i + d[i][0]
    while j < len(s) and int(s[j]):
        d[i][2] += 1
        j += 1
d1 = d.copy()

for i in d1:
    d1[i] = sum(d1[i])

key = list(d1.values())
key.sort()
sol = []

for i in range(k):
    for j in d1:
        if d1[j] == max(key):
            sol.append(j)
            key.remove(max(key))
            break
        
        
for i in sol:
    for j in range(d[i][0]):
        s[i + j] = '1'

niz = []
d2 = dict()

for i in range(len(s)):
    if len(niz) and niz[0] <= i <= niz[1]:
        continue
    if int(s[i]):
        d2[i] = 1
        j = i + 1
        while j < len(s) and int(s[j]):
            d2[i] += 1
            j += 1
        niz = [i, j]
        
print(max(d2.values()))

Submission status: Runtime Error

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

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

for i in range(10):
	A.append(0)


val = 0
if A[0] < 0:
	for i in range(2*K): 
		val += abs(A[i])
	currentMax = val
	old = abs(A[0])
	new = abs(A[1+2*K+1]) + abs(A[1+2*K+3])
	val += new - old
	if val > currentMax:
		currentMax = val
	for i in range(3, len(A)-2*K-2, 2):
		old_sum_of_two = abs(A[i]) + abs(A[i+1])
		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)
else:
	for i in range(2*K+1): 
		val += abs(A[i])
	currentMax = val
	for i in range(0, len(A)-2*K-2, 2):
		old_sum_of_two = abs(A[i]) + abs(A[i+2])
		sum_of_two = abs(A[i+2*K+1]) + abs(A[i+2*K+2])
		val += sum_of_two - old_sum_of_two
		if val > currentMax:
			currentMax = val
	print(currentMax)

Submission status: Runtime Error

Solution code:
N,K = map(int,input().split())
S = input()
N = len(S)
flag = int(S[0])
S_1 = [len(x) for x in S.split('0') if len(x) is not 0]
S_0 = [len(x) for x in S.split('1') if len(x) is not 0]

count = 0
for i in range(len(S_0)-K+1):
  if flag == 0:
    if i == 0:
      tmp = 0
    else:
      tmp = S_1[i-1]
    for j in range(i,i+K):
      if j == len(S_1):
        tmp += S_0[j]
      else:
        tmp += (S_0[j] + S_1[j])
  else:
    tmp = S_1[i+K]
    for j in range(i,i+K):
      tmp += (S_1[j] + S_0[j])
    
  if tmp > count:
    count = tmp
    
if len(S_0) == 0:
  print(S_1[0])
elif len(S_1) == 0:
  print(S_0[0])
else:
  print(count)

Submission status: Runtime Error

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

max_streek = 0
count = 1
counting = False
last = ""
maxed_streek = 0


while True:
	if k == 0:
		break

	for _ in s:
		if _ == "0":
			counting = True
			if last == "0" or counting:
				max_streek += 1
			last = "0"
		else:
			last = "1"
			counting = False
			if max_streek > maxed_streek:
				maxed_streek = max_streek 
			max_streek = 0

	for index,_ in enumerate(s):
		if _ == "0":
			counting = True
			if last == "0" or counting:
				max_streek += 1
			last = "0"
		else:
			last = "1"
			counting = False
			max_streek = 0
			
		if maxed_streek == max_streek:
			print("ここやった！")
			
			zero = "0"
			one = "1"
			max_str = zero * max_streek
			max_one = one * max_streek
			print(str(max_str),str(max_one))
			print(s)
			s = s.replace(str(max_str),str(max_streek),1)
			print(s)
	
	k -= 1
	
for _ in s:
	if _ == "1":
		counting = True
		if last == "1" or counting:
			max_streek += 1
		last = "1"
	else:
		last = "0"
		counting = False
		if max_streek > maxed_streek:
			maxed_streek = max_streek 
		max_streek = 0
		
print(maxed_streek)