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

#K回範囲を選択して反転、を繰り返し、1の連続数を最大にする
#連続した0を１度しか含まないK+1個の1の連続を探して範囲を出力する……？

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

max = 0
if S[0] == '1':
    for i, point in enumerate(change):
        if i + K*2 + 3 > len(change):
            if i % 2 == 0:
                if max < N - point:
                    max = N - point
            else:
                if max < N - point - 1:
                    max = N - point - 1
            break
        if i % 2 == 0:
            if max < change[i + 1 + K*2] - point:
                max = change[i + 1 + K*2] - point
        else:
            if max < change[i + (K-1)*2 + 1] - point:
                max = change[i + (K-1)*2 + 1] - point
else:
    for i, point in enumerate(change):
        if i + K*2 + 2 > len(change):
            if i % 2 == 0:
                if max < N - point - 1:
                    max = N - point - 1
            else:
                if max < N - point:
                    max = N - point
            break
        if i % 2 == 0:
            if i == 0:
                if max < change[i + K*2] - change[i]:
                    max = change[i + K*2] - change[i]
                continue
            if max < change[i + K*2] - change[i-1]:
                max = change[i + K*2] - change[i-1]
        else:
            if max < change[i + 1 + K*2] - point:
                max = change[i + 1 + K*2] - point
print(max)

Submission status: Wrong Answer

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

z_list = []
o_list = []
zero = 0
one = 0
most_l = s[0]
most_r = s[N - 1]
prev = s[0]
if prev == '0':
    zero += 1
else:
    one += 1
s.pop(0)
for c in s:
    if c == '0':
        zero += 1
    else:
        one += 1
    if prev != c:
        if prev == '0':
            z_list.append(zero)
            zero = 0
        else:
            o_list.append(one)
            one = 0
    prev = c
z_list.append(zero)
o_list.append(one)

# print(z_list)
# print(o_list)

z_len = len(z_list)
o_len = len(o_list)
if z_len < 3:
    real_k = K
else:
    real_k = K + 1

sum = 0
for k in range(real_k):
    if k < z_len:
        sum += z_list[k]
    if k < o_len:
        sum += o_list[k]

max = sum

for i in range(N):
    if i + real_k < z_len:
        sum -= z_list[i]
        sum += z_list[i + real_k]
    if i + real_k < o_len:
        sum -= o_list[i]
        sum += o_list[i + real_k]
    if max < sum:
        max = sum

print(max)

Submission status: Wrong Answer

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

if N == 1:
    print(1)
else:
    num_list = []
    wk = 0
    one_flg = SS[0] == "1"
    for i,S in enumerate(SS):
        if one_flg:
            if S == "1":
                wk += 1
            else:
                num_list.append(wk)
                wk = 1
                one_flg = not one_flg
        else:
            if S == "0":
                wk += 1
            else:
                num_list.append(wk)
                wk = 1
                one_flg = not one_flg

    num_list.append(wk)
    
#     print(num_list)
    
    one_flg = SS[0] == "1"
    chk_num = min(K * 2 + 1,len(num_list))
    ans = 0
    for i,num in enumerate(num_list[:-chk_num+1]):
        if i == 0:
            if one_flg:
                ans = sum(num_list[0:min(K * 2 + 1,len(num_list))])
            else:
                ans = sum(num_list[0:min(K * 2,len(num_list))])
        else:
            ans = max(ans,sum(num_list[i:i+chk_num]))
            
#         print(ans)

    print(ans)

Submission status: Wrong Answer

Solution code:
import math, string, itertools, fractions, heapq, collections, re,  array, bisect, sys, random, time, copy, functools
sys.setrecursionlimit(10**7)
inf = 10 ** 20
eps = 1.0 / 10**10
mod = 10**9+7
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]
ddn = [(-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1)]
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def _I(): return int(sys.stdin.readline())
def _F(): return float(sys.stdin.readline())
def pf(s): return print(s, flush=True)

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

"""
逆立ちを連続で何人
まず1を反転させることはないだろう
どの0を選ぶかという問題になる
仮に0が010101...と10**5/2個あったとすると、K個選ぶのを愚直にやると間に合わない
1が一番つながってるところは使いたいみたいな？
もしくは0が一番つながってるところは使いたい？
dpやるなら、何回操作してて、何番目までやってるか
メモ化再起でやりたいい
memo.get('i箇所目でk回反転したとき最長がL')

ある0の場所を反転させる時、そこから離れたところは反転させる必要がないことに着目
計算を無駄にしないようにメモする？

K回指示する場合
K回以下指示はありえない
"""
positions = []
idx = 0
keep = None
while idx < N:
    if S[idx] == '0':
        if keep == None:
            keep = idx
    else:
        # 1の場合は無視
        if keep != None:
            positions.append([keep, idx])
            keep = None
        else:
            # keepしない場合
            keep = None
    idx += 1
else:
    if keep != None:
        positions.append([keep, keep+1])
#  print(positions)
if len(positions) == 0:
    print(N)
    exit()

left = 0
ans = 0
for idx in range(len(positions)):
    left = positions[idx][0]
    if left != 0:
        minus = 1
        while True:
            if left - minus < 0:
                break
            if S[left - minus] == '1':
                minus += 1
            else:
                break
        left -= minus
    # positionから右にk個反転させる
    if idx + K -1 >= len(positions):
        break
    end = positions[idx + K-1][1]
    if end != N:
        a = 0
        while True:
            #  print(end, a)
            if end + a >= N:
                a -= 1
                break
            if S[end + a] == '1':
                a += 1
            else:
                break
        end += a

    ans = max(ans, end - left)
print(ans)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
s=input()
List=[]
i=1
j=1
while i<n:
  if s[i-1]==s[i]:
    j+=1
    i+=1
  else:
    List.append(j)
    i+=1
    j=1
List.append(j)
if s[0]=="0":
  List.insert(0,0)
ans=max(List)
rg=2*k+1
for i in range(0,len(List)-rg+1,2):
  m=sum(List[i:i+rg])
  ans=max(ans,m)
print(ans)

Submission status: Wrong Answer

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

cnt = 0
for i in range(n):
    if s[i] == "1" and cnt == 0:
        ans_0.append(i)
        cnt += 1
    elif s[i] == "0":
        cnt = 0
cnt = 0
for i in range(n):
    if s[n-1 - i] == "1" and cnt == 0:
        ans_1.append(n-1-i)
        cnt += 1
    elif s[n-1 - i] == "0":
        cnt = 0
ans_1.sort()
ans_list = []
if len(ans_0) == 0:
    print(n)
elif len(ans_0) == 1:
    if k == 1:
        print(max(n-ans_0[0], ans_1[0]+1))
    else:
        print(n)
elif len(ans_0)-1 <= k:
    print(n)
else:
    for i in range(len(ans_0) - k):
        ans_list.append(ans_1[i+k] - ans_0[i])
    print(max(ans_list) + 1)

Submission status: Wrong Answer

Solution code:
import sys
N, K = map(int,input().split())
if N <= 2:
    print(N)
    sys.exit()
S = input()
ans = 0
# 0か1の連続する文字数のリストを作る
ll = []
temp = 1
for i in range(1,N):
    if S[i-1] == S[i]:
        temp += 1
    else:
        ll.append(temp)
        temp = 1
    if i == N -1:
        ll.append(temp)
if len(ll) <= 2 * K:
    print(N)
    sys.exit()
# 累積和を作って高速化する
#print(ll)
l2 = [0] * len(ll)
for j in range(len(ll)):
    l2[j] = l2[j-1] + ll[j]
#print(l2)
# 先頭がゼロ→奇数番目がゼロ→どこかのゼロ（奇数番目）から始まって連続する2*K個の数の和の最大値
if S[0] == '0':
    ans = max(l2[:K*2])
    for i in range(len(ll)-(K*2))[K*2::2]:
        ans = max(ans,l2[i+(K*2)]-l2[i])
# 先頭が１→偶数番目がゼロ→どこかの１（奇数番目）から始まって連続する2*K+1個の数の和の最大値
else:
    ans = max(l2[:K*2+1])
    for i in range(len(ll)-(K*2+1))[K*2::2]:
        ans = max(ans,l2[i+(K*2+1)]-l2[i])
    if S[-1] == '1':
        ans = max(ans, l2[-1]-l2[-(2*K+2)])
print(ans)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
s = list(map(int, input()))
count = 0
ans = 0
j = []
x = []
for i in range(len(s)):
    if s[i] == 0:
        x.append(i)
    else:
        j.append(x)
        x = []
m = sorted(j)
try:
    for i in range(k):
        for i in m[-1]:
            s[i] = 1
        del m[-1]
except:
    pass
for i in s:
    if i == 1:
        count += 1
        if ans < count:
            ans = count
    else:
        count = 0

print(ans)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
s = input()
l = []
now = False
nown = 0

for i in range(n):
    if now ^ (s[i] == '1'):
        nown += 1
        continue
    else:
        now = not now
        l.append(nown)
        nown = 1
l.append(nown)
wa=[l[0]]
for i in range(1, len(l)):
    wa.append(wa[i - 1] + l[i])

print('wa =',wa)

ans = 0
if len(wa) < 2 * k:
    ans = n
elif wa[0] == 0:
    for i in range(len(wa) - 2 * k):
        if i % 2 == 1:
            tmp=wa[i + k * 2] - wa[i]+ 1
            ans = max(ans, tmp)
        else:
            tmp = wa[i + k * 2] - wa[i]
            ans = max(ans, tmp)
        print(i,i+k*2,tmp)


else:
    for i in range(len(wa) - 2 * k):
        if i % 2 == 1:
            tmp=wa[i + k * 2] - wa[i]
            ans = max(ans, tmp)
        else:
            tmp = wa[i + k * 2] - wa[i] + 1
            ans = max(ans, tmp)
        print(i,i+k*2,tmp)

# elif  len(wa) % 2 ==1:
# elif wa[0] !=0:
# else:
#     for i in range(1,len(wa) - 2 * k-1):
#         ans = max(ans, wa[i + k * 2+1] - wa[i])
#         print(i,i+k*2+1,ans)

print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import time

N,K = [int(x) for x in input().split()]

t1 = time.time() 
S = input()
a= S[0]
b = int(S[0])
table = [0 for i in range(N)]
c = 0
l = 0
for s in S:
    if int(s) == b:
        c += 1
    else:
        b = not(b)
        table[l]=c
        c = 1
        l+=1

table[l]=c
t2 = time.time() 
if a =='0':
    N = 2*K+1
    ans = sum(table[0:2*K])
    for i in range(1,len(table),2):
        ans = max(ans,sum(table[i:i+N]))

else:
    ans =0
    N = 2*K +1
    for i in range(0,len(table),2):
        ans = max(ans,sum(table[i:i+N]))

t3 = time.time() 
#print(table)
print(ans)
print(t2-t1)
print(t3-t2)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
S = input()
S = "0" + S + "0"
lis = []
for i in range(1, N + 2):
  if S[i - 1] == "0" and S[i] == "1":
    lis.append([i])
  if S[i - 1] == "1" and S[i] == "0":
    lis[-1].append(i - 1)
leng = len(lis)
ans = 0
if S[1] == "1":
  for i in range(leng):
    if i + K < leng:
      newan = lis[i + K][1] - lis[i][0] + 1
      ans = max(ans, newan)
  if leng - K >= 0:
    newans = N - lis[leng - K][0] + 1
    ans = max(ans, newans)
  if leng == 1 and S[N] == "1":
    ans = N
  print(ans)
if S[1] == "0":
  for i in range(leng):
    if i + K < leng:
      newan = lis[i + K][1] - lis[i][0] + 1
      ans = max(ans, newan)
  if leng - K >= 0:
    newans = N - lis[leng - K][0] + 1
    ans = max(newans, ans)
  if K - 1 < leng:
    newans = lis[K - 1][1]
    ans = max(ans, newans)
  print(ans)

Submission status: Wrong Answer

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

ans = 0
now = 0
remain = K
left = 0
right = 0
only = 0
while left < N:
    s = S[left]
    if s == "1":
        now += 1
        left += 1
        only += 1
    else:
        while right < N:
            s_r = S[right]
            if s_r == "1":
                remain -= 1
                only += 1
                now += 1
            else:
                if remain >= 1:
                    only = 0
                    now += 1
                    ans = max(ans, now)
                else:
                    ans = max(ans, now)
                    now = only
                    only = 0
                    remain += 1
                    break
            right += 1
    ans = max(ans, now)
    left += 1

print(ans)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
S = input()
split_list = [0]
section_list = [0]
for i in range(1, N):
    if S[i-1] != S[i]:
        section_list.append(i - split_list[-1])
        split_list.append(i)
split_list.append(N)
section_list.append(split_list[-1] - split_list[-2])
del split_list[0]
del section_list[0]
answer = 0
if len(section_list) <= 2 * K:
    answer= N
else:
    total = sum(section_list[:2 * K + 1])
    if S[0] == '0':
        for i in range(len(section_list) - 2 * K - 1):
            total = total - section_list[i] + section_list[i+2*K+1]
            if i % 2 == 0:
                if total >= answer:
                    answer = total
    if S[0] == '1':
        answer = total
        for i in range(len(section_list) - 2 * K - 1):
            total = total - section_list[i] + section_list[i+2*K+1]
            if i % 2 == 1:
                if total >= answer:
                    answer = total
    # 0から始まるパターン
    total = sum(section_list[:2 * K])
    if total > answer:
        answer = total
print(answer)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-

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

def array_count(arr):
    arr_target = [[]]
    cnt = 1
    for i in range(1,len(arr)):
        if arr[i] == arr[i-1]:
            cnt += 1
        else:
            arr_target.append([arr[i-1],cnt])
            cnt = 1
    arr_target.append([arr[len(arr)-1],cnt])
    del arr_target[0]
    return arr_target

ss = array_count(s)

if ss[0][0] == 0:
    ss = [[1,0]] + ss

if ss[-1][0] == 1:
    ss.append([0,0])

ss_sum = []
temp = 0
for i in range(len(ss)):
    temp += ss[i][1]
    ss_sum.append(temp)

ans = -1
for i in range(len(ss_sum)):
    temp = 0
    if i % 2 == 0:
        if i + k * 2 < len(ss_sum):
            temp = ss_sum[i+k*2] - ss_sum[i]
    else:
        if i + k * 2 + 1 < len(ss_sum):
            temp = ss_sum[i+k*2+1] -  ss_sum[i]
    ans = max(temp,ans)

if k >= int(len(ss) / 2):
    ans = ss_sum[-1]

print(ans)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
s = input()
 
if s[0]== "1":
  patterns = []
else:
  patterns = [0]

last = "1"
cnt = 1
 
for i, l in enumerate(s):
  if l == last:
    cnt += 1
    if i == len(s) - 1:
      patterns.append(cnt)
  else: # 変化があったとき
    last = l
    patterns.append(cnt)
    cnt = 1
    
max_len = 1

#print(patterns)
#偶数番目が１の人たち
for i in range(2*k+1 , len(patterns) + 1, 2):
  length = sum(patterns[i-2*k-1 : i])
  #print(i, length)
  if length >= max_len:
    max_len = length
    
print(max_len)

Submission status: Wrong Answer

Solution code:
import sys

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


tmp1 = s[0]
count = 1
data = []
zeros = 0

for i in range(1,len(s)):
    tmp2 = s[i]
    if tmp1 != tmp2:
        data.append((tmp1, count))
        if tmp1 == '0':
            zeros += 1
        tmp1 = tmp2
        count = 1
    else:
        count += 1

data.append((tmp1,count))
if tmp1 == '0':
    zeros += 1

# print(data)

if k >= zeros:
    print(len(s))
    sys.exit()

ans = 0

if s[0] == '0' and s[-1] == '0':
    anstmp = 0
    for i in range(2*k):
        anstmp += data[i][1]
    
    # print('初期',anstmp)
    ans = max(ans, anstmp)

    for i in range(zeros - k - 1):
        anstmp -= data[2*i][1]
        anstmp -= data[2*i + 1][1]
        anstmp += data[2*k + 2*i][1]
        anstmp += data[2*k + 2*i + 1][1]

        # print('次',anstmp)
        ans = max(ans, anstmp)

    i = (zeros - k - 1)
    anstmp -= data[2*i][1]
    anstmp -= data[2*i + 1][1]
    anstmp += data[2*k + 2*i][1]
    # print('次',anstmp)

    ans = max(ans, anstmp)


if s[0] == '1' and s[-1] == '0':
    anstmp = 0
    for i in range(2*k + 1):
        anstmp += data[i][1]
    
    # print('初期',anstmp)
    ans = max(ans, anstmp)

    for i in range(zeros - k - 1):
        anstmp -= data[2*i][1]
        anstmp -= data[2*i + 1][1]
        anstmp += data[2*k + 2*i + 1][1]
        anstmp += data[2*k + 2*i + 2][1]

        # print('次',anstmp)
        ans = max(ans, anstmp)

    i = (zeros - k - 1)
    anstmp -= data[2*i][1]
    anstmp -= data[2*i + 1][1]
    anstmp += data[2*k + 2*i][1]
    # print('次',anstmp)


if s[0] == '1' and s[-1] == '1':
    anstmp = 0
    for i in range(2*k + 1):
        anstmp += data[i][1]

    # print(anstmp)
    ans = max(ans, anstmp)
    
    for i in range(zeros - k):
        anstmp -= data[2*i][1]
        anstmp -= data[2*i + 1][1]
        anstmp += data[2*k + 2*i + 1][1]
        anstmp += data[2*k + 2*i + 2][1]

        ans = max(ans, anstmp)

if s[0] == '0' and s[-1] == '1':
    anstmp = 0
    for i in range(2*k):
        anstmp += data[i][1]
    
    # print('初期',anstmp)
    ans = max(ans, anstmp)

    i = 0
    anstmp -= data[2*i + 1][1]
    anstmp += data[2*k + 2*i][1]
    anstmp += data[2*k + 2*i + 1][1]

    # print('次',anstmp)
    ans = max(ans, anstmp)
    
    for i in range(1, zeros - k):
        anstmp -= data[2*i][1]
        anstmp -= data[2*i + 1][1]
        anstmp += data[2*k + 2*i][1]
        anstmp += data[2*k + 2*i + 1][1]

        # print('次',anstmp)
        ans = max(ans, anstmp)

print(ans)

Submission status: Wrong Answer

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits

def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7

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

stand = []
reverse = []
state = None
count = 0
tmp = None
for i in S:
	if i == "0":
		state = "stand"
	else:
		state = "reverse"
	if tmp == "stand" and state == "reverse":
		stand.append(count)
		count = 1
	elif tmp == "reverse" and state == "stand":
		reverse.append(count)
		count = 1
	else: # 状態が前と同じ
		count += 1
	tmp = state
if state == "stand":
	stand.append(count)
else:
	reverse.append(count)

if len(stand) == 0:
	print(sum(reverse))
else:
	if K >= len(stand):
		print(sum(stand)+sum(reverse))
	else:
		if len(stand) >= len(reverse):
			reverse.append(0)
			reverse.insert(0, 0)
		# print(stand)
		# print(reverse)
		max_ = 0
		for i in range(max(1, 1+len(reverse)-(K+1))):
			# print(reverse[i:i+K+1],stand[i:i+K])
			tmp = sum(reverse[i:i+K+1])+sum(stand[i:i+K])
			if tmp > max_:
				max_ = tmp
		print(max_)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
li = [int(i) for i in list(input())]
kukanidx = []
idx = 0
for i in li:
    if i != li[idx-1]:
        kukanidx.append(idx)
    idx += 1
idx = 1
kukanidx.append(n)
kukan = kukanidx.copy()
for i in kukanidx[1:]:
    kukan[idx] = kukanidx[idx]-kukanidx[idx-1]
    idx += 1

anskouho = []
idx = 0
for i in kukan:
    anskouho.append(sum(kukan[idx:idx+5]))
    idx += 1
    if idx > len(kukan)-5:
        break

print(max(anskouho))

Submission status: Wrong Answer

Solution code:
# coding: utf-8

import sys

n, k = [int(x) for x in input().split()]
ss = [int(x) for x in input()]

zero_array = []
one_array = []
curr_num = ss[0]
flag = 0

if curr_num == 0:
    zero_array.append(0)
else:
    one_array.append(0)
    flag = 1

for i in ss:
    if curr_num == i:
        if i == 0:
            zero_array[-1] += 1
        else:
            one_array[-1] += 1
    else:
        curr_num = i
        if i == 0:
            zero_array.append(1)
        else:
            one_array.append(1)

if k >= len(zero_array):
    print(sum(zero_array) + sum(one_array))
    sys.exit()
else:
    t = 0
    tmp = sum(zero_array[t: t+k]) + sum(one_array[t: t+k+flag])
    max_count = tmp
    # print(tmp)
    while t + k + flag < len(one_array) and t + k <= len(zero_array):
        if t+k+flag < len(one_array):
            tmp += zero_array[t+k] - zero_array[t] + one_array[t+k+flag] - one_array[t+flag-1]
        else:
            tmp += zero_array[t+k] - zero_array[t] - one_array[t+flag-1]
        # print(tmp)
        if tmp > max_count:
            max_count = tmp
            # print(t, max_count)
        t += 1

    print(max_count)
# print(zero_array)
# print(one_array)

Submission status: Wrong Answer

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

arr = []
prv = '0'
cnt = 0
for c in s:
    if c == prv:
        cnt += 1
    else:
        arr.append(cnt)
        prv = c
        cnt = 1
arr.append(cnt)
if prv == '1':
    arr.append(0)
cur = 0
for i in range(min(k, len(arr) // 2)):
    cur += arr[2 * i]
    cur += arr[2 * i + 1]
ans = cur
for i in range(0, len(arr), 2):
    if len(arr) <= i + 2 * k + 1:
        break
    if 0 < i:
        cur -= arr[i - 1]
    cur -= arr[i]
    cur += arr[i + 2 * k]
    cur += arr[i + 2 * k + 1]
    ans = max(ans, cur)
print(ans)

Submission status: Wrong Answer

Solution code:

a=[0]*(10**5+8)

N,K=map(int,(input().split()))
S=str(input())
s=len(S)
k=1
a[1]=1
for i in range(s-1):
    if S[i] == S[i+1]:
            a[k]+=1
    else:
            a[k+1]+=1
            k+=1
b=0
d=0
KK=min((10**5)//2+1,K)
if S[0]=="0":

    for j in range(10**5//2+1-K):
        c=0
        for p in range(2*KK+1):

            c+=a[p+2*j]
            d=max(d,c)
else:
    for j in range(10**5//2+1-K):
        c=0
        for p in range(2*KK+1):
            c+=a[p+2*j+1]
            d=max(d,c)
print(d)

Submission status: Wrong Answer

Solution code:
from functools import reduce
import math

def main():
    # N! を求める
    # f = math.factorial(N)
    
    # 切り捨て
    # 4 // 3
    # 切り上げ
    #-(-4 // 3)
    
    # 初期値用:十分大きい数(100億)
    # 1e10

    # 初期値用:十分小さい数(-100億)
    # -1e10
    
    # 1文字のみを読み込み
    # 入力:2
    # a = input().rstrip()
    # 変数:a='2'
    
    # スペース区切りで標準入力を配列として読み込み
    # 入力:2 4 5 7
    # a, b, c, d = (int(_) for _ in input().split())  
    # 変数:a=2 b=4 c=5 d =7
    
    # 1文字ずつ標準入力を配列として読み込み
    # 入力:2 4 5 7
    # a = list(int(_) for _ in input().split())
    # 変数:a = [2, 4, 5, 7]    

    # 1文字ずつ標準入力を配列として読み込み
    # 入力:2457
    # a = list(int(_) for _ in input())
    # 変数:a = [2, 4, 5, 7]    
    N,K = (int(_) for _ in input().split())
    S = list(int(_) for _ in input())
    l = []
    l_index = -1
    ans = 0
    max_1 = 0
    temp_max_1 = 0
    K_count = 0
    K_state = False
    for i in range(len(S)):
        if S[i] == 1:
            temp_max_1 += 1
            K_state = False
        elif S[i] == 0:
            if(K_state == True):
                temp_max_1 += 1
                l[l_index] += 1
            elif(K_count<K and K_state == False):
                K_count += 1
                K_state = True
                temp_max_1 += 1
                l_index +=1
                l.append(1)
            elif(K_count == K and K_state == False):
                ans = max(temp_max_1,ans)
                temp_max_1 = temp_max_1 - l[0]
    ans = max(temp_max_1,ans)
    print(ans)
    
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
s = input()
if s.count('0')==0:
    ans = 0
else:
    l = []
    p = 0
    for i in range(len(s)-1):
        if s[i]!= s[i+1]:
            l.append(i+1-p)
            p = i+1
    l.append(len(s)-p)
    ans = 0
    for i in range(len(l)-2):
        if s[0]=='0':
            if i==0:
                ans = max(ans, sum(l[i:i+K*2]))
            else:
                ans = max(ans, sum(l[i:i+K*2+1]))
        else:
            ans = max(ans, sum(l[i:i+K*2+1]))
print(ans)

Submission status: Wrong Answer

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


def read_ints():
    return list(map(int, read().split()))


def read_a_int():
    return int(read())


def read_matrix(H):
    '''
    H is number of rows
    '''
    return [list(map(int, read().split())) for _ in range(H)]


def read_map(H):
    '''
    H is number of rows
    文字列で与えられた盤面を読み取る用
    '''
    return [read() for _ in range(H)]


def read_col(H, n_cols):
    '''
    H is number of rows
    n_cols is number of cols

    A列、B列が与えられるようなとき
    '''
    ret = [[] for _ in range(n_cols)]
    for _ in range(H):
        tmp = list(map(int, read().split()))
        for col in range(n_cols):
            ret[col].append(tmp[col])

    return ret


N, K = read_ints()
S = read()

sum_0 = [0]
# prev = S[0]
for s, s_next in zip(S[:-2], S[1:-1]):
    if s == s_next:
        sum_0.append(sum_0[-1])
        continue

    if s == '1' and s_next == '0':
        sum_0.append(sum_0[-1] + 1)
        continue

    sum_0.append(sum_0[-1])


def get_1length(left, right):
    return len()


left = 0  # 左端初期値
ans = 1  # 解初期値
right = 0

# right-leftが答え
# sum_0[right]-sum0[left]がKを超えたらleftを進める

# しゃくとり法で溶けそうでは？
for left in range(N):
    if sum_0[right] - sum_0[left] > K:
        continue

    while sum_0[right] - sum_0[left] <= K:
        if right == N - 1:
            break
        right += 1

    ans = max(ans, right - left)  # 大きい方に更新

print(ans)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-

def readInts():
  return [int(s) for s in input().split(" ")]

class Node:
  def __init__(self, type, size):
    self.type = type
    self.size = size
    self.left = None
    self.right = None

  def sizeRight(self):
    if self.type == '0':
      if self.right:
        return self.size + self.right.size
      else:
        return self.size
    else:
      if self.right:
        return self.size + self.right.sizeRight()
      else:
        return self.size

  def sizeLeft(self):
    if self.type == '0':
      if self.left:
        return self.size + self.left.size
      else:
        return self.size
    else:
      if self.left:
        return self.size + self.left.sizeLeft()
      else:
        return self.size

  def unionRight(self):
    if self.right is None:
      return

    if self.type == '1':
      self.right.unionRight()

    nextRight = self.right.right
    self.size += self.right.size
    self.right = nextRight

  def unionLeft(self):
    if self.left is None:
      return

    if self.type == '1':
      self.left.unionLeft()

    nextLeft = self.left.left
    self.size += self.left.size
    self.left = nextLeft

def solve(N, K, S):
  chunks = []
  left = 0
  for i, c in enumerate(S):
    if S[left] != c:
      chunks.append( ((left, i - 1), S[left]) )
      left = i
  chunks.append( ((left, len(S) - 1), S[left]) )

  nodes = [Node(c, rs[1] - rs[0] + 1) for rs, c in chunks]
  for i in range(len(nodes)):
    if i > 0:
      nodes[i].left = nodes[i-1]
    if i < len(nodes) - 1:
      nodes[i].right = nodes[i+1]

  if len(nodes) == 1 and nodes[0].type == '1':
    return nodes[0].size

  maxNode = None
  maxSize = -1
  for node in nodes:
    sr = node.sizeRight()
    if sr > maxSize:
      maxSize = sr
      maxNode = node

  while K > 0 and (maxNode.left or maxNode.right):
    if maxNode.sizeRight() > maxNode.sizeLeft():
      maxNode.unionRight()
    else:
      maxNode.unionLeft()
    maxNode.type = '1'
    K -= 1

  return maxNode.size

def main():
  N, K = readInts()
  S = input()

  print(solve(N, K, S))

if __name__ == "__main__":
  main()

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
s=list(input())
st=[]
en=[]
if s[0]=="1":
    st.append(0)
for i in range(1,n):
    if s[i-1]=="0" and s[i]=="1":
        st.append(i)
    if s[i-1]=="1" and s[i]=="0":
        en.append(i)
if s[n-1]=="1":
    en.append(n-1)
ans=0
if len(en)+1<=k:
    print(n)
    exit()
if len(en)<=k and s[0]=="1":
    print(n)
    exit()
if len(en)<=k and s[n-1]=="1":
    print(n)
    exit()
if len(en)-1<=k and s[n-1]=="1" and s[0]=="1":
    print(n)
    exit()
    
if len(en)<=k:
    k=len(en)
for i in range(len(en)-k):
    ans=max(ans,en[i+k]-st[i]+1)
if s[0]=="0":
    ans=max(ans,en[k-1])
if s[n-1]=="0":
    ans=max(ans,n-st[len(st)-k]+1)
print(ans)

Submission status: Wrong Answer

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

ans = 0
now = 0
remain = K
left = 0
right = 0
only = 0
while left < N:
    s = S[left]
    if s == "1":
        now += 1
        left += 1
        only += 1
    else:
        while right < N:
            s_r = S[right]
            if s_r == "1":
                remain -= 1
                only += 1
                now += 1
            else:
                if remain >= 1:
                    only = 0
                    now += 1
                else:
                    ans = max(ans, now)
                    now = only
                    only = 0
                    remain += 1
                    break
            right += 1
    ans = max(ans, now)
    left += 1

print(ans)

Submission status: Wrong Answer

Solution code:
import math
import functools
import itertools
import numpy as np
import sys
MAX_INT = int(10e10)
MIN_INT = -MAX_INT
mod = 1000000007
sys.setrecursionlimit(1000000)
def IL(): return list(map(int,input().split()))
def SL(): return input().split()
def I(): return int(sys.stdin.readline())
def S(): return input()

N,K = IL()
s = S()

l = []

if s.count("1")==N:
    print(N)
else:
    cnt = 1
    for i in range(N):
        if s[i] == "1":
            cnt += 1
        else:
            if i != N-1:
                if s[i+1]=="0":
                    l.append([cnt,0])
                else:
                    l.append([cnt,-1])
            else:
                l.append([cnt,-1])
            cnt = 1
    else:
        l.append([cnt,-1])

    num = []

    tmp = 0
    ccnt = 0
    nn = 0
    ans = 0
    for i in range(tmp,len(l)-1):
        if l[i][1]==-1:
            ccnt += 1
        nn += l[i][0]
        if ccnt == K:
            nn += l[i+1][0] -1
            ans = max(ans,nn)
            tmp += 1
            nn = 0
            ccnt = 0
    print(ans)