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()

# 連続する同じ数字をブロックにまとめる
States, Cnts = [S[0]], [1]
index = 0
for i in range(1, N):
	if S[i] == States[index]:
		Cnts[index] += 1
	else:
		States.append(S[i])
		Cnts.append(1)
		index += 1

ans = 0
if S[0] == "1":
	for i in range(0, N - 2 * K - 1, 2):
		ans = max(ans, sum(Cnts[i: i + 2 * K + 1]))
	print(ans)
else:
	for i in range(0, N - 2 * K, 2):
		ans = max(ans, sum(Cnts[i: i + 2 * K]))
	print(ans)

Submission status: Wrong Answer

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

# 連続する同じ数字をブロックにまとめる
States, Cnts = [S[0]], [1]
index = 0
for i in range(1, N):
	if S[i] == States[index]:
		Cnts[index] += 1
	else:
		States.append(S[i])
		Cnts.append(1)
		index += 1

ans = 0
if S[0] == "1":
	for i in range(N - 2 * K - 1):
		ans = max(ans, sum(Cnts[i: i + 2 * K + 1]))
	print(ans)
else:
	for i in range(N - 2 * K):
		ans = max(ans, sum(Cnts[i: i + 2 * K]))
	print(ans)

Submission status: Wrong Answer

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

if s[0] == "0":
    s_num = [0]
else:
    s_num = []
pre = s[0]
num = 0

for i in s:
    if pre == i:
        num += 1
    else:
        s_num.append(num)
        num = 1
        pre = "1" if pre == "0" else "0"
else:
    s_num.append(num)

sums = [0]

for i,e in enumerate(s_num):
    sums.append(sums[i] + e)

ret = 0
sums_len = len(sums)-1
for i in range(1,len(s_num)//2+1,2):
    ret = max(ret,(sums[min(i+k*2+1,sums_len)] - sums[i]))
 
print(ret)

Submission status: Wrong Answer

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

if s[0] == "0":
    s_num = [0]
else:
    s_num = []
pre = s[0]
num = 0

for i in s:
    if pre == i:
        num += 1
    else:
        s_num.append(num)
        num = 1
        pre = "1" if pre == "0" else "0"
else:
    s_num.append(num)

sums = [s_num[0]]

for i,e in enumerate(s_num[1:]):
    sums.append(sums[i] + e)

ret = 0
sums_len = len(sums)-1
for i in range(0,len(s_num)//2+1,2):
    ret = max(ret,(sums[min(i+k*2+1,sums_len)] - sums[i]))
 
print(ret)

Submission status: Wrong Answer

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

if s[0] == "0":
    s_num = [0]
else:
    s_num = []
pre = s[0]
num = 0

for i in s:
    if pre == i:
        num += 1
    else:
        s_num.append(num)
        num = 1
        pre = "1" if pre == "0" else "0"
else:
    s_num.append(num)

sums = [s_num[0]]

for i,e in enumerate(s_num[1:]):
    sums.append(sums[i] + e)

ret = 0
sums_len = len(sums)-1
for i in range(1,len(s_num)//2+1,2):
    ret = max(ret,(sums[min(i+k*2+1,sums_len)] - sums[i]))
 
print(ret)

Submission status: Wrong Answer

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

if s[0] == "0":
    s_num = [0]
else:
    s_num = []
pre = s[0]
num = 0

for i in s:
    if pre == i:
        num += 1
    else:
        s_num.append(num)
        num = 1
        pre = "1" if pre == "0" else "0"
else:
    s_num.append(num)

sums = [s_num[0]]

for i,e in enumerate(s_num[1:]):
    sums.append(sums[i] + e)

ret = 0
ret = max(ret,sum(s_num[0:(2*k+1)]))
sums_len = len(sums)-1
for i in range(1,len(s_num)//2+1,2):
    ret = max(ret,(sums[min(i+k*2+1,sums_len)] - sums[i]))
 
print(ret)

Submission status: Wrong Answer

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

s_num = []
s_first = s[0]
pre = s[0]
num = 0

for i in s:
    if pre == i:
        num += 1
    else:
        s_num.append(num)
        num = 1
        pre = "1" if pre == "0" else "0"
else:
    s_num.append(num)
ret = 0
if s_first == "0":
    ret = max(ret,sum(s_num[0:(2*k)]))
    for i in range(1,len(s_num)//2):
        ret = max(ret,sum(s_num[((i-1)*2+1):(2*(i+k))]))
else:
    ret = max(ret,sum(s_num[0:(2*k+1)]))
    for i in range(1,len(s_num)//2):
        ret = max(ret,(sum(s_num[(i*2):(2*(k+i)+1)])))
 
print(ret)

Submission status: Wrong Answer

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

s_num = []
s_first = s[0]
pre = s[0]
num = 0

for i in s:
    if pre == i:
        num += 1
    else:
        s_num.append(num)
        num = 1
        pre = "1" if pre == "0" else "0"
else:
    s_num.append(num)
 
print(0)

Submission status: Wrong Answer

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

s_num = []
s_first = s[0]
pre = s[0]
num = 0

for i in s:
    if pre == i:
        num += 1
    else:
        s_num.append(num)
        num = 1
        pre = "1" if pre == "0" else "0"
else:
    s_num.append(num)
ret = []
if s_first == "0":
    ret.append(sum(s_num[0:(2*k)]))
    for i in range(1,len(s_num)//2):
        ret.append(sum(s_num[((i-1)*2+1):(2*(i+k))]))
else:
    ret.append(sum(s_num[0:(2*k+1)]))
    for i in range(1,len(s_num)//2):
        ret.append(sum(s_num[(i*2):(2*(k+i)+1)]))
 
print(max(ret))

Submission status: Wrong Answer

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

end = []
start = []

if s[0] == "1":
	start.append(0)
else:
	#仮想的な1の区間を作る
	start.append(0)
	end.append(0)

for i in range(n-1):
	if s[i] == "1" and s[i+1] == "0":
		end.append(i)
for i in range(1, n):
	if s[i-1] == "0" and s[i] == "1":
		start.append(i)

if s[-1] == "1":
	end.append(n-1)
else:
	#仮想的な1の区間を作る
	start.append(n-1)
	end.append(n-1)

ans = 0

"""print(start, end, ans)
print(len(start), len(end), ans)"""

for j in range(len(start) - k):
	temp = end[j+k] - start[j] + 1 
	if temp > ans:
		ans = temp

if len(start) <= k:
	ans = n

print(start, end, ans)

Submission status: Wrong Answer

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

end = []
start = []

if s[0] == "1":
	start.append(0)
else:
	#仮想的な1の区間を作る
	start.append(0)
	end.append(0)

for i in range(n-1):
	if s[i] == "1" and s[i+1] == "0":
		end.append(i)
for i in range(1, n):
	if s[i-1] == "0" and s[i] == "1":
		start.append(i)

if s[-1] == "1":
	end.append(n-1)
else:
	#仮想的な1の区間を作る
	start.append(n-1)
	end.append(n-1)

ans = 0

print(start, end, ans)
print(len(start), len(end), ans)

for j in range(len(start) - k):
	temp = end[j+k] - start[j] + 1 
	if temp > ans:
		ans = temp

if len(start) <= k:
	ans = n

print(start, end, ans)

Submission status: Wrong Answer

Solution code:
import numpy

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

ans = 0
cnt = 1
l = []


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


if s[0] == '0':
    l.insert(0,0)

#print(s[-1] == '0')

if s[-1] == '0':
    l.append(0)
    
l_rui = numpy.cumsum(l)
l_rui = numpy.insert(l_rui,0,0)

k = 2*k

for i in range(n):
    if i*k >= len(l_rui):
        break
    elif (i+1)*k+1 >= len(l_rui):
        ans = max(ans, l_rui[-1] - l_rui[i*k])
    else:
        ans = max(ans, l_rui[(i+1)*k+1] - l_rui[i*k])

print(ans)

Submission status: Wrong Answer

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

change = [0]
pre = S[0]
for i in range(1, N):
    if pre != S[i]:
        change.append(i)
        pre = S[i]

change.append(len(S))

ans = 0
len_ = len(change)
for i in range(len_-1):
    if S[change[i]] == "1":
        ans = max(ans, change[min(len_-1, i+2*(K-1))] - change[i])
    else:
        ans = max(ans, change[min(len_-1, i+2*K)] - change[max(0, i-1)])

print(ans)

Submission status: Wrong Answer

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

change = [0]
pre = S[0]
for i in range(1, N):
    if pre != S[i]:
        change.append(i)
        pre = S[i]

change.append(len(S))

ans = 0
len_ = len(change)
for i in range(len_-1):
    if S[change[i]] == "1":
        ans = max(ans, change[min(len_-1, i+2*(K+1))] - change[i])
    else:
        ans = max(ans, change[min(len_-1, i+2*K)] - change[max(0, i-1)])

print(ans)

Submission status: Wrong Answer

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

change = [0]
pre = S[0]
for i in range(1, N):
    if pre != S[i]:
        change.append(i)
        pre = S[i]

change.append(len(change))

ans = 0
len_ = len(change)
for i in range(len_-1):
    if S[change[i]] == "1":
        ans = max(ans, change[min(len_-1, i+2*(K+1))] - change[i])
    else:
        ans = max(ans, change[min(len_-1, i+2*K)] - change[max(0, i-1)])

print(ans)

Submission status: Wrong Answer

Solution code:
def runlen(a):
    n = len(a)
    result = []
    count = 1
    for i in range(n):
        if i == n - 1 or a[i] != a[i + 1]:
            result.append((count, a[i]))
            count = 1
        else:
            count += 1
    return result

N, K = map(int, input().split())
S = input()
rl = runlen(S)
first = 0
if rl[0][1] == '1':
    last = 2 * K + 1
else:
    last = 2 * K
rl, _ = zip(*rl)
l = len(rl)
stands = sum(rl[first: last])
result = stands
while last + 2 <= l + 1:
    stands += -sum(rl[first: first + 2]) + sum(rl[last: last + 2])
    result = max(result, stands)
    first += 2
    last += 2
print(result)

Submission status: Wrong Answer

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

now = s[0]
num = 0
for c in s:
  num += 1
  if now != c:
    block.append((now,num-1))
    num = 1
    now = c
block.append((now,num))

acc = [0]
buf = 0
for (c,x) in block:
  acc.append(buf + x)
  buf = buf + x

cand = []
l = len(acc)
for i,(c,num) in enumerate(block):
  if c == '0':
    cand.append(acc[min(i+2*(k-1)+1,l-1)] - acc[i])
  else:
    cand.append(acc[min(i+2*k+1,l-1)] - acc[i])

print(max(cand))

Submission status: Wrong Answer

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


human = []
humancount = []
for key, group in itertools.groupby(S):
    human.append(key)
    humancount.append(len(list(group)))

human[0]
l = 0
if human[0] == "1":
    r = min(2*K, N-1)
else:
    r = min(2*K-1, N-1)


count = sum(humancount[l:r+1])
ans = count
while r <= len(humancount)-3:
    count += humancount[r+1] + humancount[r+2]
    count -= humancount[l] + humancount[l+1]
    if l == 0 and human[0] == "0":
        l += 1
    else:
        l += 2
    r += 2
    ans = max(ans, count)


human = []
humancount = []
for key, group in itertools.groupby(S):
    human.append(key)
    humancount.append(len(list(group)))

S = S[::-1]

l = 0
if human[0] == "1":
    r = min(2*K, N-1)
else:
    r = min(2*K-1, N-1)


count = sum(humancount[l:r+1])
ans = max(count, ans)
while r <= len(humancount)-3:
    count += humancount[r+1] + humancount[r+2]
    count -= humancount[l] + humancount[l+1]
    if l == 0 and human[0] == "0":
        l += 1
    else:
        l += 2
    r += 2
    ans = max(ans, count)


print(ans)

Submission status: Wrong Answer

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


human = []
humancount = []
for key, group in itertools.groupby(S):
    human.append(key)
    humancount.append(len(list(group)))

human[0]
l = 0
if human[0] == "1":
    r = min(2*K, N-1)
else:
    r = min(2*K-1, N-1)


count = sum(humancount[l:r+1])
ans = count
while r <= len(humancount)-3:
    count += humancount[r+1] + humancount[r+2]
    count -= humancount[l] + humancount[l+1]
    if l == 0 and human[0] == "0":
        l += 1
    else:
        l += 2
    r += 2
    ans = max(ans, count)

print(ans)

Submission status: Wrong Answer

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


human = []
humancount = []
for key, group in itertools.groupby(S):
    human.append(key)
    humancount.append(len(list(group)))

human[0]
l = 0
if human[0] == "1":
    r = min(2*K, N-1)
else:
    r = min(2*K-1, N-1)


count = sum(humancount[l:r+1])
ans = count
while r <= len(humancount)-3:
    count += humancount[r+1] + humancount[r+2]
    count -= humancount[l] + humancount[l+1]
    l += 2
    r += 2
    ans = max(ans, count)

print(ans)

Submission status: Wrong Answer

Solution code:
#D
from itertools import accumulate
N, K = map(int, input().split())
S = input()
bef = S[0]
cnt = 1
List = []
for i in range(1, len(S)):
    now = S[i]
    #from IPython.core.debugger import Pdb; Pdb().set_trace()
    if now == bef:
        cnt += 1
    else:
        List.append(cnt)
        cnt = 1
    bef = now
else:
    List.append(cnt)

Tmp = [0] + list(accumulate(List))

if S[0] == '1':
    ans = 0
    try:
        for k in range(len(S)):
            ans = max(Tmp[2*k+2*K+1]-Tmp[2*k], ans)
    except:
        None
    print(ans)
else:
    ans = 0
    try:
        for k in range(len(S)):
            #from IPython.core.debugger import Pdb; Pdb().set_trace()
            ans = max(Tmp[2*k+2*K]-Tmp[2*k], ans)
    except:
        None
    print(ans)

Submission status: Wrong Answer

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

zo_count = [1]
for n in range(1, N):
	if S[n] == S[n - 1]:
		zo_count[-1] += 1
	else:
		zo_count += [1]

zo_sum = [0]
for zo in zo_count:
	zo_sum += [zo_sum[-1] + zo]

if N <= K + 2:
	print(zo_sum[-1])
	exit()

#print(zo_sum)

zo_max = 0
for n in range(K * 2 + (S[0] == "1"), len(zo_sum), 2):
	zo_max = max(zo_max, zo_sum[n] - zo_sum[n - (K * 2 + (S[0] == "1"))])

print(zo_max)

Submission status: Wrong Answer

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

zo_count = [1]
for n in range(1, N):
	if S[n] == S[n - 1]:
		zo_count[-1] += 1
	else:
		zo_count += [1]

zo_sum = [0]
for zo in zo_count:
	zo_sum += [zo_sum[-1] + zo]

if N <= K + 2:
	print(zo_sum[-1])
	exit()

print(zo_sum)

zo_max = 0
for n in range(K * 2 + 1, len(zo_sum)):
	zo_max = max(zo_max, zo_sum[n] - zo_sum[n - (K * 2 + 1)])

print(zo_max)

Submission status: Wrong Answer

Solution code:
N, K = map(int,input().split())
S = input()
X = [-1]*N
k = 0
i = 1
c = 0
for s in S:
	if s == str(i):
		c += 1
	else:
		X[k] =c
		i = (i+1)%2
		c = 1
		k+= 1
X[k] = c
for n in range(N):
	if X[N-n-1] != -1:
		b = N-n
		break
X = X[:b]
M = len(X)
ko = M //2
if M == 2*ko:
	X.append(0)
#print(X)
if ko <= K:
	print(N) 
else:
	now = sum(X[0:2*K+1])
	print(now)
	maxm = now
	for t in range(ko-K):
		a = X[2*t] +X[2*t+1]
		b = X[2*(K+t)+1]+X[2*(K+t)+2]
		now += b - a
		#print(now, a, b)
		maxm = max(maxm, now)
	print(maxm)

Submission status: Wrong Answer

Solution code:
N, K = map(int,input().split())
S = input()
X = [-1]*N
k = 0
i = 1
c = 0
for s in S:
	if s == str(i):
		c += 1
	else:
		X[k] =c
		i = (i+1)%2
		c = 1
		k+= 1
X[k] = c
for n in range(N):
	if X[N-n-1] != -1:
		b = N-n
		break
X.append(-1)
X = X[:b]
M = len(X)
ko = M //2
if ko <= K:
	print(N) 
else:
	now = sum(X[0:2*K+2])
	print(now)
	maxm = now
	for t in range(ko-K-1):
		a = X[2*t] +X[2*t+1]
		b = X[2*(K+t)+1]+X[2*(K+t)+2]
		now += b - a
		maxm = max(maxm, now)
	print(maxm)

Submission status: Wrong Answer

Solution code:
N, K = map(int,input().split())
S = input()
X = [-1]*N
k = 0
i = 1
c = 0
for s in S:
	if s == str(i):
		c += 1
	else:
		X[k] =c
		i = (i+1)%2
		c = 1
		k+= 1
X[k] = c
for n in range(N):
	if X[N-n-1] != -1:
		b = N-n
		break
X = X[:b]
M = len(X)
ko = M //2
if ko <= K:
	print(N) 
else:
	now = sum(X[0:2*K+(M%2)+1])
	maxm = now
	for t in range(ko-K-1):
		a = X[2*t] +X[2*t+1]
		b = X[2*(K+t)+1]+X[2*(K+t)+2]
		now += b - a
		maxm = max(maxm, now)
	print(maxm)

Submission status: Wrong Answer

Solution code:
N, K = map(int,input().split())
S = input()
X = [-1]*N
k = 0
i = 1
c = 0
for s in S:
	if s == str(i):
		c += 1
	else:
		X[k] =c
		i = (i+1)%2
		c = 1
		k+= 1
X[k] = c
for n in range(N):
	if X[N-n-1] != -1:
		b = N-n
		break
X = X[:b]
M = len(X)
ko = M //2
if ko <= K:
	print(N) 
else:
	now = sum(X[0:2*K+(M%2)+1])
	maxm = now
	for t in range(ko-K-1):
		a = X[2*t] +X[2*t+1]
		b = X[2*(K+t)+1]+X[2*(K)+3]
		now += b - a
		maxm = max(maxm, now)
	print(maxm)

Submission status: Wrong Answer

Solution code:
import numpy as np

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

S = input()
S = [int(i) for i in S]

array = np.zeros(len(S))


for i in range(len(S)-1):
    if S[i] == S[i+1]:
        array[i] = 0
    else:
        array[i] = 1

#print(array)

res = np.cumsum(array)
#print(res)
ma = int(np.max(res))

count = 0
count_old = 0

for i in range(ma-K+1):
    for j in range(len(S)):
        #print(i,j)
        if res[j] >= i and res[j] <= K+1+i:
            count += 1
    if count_old < count:
        count_old = count
        #print(count_old,i)
    #print(count_old,K+i,i,count)        
    count = 0

        
print(count)

Submission status: Wrong Answer

Solution code:
import numpy as np

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

S = input()
S = [int(i) for i in S]

array = np.zeros(len(S))


for i in range(len(S)-1):
    if S[i] == S[i+1]:
        array[i] = 0
    else:
        array[i] = 1

#print(array)

res = np.cumsum(array)
#print(res)
ma = int(np.max(res))

count = 0
count_old = 0

for i in range(ma-K+1):
    for j in range(len(S)):
        #print(i,j)
        if res[j] >= i and res[j] <= K+1+i:
            count += 1
    if count_old < count:
        count_old = count
        #print(count_old,i)
    #print(count_old,K+i,i,count)        
    count = 0

        
print(count_old)

Submission status: Wrong Answer

Solution code:
import numpy as np

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

S = input()
S = [int(i) for i in S]

array = np.zeros(len(S))


for i in range(len(S)-1):
    if S[i] == S[i+1]:
        array[i] = 0
    else:
        array[i] = 1

print(array)

res = np.cumsum(array)
print(res)
ma = int(np.max(res))

count = 0
count_old = 0

for i in range(ma-K+1):
    for j in range(len(S)):
        #print(i,j)
        if res[j] >= i and res[j] <= K+1+i:
            count += 1
    if count_old < count:
        count_old = count
        #print(count_old,i)
    #print(count_old,K+i,i,count)        
    count = 0

        
print(count_old)

Submission status: Wrong Answer

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

_, k = map(int, input().split())
s = list(map(int, list(input())))
# print(s)
is_count_zero = True
s0 = [0]
s1 = []
s0_ptr = 0
s1_ptr = -1
for c in s:
    if is_count_zero and c == 0:
        s0[s0_ptr] += 1
    elif not is_count_zero and c == 0:
        s0.append(1)
        s0_ptr += 1
        is_count_zero = True
    elif is_count_zero and c == 1:
        s1.append(1)
        s1_ptr += 1
        is_count_zero = False
    else: # not is_count_zero and c == 1
        s1[s1_ptr] += 1

# print(s0,s1)
is_end_one = True if len(s0) == len(s1) else False

if k >= len(s0):
    print(sum(s0)+sum(s1))
# elif k == 0:
#     if s1 == []:
#         print(0)
#     else:
#         print(max(s1))
else:
    ans = 0
    sum_range = 0
    n = len(s0)-k+1
    for i in range(n):
        if i == 0 and s0[0] > 0:
            sum_range = sum(s0[:k]) + sum(s1[:k])
            ans = max(ans, sum_range)
        elif i == n-1:
            sum_range = sum(s0[i:]) + sum(s1[i-1:])
            ans = max(ans, sum_range)
        elif i == 1:
            sum_range = sum_range - s0[0] + s0[k] + s1[k]
            ans = max(ans, sum_range)
        elif i != 0:
            sum_range = sum_range - s0[i-1] + s0[i+k-1] - s1[i-2] + s1[i+k-1]
            ans = max(ans, sum_range)
    print(ans)

Submission status: Wrong Answer

Solution code:
_, k = map(int, input().split())
s = list(input())
is_count_zero = True
sl = [0]
sl_ptr = 0
for c in s:
    if is_count_zero and c == '1':
        sl.append(1)
        sl_ptr += 1
        is_count_zero = False
    elif not is_count_zero and c == '0':
        sl.append(1)
        sl_ptr += 1
        is_count_zero = True
    else:
        sl[sl_ptr] += 1

# print(sl)
# print("len(sl):{} len(sl)-1)//2:{}".format(len(sl), (len(sl)-1)//2))

if k > (len(sl)-1)//2:
    print(sum(sl))
elif k == (len(sl)-1)//2:
    if sl[0] == 0:
        print(sum(sl))
    else:
        print(max(sum(sl[1:]), sum(sl[:-1])))
else:
    ans = 0
    for i in range((len(sl)+1)//2-(k-1)):
        if i == 0:
            ans = max(ans, sum(sl[:k*2-1]))
            # print("sl[:k*2-1]:{} sum:{}".format(sl[:k*2-1], sum(sl[:k*2-1])))
        else:
            ans = max(ans, sum(sl[i*2-1:(i+k)*2]))
            # print("sl[i*2-1:(i+k)*2-1]: {}".format(sl[i*2-1:(i+k)*2]))
    print(ans)

Submission status: Wrong Answer

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

_, k = map(int, input().split())
s = list(input())
is_count_zero = True
sl = [0]
sl_ptr = 0
for c in s:
    if is_count_zero and c == '1':
        sl.append(1)
        sl_ptr += 1
        is_count_zero = False
    elif not is_count_zero and c == '0':
        sl.append(1)
        sl_ptr += 1
        is_count_zero = True
    else:
        sl[sl_ptr] += 1

# print(sl)
# print("len(sl):{} len(sl)-1)//2:{}".format(len(sl), (len(sl)-1)//2))

if k > (len(sl)-1)//2:
    print(sum(sl))
elif k == (len(sl)-1)//2:
    if sl[0] == 0:
        print(sum(sl))
    else:
        print(max(sum(sl[1:]), sum(sl[:-1])))
else:
    ans = 0
    for i in range((len(sl)+1)//2-(k-1)):
        if i == 0:
            ans = max(ans, sum(sl[:k*2-1]))
            # print("sl[:k*2-1]:{} sum:{}".format(sl[:k*2-1], sum(sl[:k*2-1])))
        else:
            ans = max(ans, sum(sl[i*2-1:(i+k)*2]))
            # print("sl[i*2-1:(i+k)*2-1]: {}".format(sl[i*2-1:(i+k)*2]))
    print(ans)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
s=input()
A=[]
cur=s[0]
for i in range(1,n):
  if s[i]!=cur:
    A.append(i)
    cur=s[i]
A.append(n)
start,end=s[0],s[-1]
nn=len(A)
ans=0
if start=="0" and nn<=2*k:
  ans=n
elif start=="1" and nn<=2*k+1:
  ans=n
elif start=="0":
  ans=A[2*k-1]
  for i in range(1,nn//2-k+1):
    ans=max(ans,A[2*k-1+2*i]-A[2*i-2])
  if end=="0":
    ans=max(ans,A[-1]-A[-2*(k+1)+1])
else:
  ans=A[2*k]
  for i in range(1,(nn-1)//2-k+1):
    ans=max(ans,A[2*k+2*i]-A[2*i-1])
print(ans)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
s=input()
A=[]
cur=s[0]
for i in range(1,n):
  if s[i]!=cur:
    A.append(i)
    cur=s[i]
A.append(n)
start=s[0]
nn=len(A)
ans=0
if start=="0" and nn<=2*k:
  ans=n
elif start=="1" and nn<=2*k+1:
  ans=n
elif start=="0":
  ans=A[2*k-1]
  for i in range(1,nn//2-k+1):
    ans=max(ans,A[2*k-1+2*i]-A[2*i-2])
else:
  ans=A[2*k]
  for i in range(1,(nn-1)//2-k+1):
    ans=max(ans,A[2*k+2*i]-A[2*i-1])
print(ans)

Submission status: Wrong Answer

Solution code:
N, K  = (map(int, input().split()))
S = list(map(int, input()))
length = len(S)
lst = []

# 前処理
for i in range(length):
	if i == 0:
		lst.append(1)
		start = S[0]
	else:
		if S[i - 1] == S[i]:
			lst[-1] += 1
		else:
			lst.append(1)
				

# 探索
# 始点
maximum = 0
score = 0
counter = 0
for i in range(start, length, 2):
	if len(lst) < i + 2 * (K - 1) + 2:
		counter += 1
		if counter == 2:
			break
	else:
		if i == start:
			score = sum(lst[max(0, i - 1) : min(len(lst), i + 2 * (K - 1) + 2)])
		else:
			# print(score)
			score -= sum(lst[i - 3 : i - 1])
			# print(sum(lst[i - 3 : i - 1]))
			if len(lst) >= i + 2 * (K - 1) + 2:
				score += sum(lst[i + 2 * (K - 1) : i + 2 * (K - 1) + 2])
			# print(sum(lst[i + 2 * (K - 1) : i + 2 * (K - 1) + 2]))
		# print(max(0, i - 1), min(len(lst), i + 2 * (K - 1) + 2))
		# print("score={}".format(score))
		if score > maximum:
			maximum = score
print(maximum)

Submission status: Wrong Answer

Solution code:
N, K  = (map(int, input().split()))
S = list(map(int, input()))
length = len(S)
lst = []

# 前処理
for i in range(length):
	if i == 0:
		lst.append(1)
		start = S[0]
	else:
		if S[i - 1] == S[i]:
			lst[-1] += 1
		else:
			lst.append(1)
				

# 探索
# 始点
maximum = 0
score = 0
counter = 0
for i in range(start, length, 2):
	if len(lst) < i + 2 * (K - 1) + 2:
		counter += 1
		if counter == 2:
			break
	else:
		if i == start:
			score = sum(lst[max(0, i - 1) : min(len(lst), i + 2 * (K - 1) + 2)])
		else:
			# print(score)
			score -= sum(lst[i - 3 : i - 1])
			# print(sum(lst[i - 3 : i - 1]))
			score += sum(lst[i + 2 * (K - 1) : i + 2 * (K - 1) + 2])
			# print(sum(lst[i + 2 * (K - 1) : i + 2 * (K - 1) + 2]))
		# print(max(0, i - 1), min(len(lst), i + 2 * (K - 1) + 2))
		# print("score={}".format(score))
		if score > maximum:
			maximum = score
print(maximum)

Submission status: Wrong Answer

Solution code:
N, K  = (map(int, input().split()))
S = list(map(int, input()))
length = len(S)
lst = []

# 前処理
for i in range(length):
	if i == 0:
		lst.append(1)
		start = S[0]
	else:
		if S[i - 1] == S[i]:
			lst[-1] += 1
		else:
			lst.append(1)
				

# 探索
# 始点
maximum = 0
score = 0
for i in range(start, length, 2):
	if len(lst) >= i + 2 * (K - 1) + 2:
		if i == start:
			score = sum(lst[max(0, i - 1) : min(len(lst), i + 2 * (K - 1) + 2)])
		else:
			# print(score)
			score -= sum(lst[i - 3 : i - 1])
			# print(sum(lst[i - 3 : i - 1]))
			score += sum(lst[i + 2 * (K - 1) : i + 2 * (K - 1) + 2])
			# print(sum(lst[i + 2 * (K - 1) : i + 2 * (K - 1) + 2]))
		# print(max(0, i - 1), min(len(lst), i + 2 * (K - 1) + 2))
		# print("score={}".format(score))
		if score > maximum:
			maximum = score
print(maximum)

Submission status: Wrong Answer

Solution code:
N, K  = (map(int, input().split()))
S = list(map(int, input()))
length = len(S)
lst = []

# 前処理
for i in range(length):
	if i == 0:
		lst.append(1)
		start = S[0]
	else:
		if S[i - 1] == S[i]:
			lst[-1] += 1
		else:
			lst.append(1)
				
if (len(lst) + 1) / 2 < K:
	print(sum(lst))
else:
	# 探索
	# 始点
	maximum = 0
	score = 0
	for i in range(start, length, 2):
		if len(lst) >= i + 2 * (K - 1) + 2:
			if i == start:
				score = sum(lst[max(0, i - 1) : min(len(lst), i + 2 * (K - 1) + 2)])
			else:
				# print(score)
				score -= sum(lst[i - 3 : i - 1])
				# print(sum(lst[i - 3 : i - 1]))
				score += sum(lst[i + 2 * (K - 1) : i + 2 * (K - 1) + 2])
				# print(sum(lst[i + 2 * (K - 1) : i + 2 * (K - 1) + 2]))
			# print(max(0, i - 1), min(len(lst), i + 2 * (K - 1) + 2))
			# print("score={}".format(score))
			if score > maximum:
				maximum = score
	print(maximum)

Submission status: Wrong Answer

Solution code:
N, K  = (map(int, input().split()))
S = list(map(int, input()))
length = len(S)
lst = []

# 前処理
for i in range(length):
	if i == 0:
		lst.append(1)
		start = S[0]
	else:
		if S[i - 1] == S[i]:
			lst[-1] += 1
		else:
			lst.append(1)
				

if (len(lst) + 1) / 2 < K:
	print(sum(lst))
else:
	# 探索
	# 始点
	maximum = 0
	for i in range(start, length, 2):
		score = 0
		if len(lst) >= i + 2 * (K - 1) + 2:
			if i == start:
				score = sum(lst[max(0, i - 1) : min(len(lst), i + 2 * (K - 1) + 2)])
			else:
				score -= sum(lst[i - 3 : i - 1])
				score += sum(lst[i + 2 * (K - 1) : i + 2 * (K - 1) + 2])
			# print(max(0, i - 1), min(len(lst), i + 2 * (K - 1) + 2))
			# print("score={}".format(score))
			if score > maximum:
				maximum = score
	print(maximum)

Submission status: Wrong Answer

Solution code:
N, K  = (map(int, input().split()))
S = list(map(int, input()))
length = len(S)
lst = []

# 前処理
for i in range(length):
	if i == 0:
		lst.append(1)
		start = S[0]
	else:
		if S[i - 1] == S[i]:
			lst[-1] += 1
		else:
			lst.append(1)
				

if (len(lst) + 1) / 2 < K:
	print(sum(lst))
else:
	# 探索
	# 始点
	maximum = 0
	for i in range(start, length, 2):
		score = 0
		if len(lst) >= i + 2 * (K - 1) + 2:
			if i == start:
				score = sum(lst[max(0, i - 1) : min(len(lst), i + 2 * (K - 1) + 2)])
			else:
				score -= sum(lst[i - 3 : i - 2])
				score += sum(lst[i + 2 * (K - 1) : i + 2 * (K - 1) + 2])
			# print(max(0, i - 1), min(len(lst), i + 2 * (K - 1) + 2))
			# print("score={}".format(score))
			if score > maximum:
				maximum = score
	print(maximum)

Submission status: Wrong Answer

Solution code:
N, K  = (map(int, input().split()))
S = list(map(int, input()))
length = len(S)
lst = []

# 前処理
for i in range(length):
	if i == 0:
		lst.append(1)
		start = S[0]
	else:
		if S[i - 1] == S[i]:
			lst[-1] += 1
		else:
			lst.append(1)
				

if (len(lst) + 1) / 2 < K:
	print(sum(lst))
else:
	# 探索
	# 始点
	maximum = 0
	score = 0
	for i in range(start, length, 2):
		if len(lst) >= i + 2 * (K - 1) + 2:
			score = sum(lst[max(0, i - 1) : min(len(lst), i + 2 * (K - 1) + 2)])
			# print(max(0, i - 1), min(len(lst), i + 2 * (K - 1) + 2))
			# print("score={}".format(score))
			if score > maximum:
				maximum = score
	print(maximum)

Submission status: Wrong Answer

Solution code:
N, K  = (map(int, input().split()))
S = list(map(int, input()))
length = len(S)
lst = []

# 前処理
for i in range(length):
	if i == 0:
		lst.append(1)
		start = S[0]
	else:
		if S[i - 1] == S[i]:
			lst[-1] += 1
		else:
			lst.append(1)
				
				
# 探索
# 始点
maximum = 0
score = 0
for i in range(start, length):
	score = sum(lst[max(0, start - 1) : min(N, start + 2 * K) + 1])
	if score > maximum:
		maximum = score
print(score)

Submission status: Wrong Answer

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

a = sum(zo[0:2*k+1])
ans = a
for i in range(1,int((n-(2*k+1))/2)):
    a += sum(zo[i*(2*k+1):i*(2*k+1)+k])
    a -= sum(zo[i*k:i*k+k])
    if a > ans:
        ans = a
print(ans)

Submission status: Wrong Answer

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

st = []
preval = S[0]
cnt = 0
for i in S:
        if i == preval:
                cnt += 1
        else:
                st.append(cnt)
                cnt = 1
                preval = i
else:
        st.append(cnt)

ans = 0
for idx,val in enumerate(st):
        tmp = 0
        if idx == 0:
                tmp = sum(st[idx:idx+K+2])
        elif idx + 3 + K == len(st):
                tmp = sum(st[idx:])
        else:
                tmp = sum(st[idx:idx+K+1])
        ans = max(tmp, ans)

print(ans)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import sys

input = sys.stdin.readline

def main():
    N, K = map(int, input().split())
    S = list(input().strip())

    left = 0
    right = 0
    k = 0
    max_n = 0
    pos = []
    while right < N:
        while right+1 < N and k <= K:
            # print(left, right, k)
            right += 1
            if S[right] == "1" and S[right-1] == "0":
                k += 1
                pos.append(right)
        max_n = max(max_n, right - left)
        if len(pos) == 0:
            break
        left = pos[0]
        pos = pos[1:]
        k -= 1
    print(max_n)

main()