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:
from collections import deque
n, k = map(int, input().split())
s = list(input())
lis = []
val = [0, 0]
for i in range(n):
    if s[i] == '1' and s[i-1] == '0':
         lis.append(val)
         val = [0,0]
    val[int(s[i])] += 1
lis.append(val)
lis2 = []
val = [0,0]
s.reverse()
for i in range(n):
    if s[i] == '1' and s[i-1] == '0':
         lis2.append(val)
         val = [0,0]
    val[int(s[i])] += 1
lis2.append(val)
# 
# print(lis)
# print(lis2)
if len(lis) - k > 0:
    l = 0
    r = 0
    for i in range(len(lis) - k):
        if sum(lis[l]) < sum(lis2[r]):
            n -= sum(lis[l])
            l += 1
        else:
            n -= sum(lis2[r])
            r += 1
print(n)

Submission status: Wrong Answer

Solution code:
from collections import deque
n, k = map(int, input().split())
s = list(input())
lis = []
val = [0, 0]
for i in range(n):
    if s[i] == '1' and s[i-1] == '0':
         lis.append(val)
         val = [0,0]
    val[int(s[i])] += 1
lis.append(val)
lis2 = []
val = [0,0]
s.reverse()
for i in range(n):
    if s[i] == '1' and s[i-1] == '0':
         lis2.append(val)
         val = [0,0]
    val[int(s[i])] += 1
lis2.append(val)

print(lis)
print(lis2)
if len(lis) - k > 0:
    l = 0
    r = 0
    for i in range(len(lis) - k):
        if sum(lis[l]) < sum(lis2[r]):
            n -= sum(lis[l])
            l += 1
        else:
            n -= sum(lis2[r])
            r += 1
print(n)

Submission status: Wrong Answer

Solution code:
import sys
import time
import numpy as np
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 8)
INF = float('inf')
MOD = 10 ** 9 + 7

def main():
    N, K = map(int, readline().split())
    S = input()
    L = len(S)
    count = []
    tmp = 0
    pre = S[0]
    for i in range(L):
        if S[i] == pre:
            tmp += 1
        else:
            count.append(tmp)
            tmp = 1
            pre = S[i]
    count.append(tmp)
    count = [0] + count + [0]
    cumsum = np.cumsum(np.array(count))
    diff = 2*K +1
    ans = 0
    if diff>=len(cumsum):
        ans = cumsum[-1]
    else:
        for i in range(len(cumsum)-diff):
            tmp = cumsum[i+diff]-cumsum[i]
            ans = max(ans,tmp)
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
import time
import numpy as np
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 8)
INF = float('inf')
MOD = 10 ** 9 + 7

def main():
    N, K = map(int, readline().split())
    S = input()
    L = len(S)
    count = []
    tmp = 0
    pre = S[0]
    for i in range(L):
        if S[i] == pre:
            tmp += 1
        else:
            count.append(tmp)
            tmp = 1
            pre = S[i]
    count.append(tmp)
    count = [0] + count + [0]
    cumsum = np.cumsum(np.array(count))
    ans = 0
    diff = 2*K+1
    for i in range(max(len(count)-diff,1)):
        j = min(i+diff, len(cumsum)-1)
        tmp = cumsum[j] - cumsum[i]
        ans = max(ans,tmp)
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
s = input()
one = [i.count('1') for i in s.split('0') if i.count('1') > 0]
zero = [i.count('0') for i in s.split('1') if i.count('0') > 0]
rslt = 0
if len(one) > len(zero):
    for i in range(len(zero)):
        rslt = max(rslt, sum(one[i:i+k+1]+zero[i:i+k+1]))
    if len(one) == 1:
        rslt = 0
elif len(one) < len(zero):
    for i in range(len(one)+1):
        rslt = max(rslt, sum(one[i:i+k]+zero[i:i+k]))
else:
    for i in range(len(one)+1):
        rslt = max(rslt, sum(one[i:i+k+1]+zero[i:i+k]))
        rslt = max(rslt, sum(one[i:i+k+1]+zero[i+1:i+1+k]))
print(rslt)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
s=input()
l=[]
c=1
for i in range(n-1):
  if not s[i]==s[i+1]:
    l.append(c)
    c=0
  c+=1
l.append(c)
l=l+[0]
m=len(l)
if s[0]=='0':
  l=[0]+l
tmp=sum(l[:2*k+1])
ans=tmp
for i in range(2,m-2*k,2):
  tmp=tmp-l[i-1]-l[i-2]+l[i+2*k-1]+l[i+2*k]
  ans=max(ans,tmp)
print(ans)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
s=input()
l=[]
c=1
for i in range(n-1):
  if not s[i]==s[i+1]:
    l.append(c)
    c=0
  c+=1
l.append(c)
l=l+[0]
m=len(l)
if s[0]=='0':
  l=[0]+l
tmp=sum(l[:2*k+1])
ans=tmp
for i in range(2,m-2*k-1,2):
  tmp=tmp-l[i-1]-l[i-2]+l[i+2*k-1]+l[i+2*k]
  ans=max(ans,tmp)
print(ans)

Submission status: Wrong Answer

Solution code:
def main():  
  import sys
  input = sys.stdin.readline
  import numpy as np

  n,k = map(int,input().split())
  s = input()
  chk_0 = []
  chk_1 = []
  c0 = 0
  c1 = 0
  chk_0_append = chk_0.append
  chk_1_append = chk_1.append
  if s[0]=='0':
    c0 += 1
  else:
    c1 += 1
    chk_0_append(0)

  for i in range(n-1):
    if s[i]=='0' and s[i+1]=='0':
      c0 += 1
    if s[i]=='0' and s[i+1]=='1':
      chk_0_append(c0)
      c0 = 0
      c1 = 1
    if s[i]=='1' and s[i+1]=='0':
      chk_1_append(c1)
      c0 = 1
      c1 = 0
    if s[i]=='1' and s[i+1]=='1':
      c1 += 1

  if c0 != 0:
    chk_0_append(c0)
    chk_1_append(0)
  elif c1 != 0:
    chk_1_append(c1)
    chk_0_append(0)

  if len(chk_0) <= k:
    print(n)
    exit()

  chk_1 = tuple(chk_1)
  chk_0 = tuple(chk_0)
  ab = np.array([x+y for x,y in zip(chk_0,chk_1)])
  np.cumsum(ab,out=ab)
  ab = tuple(ab)

  ans = ab[k-1]
  for i in range(len(ab)-k):
    chk = ab[k+i]-ab[i]
    if i >= 1:
      chk += chk_1[i-1]
    ans = max(ans,chk)
  print(ans)
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

starts = [0]
ends = []

for i in range(1, n-1):
    if s[i] == '1' and s[i-1] == '0':
        starts.append(i)
    if s[i] == '1' and s[i+1] == '0' and len(starts) == len(ends) + 2:
        ends.append(i)

ends.append(n-1)

ans = 0
m = len(ends)
for i in range(m-k+1):
    ans = max(ans, ends[i+k-1] - starts[i] + 1)

print(ans)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
s = input()
sl1 = []
sl0 = []
cnt1 = 0
cnt0 = 0
for i in range(n):
    if s[i]=="0" and cnt1:
        sl1.append(cnt1)
        cnt1 = 0
    elif s[i]=="1" and cnt0:
        sl0.append(cnt0)
        cnt0 = 0
    if s[i]=="1": cnt1 += 1
    elif s[i]=="0": cnt0 += 1
if cnt1: sl1.append(cnt1)
if cnt0: sl0.append(cnt0)
if len(sl0)==k:
    ans = n
elif s[0]=="1" and s[-1]=="1":
    ans = sum(sl1[0:k+1]) + sum(sl0[0:k])
    ans_ = ans
    for i in range(k, len(sl0)):
        ans_ += sl1[i+1]+sl0[i]-sl1[i-k]-sl0[i-k]
        ans = max(ans, ans_)
elif s[0]=="0" and s[-1]=="0":
    ans_ = sum(sl1[0:k+1]) + sum(sl0[1:k+1])
    ans = max(sum(sl1[0:k]) + sum(sl0[0:k]), ans_)
    for i in range(k+1, len(sl1)):
        ans_ += sl1[i]+sl0[i+1]-sl1[i-k]-sl0[i-k]
        ans = max(ans, ans_)
elif s[0]=="1" and s[-1]=="0":
    ans = sum(sl1[0:k+1]) + sum(sl0[0:k])
    ans_ = ans
    for i in range(k, len(sl1)-1):
        ans_ += sl1[i+1]+sl0[i]-sl1[i-k]-sl0[i-k]
        ans = max(ans, ans_)
    ans = max(ans, ans_+sl0[-1]-sl1[len(sl1)-k-1]-sl0[len(sl0)-k-1])
elif s[0]=="0" and s[-1]=="1":
    ans_ = sum(sl1[0:k+1]) + sum(sl0[1:k+1])
    ans = max(sum(sl1[0:k]) + sum(sl0[0:k]), ans_)
    for i in range(k+1, len(sl1)):
        ans_ += sl1[i-1]+sl0[i]-sl1[i-k]-sl0[i-k]
        ans = max(ans, ans_)        
print(ans)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
s = input()
sl1 = []
sl0 = []
cnt1 = 0
cnt0 = 0
for i in range(n):
    if s[i]=="0" and cnt1:
        sl1.append(cnt1)
        cnt1 = 0
    elif s[i]=="1" and cnt0:
        sl0.append(cnt0)
        cnt0 = 0
    if s[i]=="1": cnt1 += 1
    elif s[i]=="0": cnt0 += 1
if cnt1: sl1.append(cnt1)
if cnt0: sl0.append(cnt0)
if len(sl0)==k:
    ans = sum(sl0)+sum(sl1)
elif s[0]=="1" and s[-1]=="1":
    ans = sum(sl1[0:k+1]) + sum(sl0[0:k])
    ans_ = ans
    for i in range(k, len(sl0)):
        ans_ += sl1[i+1]+sl0[i]-sl1[i-k]-sl0[i-k]
        ans = max(ans, ans_)
elif s[0]=="0" and s[-1]=="0":
    ans = sum(sl1[0:k]) + sum(sl0[0:k])
    ans_ = ans
    for i in range(k, len(sl1)):
        ans_ += sl1[i]+sl0[i+1]-sl1[i-k]-sl0[i-k]
        ans = max(ans, ans_)
elif s[0]=="1" and s[-1]=="0":
    ans = sum(sl1[0:k+1]) + sum(sl0[0:k])
    ans_ = ans
    for i in range(k, len(sl1)-1):
        ans_ += sl1[i+1]+sl0[i]-sl1[i-k]-sl0[i-k]
        ans = max(ans, ans_)
    ans = max(ans, ans_+sl0[-1]-sl1[len(sl1)-k-1]-sl0[len(sl0)-k-1])
elif s[0]=="0" and s[-1]=="1":
    ans_ = sum(sl1[0:k+1]) + sum(sl0[1:k+1])
    ans = max(sum(sl1[0:k]) + sum(sl0[0:k]), ans_)
    for i in range(k, len(sl1)):
        ans_ += sl1[i-1]+sl0[i]-sl1[i-k]-sl0[i-k]
        ans = max(ans, ans_)        
print(ans)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
s = input()
sl1 = []
sl0 = []
cnt1 = 0
cnt0 = 0
for i in range(n):
    if s[i]=="0" and cnt1:
        sl1.append(cnt1)
        cnt1 = 0
    elif s[i]=="1" and cnt0:
        sl0.append(cnt0)
        cnt0 = 0
    if s[i]=="1": cnt1 += 1
    elif s[i]=="0": cnt0 += 1
if cnt1: sl1.append(cnt1)
if cnt0: sl0.append(cnt0)
if s[0]=="1" and s[-1]=="1":
    ans = sum(sl1[0:k+1]) + sum(sl0[0:k])
    ans_ = ans
    for i in range(k, len(sl0)):
        ans_ += sl1[i+1]+sl0[i]-sl1[i-k]-sl0[i-k]
        ans = max(ans, ans_)
elif s[0]=="0" and s[-1]=="0":
    ans = sum(sl1[0:k]) + sum(sl0[0:k])
    ans_ = ans
    for i in range(k, len(sl1)):
        ans_ += sl1[i]+sl0[i+1]-sl1[i-k]-sl0[i-k]
        ans = max(ans, ans_)
elif s[0]=="1" and s[-1]=="0":
    if len(sl1)==k:
        print(sum(sl1)+sum(sl0))
        exit()
    else:
        ans = sum(sl1[0:k+1]) + sum(sl0[0:k])
    ans_ = ans
    for i in range(k, len(sl1)-1):
        ans_ += sl1[i+1]+sl0[i]-sl1[i-k]-sl0[i-k]
        ans = max(ans, ans_)
    ans = max(ans, ans_+sl0[-1]-sl1[len(sl1)-k-1]-sl0[len(sl0)-k-1])
elif s[0]=="0" and s[-1]=="1":
    ans_ = sum(sl1[0:k+1]) + sum(sl0[1:k+1])
    ans = max(sum(sl1[0:k]) + sum(sl0[0:k]), ans_)
    for i in range(k, len(sl1)):
        ans_ += sl1[i-1]+sl0[i]-sl1[i-k]-sl0[i-k]
        ans = max(ans, ans_)        
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)

n, k = map(int, readline().split())
s = readline().rstrip().decode()
cumsum = []
flag = True
for i in range(n):
    if s[i] == '0':
        if flag:
            cumsum.append(i)
        flag = False
    else:
        flag = True
cumsum.append(n + 1)
if len(cumsum) - 1 <= k:
    print(n)
else:
    ans = 0
    for i in range(len(cumsum) - k):
        ans = max(ans, cumsum[i + k] - cumsum[i])
    print(ans)

Submission status: Wrong Answer

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

lst = [0]
for i in range(N - 1):
    if S[i] != S[i + 1]:
        lst.append(i + 1)
lst.append(N)
# print(lst)

sub = []
for j in range(1, len(lst)):
    sub.append(S[lst[j - 1]:lst[j]])
# print(sub)
diff = [lst[i + 1] -lst[i] for i in range(len(lst) - 1)]
# print(diff)

if S[0] == '1' and len(lst) - 1 <= 2 * K + 1:
    print(N)
elif S[0] == '0' and len(lst) - 1 <= 2 * K:
    print(N)
else:
    candicate = []
    for i in range(len(lst) - 1 - K * 2):
        if S[lst[i]] == '1':
            candicate.append(lst[i + K * 2 + 1] - lst[i])
        else:
            candicate.append(lst[i + K * 2] - lst[i])
    # print(candicate)
    print(max(candicate))

Submission status: Wrong Answer

Solution code:
# coding: utf-8
import sys
import itertools

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

# 0か1の連続している人数のリスト、累積和
streak = [0, 0]
N, K = lr()
S = sr() + '2'
cur = 1
for i in range(N):
    if S[i] != S[i+1]:
        streak.append(cur)
        cur = 1
    else:
        cur += 1

streak_cum = list(itertools.accumulate(streak))
streak_cum.extend([streak_cum[-1], streak_cum[-1]])
if S[0] == '0':
    start = 0
else:
    start = 1
answer = 0

for i in range(start, len(streak_cum)-(2*K+1), 2):
    result = streak_cum[i+2*K+1] - streak_cum[i]
    if result > answer:
        answer = result

print(answer)
# 52

Submission status: Wrong Answer

Solution code:
# coding: utf-8
import sys
import itertools

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

# 0か1の連続している人数のリスト、累積和
streak = [0, 0]
N, K = lr()
S = sr() + '2'
cur = 1
for i in range(N):
    if S[i] != S[i+1]:
        streak.append(cur)
        cur = 1
    else:
        cur += 1

streak_cum = list(itertools.accumulate(streak))
streak_cum.extend([streak_cum[-1], streak_cum[-1]])
if S[0] == '0':
    start = 0
else:
    start = 1
answer = 0
for i in range(start, len(streak_cum)-(2*K+1), 2):
    result = streak_cum[i+2*K+1] - streak_cum[i]
    if result > answer:
        answer = result

print(answer)
# 52

Submission status: Wrong Answer

Solution code:
# coding: utf-8
import sys
import itertools

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

# 0か1の連続している人数のリスト、累積和
streak = [0, 0]
N, K = lr()
S = sr() + '2'
cur = 1
for i in range(N):
    if S[i] != S[i+1]:
        streak.append(cur)
        cur = 1
    else:
        cur += 1

streak_cum = list(itertools.accumulate(streak))
streak_cum.extend([streak_cum[-1], streak_cum[-1]])
if S[0] == '0':
    start = 0
else:
    start = 1
answer = max(streak)
for i in range(start, len(streak_cum)-(2*K+1), 2):
    result = streak_cum[i+2*K+1] - streak_cum[i]
    if result > answer:
        answer = result

print(answer)
# 52

Submission status: Wrong Answer

Solution code:
from itertools import accumulate

def main():
    n, k = map(int, input().split())
    s = input()
    z = [len(i) for i in s.split("1") if i]
    o = [len(i) for i in s.split("0") if i] + [0, 0]
    az = [0] + list(accumulate(z))
    ao = [0] + list(accumulate(o))
    if k < len(z):
        print(max(ao[i + k + 1] - ao[i] + az[i + k] - az[i] for i in range(len(z) - k + 1)))
    else:
        print(n)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
S=input()
X=[]
Y=[0]
_s='2'
for i,s in enumerate(S):
  if _s!=s:
    _s=s
    X.append(i)
    Y.append((int(s)+1)%2)
import itertools
Y=list(itertools.accumulate(Y))
ans=[]
for i,x in enumerate(X):
  if Y[-1]-Y[i]<=K:
    ans.append(N-x)
  else:
    if S[x]=='0':
      ans.append(X[i+2*K]-x)
    else:
      ans.append(X[i+2*K-1]-x)
print(max(ans))

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
S=input()
X=[]
Y=[0]
_s='2'
for i,s in enumerate(S):
  if _s!=s:
    _s=s
    X.append(i)
    Y.append((int(s)+1)%2)
import itertools
Y=list(itertools.accumulate(Y))
ans=[]
for i,x in enumerate(X):
  if Y[-1]-Y[i]<=K:
    ans.append(N-x)
    break
  else:
    if S[x]=='0':
      ans.append(X[i+2*K]-x)
    else:
      ans.append(X[i+2*K-1]-x)
print(max(ans))

Submission status: Wrong Answer

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

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

if S[0] == "0":
    l = -1
    r = K*2
else:
    l = 0
    r = K*2+1

res = 0

for _ in range(abs(-len(sn)//2)-K+1):
    res = max(sum(sn[max(l, 0):min(r, len(sn))]), res)
    l += 2
    r += 2

print(res)

Submission status: Wrong Answer

Solution code:
from functools import reduce


def main():
    n, k = tuple(map(int, input().split()))
    s = input().strip()
    start = 0
    count_zero = {}

    for i, c in enumerate(s):
        if c == "0":
            if i == start:
                count_zero[start] = 0

            count_zero[start] += 1
        elif c == "1":
            start = i + 1

    if len(count_zero) <= k:
        print(n)
        return

    count_zero[-1] = 1
    count_zero[n] = 1

    count_zero_list = sorted(count_zero.items(), key=lambda pair: pair[0])

    print(reduce(lambda ans, i:
        max(ans, count_zero_list[i + k + 1][0] - (count_zero_list[i][0] + count_zero_list[i][1])),
        range(0, len(count_zero) - k - 2), 0))


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
from sys import stdin
N,K = [int(x) for x in stdin.readline().rstrip().split()]
S = (stdin.readline().rstrip())
zero = []
one = []
tmp = 1
S += "@"

for i in range(N):
    if S[i] == S[i+1]:
        tmp += 1
    else:
        if S[i] == "0":
            zero.append(tmp)
        else:
            one.append(tmp)
        tmp = 1
        
if K >= len(zero):
    print(N)
else:
    if S[0] == "1":
        tmp = sum(zero[:K]) + sum(one[:K+1])
        ans = sum(zero[:K]) + sum(one[:K+1])

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

        for i in range(0,len(zero)-K):
            tmp -= zero[i]
            tmp += zero[i+K]
            if i != 0:
                tmp -= one[i]
            if not i+K >= len(one):
                tmp += one[i+K]
            ans = max(ans,tmp)
        print(ans)

Submission status: Wrong Answer

Solution code:
from sys import stdin
N,K = [int(x) for x in stdin.readline().rstrip().split()]
S = (stdin.readline().rstrip())
zero = []
one = []
tmp = 1
S += "@"

for i in range(N):
    if S[i] == S[i+1]:
        tmp += 1
    else:
        if S[i] == "0":
            zero.append(tmp)
        else:
            one.append(tmp)
        tmp = 1
        
if K >= len(zero):
    print(N)
else:
    if S[0] == "1":
        tmp = sum(zero[:K]) + sum(one[:K+1])
        ans = sum(zero[:K]) + sum(one[:K+1])

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

        for i in range(0,len(zero)-K):
            tmp -= zero[i]
            tmp += zero[i+K]
            if i == 0:
                tmp += one[i]
            else:
                tmp -= one[i]
            if not i+K+1 >= len(one):
                tmp += one[i+K+1]
            ans = max(ans,tmp)
        print(ans)

Submission status: Wrong Answer

Solution code:
from sys import stdin
N,K = [int(x) for x in stdin.readline().rstrip().split()]
S = (stdin.readline().rstrip())
zero = []
one = []
tmp = 1
S += "@"

for i in range(N):
    if S[i] == S[i+1]:
        tmp += 1
    else:
        if S[i] == "0":
            zero.append(tmp)
        else:
            one.append(tmp)
        tmp = 1
        
if K >= len(zero):
    print(N,"e")
else:
    if S[0] == "1":
        tmp = sum(zero[:K]) + sum(one[:K+1])
        ans = sum(zero[:K]) + sum(one[:K+1])

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

        for i in range(0,len(zero)-K):
            tmp -= zero[i]
            tmp += zero[i+K]
            if i == 0:
                tmp += one[i]
            else:
                tmp -= one[i]
            if not i+K+1 >= len(one):
                tmp += one[i+K+1]
            ans = max(ans,tmp)
        print(ans)

Submission status: Wrong Answer

Solution code:
from sys import stdin
N,K = [int(x) for x in stdin.readline().rstrip().split()]
S = (stdin.readline().rstrip())
zero = []
one = []
tmp = 1
S += "@"

for i in range(N):
    if S[i] == S[i+1]:
        tmp += 1
    else:
        if S[i] == "0":
            zero.append(tmp)
        else:
            one.append(tmp)
        tmp = 1
        
if K >= len(zero):
    print(N)
else:
    if S[0] == "1":
        tmp = sum(zero[:K]) + sum(one[:K+1])
        ans = sum(zero[:K]) + sum(one[:K+1])

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

        for i in range(0,len(zero)-K):
            tmp -= zero[i]
            tmp += zero[i+K]
            tmp -= one[i]
            if not i+K+1 >= len(one):
                tmp += one[i+K+1]
            ans = max(ans,tmp)
        print(ans)

Submission status: Wrong Answer

Solution code:
from sys import stdin
N,K = [int(x) for x in stdin.readline().rstrip().split()]
S = (stdin.readline().rstrip())
zero = []
one = []
tmp = 1
S += "@"

for i in range(N):
    if S[i] == S[i+1]:
        tmp += 1
    else:
        if S[i] == "0":
            zero.append(tmp)
        else:
            one.append(tmp)
        tmp = 1
        
if K >= len(zero):
    print(N)
else:
    if S[0] == "1":
        tmp = sum(zero[:K]) + sum(one[:K+1])
        ans = sum(zero[:K]) + sum(one[:K+1])

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

        for i in range(0,len(zero)-K):
            tmp -= zero[i]
            tmp += zero[i+K]
            tmp -= one[i]
            if not i+K+1 >= len(one):
                tmp += one[i+K+1]
            ans = max(ans,tmp)
        print(ans)

Submission status: Wrong Answer

Solution code:
from sys import stdin
N,K = [int(x) for x in stdin.readline().rstrip().split()]
S = (stdin.readline().rstrip())
zero = []
one = []
tmp = 1
S += "@"

for i in range(N):
    if S[i] == S[i+1]:
        tmp += 1
    else:
        if S[i] == "0":
            zero.append(tmp)
        else:
            one.append(tmp)
        tmp = 1
        
if K >= len(zero):
    print(N)
else:
    if S[0] == "1":
        tmp = sum(zero[:K]) + sum(one[:K+1])
        ans = sum(zero[:K]) + sum(one[:K+1])

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

        for i in range(0,len(zero)-K):
            if i+K+1 >= len(one):
                break
            tmp -= zero[i]
            tmp += zero[i+K]
            tmp -= one[i]
            tmp += one[i+K+1]
            ans = max(ans,tmp)
        print(ans)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
s = str(input())
head = s[0]

counts = []
_hd = head
_cnt = 0
for _s in s:
  if _s == _hd:
    _cnt += 1
  else:
    counts.append(_cnt)
    _hd = _s
    _cnt = 1
else:
  counts.append(_cnt)

acc = [0]
for c in counts:
  acc.append(acc[-1] + c)

ans = 1
if len(counts) <= 2*k:
  ans = len(s)
else:
  if head == '1':
    for i in range(2*k+1, len(counts)+1, 2):
      p = acc[i] - acc[i - (2*k+1)]
      ans = max(ans, p)
  else:
    for i in range(2*k, len(counts)+1, 2):
      p = acc[i] - acc[max(0, i-(2*k+1))]
      ans = max(ans, p)

print(ans)

Submission status: Wrong Answer

Solution code:
from sys import stdin
def main():
    input = stdin.readline
    n, k = map(int, input().split())
    s = input() + "2"

    flag = s[0]
    count = [0] if flag == "0" else []
    temp_count = 0
    for i in range(n+1):
        si = s[i]
        if flag == si:
            temp_count += 1

        else:
            count.append(temp_count)
            flag = s[i]
            temp_count = 1

    kk = k * 2 + 1
    ans = sum(count[:kk])
    temp_ans = ans
    for i in range(len(count) - kk):
        temp_ans = temp_ans - count[i] + count[i+kk]
        if i % 2 == 0:
            continue
        ans = max(ans, temp_ans)

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
s = input()
x = len(s)
lst = [0]
ans = 0
for i in range(1,x):
    if s[i-1] != s[i]:
        lst.append(i)
lst.append(x)
#if s[0] == "1" and s[x-1] == "0":
    #s.reverse()
if s[0] == "0" and s[x-1] == "1":
    if 2*k >= len(lst):
        ans = x
    else:
        i = 1
        while i+2*k+1 <= len(lst)-1:
            ##ans = max(ans,lst[i+2*k+1]-lst[i])
            i += 2
        #ans = max(ans,lst[2*k])
if s[0] == "0" and s[x-1] == "0":
    if 2*k >= len(lst):
        ans = x
    else:
        i = 1
        while i+2*k+1 <= len(lst)-1:
            ##ans = max(ans,lst[i+2*k+1]-lst[i])
            i += 2
        #ans = max(ans,lst[2*k])
        #ans = max(ans,x-lst[len(lst)-2*k-1])
if s[0] == "1" and s[x-1] == "1":
    if 2*k+1 >= len(lst):
        ans = x
    else:
        i = 0
        while i+2*k+1 <= len(lst)-1:
            #ans = max(ans,lst[i+2*k+1]-lst[i])
            i += 2
print(ans)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
s = input()
x = len(s)
lst = [0]
for i in range(1,x):
    if s[i-1] != s[i]:
        lst.append(i)
lst.append(x-1)
ans = 0
if s[0] == 1:
    i = 0
    if 2*k+1 >= len(lst)-1:
        ans = x-1
    while i+2*k+1 <= len(lst)-1:
        ans = max(ans,lst[i+2*k+1]-lst[i])
        i += 2
else:
    i = 1
    if 2*k >= len(lst)-1:
        ans = x-1
    else:
        while i+2*k+1 <= len(lst)-1:
            ans = max(ans,lst[i+2*k+1]-lst[i])
            i += 2
        ans = max(ans,lst[2*k-1])
print(ans+1)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
s = str(input())
s += "2"
if int(s[0]) == 0:
  a = [0]
  prev = "0"
  count = 1
else:
  a = []
  flag = False
  prev = "1"
  count = 1
ap = a.append
for i in range(1,n+1):
  if s[i] == prev:
    count += 1
  else:
    ap(count)
    count = 1
    prev = s[i]
if s[-1] == "0":
  ap(0)
if 2*k+1 >= len(a):
  print(sum(a))
  exit()
g = sum(a[0:2*k+1])
mx = g
i = 1
while 2*i+2*k < len(a):
  g -= (a[2*i-2]+a[2*i-1])
  g += (a[2*i+2*k-1]+a[2*i+2*k])
  if g > mx:
    mx = g
  i += 1
print(mx)

Submission status: Wrong Answer

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

t = []
c = '0'
v = 0
for i in range(n):
    if c != s[i]:
        t.append(v)
        v = 1
        c = s[i]
    else:
        v += 1
t.append(v)

r = 0
temp = 0
result = 0
for l in range(0, n, 2):
    while r - l <= k and r < len(t):
        temp += t[r]
        if r + 1 < len(t):
            temp += t[r + 1]
        r += 2

    result = max([temp, result])

    if r == l:
        r += 2
    else:
        temp -= t[l]
        if l - 1 > 0:
            temp -= t[l - 1]

print(result)

Submission status: Wrong Answer

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

now=S[0]
cnt=1
A=[] #個数配列
for i in range(n-1):
    if S[i+1]==now:
        cnt+=1
    else:
        A.append(cnt)
        cnt=1
        now=S[i+1]
if cnt>0:
    A.append(cnt)
l=len(A)
    
if S[0]=="0":
    if l<=2*k:
        ans=n
    elif 2*k+1<=l<=2*k+2:
        ans=max(sum(A[:2*k]),sum(A[1:]))
    else:
        ans=max(sum(A[:2*k]),sum(A[1:2*k+2]))
        cnt=sum(A[1:2*k+2])
        for i in range((l-2*k-2)//2):
            cnt-=A[2*i+2]+A[2*i+3]
            cnt+=A[2*k+2*i+2]+A[2*k+2*i+3]
            ans=max(cnt,ans)
        if l%2!=0:
            ans=max(sum(A[l-2*k:]),ans)
else:
    if l<=2*k+1:
        ans=n
    else:
        ans=sum(A[:2*k+1])
        cnt=ans
        for i in range((l-2*k-1)//2):
            cnt-=A[2*i]+A[2*i+1]
            cnt+=A[2*k+2*i+1]+A[2*k+2*i+2]
            ans=max(cnt,ans)
        if l%2==0:
            ans=max(sum(A[l-2*k:]),ans)
print(ans)

Submission status: Wrong Answer

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

now=S[0]
cnt=1
A=[] #個数配列
for i in range(n-1):
    if S[i+1]==now:
        cnt+=1
    else:
        A.append(cnt)
        cnt=1
        now=S[i+1]
if cnt>0:
    A.append(cnt)
l=len(A)
    
if S[0]=="0":
    if l<=2*k:
        ans=n
    elif 2*k+1<=l<=2*k+2:
        ans=max(sum(A[:2*k]),sum(A[1:]))
    else:
        ans=max(sum(A[:2*k]),sum(A[1:2*k+2]))
        cnt=ans
        for i in range((l-2*k-2)//2):
            cnt-=A[2*i+2]+A[2*i+3]
            cnt+=A[2*k+2*i+2]+A[2*k+2*i+3]
            ans=max(cnt,ans)
        if l%2!=0:
            ans=max(sum(A[l-2*k:]),ans)
else:
    if l<=2*k+1:
        ans=n
    else:
        ans=sum(A[:2*k+1])
        cnt=ans
        for i in range((l-2*k-1)//2):
            cnt-=A[2*i]+A[2*i+1]
            cnt+=A[2*k+2*i+1]+A[2*k+2*i+2]
            ans=max(cnt,ans)
        if l%2==0:
            ans=max(sum(A[l-2*k:]),ans)
print(ans)