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() + '2'

T = []
c = 1
for i in range(1, len(S)) :
    if S[i] == S[i-1] :
        c += 1
    else :
        T.append(c)
        c = 1
        
for i in range(1, len(T)) :
    T[i] += T[i-1]

ret = 0
for i in range(len(T)) :
    if i - 2*K < 0 :
        ret = max(ret, T[i])
    else :
        ret = max(ret, T[i] - (T[i-2*K] - 1))
    
print(ret)

Submission status: Wrong Answer

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

before = S[0]
cnt = 0
same_nums = []
for i in range(N):
  if S[i] == before:
    cnt += 1
  else:
    same_nums.append(cnt)
    before = S[i]
    cnt = 1
same_nums.append(cnt)
if S[0] == '0':
  sum_temp = sum(same_nums[:2*K])
  ans = sum_temp
  for i in range(0, K+1, 2):
    if i + 2*(K+1) <= len(same_nums):
      sum_temp = sum_temp - sum(same_nums[i:i+2]) + sum(same_nums[i+2*K:i+2*(K+1)])
      ans = max(ans, sum_temp)
    else:
      break
  print(ans)
else:
  sum_temp = sum(same_nums[:2*K+1])
  ans = sum_temp
  for i in range(0, K+1, 2):
    if i + 2*(K+1) <= len(same_nums):
      sum_temp = sum_temp - sum(same_nums[i:i+2]) + sum(same_nums[i+2*K+1:i+2*K+3])
      ans = max(ans, sum_temp)
    else:
      break
  print(ans)

Submission status: Wrong Answer

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

before = S[0]
zeros = []
ones = []
zero_cnt = 0
one_cnt = 0
for char in S:
  if char == before:
    if char == '0':
      zero_cnt += 1
    else:
      one_cnt += 1
    before = char
  else:
    if char == '0':
      ones.append(one_cnt)
      zero_cnt = 1
    else:
      zeros.append(zero_cnt)
      one_cnt = 1
    before = char
if S[-1] == '0':
  zeros.append(zero_cnt)
elif S[-1] == '1':
  ones.append(one_cnt)
if len(zeros) <= K:
  print(N)
else:
  if S[1] == '0':
    sum_o = 0
    for i in range(len(zeros)-K+1):
      sum_o = max(sum_o, sum(zeros[i:i+K]) + sum(ones[i:i+K]))
    print(sum_o)
  else:
    sum_o = 0
    for i in range(len(zeros)-K+1):
      sum_o = max(sum_o, sum(zeros[i:i+K]) + sum(ones[i:i+K+1]))
    print(sum_o)

Submission status: Wrong Answer

Solution code:
N,K = map(int, input().split())
S = str(input())
s1 = list(S)
right = 0
count_0 = 0
ans = 0

for left in range(N):
  while right<N and count_0 <=K:
    if s1[0] == "0" and right == 0:
      count_0 += 1
    if s1[right] == "0" and s1[right-1] == "1":
      count_0 += 1
    right += 1
    
    if count_0 <= K:
      ans = max(ans, right-left)
  if left != N-1:
    if s1[left] == "0" and s1[left+1] =="1":
      count_0 -= 1
    
print(ans)

Submission status: Wrong Answer

Solution code:
import re

N,K = map(int, input().split())
S = str(input())
s1 = list(S)
right = 0
count_0 = 0
ans = 0

for left in range(N):
  while right<N and count_0 <=K:
    if s1[0] == "0" and right == 0:
      count_0 += 1
    if s1[right] == "0" and s1[right-1] == "1":
      count_0 += 1
    right += 1
    ans = max(ans, right-left)
  if left != N-1:
    if s1[left] == "0" and s1[left+1] =="1":
      count_0 -= 1
    
print(ans)

Submission status: Wrong Answer

Solution code:
# coding:utf-8
import sys
from collections import deque
sys.setrecursionlimit(2**31-1)
input = sys.stdin.readline
write = sys.stdout.write
LMIIS = lambda : list(map(int,input().split()))
II = lambda : int(input())
dbg = lambda *something : print(*something) if DEBUG is True else 0
DEBUG = False

def main():
    N,K = LMIIS()
    S = input()
    #S = '0' + S + '0'


    max_t = [0,0,0]
    sum_max_t = 0
    max_idx = 0
    cnums = []


    right = 0
    zeros = 0
    left = 0


    for i in range(N):

        if S[i] == '1':

            right += 1


        else:
            if i > 0 and S[i-1] != '0':
                # if max_idx == 0 and S[0] == '1':
                #     continue
                if zeros != 0:
                    cnums.append([left, zeros, right])

                    if sum_max_t < left+zeros+right:
                        sum_max_t = left+zeros+right
                        max_idx = len(cnums) - 1
                    max_t = [left, zeros, right]

                left = right
                right = 0
                zeros = 1
            else:
                zeros += 1

    cnums.append([left, zeros, right])
    if sum_max_t < left + zeros + right:
        sum_max_t = left + zeros + right
        max_idx = len(cnums) - 1
        max_t = [left, zeros, right]

    left = right
    right = 0
    #
    # if S[i-1] == '0':
    #     cnums.append([left, zeros, right])
    #     if sum_max_t < left + zeros + right:
    #         sum_max_t = left + zeros + right
    #         max_idx = len(cnums) - 1
    #         max_t = [left, zeros, right]




    count = 0
    dbg(cnums, max_idx, max_t)


    while count < K and len(cnums) > 1:

        count += 1
        sumleft = 0
        sumright = 0

        dbg(cnums, max_idx, max_t)
        if max_idx >= 1:
            cnums[max_idx - 1][2] += max_t[1] + max_t[2]
            sumleft = sum(cnums[max_idx - 1])
        if max_idx < len(cnums) - 1:
            cnums[max_idx + 1][0] += max_t[1] + max_t[0]
            sumright = sum(cnums[max_idx + 1])




        if sumleft > sumright:

            sum_max_t = sumleft
            max_t = cnums[max_idx - 1]
            cnums.pop(max_idx)
            max_idx -= 1
        else:
            sum_max_t = sumright
            max_t = cnums[max_idx + 1]
            cnums.pop(max_idx)

    dbg(cnums, max_idx, max_t)



    if count < K:
        print(sum(cnums[0]))
    else:
        print(max(max_t[0],max_t[2]))





    pass
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
from itertools import groupby

N, K = map(int, input().split())
S = input()
initial = S[0]
list0 = []
list1 = []
for key, group in groupby(S):
    count = len(list(group))
    # print(key)
    # print(count)
    if key == "0":
        list0.append(count)
    else:
        list1.append(count)
leng0 = len(list0)
leng1 = len(list1)
if leng0 >= leng1:
    list1.append(0)
if leng0 < leng1:
    list0.append(0)
# print(initial)
# print(list0)
# print(list1)

# 0が続く区間の数と裏返す回数から求めるべきパターン数を決める
if leng0-K >= 0:
    num_of_patterns = leng0 - K + 1
else:
    num_of_patterns = 1


max_temp = 0

for i in list0[:K]:
    max_temp += i

if initial == "0":
    for i in list1[:K]:
        max_temp += i
else:
    for i in list1[:K+1]:
        max_temp += i
temp = max_temp
# print(temp)

if initial == "0":
    for i in range(0, num_of_patterns-1):
        delta = list0[K + i] + list1[K + i] - (list0[i] + list1[i])
        temp += delta
        if temp > max_temp:
            max_temp = temp
        # print(temp)

else:
    for i in range(0, num_of_patterns-1):
        delta = list0[K + i] + list1[K + i+1] - (list0[i] + list1[i])
        temp += delta
        if temp > max_temp:
            max_temp = temp
        # print(temp)

print(max_temp)

Submission status: Wrong Answer

Solution code:
a, b = map(int, input().split())
c = list(input() + "2")
d = 1
e = []
for i in range(1, len(c)):
    if c[i] != c[i-1]:
        if c[i-1] == "1":
            e.append(d)
        else:
            e.append(-d)
        d = 1
    else:
        d += 1
f = 0
g = 0
h = 0
for i in range(len(e)):
    g += abs(e[i])
    if f >= b and e[i] > 0:
        h = max(h, g)
    if i-b*2 >= 0:
        g -= abs(e[i-b*2])
    if e[i] < 0:
        f += 1
print(h if h != 0 else a)

Submission status: Wrong Answer

Solution code:
a, b = map(int, input().split())
c = list(input() + "2")
d = 1
e = []
for i in range(1, len(c)):
    if c[i] != c[i-1]:
        if c[i-1] == "1":
            e.append(d)
        else:
            e.append(-d)
        d = 1
    else:
        d += 1
f = 0
g = 0
h = 0
for i in range(len(e)):
    g += abs(e[i])
    if f >= b and e[i] > 0:
        h = max(h, g)
    if i-b*2 >= 0:
        g -= abs(e[i-b*2])
    if e[i] < 0:
        f += 1
print(h if len(e) != 1 else abs(e[0]))

Submission status: Wrong Answer

Solution code:
a, b = map(int, input().split())
c = list(input() + "2")
d = 1
e = []
for i in range(1, len(c)):
    if c[i] != c[i-1]:
        if c[i-1] == "1":
            e.append(d)
        else:
            e.append(-d)
        d = 1
    else:
        d += 1
f = 0
g = 0
h = 0
for i in range(len(e)):
    g += abs(e[i])
    if f >= b:
        if e[i] > 0:
            h = max(h, g)
        if i-b*2 >= 0:
            g -= abs(e[i-b*2])
    if e[i] < 0:
        f += 1
print(h if len(e) != 1 else abs(e[0]))

Submission status: Wrong Answer

Solution code:
a, b = map(int, input().split())
c = list(input() + "2")
d = 1
e = []
for i in range(1, len(c)):
    if c[i] != c[i-1]:
        if c[i-1] == "1":
            e.append(d)
        else:
            e.append(-d)
        d = 1
    else:
        d += 1
f = 0
g = 0
h = 0
print(e)
for i in range(len(e)):
    g += abs(e[i])
    if f >= b:
        if e[i] > 0:
            h = max(h, g)
        if i-b*2 >= 0:
            g -= abs(e[i-b*2])
    if e[i] < 0:
        f += 1
print(h if len(e) != 1 else abs(e[0]))

Submission status: Wrong Answer

Solution code:
a, b = map(int, input().split())
c = list(input() + "2")
d = 1
e = []
for i in range(1, len(c)):
    if c[i] != c[i-1]:
        if c[i-1] == "1":
            e.append(d)
        else:
            e.append(-d)
        d = 1
    else:
        d += 1
f = 0
g = 0
h = 0
for i in range(len(e)):
    g += abs(e[i])
    if e[i] < 0:
        f += 1
        if f > b:
            g -= abs(e[i-b*2])
    else:
        if f >= b:
            h = max(h, g)
            g -= abs(e[i-b*2])
print(h if len(e) != 1 else abs(e[0]))

Submission status: Wrong Answer

Solution code:
a, b = map(int, input().split())
c = input()
d = []
e = 1
for i in range(1, len(c)):
    if c[i] != c[i-1]:
        if c[i] == "0":
            d.append(e)
        else:
            d.append(-e)
        e = 1
    else:
        e += 1
    if len(c) - 1 == i:
        if c[i] == "1":
            d.append(e)
        else:
            d.append(-e)
g = 0
h = 0
for i in range(len(d)):
    g += abs(d[i])
    if d[0] > 0 and i >= (b * 2):
        if d[i] > 0:
            h = max(h, g)
            g -= abs(d[i-b*2])
        else:
            g -= abs(d[i-b*2])
    elif d[0] < 0 and i >= (b * 2-1):
        if d[i] > 0:
            h = max(h, g)
            g -= abs(d[i - (b * 2-1)])
        else:
            g -= abs(d[i - (b * 2-1)])
print(h if len(d) != 0 else 1)

Submission status: Wrong Answer

Solution code:
a, b = map(int, input().split())
c = input()
d = []
e = 1
for i in range(1, len(c)):
    if c[i] != c[i-1]:
        if c[i] == "0":
            d.append(e)
        else:
            d.append(-e)
        e = 1
    else:
        if i == len(c) - 1:
            if c[i] == "1":
                d.append(e)
            else:
                d.append(-e)
        e += 1
    if len(c) - 1 == i:
        if c[i] == "1":
            d.append(e)
        else:
            d.append(-e)
g = 0
h = 0
for i in range(len(d)):
    g += abs(d[i])
    if d[0] > 0 and i >= (b * 2):
        if d[i] > 0:
            h = max(h, g)
            g -= abs(d[i-b*2])
        else:
            g -= abs(d[i-b*2])
    elif d[0] < 0 and i >= (b * 2-1):
        if d[i] > 0:
            h = max(h, g)
            g -= abs(d[i - (b * 2-1)])
        else:
            g -= abs(d[i - (b * 2-1)])
print(h if len(d) != 0 else 1)

Submission status: Wrong Answer

Solution code:
a, b = map(int, input().split())
c = input()
d = []
e = 1
for i in range(1, len(c)):
    if c[i] != c[i-1]:
        if c[i] == "0":
            d.append(e)
        else:
            d.append(-e)
        e = 1
    else:
        e += 1
    if i == len(c) - 1:
        if c[i] == "1":
            d.append(e)
        else:
            d.append(-e)
g = 0
h = 0
for i in range(len(d)):
    g += abs(d[i])
    if d[0] > 0 and i >= (b * 2):
        if d[i] > 0:
            h = max(h, g)
            g -= abs(d[i-b*2])
        else:
            g -= abs(d[i-b*2])
    elif d[0] < 0 and i >= (b * 2-1):
        if d[i] > 0:
            h = max(h, g)
            g -= abs(d[i - (b * 2-1)])
        else:
            g -= abs(d[i - (b * 2-1)])
print(h if len(d) != 0 else 1)

Submission status: Wrong Answer

Solution code:
a, b = map(int, input().split())
c = input()
d = []
e = 1
for i in range(1, len(c)):
    if c[i] != c[i-1]:
        if c[i] == "0":
            d.append(e)
        else:
            d.append(-e)
        e = 1
    else:
        e += 1
        if i == len(c) - 1:
            if c[i] == "1":
                d.append(e)
            else:
                d.append(-e)
g = 0
h = 0
for i in range(len(d)):
    g += abs(d[i])
    if d[0] > 0 and i >= (b * 2):
        if d[i] > 0:
            h = max(h, g)
            g -= abs(d[i-b*2])
        else:
            g -= abs(d[i-b*2])
    elif d[0] < 0 and i >= (b * 2-1):
        if d[i] > 0:
            h = max(h, g)
            g -= abs(d[i - (b * 2-1)])
        else:
            g -= abs(d[i - (b * 2-1)])
print(h if len(d) != 0 else 1)

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
S_list = ['0'] + list(input()) + ['-1']

s_count_list =[]

cnt = 1
for i in range(1,N+2) :
    if S_list[i] == S_list[i-1] :
        cnt +=1
    else:
        s_count_list.append(cnt)
        cnt = 1
if S_list[1] == '0':
    s_count_list[0] = s_count_list[0] -1
    s_count_list.insert(0,0)
else:
    s_count_list.pop(0)

ruisekiwa = [0]
for i,s in enumerate(s_count_list):
    ruisekiwa.append(ruisekiwa[i] + s)

if len(ruisekiwa) %2 ==0:
    ruisekiwa.append(0)

answer =  1
#print(s_count_list)
#Jjkprint(ruisekiwa)
for i in range(0,len(ruisekiwa),2):
    left = i
    right = min((i + (2 * K) + 1),len(s_count_list))
    #print(left,right)
    #print(ruisekiwa[right])
    tmp = ruisekiwa[right] - ruisekiwa[left]
    answer = max(tmp,answer)

print(answer)

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
S_list = ['0'] + list(input()) + ['-1']

s_count_list =[]

cnt = 1
for i in range(1,N+2) :
    if S_list[i] == S_list[i-1] :
        cnt +=1
    else:
        s_count_list.append(cnt)
        cnt = 1
if S_list[1] == '0':
    s_count_list[0] = s_count_list[0] -1
    s_count_list.insert(0,0)
else:
    s_count_list.pop(0)

ruisekiwa = [0]
for i,s in enumerate(s_count_list):
    ruisekiwa.append(ruisekiwa[i] + s)

if len(ruisekiwa) %2 ==0:
    ruisekiwa.append(0)

answer =  1
#print(s_count_list)
#Jjkprint(ruisekiwa)
for i in range(0,len(ruisekiwa),2):
    left = i
    right = min((i + (2 * K) + 1),len(ruisekiwa)-1)
    #print(left,right)
    tmp = ruisekiwa[right] - ruisekiwa[left]
    answer = max(tmp,answer)

print(answer)

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
S_list = ['0'] + list(input()) + ['-1']

s_count_list =[]

cnt = 1
for i in range(1,N+2) :
    if S_list[i] == S_list[i-1] :
        cnt +=1
    else:
        s_count_list.append(cnt)
        cnt = 1
if S_list[1] == '0':
    s_count_list[0] = s_count_list[0] -1
    s_count_list.insert(0,0)
else:
    s_count_list.pop(0)

ruisekiwa = [0]
for i,s in enumerate(s_count_list):
    ruisekiwa.append(ruisekiwa[i] + s)

if len(ruisekiwa) %2 ==0:
    ruisekiwa.append(0)

answer =  0
#print(s_count_list)
#Jjkprint(ruisekiwa)
for i in range(0,len(ruisekiwa),2):
    left = i
    right = min((i + (2 * K) + 1),len(ruisekiwa)-1)
    #print(left,right)
    tmp = ruisekiwa[right] - ruisekiwa[left]
    answer = max(tmp,answer)

print(answer)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
N,K=map(int,input().split())
S=input()
Snumber=[]
count=0
 
for i in range(N):
    if i==0 or S[i-1]==S[i]:
        count=count+1
    else:
        Snumber.append(count)
        count=1
Snumber.append(count)
 
ans=0
 
if 1+2*K>len(Snumber):
    ans=len(S)
elif S[0]=="0":
    temporary=sum(Snumber[1:2+2*K])
    j=1
    while 1+2*K+j*2<=len(Snumber)-1:
        temporary=temporary-Snumber[2*j-1]-Snumber[2*j]+Snumber[2*K+2*j]+Snumber[1+2*K+2*j]
        if temporary>ans:
            ans=temporary
        j=j+1
    if len(Snumber)%2==1:
        C=sum(Snumber[len(Snumber)-2*K:])
    else:
        C=0
    ans=max(ans,sum(Snumber[:2*K]),C)
else:
    temporary=sum(Snumber[0:1+2*K])
    j=1
    while 2*K+j*2<=len(Snumber)-1:
        temporary=temporary-Snumber[2*j-2]-Snumber[2*j-1]+Snumber[2*K+2*j-1]+Snumber[2*K+2*j]
        if temporary>ans:
            ans=temporary
        j=j+1
    if len(Snumber)%2==0:
        C=sum(Snumber[len(Snumber)-2*K:])
    else:
        C=0
    ans=max(ans,sum(Snumber[len(Snumber)-2*K:],C))
print(ans)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
N,K=map(int,input().split())
S=input()
Snumber=[]
count=0
 
for i in range(N):
    if i==0 or S[i-1]==S[i]:
        count=count+1
    else:
        Snumber.append(count)
        count=1
Snumber.append(count)
 
ans=0
 
if 1+2*K>len(Snumber):
    ans=len(S)
elif S[0]=="0":
    temporary=sum(Snumber[1:2+2*K])
    j=1
    while 2+2*K+j*2<=len(Snumber)-1:
        temporary=temporary-Snumber[2*j-1]-Snumber[2*j]+Snumber[1+2*K+2*j]+Snumber[2+2*K+2*j]
        if temporary>ans:
            ans=temporary
        j=j+1
    if len(Snumber)%2==1:
        C=sum(Snumber[len(Snumber)-2*K:])
    else:
        C=0
    ans=max(ans,sum(Snumber[:2*K]),C)
else:
    temporary=sum(Snumber[0:1+2*K])
    j=1
    while 1+2*K+j*2<=len(Snumber)-1:
        temporary=temporary-Snumber[2*j-2]-Snumber[2*j-1]+Snumber[2*K+2*j]+Snumber[1+2*K+2*j]
        if temporary>ans:
            ans=temporary
        j=j+1
    ans=max(ans,sum(Snumber[len(Snumber)-2*K:]))
print(ans)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
s = input()
s = s + '#'
left = 0
right =0
l = []
while left < n:
    if s[right] == s[right+1]:
        right += 1
    else:
        l.append(right-left+1)
        right += 1
        left = right
if s[0] == '0':
    l.insert(0,0)
if s[n-1] == '0':
    l.append(0)
n = len(l)
l_sum = [0]*(n)
l_sum[0] = l[0]
for i in range(1,n):
    l_sum[i] = l[i] + l_sum[i-1]
if n <= 2*k + 1:
    print(l_sum[n-1])
else:
    res = l_sum[2*k]
    for i in range(1,n-2*k):
        res = max(res,l_sum[i+2*k]-l_sum[i-1])
    print(res)

Submission status: Wrong Answer

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

ans = L = R = 0

while R < N:
    if S[R] == '0':
        while R < N and S[R] == '0':
            R += 1
        while S[R] == '1':
            R += 1
        K -= 1
        if K <= 0:
            ans = max(ans, R-L)

            while L < N and S[L] == '1':
                L += 1
            while L < N and S[L] == '0':
                L += 1
    else:
        R += 1
        ans = max(ans, R-L)
print(ans)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
N,K=map(int,input().split())
S=input()
Snumber=[]
count=0

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

ans=0

if 1+2*K>len(Snumber):
    ans=sum(Snumber)
elif S[0]=="0":
    temporary=sum(Snumber[1:2+2*K])
    j=1
    while 2+2*K+j*2<=len(Snumber)-1:
        temporary=temporary-Snumber[2*j-1]-Snumber[2*j]+Snumber[1+2*K+2*j]+Snumber[2+2*K+2*j]
        if temporary>ans:
            ans=temporary
        j=j+1
    if len(Snumber)%2==0:
        C=sum(Snumber[len(Snumber)-2*K-1:])
    else:
        C=0
    ans=max(ans,sum(Snumber[:2*K]),sum(Snumber[len(Snumber)-2*K:]),C)
else:
    temporary=sum(Snumber[0:1+2*K])
    j=1
    while 1+2*K+j*2<=len(Snumber)-1:
        temporary=temporary-Snumber[2*j-2]-Snumber[2*j-1]+Snumber[2*K+2*j]+Snumber[1+2*K+2*j]
        if temporary>ans:
            ans=temporary
        j=j+1
    if len(Snumber)%2==1:
        C=sum(Snumber[len(Snumber)-2*K-1:])
    else:
        C=0
    ans=max(ans,sum(Snumber[len(Snumber)-2*K:]),C)
print(ans)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
#ABC124 D
from itertools import accumulate
import bisect
n,k = map(int,input().split())
s = list(map(int,list(input())))
#0→1 または 1→0 の切り替わるときに連続する数を入れるlist
L = [0] if s[0] == 1 else [0,0]
cnt = 1
for i in range(1,n):
    if s[i-1] != s[i]:
        L += [cnt]
        cnt = 0
    cnt += 1
L += [cnt,0]
L = list(accumulate(L))
ans = 0
if len(L) - 2*k - 1 == 0:
    print(L[-1])
    quit()
for i in range(0,len(L)-2*k-1,2):
    ans = max(ans,L[i+2*k+1] - L[i])
print(ans)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
#ABC124 D
from itertools import accumulate
import bisect
n,k = map(int,input().split())
s = list(map(int,list(input())))
#0→1 または 1→0 の切り替わるときに連続する数を入れるlist
L = [0] if s[0] == 1 else [0,0]
cnt = 1
for i in range(1,n):
    if s[i-1] != s[i]:
        L += [cnt]
        cnt = 0
    cnt += 1
L += [cnt,0]
L = list(accumulate(L))
ans = 0
if len(L) - 2*k - 1 == 0:
    print(L[-1])
    quit()
for i in range(0,len(L)-2*k-1,2):
    print(i,L[i+2*k+1],L[i])
    ans = max(ans,L[i+2*k+1] - L[i])
print(ans)

Submission status: Wrong Answer

Solution code:
a, b = map(int, input().split())
c = input()
d = []
e = 0
for i in range(1, a):
    e += 1
    if c[i] != c[i-1]:
        if c[i] == "0":
            d.append(e)
        else:
            d.append(-e)
        e = 0
    if i == (a-1):
        e += 1
        if c[i] == "1":
            d.append(e)
        else:
            d.append(-e)
        e = 0
f = 0
g = 0
h = 0
for i in range(len(d)):
    g += abs(d[i])
    if d[i] > 0 and f >= b:
        h = max(h, g)
        g -= abs(d[i-2*b])
    if d[i] < 0 and f >= b:
        g -= abs(d[i-2*b])
    if d[i] < 0 and f < b:
        f += 1
h = max(h, g)
print(h if h != 0 else a)

Submission status: Wrong Answer

Solution code:
a, b = map(int, input().split())
c = input()
d = []
e = 0
for i in range(1, a):
    e += 1
    if c[i] != c[i-1]:
        if c[i] == "0":
            d.append(e)
        else:
            d.append(-e)
        e = 0
    if i == (a-1):
        e += 1
        if c[i] == "1":
            d.append(e)
        else:
            d.append(-e)
        e = 0
f = 0
g = 0
h = 0
for i in range(len(d)):
    g += abs(d[i])
    if f >= b:
        h = max(h, g)
        g -= abs(d[i-2*b])
    if d[i] < 0 and f < b:
        f += 1
print(h if h != 0 else a)

Submission status: Wrong Answer

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

slid = []
for i in range(n):
    if i == 0:
        slid.append(0)
        continue
    if s[i] != s[i-1]:
        slid.append(i-1)
        slid.append(i)
slid.append(n-1)

zone = []
for j in range(len(slid)//2):
    zone.append([slid[j*2], slid[j*2+1]])

front = s[0]
n_zone = len(zone)
if n_zone%2 == 0:
    n_zone0 = n_zone//2
elif front == "0":
    n_zone0 = (n_zone+1)//2
else:
    n_zone0 = (n_zone-1)//2

if n_zone0 <= k:
    print(n)
    exit()

ans = 0
if front == "1":
    now = 0
else:
    ans = zone[k*2][1]
    now = 1
while True:
    if now+k*2-1 >= n_zone:
        break
    ans = max(ans, zone[now+k*2-1][1]-zone[now][0]+1)

    if now+k*2 >= n_zone:
        break
    ans = max(ans, zone[now+k*2][1]-zone[now][0]+1)
    now += 2

print(ans)

Submission status: Wrong Answer

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

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

zone = []
for j in range(len(slid)//2):
    zone.append([slid[j*2], slid[j*2+1]])

front = s[0]
n_zone = len(zone)
if n_zone%2 == 0:
    n_zone0 = n_zone//2
elif front == "0":
    n_zone0 = (n_zone+1)//2
else:
    n_zone0 = (n_zone-1)//2

if n_zone0 <= k:
    print(n)
    exit()

ans = 0
if front == "1":
    now = 0
else:
    ans = zone[k*2][1]
    now = 1

while True:
    if now+k*2-1 >= n_zone:
        break
    ans = max(ans, zone[now+k*2-1][1]-zone[now][0]+1)

    if now+k*2 >= n_zone:
        break
    ans = max(ans, zone[now+k*2][1]-zone[now][0]+1)
    now += 2

print(ans)

Submission status: Wrong Answer

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

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

zone = []
for j in range(len(slid)//2):
    zone.append([slid[j*2], slid[j*2+1]])

front = s[0]
n_zone = len(zone)
if n_zone%2 == 0:
    n_zone0 = n_zone//2
elif front == "0":
    n_zone0 = (n_zone+1)//2
else:
    n_zone0 = (n_zone-1)//2

if n_zone0 <= k:
    print(n)
    exit()

ans = 0
if front == "1":
    now = 0
else:
    ans = zone[k*2][1]
    now = 1

while True:
    if now+k*2-1 >= n_zone:
        break
    ans = max(ans, zone[now+k*2-1][1]-zone[now][0]+1)

    if now+k*2 >= n_zone:
        break
    ans = max(ans, zone[now+k*2][1]-zone[now][0]+1)
    print(ans, now)
    now += 2

print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
S=input()
X=[]
j="1"
num=0
for i in S:
    if j!=i:
        X.append(num)
        j=i
        num=1
    else:
        num+=1
X.append(num)
if j=="0":
    X.append(0)
Y=[0]
for i in X:
    Y.append(Y[-1]+i)
ans=0
for i in range(1,len(Y),2):
    if i+2*K>=len(Y):
        break
    ans=max(ans,Y[i+2*K]-Y[i-1])
if len(X)==1:
    ans=X[0]
print(ans)

Submission status: Wrong Answer

Solution code:
N,K = map(int, input().split())
S = [int(x) for x in input()]
a=[]
tmp = 0
if S[0]==1:
    a.append(0)
for i in range(N-1):
    tmp = tmp+1
    if S[i]!=S[i+1]:
        a.append(tmp)
        tmp=0
a.append(tmp+1)
if S[N-1] == 0:
    a.append(0)
if len(a)<=K*2:
    print(sum(a))
else:
    print(a)
    max_l = sum(a[:2*K])
    for i in range(K,int(len(a)/2)):
        tmp = sum(a[2*(i-K+1):(i+1)*2]) + a[2*(i-K+1)-1]
        max_l = tmp if tmp>max_l else max_l
        print(tmp)
    print(max_l)

Submission status: Wrong Answer

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

L = []
R = []
before = S[0]
cnt_zero = 0
cnt_one = 0
len_l = 0
len_r = 0
for i in range(N):
    if before == '0' and S[i] == '0':
        cnt_zero += 1
    elif before == '0' and S[i] == '1':
        before = '1'
        L.append(cnt_zero)
        len_l += 1
        cnt_zero = 0
        cnt_one += 1
    elif before == '1' and S[i] == '0':
        before = '0'
        R.append(cnt_one)
        len_r += 1
        cnt_zero += 1
        cnt_one = 0
    else:
    	cnt_one += 1
if cnt_zero > 0:
    L.append(cnt_zero)
    len_l += 1

if cnt_one > 0:
	R.append(cnt_one)
	len_r += 1
ans = 0
sum_one = 0
if S[0] == '0':
	sum_one = sum(L[:K]) + sum(R[:K])
else:
	sum_one = sum(L[:K]) + sum(R[:K+1])

ans = sum_one
for i in range(len_l)[1:]:
    if i + K >= len_r:
        break
    if S[0] == '0' and i == 1:
        sum_one += (L[K] + R[K] - L[0])
    else:
        sum_one += (L[K+i-1] + R[K+i] - L[i-1] - R[i-1])
    ans = max(ans, sum_one)
print(ans)

Submission status: Wrong Answer

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

L = []
before = '1'
cnt = 0
len_l = 0
for i in range(N):
    if before == '0' and S[i] == '0':
        cnt += 1
    elif before == '0' and S[i] == '1':
        before = '1'
        L.append(cnt)
        len_l += 1
        cnt = 0
    elif before == '1' and S[i] == '0':
        before == '0'
        cnt += 1
if cnt != 0:
    L.append(cnt)
    len_l += 1

L.sort(reverse=True)

if len_l > K:
    print(sum(L[:K]))
else:
    print(sum(L))

Submission status: Wrong Answer

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

L = []
before = ''
cnt = 0
len_l = 0
for i in range(N):
    if before == '0' and S[i] == '0':
        cnt += 1
    elif before == '0' and S[i] == '1':
        before = '1'
        L.append(cnt)
        len_l += 1
        cnt = 0
    elif before == '1' and S[i] == '0':
        before == '0'
        cnt += 1
if cnt != 0:
    L.append(cnt)
    n_l += 1

L.sort(reverse=True)

if len_l > K:
    print(sum(L[:K]))
else:
    print(sum(L))

Submission status: Wrong Answer

Solution code:
import numpy as np

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

X = np.cumsum(Cnts)  # X[i]=sum(Cnts[0:i+1])
# print(X)

ans = 0
if S[0] == "0":
	for i in range(0, len(X) - 2 * K + 1):
		ans = max(ans, X[i + 2 * K - 1] - X[i])
	print(ans)
else:
	for i in range(0, len(X) - 2 * K):
		ans = max(ans, X[i + 2 * K] - X[i])
	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 = 1
if S[0] == "1":
	for i in range(N):
		ans = max(ans, sum(Cnts[i: min(N, i + 2 * K + 1)]))
	print(ans)
else:
	for i in range(N):
		ans = max(ans, sum(Cnts[i: min(N, 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 = 1
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)