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 = str(input())
L = []
cnt = 0
x = '1'
for i in range(n):
    if s[i] == x:
        cnt += 1
    else:
        L.append(cnt)
        x = '0' if x == '1' else '1'
        cnt = 1
else:
    L.append(cnt)
syaku = sum(L[:2*k+1])
ans = syaku
for i in range(len(L)-2*k-1):
    syaku -= L[i]
    if i % 2:
        syaku += L[i+2*k] + L[i+2*k+1]
    ans = max(ans, syaku)
print(ans)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
s = str(input())
L = []
cnt = 0
x = '1'
for i in range(n):
    if s[i] == x:
        cnt += 1
    else:
        L.append(cnt)
        x = '0' if x == '1' else '1'
        cnt = 1
else:
    L.append(cnt)
ans = 0
for j in range(0, max(1,len(L)-2*k), 2):
    ans = max(ans, sum(L[j:j+2*k+1]))
print(ans)

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
S=input()
if N==1:
 print(1)
else:
 lst=[]
 ct=1
 if int(S[0])==0:
  lst.append(-1)
 ima=int(S[0])
 for i in range(1,N):
  if int(S[i])==ima:
   ct+=1
  else:
   lst.append(ct)
   ima=int(S[i])
   ct=1
 lst.append(ct)
 if ima==0:
  lst.append(-1)
 A=len(lst)-2
 if A<=K*2-1:
  print(N)
 else:
  for i in range(1,len(lst)):
   lst[i]+=lst[i-1]
  ans=0
  for i in range((len(lst)-1-2*K)//2+1):
   ans=max(ans,lst[2*i+2*K]-lst[2*i])
  print(ans+1)

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
S = list(str(input()))
arr = [0,0]
D = [0,0]
cnt = 0
d = 0

for i in range(len(S)):
  a = int( S[i] )
  if a == 1:
    cnt += 1 + d
    if d > 0:
      D.append(d)
    d = 0
  elif a == 0 and d == 0:
    arr.append(cnt)
    d += 1
  else:
    d += 1
if a == 1:
  arr.append(cnt)
if a == 0:
  arr.append(cnt+d)
arr.sort()

ans = 0
for i in range(len(arr)-K-1):
  a = arr[i+K+1] - arr[i]
  b = D[i]
  ans = max(a-b,ans)

print(ans)

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
S = list(str(input()))
arr = [0,0]
D = [0,0]
cnt = 0
d = 0

for i in range(len(S)):
  a = int( S[i] )
  if a == 1:
    cnt += 1 + d
    if d > 0:
      D.append(d)
    d = 0
  elif a == 0 and d == 0:
    arr.append(cnt)
    d += 1
  else:
    d += 1
if a == 1:
  arr.append(cnt)
arr.sort()

ans = 0
for i in range(len(arr)-K-1):
  a = arr[i+K+1] - arr[i]
  b = D[i]
  ans = max(a-b,ans)

print(ans)

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
S = list(str(input()))
arr = [0]
D = [0]
cnt = 0
d = 0

for i in range(len(S)):
  a = int( S[i] )
  if a == 1:
    cnt += 1 + d
    if d > 0:
      D.append(d)
    d = 0
  elif a == 0 and d == 0:
    arr.append(cnt)
    d += 1
  else:
    d += 1
if a == 1:
  arr.append(cnt)
arr.sort()

ans = 0
for i in range(len(arr)-K-1):
  a = arr[i+K+1] - arr[i]
  b = D[i]
  ans = max(a-b,ans)
  
print(ans)

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
S = list(str(input()))
arr = []
cnt = 0
d = 0

for i in range(len(S)):
  a = int( S[i] )
  if a == 1:
    cnt += 1 + d
    d = 0
  elif a == 0 and d == 0:
    arr.append(cnt)
    d += 1
if a == 1:
  arr.append(cnt)
arr.append(0)
arr.sort()
ans = 0
for i in range(len(arr)-3):
  a = arr[i+3] - arr[i]
  ans = max(a,ans)
  
print(ans)

Submission status: Wrong Answer

Solution code:
from itertools import groupby
import itertools
import numpy as np

n,k=map(int,input().split())
s=list(input())
k=k*2
if s[0]=="1":
    k+=1
a=[0]
for i,group in groupby(s):
    a.append(len(list(group)))
b=list(itertools.accumulate(a))
ans=0
for i in range(k,len(b)):
    ans=max(ans, b[i]-b[i-k])
if ans==0:
    ans=b[-1]-b[0]
print(ans)

Submission status: Wrong Answer

Solution code:
from itertools import groupby
import itertools
import numpy as np

n,k=map(int,input().split())
s=list(input())
k=k*2
if s[0]=="1":
    k+=1
a=[0]
for i,group in groupby(s):
    a.append(len(list(group)))
b=list(itertools.accumulate(a))
ans=0
for i in range(k,len(b)):
    print(b[i],b[i-k])
    ans=max(ans, b[i]-b[i-k])
if ans==0:
    ans=b[-1]-b[0]
print(ans)

Submission status: Wrong Answer

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

if seq[n-1]=="0":
    seq=seq+"1"
else:
    seq=seq+"0"

l=[]

if seq[0]=="0":
    l.append(0)

st=0
for i in range(n):
    if seq[i:i+2] in ("01","10"):
        l.append(i+1-st)
        st=i+1


if 2*k+1>=len(l):
    print(sum(l))
else:
    lis=[sum(l[0:2*k+1])]
    for i in range((len(l)-2*k-1)//2):
        lis.append(lis[i]-l[2*i]-l[2*i+1]+l[2*i+2*k+1]+l[2*i+2*k+2])
    print(max(lis))

Submission status: Wrong Answer

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

if seq[n-1]=="0":
    seq=seq+"1"
else:
    seq=seq+"0"

l=[]

if seq[0]=="0":
    l.append(0)

st=0
for i in range(n):
    if seq[i:i+2] in ("01","10"):
        l.append(i+1-st)
        st=i+1


if 2*k+1>=len(l):
    print(sum(l))
else:
    lis=[sum(l[0:2*k+1])]
    for i in range((len(l)-2*k-1)//2):
        lis.append(lis[i]-l[i]-l[i+1]+l[i+2*k+1]+l[i+2*k+2])
    print(max(lis))

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
S = input()
A = [0]
if S[0] == '0':
    A.append(0)
now = 1
for i, s in enumerate(S):
    if now != int(s):
        A.append(i)
        now = int(s)
    elif i == N-1:
        A.append(N)
if S[-1] == '0':
    A.append(A[-1])

num = 2 * K + 1
if num >= len(A):
    ans = N
else:
    ans = 0
    for i in range(len(A))[::2]:
        if i + num >= len(A):
            break
        ans = max(ans, A[i + num] - A[i])
print(ans)

Submission status: Wrong Answer

Solution code:
from collections import deque

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

cnt_01 = []
prev = s[0]
cnt = 0
for c in s:
    if c != prev:
        cnt_01.append(cnt)
        cnt = 1
    else: 
        cnt += 1
    prev = c
else:
    cnt_01.append(cnt)


if len(cnt_01) <= 2*k:
    print(n)
    exit()

first_que = cnt_01[0:2*k]
que = deque(first_que)

curr_sum = sum(first_que)
ans = curr_sum
for i in range(2*k, len(cnt_01)):
    poped = que.popleft()
    curr_sum -= poped
    que.append(cnt_01[i])
    curr_sum += cnt_01[i]
    ans = max(curr_sum, ans)

print(ans)

Submission status: Wrong Answer

Solution code:
from collections import deque

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

cnt_01 = []
prev = ''
cnt = 0
for c in s:
    if c == prev:
        cnt_01.append(cnt)
        cnt = 0
    else: 
        cnt += 1
    prev = c
else:
    cnt_01.append(cnt)


if len(cnt_01) <= 2*k:
    print(n)
    exit()

first_que = cnt_01[0:2*k]
que = deque(first_que)

curr_sum = sum(first_que)
ans = curr_sum
for i in range(2*k, len(cnt_01)):
    poped = que.popleft()
    curr_sum -= poped
    que.append(cnt_01[i])
    curr_sum += cnt_01[i]
    ans = max(curr_sum, ans)

print(ans)

Submission status: Wrong Answer

Solution code:
def resolve():
    N, K = list(map(int, input().split()))
    S = input()
    seq = []
    cnt = 0
    if S[0] != "1":
        seq.append(0)
    for i in range(len(S)):
        if i == N-1 or S[i] != S[i+1]:
            seq.append(cnt+1)
            cnt = 0
        else:
            cnt += 1
    if S[-1] != "1":
        seq.append(0)

    cum = [0]
    cur = 0
    for c in seq:
        cur += c
        cum.append(cur)
    
    maxlen = 0
    for i in range(0, len(cum)-(2*K+1), 2):
        l = cum[i+(2*K+1)] - cum[i]
        maxlen = max(maxlen, l)
    print(maxlen)
    

if '__main__' == __name__:
    resolve()

Submission status: Wrong Answer

Solution code:
# 累積和による解法
def resolve():
    N, K = list(map(int, input().split()))
    S = input()
    seq = []
    cnt = 0
    if S[0] != "1":
        seq.append(0)
    for i in range(len(S)):
        if i == N-1 or S[i] != S[i+1]:
            seq.append(cnt+1)
            cnt = 0
        else:
            cnt += 1
    if S[-1] != "1":
        seq.append(0)

    cum = [0]
    cur = 0
    for c in seq:
        cur += c
        cum.append(cur)
    
    maxlen = 0
    for i in range(0, len(cum)-(2*K+1), 2):
        l = cum[i+(2*K+1)] - cum[i]
        maxlen = max(maxlen, l)
    print(maxlen)
    

if '__main__' == __name__:
    resolve()

Submission status: Wrong Answer

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

if s[0] == "1":
  result = []
  for _ in range(n):
    if 2*_  <= n-1:
      result.append(sum(s_list[2*_:min(n,2*_ + 2*k + 1)]))
  for _ in range(n):
    if 2*_ + 1 <= n - 1:
      result.append(sum(s_list[2*_:min(n,2*_ + 2*k)]))
else:
  result = []
  for _ in range(n):
    if 2*_  <= n-1:
      result.append(sum(s_list[2*_:min(n,2*_ + 2*k)]))
  for _ in range(n):
    if 2*_ + 1 <= n - 1:
      result.append(sum(s_list[2*_:min(n,2*_ + 2*k + 1)]))
if n == 1:
  print(1)
else:
  print(max(result))

Submission status: Wrong Answer

Solution code:

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

a = []
f = S[0]
l = 0
for i in range(1, N+1):
    if f != S[i]:
        a.append(i-l)
        l = i
        f = S[i]
        

if S[0] == '0':
    a = [0] + a

SUM = sum(a[0:0+2*K+1])

MAX = max(0, SUM)
for i in range(2, len(a)):
    if i % 2 == 0:
        try:
            SUM = SUM - (a[i-2]+a[i-1]) + (a[i+2*K-1]+a[i+2*K])
            MAX = max(MAX, SUM)
        except:
            break

print(MAX)

Submission status: Wrong Answer

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

cnt = []
if s[0] == '0': cnt.append(0)
i = 0
while i < n:
    j = i
    while j < n and s[j] == s[i]:
        j += 1
    cnt.append(j- i)
    i = j
if s[-1] == '0': cnt.append(0)

m = len(cnt)
cs = [0] * (m+1)
for i in range(m):
    cs[i+1] = cs[i] + cnt[i]

ans = 0
for l in range(0, m, 2):
    r = l + 2 * k + 1
    if r >= m: r = m - 1
    ans = max(ans, cs[r] - cs[l])

print(ans)

Submission status: Wrong Answer

Solution code:

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

a = []
f = S[0]
l = 0
for i in range(1, N+1):
    #print(S[i])
    if f != S[i]:
        #a.append([l, i-1, int(f)])
        #a.append([i-l, int(f)])
        a.append(i-l)
        l = i
        f = S[i]
        
#print(a)

if S[0] == '0':
    a = [0] + a

#print(a[0:0+2*K+1])
SUM = sum(a[0:0+2*K+1])

MAX = max(0, SUM)
for i in range(2, len(a)):
    if i % 2 == 0:
        try:
            SUM = SUM - (a[i-2]+a[i-1]) + (a[i+2*K-1]+a[i+2*K])
            MAX = max(MAX, SUM)
        except:
            break

print(MAX)

Submission status: Wrong Answer

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

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

i = 0
while i < n:
    j = i
    while j < n and s[j] == s[i]:
        j += 1
    cnt.append(j- i)
    i = j
if s[-1] == '0': cnt.append(0)

m = len(cnt)
cs = [0] * (m+1)
for i in range(m):
    cs[i+1] = cs[i] + cnt[i]


ans = 0
for l in range(0, m, 2):
    r = l + 2 * k + 1
    if r > m: r = m - 1
    ans = max(ans, cs[r] - cs[l])

print(ans)

Submission status: Wrong Answer

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

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

i = 0
while i < n:
    j = i
    while j < n and s[j] == s[i]:
        j += 1
    cnt.append(j- i)
    i = j
if s[-1] == '0': cnt.append(0)

cs = [0] * (len(cnt)+1)
for i in range(len(cnt)):
    cs[i+1] = cs[i] + cnt[i]

ans = 0
m = 2 * k + 1
for i in range(0, len(cnt)-m+1, 2):
    ans = max(ans, cs[i+m] - cs[i])

print(ans)

Submission status: Wrong Answer

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

cnt = []
cnt0 = 0
cnt1 = 0
for i in range(n):
    if s[i] == '1':
        cnt1 += 1
        if cnt0: cnt.append(cnt0)
        cnt0 = 0
    if s[i] == '0':
        cnt0 += 1
        if cnt1: cnt.append(cnt1)
        cnt1 = 0
if cnt0: cnt.append(cnt0)
if cnt1: cnt.append(cnt1)

ans = 0
m = 2 * k + int(s[0])
for i in range(len(cnt) - m + 1):
    ans = max(ans, sum(cnt[i:i+m]))

print(ans)

Submission status: Wrong Answer

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

one = [len(v) for v in s.split('0') if v != '']
zero = [len(v) for v in s.split('1') if v != '']

ans = 0
if len(one) <= k+1:
    if len(zero) == 0:
        ans = len(one)
    else:
        ans = len(one) + max(zero)
else:
    for i in range(len(one)-k):
        ans = max(ans, sum(one[i:i+k+1])+sum(zero[i:i+k]))
print(ans)

Submission status: Wrong Answer

Solution code:
N,K=list(map(int,input().split()))
l=input()
n=l[0]
cnt=0
ans=[]
for i in l:
   if i != n:
      ans.append(cnt)
      cnt=1
      n=i
   else:
      cnt+=1
ans.append(cnt)
if l[0] == "0":
   sum_n=sum(ans[0:K*2])
   now=1
   end=K*2
else:
   sum_n=sum(ans[0:K*2+1])
   now=0
   end=K*2+1
max_n=sum_n
for i in range(len(ans)-end):
   sum_n-=ans[i]
   if now==0:
      now=1
   else:
      try:
         sum_n=sum_n+ans[end]+ans[end+1]
         end+=2
      except IndexError:
         sum_n+=ans[end]
      now=0
   max_n=max(sum_n,max_n)
print(max_n)

Submission status: Wrong Answer

Solution code:
from operator import add
from collections import deque
from itertools import accumulate

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

if N == 1:
  if S == "1" or K >= 1:
    print(1)
  else:
    print(0)
    
else:
  num = []
  cnt = 1
  ans = 0
  M = 2 * K + 1

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

  if S[0] == "0":
    num = [0] + num
  
  acc = list(accumulate(num,add))
  acc = [0] + acc + [0]
  A = len(acc)
  
  if M > N:
    for i in range(M,A,2):
      tmp = acc[i] - acc[i - M]
      ans = max(ans,tmp)
  else:
    ans = max(acc)

  print(ans)

Submission status: Wrong Answer

Solution code:
from operator import add
from collections import deque
from itertools import accumulate

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

if N == 1:
  if S == "1" or K >= 1:
    print(1)
  else:
    print(0)
    
else:
  num = []
  cnt = 1
  ans = 0
  M = min(2 * K + 1,N - 1)

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

  if S[0] == "0":
    num = [0] + num
  
  acc = list(accumulate(num,add))
  acc = [0] + acc + [0]
  A = len(acc)
  
  
  for i in range(M,A,2):
    tmp = acc[i] - acc[i - M]
    ans = max(ans,tmp)

  print(ans)

Submission status: Wrong Answer

Solution code:
from operator import add
from collections import deque
from itertools import accumulate

N,K = map(int,input().split())
S = input()
if N == 1:
  if S == "1" or K >= 1:
    print(1)
  else:
    print(0)
    
else:
  num = []
  cnt = 1
  ans = 0
  M = 2 * K + 1
  
  for i in range(1,N):
    if S[i] == S[i - 1]:
      cnt += 1
    else:
      num.append(cnt)
      cnt = 1
  num.append(cnt)

  if S[0] == "0":
    num = [0] + num
  
  que = deque([])
  
  acc = list(accumulate(num,add))
  acc = [0] + acc
  A = len(acc)
  
  for i in range(M,A,2):
    tmp = acc[i] - acc[i - M]
    ans = max(ans,tmp)
    
  print(ans)

Submission status: Wrong Answer

Solution code:
from collections import deque

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

if N == 1:
  if S == "1" or K >= 1:
    print(1)
  else:
    print(0)
    
else:
  num = []
  cnt = 1
  ans = 0
  M = 2 * K + 1
  
  for i in range(1,N):
    if S[i] == S[i - 1]:
      cnt += 1
    else:
      num.append(cnt)
      cnt = 1
  num.append(cnt)

  if S[0] == "0":
    num = [0] + num
  
  que = deque([])
  
  for i,n in enumerate(num):
    que.append(n)
    if i >= M:
      que.popleft()
    if i % 2 == 0:
      tmp = sum(que)
    ans = max(tmp,ans)
    
  print(ans)

Submission status: Wrong Answer

Solution code:
from collections import deque


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

if N == 1:
  if S == "1" or K >= 1:
    print(1)
  else:
    print(0)
    
else:
  num = deque([])
  if S[0] == "0":
    num.append(0)
  cnt = 1
  ans = 0
  M = 2 * K + 1
  
  for i in range(1,N):
    if S[i] == S[i - 1]:
      cnt += 1
    else:
      num.append(cnt)
      cnt = 1
      
    if len(num) > M:
      num.popleft()

    if S[i - 1] == "1":
      ans = max(sum(num),ans)
    
  num.append(cnt)
  if len(num) > M:
    num.popleft()
  ans = max(sum(num),ans)
  
  print(ans)

Submission status: Wrong Answer

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

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

if N == 1:
  if S == "1" or K >= 1:
    print(1)
  else:
    print(0)
    
else:
  num = deque([])
  if S[0] == "0":
    num.append(0)
  cnt = 1
  ans = 0
  M = 2 * K + 1
  
  for i in range(1,N):
    if S[i] == S[i - 1]:
      cnt += 1
    else:
      num.append(cnt)
      cnt = 1
      
    if len(num) > M:
      num.popleft()

    if S[i - 1] == "1":
      ans = max(sum(num),ans)
    
    if i == N - 1:
      num.append(cnt)
      if len(num) > M:
        num.popleft()
      if S[i] == "1":
        ans = max(sum(num),ans)
  
  print(ans)

Submission status: Wrong Answer

Solution code:
from collections import deque

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

if N == 1:
  if S == "1" or K >= 1:
    print(1)
  else:
    print(0)
    
else:
  num = deque([])
  if S[0] == "0":
    num.append(0)
  cnt = 1
  ans = 0
  M = 2 * K + 1
  
  for i in range(1,N):
    if S[i] == S[i - 1]:
      cnt += 1
    else:
      num.append(cnt)
      cnt = 1
      
    if len(num) > M:
      num.popleft()

    if S[i - 1] == "1":
      ans = max(sum(num),ans)
    
    if i == N - 1:
      num.append(cnt)
      if len(num) > M:
        num.popleft()
      if S[i] == "1":
        ans = max(sum(num),ans)
  
  print(ans)

Submission status: Wrong Answer

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

if N == 1:
  if S == "1" or K >= 1:
    print(1)
  else:
    print(0)
    
else:
  num = []
  cnt = 1
  for i in range(1,N):
    if S[i] == S[i - 1]:
      cnt += 1
    else:
      num.append(cnt)
      cnt = 1
    if i == N - 1:
      num.append(cnt)
  
  ans = 0
  
  if S[0] == "0":
    num = [0] + num
  
  num = num + [0] * K
  
  for i in range(0,len(num) - 2 * K + 2,2):
    tmp = sum(num[i:i + 2 * K + 1])
    ans = max(tmp,ans)
  
  print(ans)

Submission status: Wrong Answer

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


L = [0]
R = []
for i, (a, b) in enumerate(zip(s, s[1:])):
    if (a, b) == ('0', '1'):
        L.append(i+1)

    if (a, b) == ('1', '0'):
        R.append(i)
else:
    R.append(n-1)

ans = R[0]-L[0]+1
if s[0] == '0':
    k -= 1
for l, r in zip(L, R[k:]):
    x = r-l+1
    if ans < x:
        ans = x
print(ans)

Submission status: Wrong Answer

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


L = [0]
R = []
for i, (a, b) in enumerate(zip(s, s[1:])):
    if (a, b) == ('0', '1'):
        L.append(i+1)

    if (a, b) == ('1', '0'):
        R.append(i)
else:
    R.append(n-1)


if len(L) == len(R) == 1:
    print(n)
    exit()

ans = 0
if s[0] == '0':
    k -= 1
for l, r in zip(L, R[k:]):
    x = r-l+1
    if ans < x:
        ans = x
print(ans)

Submission status: Wrong Answer

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


L = [0]
R = []
for i, (a, b) in enumerate(zip(s, s[1:])):
    if (a, b) == ('0', '1'):
        L.append(i+1)

    if (a, b) == ('1', '0'):
        R.append(i)
else:
    R.append(n-1)

ans = 1
if s[0] == '0':
    k -= 1
for l, r in zip(L, R[k:]):
    x = r-l+1
    if ans < x:
        ans = x
print(ans)

Submission status: Wrong Answer

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

s=list(input())

memo = [0]

for i in range(1,n):
  if s[i] != s[i-1]:
    memo.append(i)

ans = 0

kyoukai_math = len(memo)

for i in range(kyoukai_math):
  if s[i] == '0':
    hasi = i+k*2
  else:
    hasi = i+k*2+1
    
  if hasi < kyoukai_math:
    ans = max(ans,memo[hasi]-memo[i])
  else:
    ans = max(ans,n-memo[i])
print(ans)

Submission status: Wrong Answer

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

s=list(input())

memo = [0]

for i in range(1,n):
  if s[i] != s[i-1]:
    memo.append(i)

if len(memo) == 1:
  print(n)
else:
  if s[-1] == s[-2]:
    memo.append(n)

  ans = 0

  kyoukai_math = len(memo)

  for i in range(kyoukai_math):
    if s[i] == '0':
      hasi = min(kyoukai_math-1,i+k*2)
    else:
      hasi = min(kyoukai_math-1,i+k*2+1)
    ans = max(ans,memo[hasi]-memo[i])
  print(ans)

Submission status: Wrong Answer

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

s=list(input())

memo = [0]

for i in range(1,n):
  if s[i] != s[i-1]:
    memo.append(i)

if len(memo) == 1:
  print(n)
else:
  if s[-1] == s[-2]:
    memo.append(n)

  ans = 0
  print(memo)

  kyoukai_math = len(memo)

  for i in range(kyoukai_math):
    if s[i] == '0':
      hasi = min(kyoukai_math-1,i+k*2)
    else:
      hasi = min(kyoukai_math-1,i+k*2+1)
    ans = max(ans,memo[hasi]-memo[i])
  print(ans)

Submission status: Wrong Answer

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

#累積和の準備
zero_list = []
one_list = []
tmp = S[0]
count = 1
pin = 0
if tmp == '1':
    zero_list.append(0)
for i in range(1,N):
    if S[i] == tmp:
        count += 1
    else:
        if tmp == '1':
            one_list.append(count)
            count = 1
            tmp = '0'
        else:
            zero_list.append(count)
            count = 1
            tmp = '1'
if tmp == '1':
    one_list.append(count)
else:
    zero_list.append(count)
        
if K >= len(zero_list):
    pin =1
    print(N)
else:
    start = sum(zero_list[:K]) + sum(one_list[:K])
    ans = start
    if len(one_list) > K:
        tmp = start - zero_list[0] + zero_list[K] + one_list[K]
    else:
        tmp = start - zero_list[0] + zero_list[K]
    if tmp > ans:
        ans = tmp
    basyo = 1
    last = len(one_list)
    while basyo + K < last:
        tmp = tmp -zero_list[basyo] - one_list[basyo -1] + zero_list[K + basyo] + one_list[K + basyo]
        if tmp > ans:
            ans = tmp
        basyo += 1
    if len(one_list) != len(zero_list) and K + basyo <  len(zero_list):
        tmp = tmp -zero_list[basyo] - one_list[basyo -1] + zero_list[K + basyo] 
        if ans > tmp:
            ans = tmp
if pin == 0:
    print(ans)

Submission status: Wrong Answer

Solution code:
def main():
    from itertools import accumulate
    n, k = map(int, input().split())

    s = input()
    a = s[0]
    b = s[-1]
    s = s.replace('01', '0?1').replace('10', '1?0').split('?')
    *t, = map(lambda x: len(x), s)

    if a == '0':
        t = [0, 0] + t
    else:
        t = [0] + t

    if b == '1':
        t += [0, 0]

    u = list(accumulate(t))
    ans = 0

    for i in range(1, len(u) - 2 * k, 2):
        ans = max(ans, u[i + 2 * k] - u[i - 1])

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def main():
    from itertools import accumulate
    n, k = map(int, input().split())
    s = input()
    a = s[0]
    b = s[-1]
    s = s.replace('01', '0?1').replace('10', '1?0').split('?')
    *t, = map(lambda x: len(x), s)
    t = list(accumulate(t))
    t = [0] + t + [t[-1]]

    if len(t) <= 2 * k + 1:
        print(n)
        exit()

    ans = 0
    for i in range(1, max(len(t) - 2 * k, 3), 2):
        ans = max(ans, t[i + 2 * k] - t[i - 1])

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def main():
    from itertools import groupby as gr
    n, k = map(int, input().split())
    s = input()
    a = s[0] == '0'
    s = s.replace('01', '0?1').replace('10', '1?0')
    *t, = map(lambda x: len(x), s.split('?'))
    t = [0] + t + [0]

    ans = 0
    l = min(len(t), 2 * k + 1)
    for i in range(len(t) - l + 1):
        ans = max(ans, sum(t[i:i + l]))

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def main():
    from itertools import groupby as gr
    n, k = map(int, input().split())
    s = input()
    a = s[0] == '0'
    s = s.replace('01', '0?1').replace('10', '1?0')
    *t, = map(lambda x: len(x), s.split('?'))
    t = [0] + t + [0]

    ans = 0
    for x in zip(*[iter(t)] * min(len(t), 2 * k + 1)):
        ans = max(ans, sum(x))

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def main():
    from itertools import groupby as gr
    n, k = map(int, input().split())
    s = input()
    a = s[0] == '0'
    s = s.replace('01', '0?1').replace('10', '1?0')
    *t, = map(lambda x: len(x), s.split('?'))
    t = [0] + t + [0]

    ans = 0
    for t in zip(*[iter(t)] * (2 * k + 1)):
        ans = max(ans, sum(t))

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
L=list(map(int,input()))
l=[]
countk=0
if L[0]==1:
    l.append(1)
else:
    l.append(-1)
    countk=1
for i in range(1,N):
    if L[i]==L[i-1]:
        if l[-1]<0:
            l[-1]+=-1
        else:
            l[-1]+=1
    else:
        if L[i]==1:
            l.append(1)
        else:
            l.append(-1)
            countk+=1
count=0
x=0
for i in range(len(l)):
    m=l[i]
    if l[i]<0:
        m=l[i]*(-1)
        count+=1
    x+=m
    if count==1:
        start=i-1
    if count==K:
        end=i
        if i==len(l)-1:
            break
        x+=l[i+1]
        break
X=x
if count<K:
    print(x)
else:
    for i in range(K,countk):
        if start==0:
            m=-1*l[start]
        else:
            m=-1*l[start]+l[start-1]
        if end+3==len(l):
            M=l[end+2]
        else:
            M=l[end+2]*(-1)+l[end+3]
        X=max(x,x+M-m)
        x=x+M-m
        start+=2
        end+=2
    print(X)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-

#############
# Libraries #
#############

import sys
input = sys.stdin.readline

import math
#from math import gcd
import bisect
from collections import defaultdict
from collections import deque
from functools import lru_cache


#############
# Constants #
#############

MOD = 10**9+7
INF = float('inf')

#############
# Functions #
#############

######INPUT######
def I(): return int(input().strip())
def S(): return input().strip()
def IL(): return list(map(int,input().split()))
def SL(): return list(map(str,input().split()))
def ILs(n): return list(int(input()) for _ in range(n))
def SLs(n): return list(input().strip() for _ in range(n))
def ILL(n): return [list(map(int, input().split())) for _ in range(n)]
def SLL(n): return [list(map(str, input().split())) for _ in range(n)]

######OUTPUT######
def P(arg): print(arg); return
def Y(): print("Yes"); return
def N(): print("No"); return
def E(): exit()
def PE(arg): print(arg); exit()
def YE(): print("Yes"); exit()
def NE(): print("No"); exit()

#####Shorten#####
def DD(arg): return defaultdict(arg)


#####Inverse#####
def inv(n): return pow(n, MOD-2, MOD)

######Combination######
kaijo_memo = []
def kaijo(n):
  if(len(kaijo_memo) > n):
    return kaijo_memo[n]
  if(len(kaijo_memo) == 0):
    kaijo_memo.append(1)
  while(len(kaijo_memo) <= n):
    kaijo_memo.append(kaijo_memo[-1] * len(kaijo_memo) % MOD)
  return kaijo_memo[n]

gyaku_kaijo_memo = []
def gyaku_kaijo(n):
  if(len(gyaku_kaijo_memo) > n):
    return gyaku_kaijo_memo[n]
  if(len(gyaku_kaijo_memo) == 0):
    gyaku_kaijo_memo.append(1)
  while(len(gyaku_kaijo_memo) <= n):
    gyaku_kaijo_memo.append(gyaku_kaijo_memo[-1] * pow(len(gyaku_kaijo_memo),MOD-2,MOD) % MOD)
  return gyaku_kaijo_memo[n]

def nCr(n,r):
  if(n == r):
    return 1
  if(n < r or r < 0):
    return 0
  ret = 1
  ret = ret * kaijo(n) % MOD
  ret = ret * gyaku_kaijo(r) % MOD
  ret = ret * gyaku_kaijo(n-r) % MOD
  return ret

######Factorization######
def factorization(n):
  arr = []
  temp = n
  for i in range(2, int(-(-n**0.5//1))+1):
    if temp%i==0:
      cnt=0
      while temp%i==0: 
        cnt+=1 
        temp //= i
      arr.append([i, cnt])
  if temp!=1:
    arr.append([temp, 1])
  if arr==[]:
    arr.append([n, 1])
  return arr

#####MakeDivisors######
def make_divisors(n):
  divisors = []
  for i in range(1, int(n**0.5)+1):
    if n % i == 0:
      divisors.append(i)
      if i != n // i: 
        divisors.append(n//i)
  return divisors

#####GCD#####
def gcd(a, b):
    while b:
        a, b = b, a % b
    return a

#####LCM#####
def lcm(a, b):
    return a * b // gcd (a, b)

#####BitCount#####
def count_bit(n):
  count = 0
  while n:
    n &= n -1
    count += 1
  return count

#####ChangeBase#####
def base_10_to_n(X, n):
  if X//n:
    return base_10_to_n(X//n, n)+[X%n]
  return [X%n]

def base_n_to_10(X, n):
  return sum(int(str(X)[-i-1])*n**i for i in range(len(str(X))))

#####IntLog#####
def int_log(n, a):
  count = 0
  while n>=a:
    n //= a
    count += 1
  return count

#############
# Main Code #
#############

N,K = IL()
S = S()

zero = []
one = []

prev = None
count = 1
for s in S:
  if s == prev:
    count += 1
  else:
    if prev == "0":
      zero.append(count)
    if prev == "1":
      one.append(count)
    count = 1
  prev = s
if prev == "0":
  zero.append(count)
if prev == "1":
  one.append(count)
  
if len(zero) <= K:
  print(N)
  exit()

ruiseki_zero = [0]
ruiseki_one = [0]
for i in range(len(zero)):
  ruiseki_zero.append(ruiseki_zero[-1]+zero[i])
for i in range(len(one)):
  ruiseki_one.append(ruiseki_one[-1]+one[i])

MAX = 0
if S[0] == "0":
  for i in range(len(ruiseki_zero)-K):
    MAX = max(MAX,ruiseki_zero[i+K]-ruiseki_zero[i]+ruiseki_one[min(i+K,len(ruiseki_one)-1)]-ruiseki_one[max(i-1,0)])
else:
  for i in range(len(ruiseki_zero)-K):
    MAX = max(MAX,ruiseki_zero[i+K]-ruiseki_zero[i]+ruiseki_one[min(i+K+1,len(ruiseki_one)-1)]-ruiseki_one[max(i-1,0)])

print(MAX)