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()
First=S[0]
Now=First
cnt=0
A=[0]
for s in S:
    if Now=='1' and s=='0':
        A+=[cnt]
        Now='0'
    elif Now=='0' and s=='1':
        A+=[cnt]
        Now='1'
    cnt+=1
A+=[N]
dp=2*K+1
ans=[]

if First=="0":
    p=1
    ans.append(A[min(dp-1,len(A)-1)])
else:
    p=0
    ans.append(A[min(dp,len(A)-1)])

while p+dp<len(A):
	ans.append(A[p+dp]-A[p])
	p+=2

print(A)
print(max(ans))

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
S=input()
First=S[0]
Now=First
cnt=0
A=[0]
for s in S:
    if Now=='1' and s=='0':
        A+=[cnt]
        Now='0'
    elif Now=='0' and s=='1':
        A+=[cnt]
        Now='1'
    cnt+=1
A+=[N]
dp=2*K+1
ans=[]

if First=="0":
    p=1
    ans.append(A[min(dp-1,len(A)-1)])
else:
    p=0
    ans.append(A[min(dp,len(A)-1)])

while p+dp<len(A):
	ans.append(A[p+dp]-A[p])
	p+=2

#print(A)
print(ans)

Submission status: Wrong Answer

Solution code:
from sys import exit
N,K = [int(n) for n in input().split()]
# N = int(input())
# a = [int(input()) for _ in range(N)]
S = str(input())
L = len(S)
lis = []
if(S[0] == "0"):#偶数Indexがブリッジ
    lis.append(0)
tmp = S[0]

for i in range(1,L):
    if(S[i]!=S[i-1]):
        lis.append(len(tmp))
        tmp =S[i]
    else:
        tmp+=S[i]
lis.append(len(tmp))
#0で終わっていたら最後に１が0個であるとする
if("0" in tmp):
    lis.append(0)
# print(lis)

#累積和をとる
L = len(lis)
# print(L)
asum = [0]*(L+1)
asum[0] = 0
for i in range(1,L+1):
    asum[i] = asum[i-1] + lis[i-1]
# print(asum)

#尺をとる
l = 0
ans = max(lis) if l+2*K+1 < L+1 else sum(lis)
for r in range(l+2*K+1 , L+1):
    # print(l,r)
    # print(asum[r] - asum[l])
    ans = max(asum[r] - asum[l],ans)
    l+=1
print(ans)

# 14 2
# 11101010110011
# 10 1
# 0000000001

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
import sys
import itertools

def solve(N: int, K: int, S: str):
    border = []
    prev = False
    first = S[0]
    for i, c in enumerate(S):
        if c != prev:
            border.append(i)
            prev = c
    border.append(N)
    size = len(border) // 2

    if K >= size:
        print(border[-1] - border[0])
        return
    
    res = 0
    for i in range(size-K):
        if first == '1': 
            res = max(res, border[min([2*i + 2*K + 1, N])] - border[2*i])
        else: 
            res = max(res, border[2*i + 2*K] - border[max([2*i - 1, 0])])
    print(res)
    return


# Generated by 1.1.4 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    K = int(next(tokens))  # type: int
    S = next(tokens)  # type: str
    solve(N, K, S)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

# print(N, K)
S = input()
index_array = [0]
now_letter = "1"
for i, s in enumerate(S):
    if s != now_letter:
        now_letter = s
        index_array.append(i)
else:
    if now_letter == "1":
        index_array.append(N)
# print(index_array)
# print(index_array)
ans = 0
array_len = len(index_array)
if (K + 1) * 2 >= array_len:
    print(N)
    exit()
candidate = array_len // 2 - K
for i in range(candidate):
    # print(index_array[(i + K) * 2 + 1] - index_array[i * 2])
    ans = max(index_array[(i + K) * 2 + 1] - index_array[i * 2], ans)
print(int(ans))

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
S=input()
First=S[0]
Last=S[N-1]

Now=First
cnt=0
A=[0,0]
for s in S:
    if Now=='1' and s=='0':
        A+=[cnt]
        Now='0'
    elif Now=='0' and s=='1':
        A+=[cnt]
        Now='1'
    cnt+=1
A+=[cnt,cnt]


ans=0
dp=2*K+1
if First=="0":
    p=0
else:
    p=1
while p+dp<len(A):
   if A[p+dp]-A[p]>ans:ans=A[p+dp]-A[p]
   p+=2
print(ans)

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
S=input()
First=S[0]
Last=S[N-1]

Now=First
cnt=0
A=[0,0]
for s in S:
    if Now=='1' and s=='0':
        A+=[cnt]
        Now='0'
    elif Now=='0' and s=='1':
        A+=[cnt]
        Now='1'
    cnt+=1
A+=[cnt,cnt]


print(S)
print(A)
ans=0
dp=2*K+1
if First=="0":
    p=0
else:
    p=1
while p+dp<len(A):
   if A[p+dp]-A[p]>ans:ans=A[p+dp]-A[p]
   p+=2
print(ans)

Submission status: Wrong Answer

Solution code:
N , K = map(int,input().split())
S = input()
L = []
flag = int(S[0])
for x in S:
    L.append(x)
cnt = 1
C = []
for i in range(len(L)):
    try:
        if L[i] == L[i+1]:
            cnt += 1
        else:
            C.append(cnt)
            cnt = 1
    except:
        pass
C.append(cnt)
ans = 0
from itertools import accumulate
accumulated =  [0]+list(accumulate(C))
ans = 0
A = 0
k = min(K,len(C)//2-flag%2)
for i in range(len(C)):
    try:
        if (flag + i) % 2 == 0:
            A =max(A, accumulated[i+2*k-1] - accumulated[i])
            ans = accumulated[i+2*k] - accumulated[i]
        else:
            A =max(A, accumulated[i+2*k-1] - accumulated[i])
            A =max(A, accumulated[i+2*k] - accumulated[i])
            ans = accumulated[i+2*k+1] - accumulated[i]
        A = max(ans,A)
    except:
        pass
print(A)

Submission status: Wrong Answer

Solution code:
N , K = map(int,input().split())
S = input()
L = []
flag = int(S[0])
for x in S:
    L.append(x)
cnt = 1
C = []
for i in range(len(L)):
    try:
        if L[i] == L[i+1]:
            cnt += 1
        else:
            C.append(cnt)
            cnt = 1
    except:
        pass
C.append(cnt)
ans = 0
from itertools import accumulate
accumulated =  [0]+list(accumulate(C))
ans = 0
A = 0
k = len(C) // 2 + (flag)
for i in range(len(C)):
    try:
        if (flag + i) % 2 == 0:
            A =max(A, accumulated[i+2*k-1] - accumulated[i])
            ans = accumulated[i+2*k] - accumulated[i]
        else:
            A =max(A, accumulated[i+2*k-1] - accumulated[i])
            A =max(A, accumulated[i+2*k] - accumulated[i])
            ans = accumulated[i+2*k+1] - accumulated[i]
        A = max(ans,A)
    except:
        pass
print(A)

Submission status: Wrong Answer

Solution code:
N , K = map(int,input().split())
S = input()
L = []
flag = int(S[0])
for x in S:
    L.append(x)
cnt = 1
C = []
for i in range(len(L)):
    try:
        if L[i] == L[i+1]:
            cnt += 1
        else:
            C.append(cnt)
            cnt = 1
    except:
        pass
C.append(cnt)
ans = 0
from itertools import accumulate
accumulated =  [0]+list(accumulate(C))
ans = 0
A = 0
k = K
for i in range(len(C)):
    try:
        if (flag + i) % 2 == 0:
            k = min(k,len(C)//2+1)
            A =max(A, accumulated[i+2*k-1] - accumulated[i])
            ans = accumulated[i+2*k] - accumulated[i]
        else:
            k = min(k,len(C)//2)
            A =max(A, accumulated[i+2*k-1] - accumulated[i])
            A =max(A, accumulated[i+2*k] - accumulated[i])
            ans = accumulated[i+2*k+1] - accumulated[i]
        A = max(ans,A)
    except:
        pass
print(A)

Submission status: Wrong Answer

Solution code:
N , K = map(int,input().split())
S = input()
L = []
flag = int(S[0])
for x in S:
    L.append(x)
cnt = 1
C = []
for i in range(len(L)):
    try:
        if L[i] == L[i+1]:
            cnt += 1
        else:
            C.append(cnt)
            cnt = 1
    except:
        pass
C.append(cnt)
ans = 0
from itertools import accumulate
accumulated =  [0]+list(accumulate(C))
ans = 0
A = 0
k = K
for i in range(len(C)):
    try:
        if (flag + i) % 2 == 0:
            k = min(k,len(C)//2+1)
            ans = accumulated[i+2*k] - accumulated[i]
        else:
            k = min(k,len(C)//2)
            A =max(A, accumulated[i+2*k] - accumulated[i])
            ans = accumulated[i+2*k+1] - accumulated[i]
        A = max(ans,A)
    except:
        pass
print(A)

Submission status: Wrong Answer

Solution code:
N , K = map(int,input().split())
S = input()
L = []
flag = int(S[0])
for x in S:
    L.append(x)
cnt = 1
C = []
for i in range(len(L)):
    try:
        if L[i] == L[i+1]:
            cnt += 1
        else:
            C.append(cnt)
            cnt = 1
    except:
        pass
C.append(cnt)
ans = 0
from itertools import accumulate
accumulated =  [0]+list(accumulate(C))
ans = 0
A = 0
for i in range(len(C)):
    try:
        if (flag + i) % 2 == 0:
            K = min(K,len(C)//2+1)
            ans = accumulated[i+2*K] - accumulated[i]
        else:
            K = min(K,len(C)//2)
            ans = accumulated[i+2*K+1] - accumulated[i]
        A = max(ans,A)
    except:
        pass
print(A)

Submission status: Wrong Answer

Solution code:
N , K = map(int,input().split())
S = input()
L = []
flag = int(S[0])
for x in S:
    L.append(x)
cnt = 1
C = []
for i in range(len(L)):
    try:
        if L[i] == L[i+1]:
            cnt += 1
        else:
            C.append(cnt)
            cnt = 1
    except:
        pass
C.append(cnt)
ans = 0
from itertools import accumulate
accumulated =  [0]+list(accumulate(C))
ans = 0
A = 0
for i in range(len(C)):
    try:
        if (flag + i) % 2 == 0:
            K = min(K,len(C)//A+1)
            ans = accumulated[i+2*K] - accumulated[i]
        else:
            K = min(K,len(C)//2)
            ans = accumulated[i+2*K+1] - accumulated[i]
        A = max(ans,A)
    except:
        pass
print(A)

Submission status: Wrong Answer

Solution code:
N , K = map(int,input().split())
S = input()
L = []
flag = int(S[0])
for x in S:
    L.append(x)
cnt = 1
C = []
for i in range(len(L)):
    try:
        if L[i] == L[i+1]:
            cnt += 1
        else:
            C.append(cnt)
            cnt = 1
    except:
        pass
C.append(cnt)
ans = 0
from itertools import accumulate
accumulated =  [0]+list(accumulate(C))
ans = 0
if len(C) == 1:
    ans = C[0]
elif len(C) == 2:
    ans = C[0] + C[2]

if flag == 1:
    K = min(K,len(C)//2)
    try:
        for i in range(len(C)):
            if i % 2 == 0:
                cand = accumulated[2*K+i+1] - accumulated[i]
            else:
                cand = accumulated[2*K+i] - accumulated[i]
            ans = max(cand,ans)
    except:
        pass
else:
    K = min(K,(len(C)//2+1))
    try:
        for i in range(len(C)):
            if i % 2 == 1:
                cand = accumulated[2*K+i+1] - accumulated[i]
            else:
                cand = accumulated[2*K+i] - accumulated[i]
            ans = max(cand,ans)
    except:
        pass
if ans == 0:
    ans = sum(C)
print(ans)

Submission status: Wrong Answer

Solution code:
N , K = map(int,input().split())
S = input()
L = []
flag = int(S[0])
for x in S:
    L.append(x)
cnt = 1
C = []
for i in range(len(L)):
    try:
        if L[i] == L[i+1]:
            cnt += 1
        else:
            C.append(cnt)
            cnt = 1
    except:
        pass
C.append(cnt)
ans = 0
from itertools import accumulate
accumulated =  [0]+list(accumulate(C))
ans = 0

if len(C) == 1:
    ans = C[0]
elif len(C) == 2:
    ans = C[0] + C[2]

if flag == 1:
    try:
        for i in range(len(C)):
            if i % 2 == 0:
                cand = accumulated[2*K+1+i] - accumulated[i]
            else:
                cand = accumulated[2*K+i] - accumulated[i]
            ans = max(cand,ans)
    except:
        pass
else:
    try:
        for i in range(len(C)):
            if i % 2 == 1:
                cand = accumulated[2*K+1+i] - accumulated[i]
            else:
                cand = accumulated[2*K+i] - accumulated[i]
            ans = max(cand,ans)
    except:
        pass

print(ans)

Submission status: Wrong Answer

Solution code:
N , K = map(int,input().split())
S = input()
L = []
flag = int(S[0])
for x in S:
    L.append(x)
cnt = 1
C = []
for i in range(len(L)):
    try:
        if L[i] == L[i+1]:
            cnt += 1
        else:
            C.append(cnt)
            cnt = 1
    except:
        pass
C.append(cnt)
ans = 0
from itertools import accumulate
accumulated =  [0]+list(accumulate(C))
ans = 0
print(accumulated)
if len(C) == 1:
    ans = C[0]
elif len(C) == 2:
    ans = C[0] + C[2]

if flag == 1:
    try:
        for i in range(len(C)):
            if i % 2 == 0:
                cand = accumulated[2*K+1+i] - accumulated[i]
            else:
                cand = accumulated[2*K+i] - accumulated[i]
            ans = max(cand,ans)
    except:
        pass
else:
    try:
        for i in range(len(C)):
            if i % 2 == 1:
                cand = accumulated[2*K+1+i] - accumulated[i]
            else:
                cand = accumulated[2*K+i] - accumulated[i]
            ans = max(cand,ans)
    except:
        pass

print(ans)

Submission status: Wrong Answer

Solution code:
# input
#n, k = list(map(int, input().split()))
#s = input()
n=14
k=2
s='11101010110011'

# solve

##. number of consecutive 0 groups and 1 groups
##. ex:
##.  s = '11101010110011'
##.       0: 0, 0, 0, 00
##.       1: 111, 1, 1, 11, 11
##.  zeros = [1, 1, 1, 2]
##.  ones = [3, 1, 1, 2, 2]
##.   (always count the edges of s as 1)
zeros = []
ones = []
_c = '1'
count = 0
for c in s:
	if _c == c:
		count += 1
	else:
		_c = c
		if c == '0':
			zeros.append(count)
		else:
			ones.append(count)
		count = 1

if c == '0':
	zeros.append(count)
	ones.append(0)
else:
	ones.append(count)

## flip k consecutive zero groups
ans = 0
i = 0
while i + k <= len(zeros):
	_ans = sum(zeros[i:i+k]) + sum(ones[i:i+k+1])
	ans = max(ans, _ans)
	i += 1

print(ans)

Submission status: Wrong Answer

Solution code:
n,k = list(map(int, input().split()))
s = input()
if (s.find('0') < 0) or (s.find('1') < 0):
	print(len(s))
	quit()
else:
	s += '0' if s[n-1] == '1' else '1'
c = []

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

for i in range(1, len(c)):
	c[i][1] += c[i-1][1]

if 2*k >= len(c):
	print(c[-1][1])
	quit()
else:
	p = 0
	res = 0
	if s[0][0] == '0':
		res = c[1][1] if k == 1 else c[2*k+1][1]
		p += 1
	else:
		res = c[2][1] if k == 1 else c[2*k][1]
		p += 2

	while p+2*k < len(c):
		res = max(res, c[p+2*k][1] - c[p-1][1])
		p += 2

	res = max(res, c[-1][1] - c[(len(c)-1) - 2*k][1]) if c[-1][0] == '0' else max(res, c[-1][1] - c[(len(c)-1) - (2*k+1)][1])

	print(res)
	quit()

Submission status: Wrong Answer

Solution code:
n,k = list(map(int, input().split()))
s = input()
s += '0' if s[n-1] == '1' else '1'
c = []

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

for i in range(1, len(c)):
	c[i][1] += c[i-1][1]

if 2*k >= len(c):
	print(c[-1][1])
	quit()
else:
	p = 0
	res = 0
	if s[0][0] == '0':
		res = c[1][1] if k == 1 else c[2*k+1][1]
		p += 1
	else:
		res = c[2][1] if k == 1 else c[2*k][1]
		p += 2

	while p+2*k < len(c):
		res = max(res, c[p+2*k][1] - c[p-1][1])
		p += 2

	res = max(res, c[-1][1] - c[(len(c)-1) - 2*k][1]) if c[-1][0] == '0' else max(res, c[-1][1] - c[(len(c)-1) - (2*k+1)][1])

	print(res)
	quit()

Submission status: Wrong Answer

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

hm_1 = [0] * N
hm_2 = [0] * N

for i in range(N-1,-1,-1):
	if i == N-1:
		if S[i] == '1':
			hm_1[i] = 1
	else:
		if S[i] == '0':
			hm_1[i] = 0
		else:
			hm_1[i] = hm_1[i+1] + 1
		
for i in range(N-1,-1,-1):
	if i == N-1:
		if S[i] == '0':
			hm_2[i] = 1
	else:
		if S[i] == '1':
			hm_2[i] = 0
		else:
			hm_2[i] = hm_2[i+1] + 1
#print (hm_1)
#print (hm_2)


rl = []
# run length 
# 1 - 0 - 1 - 0 - 1 - ...
i = 0
while i < N :
	if hm_1[i] != 0:
		rl.append(hm_1[i])
		i += hm_1[i]
	else:
		rl.append(hm_2[i])
		i += hm_2[i]

#print (rl)
# rl: index==even -> '1', index==odd -> '0'
# s : sum list
s = [0] * (len(rl) + 1)


for j in range(len(rl)):
	s[j+1] = s[j] + rl[j]
#print (s)

ans = 0
for ra in range(len(s)):
	tmp = 0
	right = min(ra + 2 * K + 1, len(s)-1)
	tmp = s[right] - s[ra]
	ans = max(tmp,ans)

print (ans)

Submission status: Wrong Answer

Solution code:
n,k = list(map(int, input().split()))
s = input()
if s[n-1] == '1':
	s += '0'
else:
	s += '1'
c = []

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

for i in range(1, len(c)):
	c[i][1] += c[i-1][1]

if 2*k >= len(c):
	print(c[-1][1])
	quit()
else:
	p = 0
	res = 0
	if s[0][0] == '0':
		res = c[1][1] if k == 1 else c[2*k+1][1]
		p += 1
	else:
		res = c[2][1] if k == 1 else c[2*k][1]
		p += 2

	while p+2*k < len(c):
		res = max(res, c[p+2*k][1] - c[p-1][1])
		p += 2

	print(res)
	quit()

Submission status: Wrong Answer

Solution code:
n, k = [int(i) for i in input().split()]
s = [1] + [int(i) for i in list(input())] + [1,0]
one2zero = [i for i in range(n+2) if s[i] == 1 and s[i+1] == 0]
ans = 1
for i in range(len(one2zero)-k):
  if one2zero[i + k] - one2zero[i] > ans:
    ans = one2zero[i + k] - one2zero[i]
print(ans)

Submission status: Wrong Answer

Solution code:
n, k = [int(i) for i in input().split()]
s = [1] + [int(i) for i in list(input())] + [1,0]
one2zero = [i for i in range(n+2) if s[i] == 1 and s[i+1] == 0]
print(one2zero)
ans = 1
for i in range(len(one2zero)-k):
  if one2zero[i + k] - one2zero[i] > ans:
    ans = one2zero[i + k] - one2zero[i]
print(ans)

Submission status: Wrong Answer

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

l = []
one = []
cnt_0 = 0
cnt_1 = 0

for i in s:
    if i == '0':
        if cnt_1 != 0:
            l.append(cnt_1)
            one.append((cnt_1))
            cnt_1 = 0
        cnt_0 += 1
    if i =='1':
        if cnt_0 != 0:
            l.append(cnt_0)
            cnt_0 = 0
        cnt_1 += 1

if cnt_1 != 0:
    l.append(cnt_1)
    one.append(cnt_1)
if cnt_0 != 0:
    l.append(cnt_0)

#print(one)
#print(l)

ans = 0

if len(one) == 0:
    print(sum(l))

elif len(one) == 1:
    for i in range(len(l)):
        if ans < sum(l[i:i+2]):
            ans = sum(l[i:i+2])

if len(one) >= 2:
    for i in range(len(l)):
        if ans < sum(l[i:(k*2+1)+1]):
            ans = sum(l[i:(k*2+1)+1])


print(ans)

Submission status: Wrong Answer

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

seq = []
tmp = 0
for i in range(N-1):
    if S[i] != S[i+1]:
        tmp += 1
        seq.append(tmp)
        tmp = 0
            
    else:
        tmp += 1

seq.append(tmp+1)

seq_sum = [0 for _ in range(len(seq)+1)]

for i in range(len(seq)):
    seq_sum[i+1] = seq_sum[i] + seq[i]

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

            
print(res)

Submission status: Wrong Answer

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

seq = []
tmp = 0
for i in range(N-1):
    if S[i] != S[i+1]:
        tmp += 1
        seq.append(tmp)
        tmp = 0
            
    else:
        tmp += 1

seq.append(tmp+1)

seq_sum = [0 for _ in range(len(seq)+1)]

for i in range(len(seq)):
    seq_sum[i+1] = seq_sum[i] + seq[i]

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

            
print(res)

Submission status: Wrong Answer

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

seq = []
tmp = 0
for i in range(N-1):
    if S[i] != S[i+1]:
        tmp += 1
        seq.append(tmp)
        tmp = 0
            
    else:
        tmp += 1

seq.append(tmp+1)

seq_sum = [0 for _ in range(len(seq)+1)]

for i in range(len(seq)):
    seq_sum[i+1] = seq_sum[i] + seq[i]

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


print(res)

Submission status: Wrong Answer

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

length_list = []

tmp_one_length = 0
tmp_zero_length = 0

for i in range(n):
    if s[i] == "1" and i > 0 and s[i-1] == "0":
        length_list.append([tmp_zero_length, tmp_one_length])
        tmp_one_length = 0
        tmp_zero_length = 0
    if s[i] == "1":
        tmp_one_length += 1
    elif s[i] == "0":
        tmp_zero_length += 1
if tmp_zero_length > 0:
    length_list.append([tmp_zero_length, 0])
suff = tmp_one_length

ans_list = []
if len(length_list) > k: 
    start_idx = 0
    while start_idx + k < len(length_list):
        if start_idx == 0:
            length = 0
            for i in range(k):
                length += length_list[i][0]
                length += length_list[i][1]
        else:
            length = ans_list[len(ans_list)-1] - length_list[start_idx-1][0] - length_list[start_idx-1][1] + length_list[start_idx+k][0] + length_list[start_idx+k][1]
        if start_idx + k == len(length_list)-1:
            length += suff
        ans_list.append(length)
        start_idx += 1
else:
    ans_list.append(n)
        
print(max(ans_list))

Submission status: Wrong Answer

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

length_list = []

tmp_one_length = 0
tmp_zero_length = 0

for i in range(n):
    if s[i] == "1" and i > 0 and s[i-1] == "0":
        length_list.append([tmp_zero_length, tmp_one_length])
        tmp_one_length = 0
        tmp_zero_length = 0
    if s[i] == "1":
        tmp_one_length += 1
    elif s[i] == "0":
        tmp_zero_length += 1
if tmp_zero_length > 0:
    length_list.append([tmp_zero_length, 0])
suff = tmp_one_length
print(length_list)

ans_list = []
if len(length_list) > k: 
    start_idx = 0
    while start_idx + k < len(length_list):
        if start_idx == 0:
            length = 0
            for i in range(k):
                length += length_list[i][0]
                length += length_list[i][1]
        else:
            length = ans_list[len(ans_list)-1] - length_list[start_idx-1][0] - length_list[start_idx-1][1] + length_list[start_idx+k][0] + length_list[start_idx+k][1]
        if start_idx + k == len(length_list)-1:
            length += suff
        ans_list.append(length)
        start_idx += 1
else:
    ans_list.append(n)
        
print(max(ans_list))

Submission status: Wrong Answer

Solution code:
if __name__ == "__main__":
    n, k = map(int, input().split())
    s = input()
    res = 0
    b = []
    count = 0
    temp = 0
    isStand = False
    for i,ss in enumerate(s):
        if ss == "0":
            if not isStand:
                isStand = True
        else:
            if isStand:
                isStand = False
                b.append(i)
    b.insert(0, 0)
    if s[-1] == "0":
        b.append(n-1)
    else:
        b.remove(b[-1])
        b.append(n-1)
    if len(b) <= k:
        res = n
    else:
        res = 1 + max([b[i] - b[i - k] for i in range(k, len(b))])
#        res2 = [sum(b[i:2*k+1+i]) for i in range(0,len(b)-1,2)]
#        res = max(res2)
    print(res)

Submission status: Wrong Answer

Solution code:

if __name__ == "__main__":
    n, k = map(int, input().split())
    s = input()
    res = 0
    b = []
    count = 0
    temp = 0
    isStand = False
    for i,ss in enumerate(s):
        if ss == "0":
            if not isStand:
                isStand = True
                b.append(i)
        else:
            if isStand:
                isStand = False
                b.append(i)
    if len(b) < 2*k:
        res = n
    else:
        if b[0] == 0:
            b.insert(0,0)
        res = 1 + max([b[i] - b[i - 2 * k] for i in range(2 * k, len(b), +2)])
#        res2 = [sum(b[i:2*k+1+i]) for i in range(0,len(b)-1,2)]
#        res = max(res2)
    print(res)

Submission status: Wrong Answer

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

if __name__ == "__main__":
    n, k = map(int, input().split())
    s = input()
    res = 0
    b = []
    count = 0
    temp = 0
    isStand = False
    for ss in s:
        if ss == "0":
            if not isStand:
                isStand = True
                b.append(temp)
                temp = 0
        else:
            if isStand:
                isStand = False
                b.append(temp)
                temp = 0
        temp += 1
    b.append(temp)
    if len(b) < 2*k:
        res = sum(b)
    else:
#        res2 = []
#        for i in range(0,len(b),+2):
#            res2.append(sum(b[i:2*k+1+i]))
        res2 = [sum(b[i:2*k+1+i]) for i in range(0,len(b)+2)]
        res = max(res2)
    print(res)

Submission status: Wrong Answer

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

S = input()

k = K * 2 + 1

a = []
f = S[0]
l = S[-1]

count = 1
for i in range(N-1):
    if S[i] == S[i+1]:
        count += 1
    else:
        a.append(count)
        count = 1
a.append(count)

ze = int(len(a)/2)

if f == '0' and len(a) % 2 != 0:
    ze += 1

if K >= ze:
    print(sum(a))
else:
    ans = 0
    if f == '1':
        if l == '1':
            for i in range(ze - K +1):
                if ans < sum(a[i*2:i*2+k]):
                    ans = sum(a[i*2:i*2+k])
        else:
            for i in range(ze - K +1):
                if i != ze - K:
                    if ans < sum(a[i*2:i*2+k]):
                        ans = sum(a[i*2:i*2+k])
                else:
                    if ans < sum(a[i*2:i*2+k-1]):
                        ans = sum(a[i*2:i*2+k-1])
    else:
        ans = sum(a[:k-1])
        if l == '1':
            for i in range(1,ze - K +1):
                if ans < sum(a[i*2:i*2+k]):
                    ans = sum(a[i*2:i*2+k])
        else:
            for i in range(1,ze - K +1):
                if i != ze - K:
                    if ans < sum(a[i*2:i*2+k]):
                        ans = sum(a[i*2:i*2+k])
                else:
                    if ans < sum(a[i*2:i*2+k-1]):
                        ans = sum(a[i*2:i*2+k-1])

    print(ans)

Submission status: Wrong Answer

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

S = input()

k = K * 2 + 1

a = []
f = S[0]
l = S[-1]

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

a.append(count)


if f == '1':
    if k >= len(a):
        print(sum(a))
    else:
        ans = 0
        for i in range(len(a) - k + 1):
            if ans < sum(a[i:k+i]):
                ans = sum(a[i:k+i])

        print(ans)

else:
    ze = int(len(a) / 2)
    if len(a) % 2 != 0:
        ze += 1
    if K >= ze:
        print(sum(a))
    else:
        ans = sum(a[:k-1])
        for i in range(1,len(a)-k):
            if ans < sum(a[i:k+i]):
                ans = sum(a[i:k+i])
        
        print(ans)

Submission status: Wrong Answer

Solution code:
import re
n,k=map(int, input().split())
s=input()
i=[[m.start(),m.end()] for m in re.finditer("1(?=0)|0(?=1)",s)]
i.insert(0,[0,0]);i.append([len(s)-1,0])
ss=[i[j+1][0]-i[j][1]+1 for j in range(len(i)-1)]
is1ststr1 = True if s[0]=="1" else False
if not is1ststr1: ss.insert(0,0);ss.append(0)
res=[sum(ss[2*m:2*(m+k)+1]) for m in range(len(ss)//k+1)]
print(max(res))

Submission status: Wrong Answer

Solution code:

import sys
input = sys.stdin.readline
N, K = map(int, input().split())
S = input()
first = S[0]
a = []

ct = 1
before = '1'
for s in S:
    if s == before:
        ct += 1
    else:
        a = a + [ct]
        ct = 1
        before = s
a = a + [ct]

if first == '0':
    zero = a[0::2]
    one = a[1::2]
    ite = max(len(zero)-K+1, 1)
    ans = 0
    for i in range(ite):
        cand0 = sum(zero[i:i+K]) + sum(one[i:i+K])
        cand1 = sum(zero[i+1:i+K+1]) + sum(one[i:i+K+1])
        if cand0 > ans:
            ans = cand0
        if cand1 > ans:
            ans = cand1

else:
    one = a[0::2]
    zero = a[1::2]
    ite = max(len(one)-K+1, 1)
    ans = 0
    for i in range(ite):
        cand0 = sum(zero[i:i+K]) + sum(one[i+1:i+K+1])
        cand1 = sum(zero[i:i+K]) + sum(one[i:i+K+1])
        if cand0 > ans:
            ans = cand0
        if cand1 > ans:
            ans = cand1

print(ans)

Submission status: Wrong Answer

Solution code:
k = int(input().split(" ")[1])
s = input()

arr = []
if s[0] is "0": 
  arr.append(0)
arr.append(1)
for i in range(1,len(s)):
  if s[i-1] is s[i]:
    arr[-1] += 1
  else:
    arr.append(1)
if s[-1] is "0": 
  arr.append(0)

t = 2*k+1
if t>len(s):
  print(len(s))
else:
  temp = sum(arr[0:t])
  m = temp
  for i in range(2,len(arr)-t+1,2):
    m = max(m,temp-arr[i-2]-arr[i-1]+arr[i+t-2]+arr[i+t-1])
  print(m)

Submission status: Wrong Answer

Solution code:
k = int(input().split(" ")[1])
s = input()

arr = []
if s[0] is "0": 
  arr.append(0)
arr.append(1)
for i in range(1,len(s)):
  if s[i-1] is s[i]:
    arr[-1] += 1
  else:
    arr.append(1)
if s[-1] is "0": 
  arr.append(0)

t = 2*k+1
if t>=len(s):
  print(len(s))
else:
  temp = sum(arr[0:t])
  m = temp
  for i in range(2,len(arr)-t+1,2):
    m = max(m,temp-arr[i-2]-arr[i-1]+arr[i+t-2]+arr[i+t-1])
  print(m)