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:
from sys import exit
def groupby(a):
    a2=[[a[0],1]]
    for i in range(1,len(a)):
        if a2[-1][0]==a[i]:
            a2[-1][1]+=1
        else:
            a2.append([a[i],1])
    return a2

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

if

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

Submission status: Runtime Error

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

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

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

Submission status: Runtime Error

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

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

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

Submission status: Runtime Error

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

n,k=map(int,input().split())
s=list(input())
x=groupby(s)
l=len(x)
y=[x[i][1] for i in range(l)]
if x[0][0]=="1":
    su=sum(y[:2*k+1])
    ans=su
    l_sub=(l-(2*k+1))//2
    for i in range(l_sub):
        su-=y[2*i]
        su-=y[2*i+1]
        su+=y[2*k+1+(2*i)]
        su+=y[2*k+1+(2*i+1)]
        ans=max(su,ans)
    if l%2==0:
        su-=y[2*l_sub]
        su+=y[2*k+1+2*i]
        ans=max(su,ans)
    print(ans)
else:
    su=sum(y[:2*k])
    ans=su
    y.pop(0)
    l-=1
    l_sub=(l-(2*k+1))//2
    for i in range(l_sub):
        su-=y[2*i]
        su-=y[2*i+1]
        su+=y[2*k+1+(2*i)]
        su+=y[2*k+1+(2*i+1)]
        ans=max(su,ans)
    if l%2==0:
        su-=y[2*l_sub]
        su+=y[2*k+1+2*l_sub]
        ans=max(su,ans)
    print(ans)

Submission status: Runtime Error

Solution code:
import os, sys, re, math

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

bunches = []
for s in S:
    if not bunches:
        bunches.append(1 if s == '1' else -1)
    else:
        if s == '1':
            if bunches[-1] > 0:
                bunches[-1] += 1
            else:
                bunches.append(1)
        else:
            if bunches[-1] < 0:
                bunches[-1] -= 1
            else:
                bunches.append(-1)

max_count = 0

if len(bunches) == 1:
    print(1)
    sys.exit(0)

for i in range(1, K + 1):
    nstart = 0 if bunches[0] < 0 else 1
    mcount = 0
    while nstart < len(bunches):
        nidx = nstart
        count = 0 if nstart == 0 else bunches[nstart - 1]
        for j in range(i):
            if nidx == len(bunches):
                break
            count -= bunches[nidx]
            if nidx < len(bunches) - 1:
                count += bunches[nidx + 1]
            nidx += 2
        mcount = max(count, mcount)
        nstart += 2
    max_count = max(mcount, max_count)

print(max_count)

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(2000)

n, k = list(map(int, input().split(' ')))
s = input()
ll = []
prev = ''
temp = 0
for c in s:
  t = int(c)
  if t != prev:
    prev = t
    if temp:
      ll.append(temp)
    temp = 0
  temp += 1 if t else -1
if temp:
  ll.append(temp)

def f(index, l, k):
  if k == 0 or index >= len(l):
    return max(l)
  if l[index] > 0:
    return f(index+1, l, k)
  mmm = -l[index] + (l[index-1] if index >= 1 else 0) + (l[index+1] if index+1 < len(l) else 0)
  if len(l) == 1:
    new_l = None
  elif index == 0:
    new_l = [mmm] + l[2:]
  elif index == 1:
    new_l = [mmm] + l[3:]
  elif index == len(l)-1:
    new_l = l[:-2] + [mmm]
  elif index == len(l)-2:
    new_l = l[:-3] + [mmm]
  else:
    new_l = l[:index-2] + [mmm] + l[index+2:]
  t1 = f(index + 1, l[:], k)
  t2 = f(index - 1, new_l, k-1) if new_l else 0
  return max(t1, t2)  

print(f(0, ll, k))

Submission status: Runtime Error

Solution code:
n, k = list(map(int, input().split(' ')))
s = input()
ll = []
prev = ''
temp = 0
for c in s:
  t = int(c)
  if t != prev:
    prev = t
    if temp:
      ll.append(temp)
    temp = 0
  temp += 1 if t else -1
if temp:
  ll.append(temp)

def f(index, l, k):
  if k == 0 or index >= len(l):
    return max(l)
  if l[index] > 0:
    return f(index+1, l, k)
  mmm = -l[index] + (l[index-1] if index >= 1 else 0) + (l[index+1] if index+1 < len(l) else 0)
  if len(l) == 1:
    new_l = None
  elif index == 0:
    new_l = [mmm] + l[2:]
  elif index == 1:
    new_l = [mmm] + l[3:]
  elif index == len(l)-1:
    new_l = l[:-2] + [mmm]
  elif index == len(l)-2:
    new_l = l[:-3] + [mmm]
  else:
    new_l = l[:index-2] + [mmm] + l[index+2:]
  t1 = f(index + 1, l[:], k)
  t2 = f(index - 1, new_l, k-1) if new_l else 0
  return max(t1, t2)  

print(f(0, ll, k))

Submission status: Runtime Error

Solution code:
import os, sys, re, math

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

bunches = []
for s in S:
    if not bunches:
        bunches.append(1 if s == '1' else -1)
    else:
        if s == '1':
            if bunches[-1] > 0:
                bunches[-1] += 1
            else:
                bunches.append(1)
        else:
            if bunches[-1] < 0:
                bunches[-1] -= 1
            else:
                bunches.append(-1)

max_count = len(list(filter(lambda x: x > 0, bunches)))

if len(bunches) == 1:
    print(1)
    sys.exit(0)

for i in range(1, K + 1):
    nstart = 0 if bunches[0] < 0 else 1
    mcount = 0
    while nstart < len(bunches):
        nidx = nstart
        count = 0 if nstart == 0 else bunches[nstart - 1]
        for j in range(i):
            if nidx == len(bunches):
                break
            count -= bunches[nidx]
            if nidx < len(bunches) - 1:
                count += bunches[nidx + 1]
            nidx += 2
        mcount = max(count, mcount)
        nstart += 2
    max_count = max(mcount, max_count)

print(max_count)

Submission status: Runtime Error

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

#左の1の連続数、それ自体の0の連続数、右の1の連続数を記した、
#0の塊についての情報を持つ配列を作る。

#そのためにまず連続数を記録した配列…
renzoku=[] #1と0は交互。
temp=1 #現在調査してる塊についての連続数を一時期録
num=0
while 1:
  if num==n-1:
    renzoku.append(temp)
    break
  else:
    if s[num]==s[num+1]:
      temp+=1
    else:
      renzoku.append(temp)
      temp=1
  num+=1

zeroinfo=[]


if s[0]=="0": #1つ目の塊が0な場合
  for i in range(0,len(renzoku),2):
    if i==0:
      a=[0,renzoku[i],renzoku[i+1]]
      zeroinfo.append(a)
    elif i==len(renzoku)-1:
      a=[renzoku[i-1],renzoku[i],0]
      zeroinfo.append(a)
    else:
      a=[renzoku[i-1],renzoku[i],renzoku[i+1]]
      zeroinfo.append(a)
else:
  for i in range(1,len(renzoku),2):
    if i!=len(renzoku)-1:
      a=[renzoku[i-1],renzoku[i],renzoku[i+1]]
      zeroinfo.append(a)
    else:
      a=[renzoku[i-1],renzoku[i],0]
      zeroinfo.append(a)
#print(zeroinfo)
len0=len(zeroinfo)

if len0<=k:
  print(n)
  exit()
ans=0
for i in range(len0-k+1):
  temp=0
  #それぞれの連続和を比較
  for j in range(i,i+k):
    if j!=i+k-1:
      temp+=zeroinfo[j][0]+zeroinfo[j][1]
    else:
      temp+=sum(zeroinfo[j])
  ans=max(ans,temp)

print(ans)

Submission status: Runtime Error

Solution code:

import numpy as np
import sys
sys.setrecursionlimit(100000)

directions = np.array([[1, 0], [0, 1], [-1, 0], [0, -1]])


def acinput():
    return list(map(int, input().split(" ")))


sys.setrecursionlimit(100000)


directions = [[1, 0], [0, 1], [-1, 0], [0, -1]]
directions = list(map(np.array, directions))


def acinput():
    return list(map(int, input().split(" ")))


N, K = acinput()
s = list(map(int, list(input())))

nums = [1]
for i in range(1, N):
    if s[i] == s[i-1]:
        nums[-1] += 1
    else:
        nums.append(1)

if s[0] != 1:
    nums.insert(0, 0)
nums_cs = np.cumsum(nums)

res = nums_cs[2*K]
# if len(nums_cs) > 1:
#     res = nums_cs[min(len(nums_cs)-1, 2*K-1)]
# else:
#     res = nums_cs[0]

for i in range(1,len(nums_cs)-2*K+1):
    if i % 2 == 0:
        #print(i)
        res = max(res, nums_cs[min(i+2*K,len(nums_cs)-1)]-nums_cs[i-1])


print(res)

Submission status: Runtime Error

Solution code:

import numpy as np
import sys
sys.setrecursionlimit(100000)

directions = np.array([[1, 0], [0, 1], [-1, 0], [0, -1]])


def acinput():
    return list(map(int, input().split(" ")))


sys.setrecursionlimit(100000)


directions = [[1, 0], [0, 1], [-1, 0], [0, -1]]
directions = list(map(np.array, directions))


def acinput():
    return list(map(int, input().split(" ")))


N, K = acinput()
s = list(map(int, list(input())))

nums = [1]
for i in range(1, N):
    if s[i] == s[i-1]:
        nums[-1] += 1
    else:
        nums.append(1)

if s[0] != 1:
    nums.insert(0, 0)
nums_cs = np.cumsum(nums)

res = nums_cs[2*K]
# if len(nums_cs) > 1:
#     res = nums_cs[min(len(nums_cs)-1, 2*K-1)]
# else:
#     res = nums_cs[0]

for i in range(1,len(nums_cs)-2*K+1):
    if i % 2 == 0:
        print(i)
        res = max(res, nums_cs[min(i+2*K,len(nums_cs)-1)]-nums_cs[i-1])


print(res)

Submission status: Runtime Error

Solution code:

import numpy as np
import sys
sys.setrecursionlimit(100000)

directions = np.array([[1, 0], [0, 1], [-1, 0], [0, -1]])


def acinput():
    return list(map(int, input().split(" ")))


sys.setrecursionlimit(100000)


directions = [[1, 0], [0, 1], [-1, 0], [0, -1]]
directions = list(map(np.array, directions))


def acinput():
    return list(map(int, input().split(" ")))


N, K = acinput()
s = list(map(int, list(input())))

nums = [1]
for i in range(1, N):
    if s[i] == s[i-1]:
        nums[-1] += 1
    else:
        nums.append(1)

if s[0] != 1:
    nums.insert(0, 0)
nums_cs = np.cumsum(nums)

res = nums_cs[2*K]
# if len(nums_cs) > 1:
#     res = nums_cs[min(len(nums_cs)-1, 2*K-1)]
# else:
#     res = nums_cs[0]

for i in range(1,len(nums_cs)-2*K):
    if i % 2 == 0:
        res = max(res, nums_cs[i+2*K]-nums_cs[i-1])


print(res)

Submission status: Runtime Error

Solution code:

import numpy as np
import sys
sys.setrecursionlimit(100000)

directions = np.array([[1, 0], [0, 1], [-1, 0], [0, -1]])


def acinput():
    return list(map(int, input().split(" ")))


sys.setrecursionlimit(100000)


directions = [[1, 0], [0, 1], [-1, 0], [0, -1]]
directions = list(map(np.array, directions))


def acinput():
    return list(map(int, input().split(" ")))


N, K = acinput()
s = list(map(int, list(input())))

nums = [1]
for i in range(1, N):
    if s[i] == s[i-1]:
        nums[-1] += 1
    else:
        nums.append(1)


nums_cs = np.cumsum(nums)



#print(res)
if s[0]==1:
    if len(nums_cs) > 1:
       res = nums_cs[min(len(nums_cs)-1,2*K)]
    else:
        res=nums_cs[0]
    for i in range(1,len(nums_cs)-2*K):
        if i%2==0:
            res=max(res,nums_cs[i+2*K+1]-nums_cs[i-1])
else:    
    if len(nums_cs) > 1:
       res = nums_cs[min(len(nums_cs)-1,2*K-1)]
    else:
        res=nums_cs[0]

    for i in range(1,len(nums_cs)-2*K-1):
        if i%2==1:
            res=max(res,nums_cs[i+2*K+1]-nums_cs[i])


print(res)

Submission status: Runtime Error

Solution code:

import numpy as np
import sys
sys.setrecursionlimit(100000)

directions = np.array([[1, 0], [0, 1], [-1, 0], [0, -1]])


def acinput():
    return list(map(int, input().split(" ")))


sys.setrecursionlimit(100000)


directions = [[1, 0], [0, 1], [-1, 0], [0, -1]]
directions = list(map(np.array, directions))


def acinput():
    return list(map(int, input().split(" ")))


N, K = acinput()
s = list(map(int, list(input())))

nums = [1]
for i in range(1, N):
    if s[i] == s[i-1]:
        nums[-1] += 1
    else:
        nums.append(1)


nums_cs = np.cumsum(nums)

if len(nums_cs) != 1:
       res = nums_cs[2*K-1]
else:
    res=nums_cs[0]

#print(res)
if s[0]==1:

    for i in range(1,len(nums_cs)-2*K-1):
        if i%2==0:
            res=max(res,nums_cs[i+2*K+1]-nums_cs[i-1])
else:    

    for i in range(1,len(nums_cs)-2*K-1):
        if i%2==1:
            res=max(res,nums_cs[i+2*K+1]-nums_cs[i])


print(res)

Submission status: Runtime Error

Solution code:

import sys
sys.setrecursionlimit(100000)
import numpy as np

directions=np.array([[1,0],[0,1],[-1,0],[0,-1]])


def acinput():
    return list(map(int,input().split(" ")))




import sys
sys.setrecursionlimit(100000)

import numpy as np

directions = [[1, 0], [0, 1], [-1, 0], [0, -1]]
directions = list(map(np.array, directions))

def acinput():
    return list(map(int, input().split(" ")))

N,K=acinput()
s=list(map(int,list(input())))

nums=[1]
for i in range(1,N):
    if s[i]==s[i-1]:
        nums[-1]+=1
    else:
        nums.append(1)


nums_cs=np.cumsum(nums)

if K>1:
    res=nums_cs[2*K-1]
else:
    res=nums_cs[0]
for i in range(len(nums_cs)-2*K-1):
    res=max(res,nums_cs[i+2*K+1]-nums_cs[i])



print(res)

Submission status: Runtime Error

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

#K=0
#S="1111111"
#N=len(S)

LIST=[[[0] for j in range (2)]for i in range(N)]

t=S[0]
c=0
k=0
for i in range(N):
    if(t!=S[i]):
        LIST[k][0]=c
        LIST[k][1]=t
        t=S[i]
        k+=1
        c=0
    c+=1

LIST[k][0]=c
LIST[k][1]=t
LIST=LIST[0:k+1]
MAX=0

s=0
l=0
r=0
f=0
if(LIST[0][1]=="1"):
    s+=LIST[0][0]
    l=0
    r=1
    f=1
MAX=0

while(K>0 and N>r ):
    s+=LIST[r][0]
    K-=1
    if(len(LIST)-1==r):
        break
    r+=1
    s+=LIST[r][0]
    if(len(LIST)-1==r):
        break
    r+=1
MAX=max(s,MAX)

if f==1:
    while(r<len(LIST)):
        if(l<len(LIST)):
            s-=LIST[l][0]
            l+=1
        if(l<len(LIST)):
            s-=LIST[l][0]
            l+=1

        s+=LIST[r][0]
        MAX=max(s,MAX)
        if(len(LIST)-1==r):
            break
        r+=1
        s+=LIST[r][0]
        MAX=max(s,MAX)
        if(len(LIST)-1==r):
            break
        r+=1
else:
    s-=LIST[l][0]
    l+=1

    s+=LIST[r][0]
    MAX=max(s,MAX)
    if(len(LIST)-1==r):
        print(MAX)
        exit()
    r+=1
    s+=LIST[r][0]
    MAX=max(s,MAX)
    if(len(LIST)-1==r):
        print(MAX)
        exit()
    r+=1
    while(r<len(LIST)):
        s-=LIST[l][0]
        l+=1
        s-=LIST[l][0]
        l+=1

        s+=LIST[r][0]
        MAX=max(s,MAX)
        if(len(LIST)-1==r):
            break
        r+=1
        s+=LIST[r][0]
        MAX=max(s,MAX)
        if(len(LIST)-1==r):
            break
        r+=1

print(MAX)

Submission status: Runtime Error

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

#K=1
#S="1"
#N=len(S)

LIST=[[[0] for j in range (2)]for i in range(N)]

t=S[0]
c=0
k=0
for i in range(N):
    if(t!=S[i]):
        LIST[k][0]=c
        LIST[k][1]=t
        t=S[i]
        k+=1
        c=0
    c+=1

LIST[k][0]=c
LIST[k][1]=t
LIST=LIST[0:k+1]
MAX=0

s=0
l=0
r=0
f=0
if(LIST[0][1]=="1"):
    s+=LIST[0][0]
    l=0
    r=1
    f=1
MAX=0

while(K>0 and N>r ):
    s+=LIST[r][0]
    K-=1
    if(len(LIST)-1==r):
        break
    r+=1
    s+=LIST[r][0]
    if(len(LIST)-1==r):
        break
    r+=1
MAX=max(s,MAX)

if f==1:
    while(r<N):
        s-=LIST[l][0]
        l+=1
        s-=LIST[l][0]
        l+=1

        s+=LIST[r][0]
        MAX=max(s,MAX)
        if(len(LIST)-1==r):
            break
        r+=1
        s+=LIST[r][0]
        MAX=max(s,MAX)
        if(len(LIST)-1==r):
            break
        r+=1
else:
    s-=LIST[l][0]
    l+=1

    s+=LIST[r][0]
    MAX=max(s,MAX)
    if(len(LIST)-1==r):
        print(MAX)
        exit()
    r+=1
    s+=LIST[r][0]
    MAX=max(s,MAX)
    if(len(LIST)-1==r):
        print(MAX)
        exit()
    r+=1
    while(r<N):
        s-=LIST[l][0]
        l+=1
        s-=LIST[l][0]
        l+=1

        s+=LIST[r][0]
        MAX=max(s,MAX)
        if(len(LIST)-1==r):
            break
        r+=1
        s+=LIST[r][0]
        MAX=max(s,MAX)
        if(len(LIST)-1==r):
            break
        r+=1

print(MAX)

Submission status: Runtime Error

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

#K=2
#S="011101010110011"
#N=len(S)

LIST=[[[0] for j in range (2)]for i in range(N)]

t=S[0]
c=0
k=0
for i in range(N):
    if(t!=S[i]):
        LIST[k][0]=c
        LIST[k][1]=t
        t=S[i]
        k+=1
        c=0
    c+=1

LIST[k][0]=c
LIST[k][1]=t
LIST=LIST[0:k+1]
MAX=0

s=0
l=0
r=0
f=0
if(LIST[0][1]=="1"):
    s+=LIST[0][0]
    l=0
    r=1
    f=1
MAX=0

while(K>0):
    s+=LIST[r][0]
    K-=1
    if(len(LIST)-1==r):
        break
    r+=1
    s+=LIST[r][0]
    if(len(LIST)-1==r):
        break
    r+=1

if f==1:
    while(r<N):
        s-=LIST[l][0]
        l+=1
        s-=LIST[l][0]
        l+=1

        s+=LIST[r][0]
        MAX=max(s,MAX)
        if(len(LIST)-1==r):
            break
        r+=1
        s+=LIST[r][0]
        MAX=max(s,MAX)
        if(len(LIST)-1==r):
            break
        r+=1
else:
    s-=LIST[l][0]
    l+=1

    s+=LIST[r][0]
    MAX=max(s,MAX)
    if(len(LIST)-1==r):
        print(MAX)
        exit()
    r+=1
    s+=LIST[r][0]
    MAX=max(s,MAX)
    if(len(LIST)-1==r):
        print(MAX)
        exit()
    r+=1
    while(r<N):
        s-=LIST[l][0]
        l+=1
        s-=LIST[l][0]
        l+=1

        s+=LIST[r][0]
        MAX=max(s,MAX)
        if(len(LIST)-1==r):
            break
        r+=1
        s+=LIST[r][0]
        MAX=max(s,MAX)
        if(len(LIST)-1==r):
            break
        r+=1

print(MAX)

Submission status: Runtime Error

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

#N=14
#K=2
#S="11101010110011"

LIST=[[[0] for j in range (2)]for i in range(N)]

t=S[0]
c=0
k=0
for i in range(N):
    if(t!=S[i]):
        LIST[k][0]=c
        LIST[k][1]=t
        t=S[i]
        k+=1
        c=0
    c+=1

LIST[k][0]=c
LIST[k][1]=t
LIST=LIST[0:k+1]
MAX=0

def calc(i,s,K):
    global MAX
    MAX=max(MAX,s)
    if(i==len(LIST)):
        return

    if(LIST[i][1]=="1"):
        s+=LIST[i][0]
        MAX=max(MAX,s)
        i+=1
        if(i==len(LIST)):
            return
    
    if(K>=1):
        calc(i+1,s+LIST[i][0],K-1)
    calc(i+1,0,K)

calc(0,0,K)
print(MAX)

Submission status: Runtime Error

Solution code:
sys.setrecursionlimit(10 ** 9)
N,K=map(int,input().split())
S=input()

#N=14
#K=2
#S="11101010110011"

LIST=[[[0] for j in range (2)]for i in range(N)]

t=S[0]
c=0
k=0
for i in range(N):
    if(t!=S[i]):
        LIST[k][0]=c
        LIST[k][1]=t
        t=S[i]
        k+=1
        c=0
    c+=1

LIST[k][0]=c
LIST[k][1]=t
LIST=LIST[0:k+1]
MAX=0

def calc(i,s,K):
    global MAX
    MAX=max(MAX,s)
    if(i==len(LIST)):
        return

    if(LIST[i][1]=="1"):
        s+=LIST[i][0]
        MAX=max(MAX,s)
        i+=1
        if(i==len(LIST)):
            return
    
    if(K>=1):
        calc(i+1,s+LIST[i][0],K-1)
    calc(i+1,0,K)

calc(0,0,K)
print(MAX)

Submission status: Runtime Error

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

#N=14
#K=2
#S="11101010110011"

LIST=[[[0] for j in range (2)]for i in range(N)]

t=S[0]
c=0
k=0
for i in range(N):
    if(t!=S[i]):
        LIST[k][0]=c
        LIST[k][1]=t
        t=S[i]
        k+=1
        c=0
    c+=1

LIST[k][0]=c
LIST[k][1]=t
LIST=LIST[0:k+1]
MAX=0

def calc(i,s,K):
    global MAX
    MAX=max(MAX,s)
    if(i==len(LIST)):
        return

    if(LIST[i][1]=="1"):
        s+=LIST[i][0]
        MAX=max(MAX,s)
        i+=1
        if(i==len(LIST)):
            return
    
    if(K>=1):
        calc(i+1,s+LIST[i][0],K-1)
    calc(i+1,0,K)

calc(0,0,K)
print(MAX)

Submission status: Runtime Error

Solution code:
import sys

if sys.platform =='ios':
	sys.stdin=open('Untitled.txt')
input = sys.stdin.readline
def INT(): return int(input())
def MAP(): return [int(s) for s in input().split()]

N, K = MAP()
S = input().rstrip()

A = [int(s) for s in S]
#print(A)

last_ones = 0
left_ones = 0
zeros = []
cur = -1

for i, a in enumerate(A):
	if a == 0 and i == 0:
		cur = i
	if a == 0 and A[i-1] == 1:
		cur = i 
	if a == 1 and A[i-1] == 1:
		last_ones += 1
	if a == 1 and A[i-1] == 0:
		zeros.append([cur, i, i-cur, last_ones, 0, 0])
		last_ones = 1
else:
	if a == 0:
		zeros.append([cur, i+1, i+1-cur, last_ones, 0, 0])
	else:
		left_ones = last_ones

#print(zeros)

for i in range(len(zeros)):
	if i < len(zeros)-1:
		zeros[i][4] = zeros[i+1][3]
		zeros[i][5] = zeros[i][2] + zeros[i][3] + zeros[i][4]
	else:
		zeros[i][5] = zeros[i][2] + zeros[i][3]
		if left_ones: zeros[i][5] += left_ones

#print(zeros)
if len(zeros) == 0:
	print(1)
	sys.exit()

max_zeros = 0
for i in range(K):
	max_zeros += zeros[i][5]
max_pos = 0

sum_zeros = max_zeros
for left in range(1, len(zeros)-K+1):
	sum_zeros -= zeros[left-1][5]
	sum_zeros += zeros[left+K-1][5]
	#print(left, sum_zeros)
	if sum_zeros > max_zeros:
		max_pos = left

#print(max_pos)
ans = 0
for i in range(max_pos, max_pos+K):
	ans += (zeros[i][2]+zeros[i][3]+zeros[i][4])

print(ans)

Submission status: Runtime Error

Solution code:
import sys
if sys.platform in ['ios', 'win32', 'darwin']:
	sys.stdin=open('Untitled.txt')
input = sys.stdin.readline
def INT(): return int(input())
def MAP(): return [int(x) for x in input().split()]

N, K = MAP()
S = input().rstrip()
S = [int(s) for s in S]

A = []
now = -1
l = -1
for i,s in enumerate(S):
	if now == -1:
		l = i
		now = s
	elif now == s: continue
	elif now != s:
		A.append([int(now), l, i, i-l])
		now = s
		l = i
else:
	A.append([int(now), l, i+1, i-l+1])
# print(A)

if S[0] == 1:
	left, right = 0, 2*K+1
else:
	left, right = 0, 2*K
sum_A = sum(a[3] for a in A[left:right])
# print(sum_A)

ans = sum_A
for i in range(1, N-right-2):
	now, fr, to, ln = A[i]
	if now:
		sum_A -= A[i-1][3]
		sum_A += A[i+1][3]
		sum_A += A[i+2][3]
		ans = max(ans, sum_A)
	else:
		sum_A -= A[i-1][3]
		ans = max(ans, sum_A)
	# print(now, fr, to, sum_A)
print(ans)

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
s=list(str(input()))
r=[]
l=[]
c=0
d=1
for i in range(n):
	if s[i]=='1' and d==1:
		r.append(i)
		c=1
		d=0
	elif s[i]=='0' and c==1:
		l.append(i-1)
		c=0
		d=1
if c>0:
	l.append(i)
if len(r)<=k+1:
	r=[0]*(k+1-len(r))+r+[n-1]*(k+1-len(r))
	l=[0]*(k+1-len(l))+l+[n-1]*(k+1-len(l))
ans=0
for i in range(k+1):
	ans=max(ans,l[i+k]-r[i]+1)
print(ans)

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
s=list(str(input()))
l=[]
c=0
for i in range(n):
	if s[i]=='1' and c==0:
		l.append(i)
		c=1
	elif s[i]=='0':
		c=0
for i in range(n-1,-1,-1):
	if s[i]=='1':
		a=i
		break
else:
	a=n-1
l=[0]+l[1:-1]+[a]
#print(l)
m=len(l)
r=[]
for i in range(m-k):
	r.append(l[i+k]-l[i]+1)
print(max(r))

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
s=list(str(input()))
l=[]
c=0
for i in range(n):
	if s[i]=='1' and c==0:
		l.append(i)
		c=1
	elif s[i]=='0':
		c=0
for i in range(n):
	if s[i]=='1':
		a=i
l=[0]+l[1:-1]+[a]
#print(l)
m=len(l)
r=[]
for i in range(m-k):
	r.append(l[i+k]-l[i]+1)
print(max(r))

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
s=input()
num=[0]
c=1
for i in range(1,n):

    if s[i]==s[i-1]:
        c+=1
    else:
        num.append(c)
        c=1
num.append(c)
num.append(0)
#print(num)

ans=0
#print(num)
if s[0]=="0":
    if len(num)-2*k+1<=0:
        ans=sum(num)
    else:
        asum=sum(num[0:2*k+1])
        ans=asum
        #print(asum)
        for i in range(2,len(num)-2*k,2):
            #print(asum,num[i+2*k],num[i+2*k-1],num[i-1],num[i-2])
            asum=asum+num[i+2*k]+num[i+2*k-1]-num[i-1]-num[i-2]
            ans=max(ans,asum)
if s[0]=="1":
    if len(num)-2*k+2<=0:
        ans=sum(num)
    else:
        asum=sum(num[1:2*k+2])
        ans=asum
        for i in range(3,len(num)-2*k+1,2):
            #print(asum,num[i+2*k],num[i+2*k-1],num[i-1],num[i-2])
            asum=asum+num[i+2*k]+num[i+2*k-1]-num[i-1]-num[i-2]
            ans=max(ans,asum)
print(ans)

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
s=input()
num=[0]
c=1
for i in range(1,n):

    if s[i]==s[i-1]:
        c+=1
    else:
        num.append(c)
        c=1
num.append(c)
num.append(0)
#print(num)

ans=0
print(num)
if s[0]=="0":
    if len(num)-2*k+1<=0:
        ans=sum(num)
    else:
        asum=sum(num[0:2*k+1])
        ans=asum
        #print(asum)
        for i in range(2,len(num)-2*k,2):
            print(asum,num[i+2*k],num[i+2*k-1],num[i-1],num[i-2])
            asum=asum+num[i+2*k]+num[i+2*k-1]-num[i-1]-num[i-2]
            ans=max(ans,asum)
if s[0]=="1":
    if len(num)-2*k+2<=0:
        ans=sum(num)
    else:
        asum=sum(num[1:2*k+2])
        ans=asum
        for i in range(3,len(num)-2*k+1,2):
            #print(asum,num[i+2*k],num[i+2*k-1],num[i-1],num[i-2])
            asum=asum+num[i+2*k]+num[i+2*k-1]-num[i-1]-num[i-2]
            ans=max(ans,asum)
print(ans)

Submission status: Runtime Error

Solution code:
import math

INF = float("inf")


def main():
    N, K = map(int, input().split())
    S = input()
    zero_list = []
    A = []
    one = 0
    zero = 0

    for i in range(N):
        if S[i] == '1':
            one += 1
            if zero != 0:
                A.append(zero)
                zero_list.append(zero)
                zero = 0
        else:
            zero += 1
            if one != 0:
                A.append(one)
                one = 0

    if one != 0:
        A.append(one)
    elif zero != 0:
        A.append(zero)
        zero_list.append(zero)

    if len(zero_list) <= K:
        print(N)
        return

    section = 0
    if S[0] == '1':
        section = 3 + (K-1) * 2
    else:
        section = 2 + (K-1) * 2

    if S[0] == '1':
        print(*A, section)
        m = 0
        for i in range(len(A) - section):
            if i % 2 == 0:
                section = 3 + (K-1) * 2
                # print(A[i:max(i+section, len(A)-1)])
                m = max(m, sum(A[i:i+section]))
            else:
                section = 2 + (K-1) * 2
                # print(A[i:max(i+section, len(A)-1)])
                m = max(m, sum(A[i:i+section]))
        print(m)
    else:
        m = 0
        for i in range(len(A) - section):
            if i % 2 == 0:
                section = 2 + (K-1) * 2
                # print(A[i:max(i+section, len(A)-1)])
                m = max(m, sum(A[i:i+section]))
            else:
                section = 3 + (K-1) * 2
                # print(A[i:max(i+section, len(A)-1)])
                m = max(m, sum(A[i:i+section]))
        print(m)


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
n,k = [int(_) for _ in input().split()]
s = input()

c0 = 0
c1 = 0
a = [0]
b = []
v = 0
while s[v] == '0':
    c0 += 1
    s = s[1:]
b.append(c0)
c0 = 0
for i in range(len(s)):
    if s[i] == '1':
        c1 += 1
        if c0 != 0:
            b.append(c0)
        c0 = 0
    else:
        c0 += 1
        if c1 != 0:
            a.append(c1)
        c1 = 0
if c0 != 0:
    b.append(c0)
else:
    a.append(c1)
    b.append(0)
a.append(0)
# print(a)
# print(b)

s_a = [0]
s_b = [0]
for i in range(len(a)):
    s_a.append(s_a[i]+a[i])
for i in range(len(b)):
    s_b.append(s_b[i]+b[i])
# print(s_a)
# print(s_b)

ans = 0
if len(b) <= k:
    ans = n
else:
    for i in range(len(a)-k):
        # print(s_a[i+k+1] - s_a[i] + s_b[i+k] - s_b[i])
        ans = max(ans, s_a[i+k+1] - s_a[i] + s_b[i+k] - s_b[i])  
print(ans)

Submission status: Runtime Error

Solution code:
from collections import deque

def main():
    n,k = map(int,input().split())
    raw_list = input()
    
    q = deque([])

    startfeet = 0
    endfeet = 0
    max_len = 0
    for i in range(n):
        item = raw_list[i]
        if item == '0':
            endfeet = get_endhand(raw_list,i+1,n)
            append_list = [startfeet,endfeet]
            startfeet = get_startfeet(raw_list,i+1,n)

            if len(q) == k:
                if q[k-1][1] - q[0][0] + 1 > max_len:
                    max_len = q[k-1][1] - q[0][0] + 1
                q.popleft()
                q.append(append_list)

            else:
                q.append(append_list)
    print(max_len)

def get_startfeet(raw_list,i,n):
    for index in range(i,n):
        if raw_list[index-1] == '0' and raw_list[index] == '1':
            return index
    return index

def get_endhand(raw_list,i,n):
    for index in range(i,n):
        if raw_list[index-1] == '1' and raw_list[index] == '0':
            return index-1
    return index

if __name__ == "__main__":
    main()