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: Wrong Answer

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+1)//2<=k:
        ans=sum(num)
    else:
        asum=sum(num[0:2*k+1])
        for i in range(2,len(num)-2*k-1,2):
            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)//2<=k:
        ans=sum(num)
    else:
        asum=sum(num[1:2*k+2])
        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: Wrong Answer

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
if len(num)-2*k-1<=0:
    ans=sum(num)
else:
    if s[0]=="0":

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

Submission status: Wrong Answer

Solution code:
import numpy as np
 
N,K = map(int, input().split())
S = [int(x) for x in input()]
 
memo = np.zeros(10**5+1, dtype = np.int64)
W = min(2*K, 10**5) #Nの最大値
 
current = 1 # 最初は1が0個ある
p = 0
 
for x in S: #1から始めている。
  if x == current:
    memo[p] += 1
  else:
    current = 1 - current #if文の代わり。スマート。
    p += 1 #memoを作りだす過程。
    memo[p] += 1
print(memo[:10])

ans = memo[:W+1].sum()
print(ans)

x = ans
for r in range(2,10**5+1,2): #2個ずつずらす。maxを求めるので配列が大きすぎるのは問題ない。
  if r+W > 10**5:
    break
  x = x-memo[r-2]-memo[r-1]+memo[r+W]+memo[r+W-1]
  if x > ans:
    ans = x

print(ans)

Submission status: Wrong Answer

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

for i in range(N):
    if S[i] == 1:
        if tmp != 0:
            s.append((tmp, i-1))
        tmp = 0
    else:
        tmp += 1
        if i == N-1:
            s.append((tmp, i))
if len(s) <= K:
    print(N)
    exit(0)
ans = 0
for i in range(len(s)-K):
    tmp = 0
    if i+K == len(s)-1:
        tmp = N-1
    else:
        tmp = s[i+K][1]-s[i+K][0]

    if i != 0:
        tmp -= s[i-1][1]+2
    ans = max(ans, tmp)

print(ans)

Submission status: Wrong Answer

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

for i in range(N):
    if S[i] == 1:
        if tmp != 0:
            s.append((tmp, i))
        tmp = 0
    else:
        tmp += 1
        if i == N-1:
            s.append((tmp, i+1))
if len(s) <= K:
    print(N)
    exit(0)
ans = 0
for i in range(len(s)-K):
    tmp = 0
    if i+K == len(s)-1:
        tmp = N-1
    else:
        tmp = s[i+K][1]-s[i+K][0]

    if i != 0:
        tmp -= s[i-1][1]+1
    ans = max(ans, tmp)

print(ans)

Submission status: Wrong Answer

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

for i in range(N):
    if S[i] == 1:
        if tmp != 0:
            s.append((tmp, i))
        tmp = 0
    else:
        tmp += 1
        if i == N-1:
            s.append((tmp, i))
if len(s) <= K:
    print(N)
    exit(0)
ans = 0
for i in range(len(s)-K):
    tmp = 0
    if i+K == len(s)-1:
        tmp = N-1
    else:
        tmp = s[i+K][1]-s[i+K][0]+1

    if i != 0:
        tmp -= s[i-1][1]+1
    ans = max(ans, tmp)

print(ans)

Submission status: Wrong Answer

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

for i in range(N):
  if S[i]==1:
    if tmp!=0:
      s.append((tmp,i))
    tmp=0
  else:
    tmp+=1
    if i ==N-1:
      s.append((tmp,i))
if len(s)<=K:
  print(N)
  exit(0)
ans=0
for i in range(len(s)-K+1):
  tmp=0
  if i == len(s)-K:
    tmp= N
  else:
    tmp=s[i+K][1]-s[i+K][0]+1
  
  if i !=0:
    tmp-=s[i-1][1]
  ans=max(ans,tmp)
print(ans)

Submission status: Wrong Answer

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

for i in range(N):
  if S[i]==1:
    if tmp!=0:
      s.append((tmp,i))
    tmp=0
  else:
    tmp+=1
    if i ==N-1:
      s.append((tmp,i))
if len(s)<=K:
  print(N)
  exit(0)
ans=0
for i in range(len(s)-K+1):
  tmp=0
  if i == len(s)-K:
    tmp= N
  else:
    tmp=s[i+K][1]-s[i+K][0]+1
  
  if i !=0:
    print(tmp,s[i-1][1])
    tmp-=s[i-1][1]
  ans=max(ans,tmp)
print(s)
print(ans)

Submission status: Wrong Answer

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

for i in range(N):
  if S[i]==1:
    if tmp!=0:
      s.append((tmp,i))
    tmp=0
  else:
    tmp+=1
    if i ==N-1:
      s.append((tmp,i))
if len(s)<=K:
  print(N)
  exit(0)
s2=[[0,0] for i in range(len(s)+1)]

for i in range(len(s)):
  s2[i+1][0]=s2[i][0]+s[i][0]
  s2[i+1][1]=s[i][1]
ans=0
for i in range(len(s2)-K-1):
  ans=max(ans,s2[i+K+1][1]-s2[i][1])

print(ans)

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
S=list(input())
S=list(map(int,S))
s=[]
s.append(0)
for i in range(1,N):
  if S[i]==1 and S[i-1]==0:
    s.append(i-1)
  elif i==N-1 and S[i]==0:
    s.append(i)
s.append(N-1)
if len(s)<=K+2:
  print(len(S))
  exit(0)
ans=0
for i in range(1,max(len(s)-K,0)):
  ans=max(s[i+K]-(s[i-1]),ans)

print(ans)

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
S=list(input())
S=list(map(int,S))
s=[]
s.append(0)
for i in range(1,N):
  if S[i]==1 and S[i-1]==0:
    s.append(i-1)
  elif i==N-1 and S[i]==0:
    s.append(i)
s.append(N)
if len(s)<=K+2:
  print(len(S))
  exit(0)
ans=0
for i in range(1,max(len(s)-K,0)):
  ans=max(s[i+K]-(s[i-1])-1,ans)

print(ans)

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
S=list(input())
S=list(map(int,S))
s=[]
s.append(0)
for i in range(1,N):
  if S[i]==1 and S[i-1]==0:
    s.append(i-1)
  elif i==N-1 and S[i]==0:
    s.append(i)
s.append(N)
if len(s)<=K+2:
  print(len(S))
  exit(0)
ans=0
print(s)
for i in range(1,max(len(s)-K,0)):
  ans=max(s[i+K]-(s[i-1])-1,ans)

print(ans)

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
S=list(input())
S=list(map(int,S))
s=[]
s.append(0)
for i in range(1,N):
  if S[i]==1 and S[i-1]==0:
    s.append(i-1)
  elif i==N-1 and S[i]==0:
    s.append(i)
if len(s)<=K+1:
  print(len(S))
  exit(0)
ans=0

for i in range(1,max(len(s)-K,0)):
  ans=max(s[i+K]-(s[i-1]),ans)

print(ans)

Submission status: Wrong Answer

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

def renzoku(s):
    n=len(s)
    lis=[]
    c=1
    if len(s)==1:
        return [1]
    for i in range(n-1):
        if s[i+1]==s[i]:
            c+=1
        else:
            lis.append(c)
            c=1
        if i==n-2:
            lis.append(c)
    return lis



r=renzoku(s)
i=0
if s[0]=="0":
    r=[0]+r
if s[-1]=="0":
    r=r+[0]

cand = sum(r[:min(2*k+1,len(r))])
ans=cand
while 2*k+3 + 2*i<=len(r):
    cand += r[2*k+1+2*i]+r[2*k+2+2*i]-r[2*i]-r[2*i+1]
    if cand>ans:
        ans=cand
    print(i,ans,2*k+2+2*i)
    i+=1
print(ans)

Submission status: Wrong Answer

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

def renzoku(s):
    n=len(s)
    lis=[]
    c=1
    if len(s)==1:
        return [1]
    for i in range(n-1):
        if s[i+1]==s[i]:
            c+=1
        else:
            lis.append(c)
            c=1
        if i==n-2:
            lis.append(c)
    return lis



r=renzoku(s)
i=0
if s[0]=="0":
    r=[0]+r

cand = sum(r[:min(2*k+1,len(r))])
ans=cand
while 2*k+3 + 2*i<=len(r):
    cand += r[2*k+1+2*i]+r[2*k+2+2*i]-r[2*i]-r[2*i+1]
    if cand>ans:
        ans=cand
#    print(i,ans)
    i+=1
print(ans)

Submission status: Wrong Answer

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

    t = []
    cont = 0
    p = '1'
    for c in s:
        if c == p:
            cont += 1
        else:
            t.append(cont)  # * (1 if p == '0' else -1))
            p = c
            cont = 1
    t.append(cont)  # * (1 if p == '0' else -1))
    if len(t) % 2 == 1:
        t.append(0)
    # [直立,逆立,...直立,逆立]

    st = t[::2]  # 直立
    acc = [0]  # 直立,逆立の累積和

    p = 0
    j = 0
    while j < len(t) - 1:
        p += sum(t[j:j + 2])
        j += 2
        acc.append(p)

    ret = 0
    for j, (x, acc_) in enumerate(zip(st, acc)):
        ret = max(ret, (x + acc_) - acc[max(0, j - k)])
    return ret


if __name__ == '__main__':
    print(solve())

Submission status: Wrong Answer

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

    t = []
    cont = 0
    p = '1'
    for c in s:
        if c == p:
            cont += 1
        else:
            t.append(cont)  # * (1 if p == '0' else -1))
            p = c
            cont = 1
    t.append(cont)  # * (1 if p == '0' else -1))
    if len(t) % 2 == 1:
        t.append(0)
    # [直立,逆立,...直立,逆立]

    st = t[::2]  # 直立
    acc = [0]  # 直立,逆立の累積和

    p = 0
    for x in zip(t, t[1:]):
        p += sum(x)
        acc.append(p)

    ret = 0
    for j, (x, acc_) in enumerate(zip(st, acc)):
        ret = max(ret, (x + acc_) - acc[max(0, j - k)])
    return ret


if __name__ == '__main__':
    print(solve())

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline

def print_ans(N, K, S):
    """Test Case
    >>> print_ans(14, 2, "11101010110011")
    8
    >>> print_ans(5, 1, "00010")
    4
    >>> print_ans(1, 1, "1")
    1
    """
    list_zero = []
    list_one = []
    first = S[0]
    tmp_zero = ""
    tmp_one = ""
    ans = 0
    if len(S) == 1:
        print("1")
    else:
        if  S[0] == "0":
            tmp_zero = tmp_zero + S[0]
        else:
            tmp_one = tmp_one + S[0]
        #create 0 1 list
        for i in range(1, N):
            if S[i] == "0":
                tmp_zero = tmp_zero + S[i]
                if S[i - 1] != S[i]:
                    list_one.append(len(tmp_one))
                    tmp_one = ""
            else:
                tmp_one = tmp_one + S[i]
                if S[i - 1] != S[i]:
                    list_zero.append(len(tmp_zero))
                    tmp_zero = ""
        if len(tmp_zero) > 0:
            list_zero.append(len(tmp_zero))
        if len(tmp_one) > 0:
            list_one.append(len(tmp_one))

        if first == "1":
            for i in range(len(list_zero) - K + 1):
                ans = max(ans, sum(list_zero[i:i+K]) + sum(list_one[i:i+K+1]) )
        else:
            for i in range(len(list_zero) - K + 1):
                ans = max(ans, sum(list_zero[i:i+K]) + sum(list_one[i:i+K+1]) )
        print(ans)

    

if __name__ == '__main__':
    N, K = map(int, input().rstrip().split())
    S = input().rstrip()
    print_ans(N, K, S)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations, product
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor
from operator import mul
from functools import reduce


sys.setrecursionlimit(2147483647)
INF = 10 ** 13
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 1000000007



n, k = LI()
s = S()
X = []
if s[0] == '0':
    X += [0]

cnt = 1
for i in range(n - 1):
    if s[i] != s[i + 1]:
        X += [cnt]
        cnt = 1
    else:
        cnt += 1
X += [cnt]

if len(X) <= 2 * k + 1:
    print(n)
else:
    ans = 0
    acc = list(accumulate([0] + X))
    for j in range(2 * k + 1, len(acc), 2):
        ans = max(acc[j] - acc[j - (2 * k + 1)], ans)
    print(ans)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations, product
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor
from operator import mul
from functools import reduce


sys.setrecursionlimit(2147483647)
INF = 10 ** 13
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 1000000007



n, k = LI()
s = S()
X = []
if s[0] == '0':
    X += [0]

cnt = 1
for i in range(n - 1):
    if s[i] != s[i + 1]:
        X += [cnt]
        cnt = 1
    else:
        cnt += 1
X += [cnt]

if len(X) < 2 * k + 1:
    print(n)
else:
    ans = 0
    acc = list(accumulate([0] + X))
    for j in range(2 * k + 1, len(acc), 2):
        ans = max(acc[j] - acc[j - (2 * k + 1)], ans)
    print(ans)

Submission status: Wrong Answer

Solution code:
import sys
input =  sys.stdin.readline

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

cnt = 0
cnt_list = []
for i in range(n-1):
    if s[i] == s[i+1]:
        cnt += 1
    else:
        cnt_list.append(cnt+1)
        cnt = 0
else:
    cnt_list.append(cnt+1)

ma = 0

for i in range(len(cnt_list)):
    if s[i] == '0':
        l = i+2*k
    else:
        l = i+2*k+1
    # if l > len(cnt_list):
    #     break
    ma_tmp = sum(cnt_list[i:l])
    if ma_tmp > ma:
        ma = ma_tmp
print(ma)

Submission status: Wrong Answer

Solution code:
import sys
input =  sys.stdin.readline

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

cnt = 0
cnt_list = []
for i in range(n-1):
    if s[i] == s[i+1]:
        cnt += 1
    else:
        cnt_list.append(cnt+1)
        cnt = 0
else:
    cnt_list.append(cnt+1)

ma = 0

for i in range(len(cnt_list)):
    if s[i] == '0':
        l = i+2*k
    else:
        l = i+2*k+1
    if l > len(cnt_list):
        break
    ma_tmp = sum(cnt_list[i:l])
    if ma_tmp > ma:
        ma = ma_tmp
print(ma)

Submission status: Wrong Answer

Solution code:
import sys
input =  sys.stdin.readline

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

cnt = 0
cnt_list = []
for i in range(n-1):
    if s[i] == s[i+1]:
        cnt += 1
    else:
        cnt_list.append(cnt+1)
        cnt = 0
else:
    cnt_list.append(cnt+1)

ma = 0

for i in range(len(cnt_list)-2*k+2):
    if s[i] == '0':
        ma_tmp = sum(cnt_list[i:i+2*k])
    else:
        ma_tmp = sum(cnt_list[i:i+2*k+1])
    if ma_tmp > ma:
        ma = ma_tmp
print(ma)

Submission status: Wrong Answer

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

A = []
t = S[0]
cnt = 1
for i in range(1,N):
	if S[i] == t:
		cnt += 1
	else:
		A.append(cnt)
		t = S[i]
		cnt = 1
A.append(cnt)

if S[0] == "1" and S[-1] == "0":
	A = A[::-1] + [0]
	N += 1
elif S[0] == "0" and S[-1] == "1":
	A = A + [0]
	N += 1
elif S[0] == "0" and S[-1] == "0":
	   A = [0] + A + [0]
	   N += 2

ans = sum(A[:2*K+1])
tmp = ans
tK = 2*K
for i in range((N-1)//2-K):
	ind = 2*i
	tmp = tmp - sum(A[ind:ind+2]) + sum(A[ind+tK+1:ind+tK+3])
	ans = max(ans, tmp)

print(ans)

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
S = str(input())
X=[1]
for i in range(1,N):
  if S[i]==S[i-1]:
    X[-1] +=1
  else:
    X +=[1]

if S[0]=='0' and S[-1]=='0':
  result=max(sum(X[:2*K]),sum(X[-2*K:]))
  for i in range(1,N-2*K-1,2):
    result=max(result,sum(X[i:i+2*K+1]))
    
elif S[0]=='0' and S[-1]=='1':
  result=sum(X[:2*K])
  for i in range(1,N-2*K-1,2):
    result=max(result,sum(X[i:i+2*K+1]))

elif S[0]=='1' and S[-1]=='0':
  result=sum(X[-2*K:])
  for i in range(0,N-2*K-1,2):
    result=max(result,sum(X[i:i+2*K+1]))

elif S[0]=='1' and S[-1]=='1':
  result = 0
  for i in range(0,N-2*K,2):
    result=max(result,sum(X[i:i+2*K+1]))
print(result)

Submission status: Wrong Answer

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

    # print(*A, section)
    m = 0
    for i in range(len(A) - section):
        # 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: Wrong Answer

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 = K + 2 + (K-1) * 2
    else:
        section = K + 1 + (K-1) * 2

    # print(*A)
    m = 0
    for i in range(len(A) - section):
        # print(A[i:i+section])
        m = max(m, sum(A[i:i+section]))

    print(m)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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)

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

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

    # print(*A)
    m = 0
    for i in range(len(A) - section):
        # print(A[i:i+section])
        m = max(m, sum(A[i:i+section]))

    print(m)





if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
S=str(input())
list_0=[]
list_1=[]
c_0=0
c_1=0
for i in range(N):
  if S[i]=='0':
    c_0 +=1
    if c_1!=0:
      list_1 +=[c_1]
    c_1=0
  elif S[i]=='1':
    c_1 +=1
    if c_0!=0:
      list_0 +=[c_0]
    c_0=0
  if i==N-1:
    if c_0!=0:
      list_0 +=[c_0]
    elif c_1!=0:
      list_1 +=[c_1]
result=0

if len(list_0)<=K:
  result=N
else:
  if S[0]=='1' and S[-1]=='1':
    for i in range(len(list_1)-K):
      result = max(result,sum(list_1[i:i+K+1])+sum(list_0[i:i+K]))
  
  elif S[0]=='1' and S[-1]=='0':
    result=sum(list_1[-K:])+sum(list_0[-K:])
    for i in range(len(list_1)-K):
      result = max(result,sum(list_1[i:i+K+1])+sum(list_0[i:i+K]))
  
  elif S[0]=='0' and S[-1]=='0':
    result=max(sum(list_0[:K])+sum(list_1[:K]),sum(list_0[-K:])+sum(list_1[-K:]))
    for i in range(len(list_1)-K-1):
      result = max(result,sum(list_1[i:i+K+2])+sum(list_0[i+1:i+K+2]))
  
  elif S[0]=='0' and S[-1]=='1':
    result=sum(list_0[:K])+sum(list_1[:K])
    for i in range(len(list_1)-K-1):
      result = max(result,sum(list_1[i:i+K+2])+sum(list_0[i+1:i+K+2]))
print(result)

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
S=str(input())
list_0=[]
list_1=[]
c_0=0
c_1=0
for i in range(N):
  if S[i]=='0':
    c_0 +=1
    if c_1!=0:
      list_1 +=[c_1]
    c_1=0
  elif S[i]=='1':
    c_1 +=1
    if c_0!=0:
      list_0 +=[c_0]
    c_0=0
  if i==N-1:
    if c_0!=0:
      list_0 +=[c_0]
    elif c_1!=0:
      list_1 +=[c_1]
result=0

if len(list_0)<=K:
  result=N
else:
  if S[0]=='1' and S[-1]=='1':
    for i in range(len(list_1)-K):
      result = max(result,sum(list_1[i:i+K+1])+sum(list_0[i:i+K]))
  
  elif S[0]=='1' and S[-1]=='0':
    result=sum(list_1[-K:])+sum(list_0[-K:])
    for i in range(len(list_1)-K):
      result = max(result,sum(list_1[i:i+K+1])+sum(list_0[i:i+K]))
  
  elif S[0]=='0' and S[-1]=='0':
    result=max(sum(list_0[:K])+sum(list_1[:K]),sum(list_0[-K:])+sum(list_1[-K:]))
    for i in range(len(list_1)-K-1):
      result = max(result,sum(list_1[i:i+K+2])+sum(list_0[i+1:i+K+2]))
  
  elif S[0]=='0' and S[-1]=='1':
    result=sum(list_0[:K+1])+sum(list_1[:K+1])
    for i in range(len(list_1)-K-1):
      result = max(result,sum(list_1[i:i+K+2])+sum(list_0[i+1:i+K+2]))
print(result)

Submission status: Wrong Answer

Solution code:
n,k = [int(_) for _ in input().split()]
s = input()
a = []
c = 0
for i in range(len(s)):
    if s[i] == '1':
        for j in range(c):
            a.append(1/c)
        a.append(0)
        c = 0
    else:
        c += 1
if c >= 1:
    for j in range(c):
        a.append(1/c)
# print(a)

s = [0]*(n+1)
for i in range(n):
    s[i+1] = s[i] + a[i]
# print(s)

low = 0
high = n

while low + 1 < high:
    mid = (low + high) //2
    # print(low, mid, high)
    ref = False
    for m in range(0,n-mid+1):
        
        # print(s[m+mid] - s[m])
        if s[m+mid] - s[m] <= k:
            ref = True
    # print(ref)
    if ref:
        low = mid
    else:
        high = mid - 1

ans = False
for m in range(0,n-high+1):
    if s[m+high] - s[m] <= k:
        ans = True
if ans:
    print(high)
else:
    print(low)

Submission status: Wrong Answer

Solution code:
n,k = [int(_) for _ in input().split()]
s = input()
a = []
c = 0
for i in range(len(s)):
    if s[i] == '1':
        for j in range(c):
            a.append(1/c)
        a.append(0)
        c = 0
    else:
        c += 1
if c >= 1:
    for j in range(c):
        a.append(1/c)
# print(a)

s = [0]*(n+1)
for i in range(n):
    s[i+1] = s[i] + a[i]
# print(s)

low = 1
high = n

while low + 1 < high:
    mid = (low + high) //2
    # print(low, mid, high)
    ref = False
    for m in range(0,n-mid+1):
        
        # print(s[m+mid] - s[m])
        if s[m+mid] - s[m] <= k:
            ref = True
    # print(ref)
    if ref:
        low = mid
    else:
        high = mid - 1

ans = False
for m in range(0,n-high+1):
    if s[m+high] - s[m] <= k:
        ans = True
if ans:
    print(high)
else:
    print(low)

Submission status: Wrong Answer

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 n-1

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 n-1

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
n,k = [int(_) for _ in input().split()]
s = input()
a = []
c = 0
for i in range(len(s)):
    if s[i] == '1':
        for j in range(c):
            a.append(1/c)
        a.append(0)
        c = 0
    else:
        c += 1
if c >= 1:
    for j in range(c):
        a.append(1/c)
# print(a)

s = [0]*(n+1)
for i in range(n):
    s[i+1] = s[i] + a[i]
# print(s)

low = 1
high = n

while low + 1 < high:
    mid = (low + high) //2
    # print(low, mid, high)
    ref = False
    for m in range(0,n-mid+1):
        
        # print(s[m+mid] - s[m])
        if s[m+mid] - s[m] <= k:
            ref = True
    # print(ref)
    if ref:
        low = mid
    else:
        high = mid - 1

print(low)

Submission status: Wrong Answer

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

l, r = 0, 0
cont = 0
alt = 1 if S[l] == 0 and S[r] == 1 else 0
while True:
    if r >= N-1:
        cont = max(cont, r-l+1)
        break
    if alt < K:
        r += 1
        if S[r-1] == '0' and S[r] == '1':
            alt += 1
    if alt == K:
        cont = max(cont, r-l+1)
        l += 1
        if S[l] == '0' and S[l+1] == '1':
            alt -= 1

print(cont)

Submission status: Wrong Answer

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

l, r = 0, 1
cont = 0
alt = 1 if S[l] == 0 and S[r] == 1 else 0
while True:
    if r >= N-1:
        cont = max(cont, r-l)
        break
    if alt < K:
        r += 1
        if S[r-1] == '0' and S[r] == '1':
            alt += 1
    if alt == K:
        cont = max(cont, r-l+1)
        l += 1
        if S[l] == '0' and S[l+1] == '1':
            alt -= 1

print(cont)

Submission status: Wrong Answer

Solution code:
from collections import deque
n, k = map(int, input().split())
s = input() + "0"
f = 0
d = deque()
ans = 0
for i in range(n):
  if s[i] == "0" and s[i+1] == "1":
    d.append(i+1)
  if s[i] == "1" and s[i+1] == "0":
    if len(d) == k:
      l = i
      ans = max(l-f+1, ans)
      f = d.popleft()
if ans > 0:
  print(ans)
else:
  print(n)

Submission status: Wrong Answer

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

l0 = []
l1 = []
pred = s[0]
if s[0] == "0":
    l0.append(1)
    l1.append(0)
else:
    l1.append(1)

for i in range(1,n):
    if pred == s[i]:
        if s[i] == "0":
            l0[-1] += 1
        else:
            l1[-1] += 1
    else:
        if s[i] == "0":
            l0.append(1)
        else:
            l1.append(1)
    pred = s[i]
if s[-1] == "0":    
    l1.append(0)

if k >= len(l0):
    print(n)
else:
    while (k>0 and len(l0)>0):
        n0 = len(l0)
        nl0 = []
        for i in range(n0):
            nl0.append(l0[i]+l1[i]+l1[i+1])
        nl0 = np.array(nl0)
        key = np.argmax(nl0)
        l1[key] += l0[key] + l1[key+1]
        del l0[key]
        del l1[key+1]
        k -= 1
    print(max(l1))

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
s=input()
if n==1:
    print(1)
    exit()
Is=[0]
for i in range(1,n):
    if s[i]!=s[i-1]:
        Is.append(i)
Is.append(n)
m=len(Is)
ans=0
for p in range(m):
    if Is[p]!=n and s[Is[p]]=="1":
        if p+2*k+1>=m:
            continue
        ans=max(ans,Is[p+2*k+1]-Is[p])
    else:
        if p+2*k>=m:
            continue
        ans=max(ans,Is[p+2*k]-Is[p])

print(ans)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
s=input()
if n==1:
    print(1)
    exit()
Is=[0]
for i in range(1,n):
    if s[i]!=s[i-1]:
        Is.append(i)
Is.append(n)
m=len(Is)
ans=0
for p in range(m):
    if s[p]=="1":
        if p+2*k+1>=m:
            continue
        ans=max(ans,Is[p+2*k+1]-Is[p])
    else:
        if p+2*k>=m:
            continue
        ans=max(ans,Is[p+2*k]-Is[p])

print(ans)

Submission status: Wrong Answer

Solution code:
from collections import deque

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

hoge = deque()

now = int(S[0])
Sum = 1
for i in range(1, N):
    if now == int(S[i]):
        Sum += 1
    else:
        hoge.append(Sum)

        Sum = 1
        now = (now + 1) % 2

    if i == N - 1:
        hoge.append(Sum)

if N == 1:
    hoge.append(1)

rui = deque()
rui.append(0)
Sum = 0

for i in range(len(hoge)):
    Sum += hoge[i]
    rui.append(Sum)

first = int(S[0])
max = max(rui)

for i in range(first, len(rui), 2):

    if 2 * K + i == len(rui):
        i -= 1
    elif 2 * K + i > len(rui):
        break

    if rui[2 * K + i] - rui[i - first] > max:
        max = rui[2 * K + i] - rui[i - first]

    print(rui[2 * K + i] - rui[i - first])

print(max)

Submission status: Wrong Answer

Solution code:
from collections import deque

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

hoge = deque()

now = int(S[0])
Sum = 1
for i in range(1, N):
    if now == int(S[i]):
        Sum += 1
    else:
        hoge.append(Sum)

        Sum = 1
        now = (now + 1) % 2

    if i == N - 1:
        hoge.append(Sum)

if N == 1:
    hoge.append(1)

rui = deque()
rui.append(0)
Sum = 0

for i in range(len(hoge)):
    Sum += hoge[i]
    rui.append(Sum)

first = int(S[0])
max = max(rui)

for i in range(first, len(rui), 2):

    if 2 * K + i == len(rui):
        i -= 1
    elif 2 * K + i > len(rui):
        break

    if rui[2 * K + i] - rui[i - first] > max:
        max = rui[2 * K + i] - rui[i - first]

    print(rui[2 * K + i] - rui[i - first])

print("max", max)