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:
import itertools
N, K = map(int, input().split())
S = input()
flg = 1 if S[0] == '1' else 0
gr = itertools.groupby(S)
ans, tmp = 0, 0
check_0, check_1 = [], []
for key, group in gr:
    target = len(list(group))
    tmp += target
    if key == '1':
        check_1.append(target)
    else:
        check_0.append(target)
    if len(check_0) > K:
        if key == '0':
            tmp -= check_0[len(check_0)-(K+1)]+check_1[len(check_1)-(K+flg)]
    ans = max(ans,tmp)
print(ans)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
s=input()
s01=[[1,s[0]]]
for i in range(1,n):
  if s[i]==s[i-1]:
    s01[-1][0]+=1
  else:
    s01.append([1,s[i]])
a=len([i0 for i0,i1 in s01 if i1=='0'])
if a<=k:
  print(n)
  exit()
# 累計和
si=0
sums=[0]
for s01i in s01:
  si+=s01i[0]
  sums.append(si)
ans=0
#print(s01)
#print(sums)
l=0 if s[0]=='0' else 1
for i in range(l,len(s01)-1,2):
  l=max(0,i-1)
  r=min(len(sums)-1,2*k+i)
  #print(sums[r]-sums[l],i,l,r)
  ans=max(ans,sums[r]-sums[l])
print(ans)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
s=input()
s01=[[1,s[0]]]
for i in range(1,n):
  if s[i]==s[i-1]:
    s01[-1][0]+=1
  else:
    s01.append([1,s[i]])
a=len([i0 for i0,i1 in s01 if i1=='0'])
if a<=k:
  print(n)
  exit()
# 累計和
si=0
sums=[0]
for s01i in s01:
  si+=s01i[0]
  sums.append(si)
ans=0
#print(s01)
#print(sums)
l=0 if s[0]=='0' else 1
for i in range(l,len(s01)-k,2):
  l=max(0,i-1)
  r=min(len(sums)-1,2*(k-1)+2+i)
  #print(sums[r]-sums[l],i,l,r)
  ans=max(ans,sums[r]-sums[l])
print(ans)

Submission status: Wrong Answer

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

if(S[0] == "1"):
    s = []
else:
    s = [0]
cnt = 1
tmp = S[0]
for i in range(1, len(S)):
    if(tmp == S[i]):
        cnt += 1
    else:
        s.append(cnt)
        tmp = S[i]
        cnt = 1
s.append(cnt)
if(len(s)//2 <= K):
    ans = sum(s)        
else:
    ans = sum(s[:(2*K+1)])
    tmp = ans
    cnt = 0
    for i in range(2*K+1, len(s)-1, 2):
        if(ans < tmp-s[cnt]-s[cnt+1]+s[i]+s[i+1]):
            ans = tmp-s[cnt]-s[cnt+1]+s[i]+s[i+1]
            tmp = tmp-s[cnt]-s[cnt+1]+s[i]+s[i+1]
        else:
            tmp = tmp-s[cnt]-s[cnt+1]+s[i]+s[i+1]
        cnt += 2

print(ans)

Submission status: Wrong Answer

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

s = [0]
cnt = 1
tmp = S[0]
for i in range(1, len(S)):
    if(tmp == S[i]):
        cnt += 1
    else:
        s.append(cnt)
        tmp = S[i]
        cnt = 1
s.append(cnt)
if(len(s)//2 <= K):
    ans = sum(s)        
else:        
    if(s[0] == "0"):
        ans = sum(s[:2*K])
        tmp = ans
        cnt = 0
        for i in range(2*K+1, len(s)-1, 2):
            if(ans < tmp-s[cnt]-s[cnt+1]+s[i]+s[i+1]):
                ans = tmp-s[cnt]-s[cnt+1]+s[i]+s[i+1]
                tmp = ans
            cnt += 2
    else:
        ans = sum(s[:(2*K+1)])
        tmp = ans
        cnt = 0
        for i in range(2*K+2, len(s)-1, 2):
            if(ans < tmp-s[cnt]-s[cnt+1]+s[i]+s[i+1]):
                ans = tmp-s[cnt]-s[cnt+1]+s[i]+s[i+1]
                tmp = ans
            cnt += 2

print(ans)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**8)
def ii(): return int(sys.stdin.readline())
def mi(): return map(int, sys.stdin.readline().split())
def li(): return list(map(int, sys.stdin.readline().split()))
def li2(N): return [list(map(int, sys.stdin.readline().split())) for _ in range(N)]
def dp2(ini, i, j): return [[ini]*i for _ in range(j)]
def dp3(ini, i, j, k): return [[[ini]*i for _ in range(j)] for _ in range(k)]
#import bisect #bisect.bisect_left(B, a)
#from collections import defaultdict #d = defaultdict(int) d[key] += value
#from itertools import accumulate #list(accumulate(A))

N, K = mi()
S = input()

len_list = []

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

if S[0] == '1' or S[1] != '1':
    len_list = [0] + len_list
#print(len_list)
i = 0
wa = 0
while True:
    if i+2*K+1 < N:
        wa = max(wa, sum(len_list[i:i+2*K+1]))
    else:
        wa = max(wa, sum(len_list[i:]))
        break
    i += 1
print(wa)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**8)
def ii(): return int(sys.stdin.readline())
def mi(): return map(int, sys.stdin.readline().split())
def li(): return list(map(int, sys.stdin.readline().split()))
def li2(N): return [list(map(int, sys.stdin.readline().split())) for _ in range(N)]
def dp2(ini, i, j): return [[ini]*i for _ in range(j)]
def dp3(ini, i, j, k): return [[[ini]*i for _ in range(j)] for _ in range(k)]
#import bisect #bisect.bisect_left(B, a)
#from collections import defaultdict #d = defaultdict(int) d[key] += value
#from itertools import accumulate #list(accumulate(A))

N, K = mi()
S = input()
zeros = [[0, 0]]

cnt = 0
for i in range(N):
    if S[i] == '0':
        if cnt == 0 and len(zeros) > 1:
            zeros[-1][1] = i-1
        cnt += 1
    else:
        if cnt > 0:
            zeros.append([cnt, 0])
            cnt = 0
if cnt > 0:
    zeros.append([cnt, N-1])
if zeros[-1][1] == 0:
    zeros[-1][1] = N-1

l_zeros = len(zeros)
wa = 0
if K >= l_zeros-1:
    print(N)
    exit()
for i in range(l_zeros-K):
    wa = max(zeros[i+K][1] - zeros[i][1] + 1, wa)

print(wa)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**8)
def ii(): return int(sys.stdin.readline())
def mi(): return map(int, sys.stdin.readline().split())
def li(): return list(map(int, sys.stdin.readline().split()))
def li2(N): return [list(map(int, sys.stdin.readline().split())) for _ in range(N)]
def dp2(ini, i, j): return [[ini]*i for _ in range(j)]
def dp3(ini, i, j, k): return [[[ini]*i for _ in range(j)] for _ in range(k)]
#import bisect #bisect.bisect_left(B, a)
#from collections import defaultdict #d = defaultdict(int) d[key] += value
#from itertools import accumulate #list(accumulate(A))

N, K = mi()
S = input()
zeros = [[0, 0]]

cnt = 0
for i in range(N):
    if S[i] == '0':
        if cnt == 0 and len(zeros) > 1:
            zeros[-1][1] = i-1
        cnt += 1
    else:
        if cnt > 0:
            zeros.append([cnt, 0])
            cnt = 0
if cnt > 0:
    zeros.append([cnt, N-1])
if zeros[-1][1] == 0:
    zeros[-1][1] = N-1

l_zeros = len(zeros)
wa = 0
if K >= l_zeros-1:
    print(N)
    exit()
for i in range(l_zeros-K):
    print(zeros[i+K][1] - zeros[i][1] + 1)
    wa = max(zeros[i+K][1] - zeros[i][1] + 1, wa)

print(wa)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**8)
def ii(): return int(sys.stdin.readline())
def mi(): return map(int, sys.stdin.readline().split())
def li(): return list(map(int, sys.stdin.readline().split()))
def li2(N): return [list(map(int, sys.stdin.readline().split())) for _ in range(N)]
def dp2(ini, i, j): return [[ini]*i for _ in range(j)]
def dp3(ini, i, j, k): return [[[ini]*i for _ in range(j)] for _ in range(k)]
#import bisect #bisect.bisect_left(B, a)
#from collections import defaultdict #d = defaultdict(int) d[key] += value
#from itertools import accumulate #list(accumulate(A))

N, K = mi()
S = input()
zeros = [[0, 0]]

cnt = 0
for i in range(N):
    if S[i] == '0':
        if cnt == 0 and len(zeros) > 1:
            zeros[-1][1] = i-1
        cnt += 1
    else:
        if cnt > 0:
            zeros.append([cnt, 0])
            cnt = 0
if cnt > 0:
    zeros.append([cnt, N-1])
if zeros[-1][1] == 0:
    zeros[-1][1] = N-1

print(zeros)
l_zeros = len(zeros)
wa = 0
if K >= l_zeros-1:
    print(N)
    exit()
for i in range(l_zeros-K):
    print(zeros[i+K][1] - zeros[i][1] + 1)
    wa = max(zeros[i+K][1] - zeros[i][1] + 1, wa)

print(wa)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**8)
def ii(): return int(sys.stdin.readline())
def mi(): return map(int, sys.stdin.readline().split())
def li(): return list(map(int, sys.stdin.readline().split()))
def li2(N): return [list(map(int, sys.stdin.readline().split())) for _ in range(N)]
def dp2(ini, i, j): return [[ini]*i for _ in range(j)]
def dp3(ini, i, j, k): return [[[ini]*i for _ in range(j)] for _ in range(k)]
#import bisect #bisect.bisect_left(B, a)
#from collections import defaultdict #d = defaultdict(int) d[key] += value
#from itertools import accumulate #list(accumulate(A))

N, K = mi()
S = input()
zeros = [[0, 0]]

cnt = 0
for i in range(N):
    if S[i] == '0':
        if cnt == 0 and len(zeros) > 1:
            zeros[-1][1] = i-1
        cnt += 1
    else:
        if cnt > 0:
            zeros.append([cnt, 0])
            cnt = 0
if cnt > 0:
    zeros.append([cnt, N-1])
if zeros[-1][1] == 0:
    zeros[-1][1] = N-1

l_zeros = len(zeros)
wa = 0
if K >= l_zeros:
    print(N)
    exit()
for i in range(l_zeros-K):
    wa = max(zeros[i+K][1] - zeros[i][1] + 1, wa)

print(wa)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
s = input()
s1 = []
count = 0
if n>1:
    if s[0] == "0":
        s1.append(0)
    for i in range(n-1):
        if s[i+1] == s[i]:
            count += 1
            if i+1 == n-1:
                s1.append(count+1)
        elif s[i+1] != s[i]:
            count += 1
            s1.append(count)
            count = 0
            if i+1 == n-1:
                s1.append(count+1)
else:
    if s == "1":
        s1.append(1)
    else:
        s1.append(0)
        s1.append(1)
#print(s1)

# s1の累積和
s2 = [0]*(len(s1)+1)
s2[1] = s1[0]
for i in range(1, len(s1)):
    s2[i+1] = s1[i] + s2[i]
#print(s2)

ans = 0
if len(s2) > 2*k+1:
    for l in range(len(s2)-(2*k+1))[::2]:
        res = s2[2*k+1+l] - s2[l]
        ans = max(ans, res)
else:
    ans = n
print(ans)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
s = input()
s1 = []
count = 0
if n>1:
    if s[0] == "0":
        s1.append(0)
    for i in range(n-1):
        if s[i+1] == s[i]:
            count += 1
            if i+1 == n-1:
                s1.append(count+1)
        elif s[i+1] != s[i]:
            count += 1
            s1.append(count)
            count = 0
            if i+1 == n-1:
                s1.append(count+1)
else:
    if s == "1":
        s1.append(1)
    else:
        s1.append(0)
        s1.append(1)
#print(s1)

# s1の累積和
s2 = [0]*(len(s1)+1)
s2[1] = s1[0]
for i in range(1, len(s1)):
    s2[i+1] = s1[i] + s2[i]
#print(s2)

ans = 0
if len(s2) > 2*k+1:
    for l in range(len(s2)-(2*k+1))[::2]:
        res = s2[2*k+1+l] - s2[l]
        ans = max(ans, res)
else:
    ans = s2[-1]
print(ans)

Submission status: Wrong Answer

Solution code:
import numpy as np

def rle(x):
    ''' Run Length Encoding

    rle('001111000000011')
    (array([2, 4, 7, 2]),
     array(['0', '1', '0', '1'], dtype='<U1'))

    rle('aabcccbbbaab')
    (array([2, 1, 3, 3, 2, 1]),
     array(['a', 'b', 'c', 'b', 'a', 'b'], dtype='<U1'))
    '''
    x = np.array(list(x))
    n = len(x)
    if n == 0:
        return (1, '')
    y = list(map(lambda a, b: a != b, x[1:], x[:-1]))
    i = np.hstack((np.where(y)[0]+1, n))
    return (np.diff(np.hstack((0, i))), x[i-1])


def ABC124D(n, k, s):
    s = list(s)
    Max = 0
    for i in range(k):
        count, value = rle(s)
        if len(value) == 1:
            return count[0]
        cumsum = np.cumsum(count)
        R = cumsum-1
        L = np.hstack((0, cumsum[:-1]))
        # print("==================")
        # print(f"L = {L}")
        # print(f"R = {R}")
        max2 = 0
        for j in range(len(value)):
            if value[j] == '0':
                t = s.copy()
                # print(f"L = {L[j]}  R = {R[j]}")
                t[L[j]:R[j]+1] = np.repeat('1', R[j]+1 - L[j])
                count2, value2 = rle(t)
                # print(f"s = {s}")
                # print(f"t = {t}")
                count2[value2 == '0'] = 0
                if max(count2) > max2:
                    max2, L2, R2 = max(count2), L[j], R[j]
        Max = max(Max, max2)
        s[L2:R2+1] = np.repeat('1', R2+1 - L2)
    print(Max)

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

Submission status: Wrong Answer

Solution code:
from itertools import accumulate, permutations, combinations, combinations_with_replacement, groupby, product
# import math
# import numpy as np  # Pythonのみ！
# from operator import xor
# import re
# from scipy.sparse.csgraph import connected_components  # Pythonのみ！
# ↑cf.  https://note.nkmk.me/python-scipy-connected-components/
# from scipy.sparse import csr_matrix
# import string
# 途中
import sys

sys.setrecursionlimit(10 ** 5 + 10)


def input(): return sys.stdin.readline().strip()


def resolve():
    N, K = map(int, input().split())
    S = input()
    S = [int(c) for c in S]

    gr = groupby(S)
    cnt = 0
    keys = []
    lens = []
    for key, group in gr:  # groupはイテレータ。list(group)でリスト化
        """ex.
        0: [0, 0, 0]
        1: [1, 1]
        0: [0, 0, 0]
        1: [1, 1]
        0: [0]
        1: [1]
        """
        keys.append(key)
        lens.append(len(list(group)))
    if keys[0] == 0:
        keys = [1] + keys
        lens = [0] + lens
    ans = 0
    i = 0
    while True:
        if i + 2 * K + 1 < len(keys):
            val = sum(lens[i:i + 2 * K + 1])
            ans = max(val, ans)
            i += 1
        else:
            break
    print(ans)


resolve()

Submission status: Wrong Answer

Solution code:
from itertools import*
n, k = map(int, input().split())
s = input()
d = [len(list(t)) for _, t in groupby(s)]
if s[0] == "0":
    d = [0] + d
a = [0] + list(accumulate(d))
m = l = 0
while True:
  try:
    m = max(m, a[l+2*k+1] - a[l])
    l += 2
  except IndexError:
    break
if 2*k+1 > len(d):
  m = n
print(m)

Submission status: Wrong Answer

Solution code:
from itertools import*
n, k = map(int, input().split())
s = input()
d = [len(list(t)) for _, t in groupby(s)]
if s[0] == "0":
    d = [0] + d
a = [0] + list(accumulate(d))
m = l = 0
while True:
  try:
    m = max(m, a[l+2*k+1] - a[l])
    l += 2
  except IndexError:
    break
print(m)

Submission status: Wrong Answer

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

zero_index_left = []
flag = False
for i in range(N):
    if flag is False and S[i] == "0":
        zero_index_left.append(i)
        flag = True
    elif S[i] == "1":
        flag = False

if K >= len(zero_index_left):
    print(N)
    exit()
    
zero_index_right = []
S = S[:: -1]
flag = False
for i in range(N):
    if flag is False and S[i] == "0":
        zero_index_right.append(i)
        flag = True
    elif S[i] == "1":
        flag = False
        
ans = 0
for i in range(len(zero_index_left) - K):
    if i == 0:
        ans = max(ans, zero_index_left[i + K])
        ans = max(ans, zero_index_right[i + K])
    else:
        ans = max(ans, zero_index_left[i + K] - zero_index_right[-i] + 1 - 1)
        ans = max(ans, zero_index_right[i + K] - zero_index_left[-i] + 1 - 1)
        
print(ans)

Submission status: Wrong Answer

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

zero_index_left = []
flag = False
for i in range(N):
    if flag is False and S[i] == "0":
        zero_index_left.append(i)
        flag = True
    elif S[i] == "1":
        flag = False

if K >= len(zero_index_left):
    print(N)
    exit()
    
zero_index_right = []
flag = False
for i in range(N):
    if flag is False and S[:: -1][i] == "0":
        zero_index_right.append(i)
        flag = True
    elif S[:: -1][i] == "1":
        flag = False
        
ans = 0
for i in range(len(zero_index_left) - K):
    if i == 0:
        ans = max(ans, zero_index_left[i + K])
    else:
        ans = max(ans, zero_index_left[i + K] - zero_index_right[-i] - 1)
for i in range(len(zero_index_right) - K):
    if i == 0:
        ans = max(ans, zero_index_right[i + K])
    else:
        ans = max(ans, zero_index_right[i + K] - zero_index_left[-i] - 1)
        
print(ans)

Submission status: Wrong Answer

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

from itertools import groupby,accumulate
gr=groupby(s)
l=[]
for K,g in gr:
  l.append(len(list(g)))

if len(l)%2==0:
  #計算の都合上個数調整
  l.append(0)
l=[0]+l
l_sum=list(accumulate(l))

add=2*k
if s[0]=="1":
  add+=1
ans=0
for i in range(0,len(l)-1,2):
  left=i
  right=min(i+add,int(len(l)-1))
  tmp=l_sum[right]-l_sum[left]
  ans=max(tmp,ans)
print(ans)

Submission status: Wrong Answer

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

from itertools import groupby,accumulate
gr=groupby(s)
l=[]
for K,g in gr:
  l.append(len(list(g)))

if len(l)%2==0:
  #計算の都合上個数調整
  l.append(0)
l=[0]+l
l_sum=list(accumulate(l))

add=2*k
if s[0]=="1":
  add+=1
ans=0
for i in range(0,len(l),2):
  left=i
  right=min(i+add,int(len(l)-1))
  tmp=l_sum[right]-l_sum[left]
  ans=max(tmp,ans)
print(ans)

Submission status: Wrong Answer

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

from itertools import groupby,accumulate
gr=groupby(s)
l=[]
for K,g in gr:
  l.append(len(list(g)))

# if len(l)%2==0:
  # 計算の都合上個数調整
  # l.append(0)
l=[0]+l
l_sum=list(accumulate(l))

add=2*k
if s[0]=="1":
  add+=1
ans=0
for i in range(0,len(l),2):
  left=i
  right=min(i+add,int(len(l)-1))
  tmp=l_sum[right]-l_sum[left]
  ans=max(tmp,ans)
print(ans)

Submission status: Wrong Answer

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

if n==1:
  if s=="1":
    print(1)
  else:
    print(1)
  exit(0)

from itertools import groupby,accumulate
gr=groupby(s)
l=[]
for K,g in gr:
  l.append(len(list(g)))
l=[0]+l
# print(l)
l_acc=list(accumulate(l))
# print(l_acc)
kukan=0
if s[0]=="0":
  kukan=2*k
else:
  kukan=2*k+1
ans=0
for i in range(len(l)-kukan+1):
  if i+kukan<len(l):
    ans=max(ans,l_acc[i+kukan]-l_acc[i])
print(ans)

Submission status: Wrong Answer

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

flg = False
if s[0] == "0":
    flg = True

l = len(res)
su = [0] * (l + 1)
for i in range(1, l + 1):
    su[i] = su[i - 1] + res[i - 1]

if l - 2 * k < 0:
    print(n)
    exit()

ans = 1
if not flg:
    for i in range(l - 2 * k):
        ans = max(ans, su[2 * k + 1 + i] - su[i])
else:
    for i in range(l - 2 * k):
        ans = max(ans, su[2 * k + i] - su[i])
print(ans)

Submission status: Wrong Answer

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

flg = False
if s[0] == "0":
    flg = True

l = len(res)
su = [0] * (l + 1)
for i in range(1, l + 1):
    su[i] = su[i - 1] + res[i - 1]

ans = 1
if not flg:
    for i in range(l - 2 * k):
        ans = max(ans, su[2 * k + 1 + i] - su[i])
else:
    for i in range(l - 2 * k):
        ans = max(ans, su[2 * k + i] - su[i])
print(ans)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
s = input()
lis = [0]
j = 0
if s[0] == '0':
    lis.append(0)
    j = 1
#print(len(lis))
lis[j] += 1
for i in range(1,n):
    #print(len(lis))
    if s[i] != s[i-1]:
        j += 1
        lis.append(0)
    lis[j] += 1
if s[n-1] == '0':
    lis.append(0)
#print(lis)
com = [0] * (len(lis) + 1)
for i in range(len(lis)):
    com[i+1] = com[i] + lis[i]
#print(com)
ans = 0
for i in range(len(lis)):
    if i % 2 == 0:
        ans = max(ans,lis[i])


for i in range(len(com) - (k*2+1)+1):
    if i % 2 == 1:
        continue
    val = com[i+(k*2+1)] - com[i]
    ans = max(val,ans)
print(ans)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
s = input()
lis = [0]
j = 0
if s[0] == '0':
    lis.append(0)
    j = 1
#print(len(lis))
lis[j] += 1
for i in range(1,n):
    #print(len(lis))
    if s[i] != s[i-1]:
        j += 1
        lis.append(0)
    lis[j] += 1
if s[n-1] == '0':
    lis.append(0)
#print(lis)
com = [0] * (len(lis) + 1)
for i in range(len(lis)):
    com[i+1] = com[i] + lis[i]
#print(com)
ans = 0
for i in range(len(lis)):
    if i % 2 == 0:
        ans = max(ans,lis[i])


for i in range(len(com) - (k*2+1)):
    if i % 2 == 1:
        continue
    val = com[i+(k*2+1)] - com[i]
    ans = max(val,ans)
print(ans)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
s = input()
lis = [0]
j = 0
if s[0] == '0':
    lis.append(0)
    j = 1
#print(len(lis))
lis[j] += 1
for i in range(1,n):
    #print(len(lis))
    if s[i] != s[i-1]:
        j += 1
        lis.append(0)
    lis[j] += 1
#print(lis)
com = [0] * (len(lis) + 1)
for i in range(len(lis)):
    com[i+1] = com[i] + lis[i]
#print(com)
ans = 0
for i in range(len(lis)):
    if i % 2 == 0:
        ans = max(ans,lis[i])


for i in range(len(com) - (k*2+1)):
    if i % 2 == 1:
        continue
    val = com[i+(k*2+1)] - com[i]
    ans = max(val,ans)
print(ans)

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
S=input()
l=[]
flag=S[0]
cnt=0
cuml=[]
for i in range(N-1):
    if S[i+1]!=S[i]:
        l.append(cnt+1)
        cnt=0
    else:
        cnt=cnt+1
l.append(cnt+1)
#print(l)
cuml.append(0)
cnt=0
for i in range(len(l)):
    cnt=cnt+l[i]
    cuml.append(cnt)
#print(cuml)
i=0
ma=0
n=len(cuml)
if flag=="1":
    if ((n-1)//2)>=K:
        while True:
            if (i%2==0)and(i+2*K+1<n):
                cnt=cuml[i+2*K+1]-cuml[i]
            elif (i%2==1)and(i+2*K<n):
                cnt = cuml[i + 2 * K] - cuml[i]
            else:
                break
            #print(cnt)
            ma=max(ma,cnt)
            i=i+1
    else:
        ma=len(S)
else:
    if (n//2)>=K:
        while True:
            if (i%2==1)and(i+2*K+1<n):
                cnt=cuml[i+2*K+1]-cuml[i]
            elif (i%2==0)and(i+2*K<n):
                cnt = cuml[i + 2 * K] - cuml[i]
            else:
                break
            ma=max(ma,cnt)
            #print(cnt)
            i=i+1
    else:
        ma=len(S)
print(ma)

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
S=input()
l=[]
flag=S[0]
cnt=0
cuml=[]
for i in range(N-1):
    if S[i+1]!=S[i]:
        l.append(cnt+1)
        cnt=0
    else:
        cnt=cnt+1
l.append(cnt+1)
#print(l)
cuml.append(0)
cnt=0
for i in range(len(l)):
    cnt=cnt+l[i]
    cuml.append(cnt)
#print(cuml)
i=0
ma=0
n=len(cuml)
if flag=="1":
    while True:
        if (i%2==0)and(i+2*K+1<n):
            cnt=cuml[i+2*K+1]-cuml[i]
        elif (i%2==1)and(i+2*K<n):
            cnt = cuml[i + 2 * K] - cuml[i]
        else:
            break
        #print(cnt)
        ma=max(ma,cnt)
        i=i+1
else:
    while True:
        if (i%2==1)and(i+2*K+1<n):
            cnt=cuml[i+2*K+1]-cuml[i]
        elif (i%2==0)and(i+2*K<n):
            cnt = cuml[i + 2 * K] - cuml[i]
        else:
            break
        ma=max(ma,cnt)
        #print(cnt)
        i=i+1
if ma==0:
    ma=len(S)
print(ma)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
s = input()
T0 = []  # 連続する0の個数を左からカウントしたもの
T1 = []  # 連続する1の個数を左からカウントしたもの
tmp0 = 0
tmp1 = 0
for i in range(n):
    if s[i] == '0':
        if tmp1 > 0:
            T1.append(tmp1)
            tmp1 = 0
        tmp0 += 1
    else:
        if tmp0 > 0:
            T0.append(tmp0)
            tmp0 = 0
        tmp1 += 1
    if i == n-1:
        if tmp0 > 0:
            T0.append(tmp0)
        elif tmp1 > 0:
            T1.append(tmp1)

if len(T0) <= k:
    print(n)
else:
    idx = 0
    tmp = sum(T0[:k])
    for i in range(1, len(T0)-k+1):
        new = tmp + T0[i+k-1] - T0[i-1]
        if new > tmp:
            idx = i
            tmp = new
    if s[0] == '0':
        print(sum(T0[idx:idx+k]) + sum(T1[max(idx-1, 0):idx+k]))
    else:
        print(sum(T0[idx:idx+k]) + sum(T1[idx:idx+k+1]))

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
s = input()
T0 = []  # 連続する0の個数を左からカウントしたもの
T1 = []  # 連続する1の個数を左からカウントしたもの
tmp0 = 0
tmp1 = 0
for i in range(n):
    if s[i] == '0':
        if tmp1 > 0:
            T1.append(tmp1)
            tmp1 = 0
        tmp0 += 1
    else:
        if tmp0 > 0:
            T0.append(tmp0)
            tmp0 = 0
        tmp1 += 1
    if i == n-1:
        if tmp0 > 0:
            T0.append(tmp0)
        elif tmp1 > 0:
            T1.append(tmp1)

if len(T0) <= k:
    print(n)
else:
    idx, tmp = 0, 0
    for i in range(len(T0) - k + 1):
        if sum(T0[i:i+k]) > tmp:
            idx = i
            tmp = sum(T0[i:i+k])
    if s[0] == '0':
        print(sum(T0[idx:idx+k]) + sum(T1[idx-1:idx+k]))
    else:
        print(sum(T0[idx:idx+k]) + sum(T1[idx:idx+k+1]))

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
s = input()
a = []
c = 1
for i in range(n-1):
    if s[i]==s[i+1]:
        c+=1
    else:
        a.append(c)
        c = 1
a.append(c)
b = [0]
for i in range(len(a)):
    b.append(b[-1]+a[i])
l = 2*k+1
n=len(b)
st=int(s[0])
ans = 0
for i in range(st,n,2):
    ans = max(ans,b[i]-b[max(0,i-l)])
print(ans)

Submission status: Wrong Answer

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

if s[0]=='0':
    l = min(len(a),2*k)
    ans = max(a[1::2]) if len(a)!=1 else 0
else:
    l = min(len(a),2*k+1)
    ans = max(a[::2])


b = [0]
for i in range(len(a)):
    b.append(b[-1]+a[i])
n=len(b)-1
for i in range(0,len(b),2):
    ans = max(ans,b[min(i+l,n)]-b[max(0,i)])
print(ans)

Submission status: Wrong Answer

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

if s[0]=='0':
    l = min(len(a),2*k)
    ans = max(a[1::2]) if len(a)!=1 else 0
else:
    l = min(len(a),2*k+1)
    ans = max(a[::2])


b = [0]
for i in range(len(a)):
    b.append(b[-1]+a[i])
n=len(b)-1
for i in range(len(b),2):
    ans = max(ans,b[min(i+l,n)]-b[max(0,i)])
print(ans)

Submission status: Wrong Answer

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

if s[0]=='0':
    l = min(len(a),2*k)
    ans = max(a[1::2]) if len(a)!=1 else 0
else:
    l = min(len(a),2*k+1)
    ans = max(a[::2])


b = [0]
for i in range(len(a)):
    b.append(b[-1]+a[i])
n=len(b)-1
for i in range(len(b)):
    ans = max(ans,b[min(i+l,n)]-b[max(0,i)])
print(ans)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
s = input()
a = []
c = 1
for i in range(n-1):
    if s[i]==s[i+1]:
        c+=1
    else:
        a.append(c)
        c = 1
a.append(c)
if s[0]=='0':
    l = min(len(a),2*k)
    ans = max(a[1::2]) if len(a)!=1 else 0
else:
    l = min(len(a),2*k+1)
    ans = max(a[::2])


b = [0]
for i in range(len(a)):
    b.append(b[-1]+a[i])

for i in range(len(b)-l):
    ans = max(ans,b[i+l]-b[i])
print(ans)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
s = input()
a,b = [],[]
c = 1
for i in range(n-1):
    if s[i]==s[i+1]:
        c+=1
    else:
        a.append(c)
        c = 1
a.append(c)
if s[0]=='0':
    l = min(len(a),2*k)
    ans = max(a[1::2]) if len(a)!=1 else 0
else:
    l = min(len(a),2*k+1)
    ans = max(a[::2])

for i in range(n-l):
    ans = max(ans,sum(a[i:i+l]))
print(ans)

Submission status: Wrong Answer

Solution code:
# coding: utf-8

import sys
import math
import collections
import itertools
INF = 10 ** 13
MOD = 10 ** 9 + 7
def input() : return sys.stdin.readline().strip()
def lcm(x, y) : return (x * y) // math.gcd(x, y)
def I() : return int(input())
def LI() : return [int(x) for x in input().split()]
def RI(N) : return [int(input()) for _ in range(N)]
def LRI(N) : return [[int(x) for x in input().split()] for _ in range(N)]
def LS() : return input().split()
def RS(N) : return [input() for _ in range(N)]
def LRS(N) : return [input().split() for _ in range(N)]
def PL(L) : print(*L, sep="\n")
def YesNo(B) : print("Yes" if B else "No")
def YESNO(B) : print("YES" if B else "NO")

N, K = LI()
S = input()

nums = []
now = 1
cnt = 0
for s in S:
    if s == str(now):
        cnt += 1
    else:
        now = 1 - now
        nums.append(cnt)
        cnt = 1
if cnt != 0:
    nums.append(cnt)
if now == 0:
    nums.append(0)

C = [0] * (len(nums)+1)
for i in range(len(nums)):
    C[i+1] = C[i] + nums[i]

ans = 0
m = 2 * K + 1
for i in range(0, len(nums)-m+1, 2):
    ans = max(ans, C[i+m] - C[i])

if len(nums) != 1:
    print(ans)
else:
    print(nums[0])

Submission status: Wrong Answer

Solution code:
# coding: utf-8

import sys
import math
import collections
import itertools
INF = 10 ** 13
MOD = 10 ** 9 + 7
def input() : return sys.stdin.readline().strip()
def lcm(x, y) : return (x * y) // math.gcd(x, y)
def I() : return int(input())
def LI() : return [int(x) for x in input().split()]
def RI(N) : return [int(input()) for _ in range(N)]
def LRI(N) : return [[int(x) for x in input().split()] for _ in range(N)]
def LS() : return input().split()
def RS(N) : return [input() for _ in range(N)]
def LRS(N) : return [input().split() for _ in range(N)]
def PL(L) : print(*L, sep="\n")
def YesNo(B) : print("Yes" if B else "No")
def YESNO(B) : print("YES" if B else "NO")

N, K = LI()
S = input()

nums = []
now = 1
cnt = 0
for s in S:
    if s == str(now):
        cnt += 1
    else:
        now = 1 - now
        nums.append(cnt)
        cnt = 1
if cnt != 0:
    nums.append(cnt)
if now == 0:
    nums.append(0)

C = [0] * (len(nums)+1)
for i in range(len(nums)):
    C[i+1] = C[i] + nums[i]

ans = 0
m = 2 * K + 1
for i in range(0, len(nums)-m+1, 2):
    ans = max(ans, C[i+m] - C[i])

if len(nums) != 1:
    print(ans)
else:
    print(1)