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:
# -*- coding: utf-8 -*-
import bisect
import heapq
import math
import random
import sys
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations
from operator import add, mul, sub

sys.setrecursionlimit(100000)


def read_int():
    return int(input())


def read_int_n():
    return list(map(int, input().split()))


def read_float():
    return float(input())


def read_float_n():
    return list(map(float, input().split()))


def read_str():
    return input().strip()


def read_str_n():
    return list(map(str, input().split()))


def error_print(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.time()
        ret = f(*args, **kwargs)
        e = time.time()

        error_print(e - s, 'sec')
        return ret

    return wrap


@mt
def slv(N, K, S):
    s1 = list(map(lambda x: len(x), filter(lambda x: x != '', S.split('0'))))
    s0 = list(map(lambda x: len(x), filter(lambda x: x != '', S.split('1'))))
    if S[0] == '0':
        s1.insert(0, 0)
    if S[-1] == '0':
        s1.append(0)
    s = []
    i = 0
    for i in range(len(s0)):
        s.append(s1[i])
        s.append(s0[i])
    s.extend(s1[i+1:])

    sa = [0]
    for v in s:
        sa.append(sa[-1]+v)

    ans = 0
    for i in range(0, len(sa)-(2*K+1), 2):
        ans = max(ans, sa[i+2*K+1]-sa[i])

    return ans


def main():
    N, K = read_int_n()
    S = read_str()
    rS = ''.join(['0' if c == '1' else '1' for c in S])
    print(max(slv(N, K, S), slv(N, K-1, rS)))

    # N = 20
    # K = random.randint(1, 10)
    # S = ''.join(random.choices('01', k=N))
    # rS = ''.join(['0' if c == '1' else '1' for c in S])
    # print(max(slv(N, K, S), slv(N, K-1, rS)))

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# https://atcoder.jp/contests/abc124/submissions/4948803 さんの回答を写経
from itertools import accumulate


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

    cnts = []
    prev = '1'
    cnt = 0  # 連続した要素の数のカウント用

    # 1始まり0終わりで出現回数をカウント
    for c in s:
        if prev == c:
            cnt += 1
        else:
            cnts.append(cnt)
            cnt = 1
        prev = c
    cnts.append(cnt)
    if prev == '0':
        cnts.append(0)

    # 反転し得る範囲の大きさ: k=2なら4つまたは5つの連続した部分の中の0が連続した部分を反転し得る
    w = 2 * k + 1

    # 0の連続した部分の数がk個以下 -> すべての0を反転できるからnが答え
    if len(cnts) <= w:
        return n

    # 累積数を計算
    acc = [0] + list(accumulate(cnts))
    ans = 0
    # 0, 1の順番に並んでいるので2個刻みで数を数える
    # ループの終了条件をどう決めたかが分からない
    for i in range(0, len(cnts) - w + 1, 2):
        # k個反転したときの長さをansと比較して最大値を取る
        # 反転し得る範囲の長さはwとして求めているので、acc[i+w] - acc[i]で反転込みの連続した1の長さが求められる
        ans = max(ans, acc[i+w] - acc[i])


# NOTE 正回答にならないように余分な文字列を付与
print("ans = ", solve())

Submission status: Wrong Answer

Solution code:
import sys
stdin = sys.stdin 
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline()
N,K=na();S=ns()
l=[]
N=len(S)
if(S[0]=="0"):
    l.append(0)
b=S[0]
ct=1
for i in S[1:]:
    if(b!=i):
        l.append(ct)
        ct=1
        b=i
    else:
        ct+=1

if(ct):
    l.append(ct)
len_l=len(l)
if(N//2 < K):
    print(N)
    exit()

if(len_l%2==0):
    l.append(0)
    len_l+=1
ans = sum(l[:2*K+1])
res=0
for i in range(0,len_l,2):
    res = max(res, sum(l[i:2*K+1+i]))
#print(l)
print(res)

Submission status: Wrong Answer

Solution code:
import itertools

n,k = map(int, input().split())
s_str = input()
s = []
for i in s_str:
    s.append(int(i))

s_encoded = []
now = 0
tmp = 0
for i in range(len(s)):
    if i == 0:
        if s[0] == 1:
            s_encoded.append(0)
            now = 1
        else:
            pass
    
    if s[i]==now:
        tmp=tmp+1
    else:
        s_encoded.append(tmp)
        tmp=1
        now = 1 - now
                
    if i==len(s)-1:
        s_encoded.append(tmp)
        
sum_before = []
sum_tmp=0
for i in range(len(s_encoded)):
    sum_tmp = sum_tmp+s_encoded[i]
    sum_before.append(sum_tmp)
#print(s_encoded)
#print(sum_before)
max_line=1
left = 0
right = left +  2*k - (left+1)%2
while right<=len(sum_before)-1:
    line = sum_before[right] - sum_before[left-1]
    if max_line<line:
        max_line=line
    left = left + 1
    right = left +  2*k - (left+1)%2
print(max_line)

Submission status: Wrong Answer

Solution code:
from sys import stdin
from collections import deque

N, K = [int(x) for x in stdin.readline().rstrip().split()]
S = deque([int(x) for x in stdin.readline().rstrip()])

# 連続して立っている人のカウント
straight_stand_cnt = 0

l = 0
r = 0
ans = 1

while r < N-1:
    # for DEBUG
    #print(l, r)
    if straight_stand_cnt <= K:
        if S[r] == 0 and S[r+1] == 1:
            straight_stand_cnt += 1

        ans = max(ans, r-l+1)
        r += 1
    else:
        if S[l] == 0 and S[l+1] == 1:
            straight_stand_cnt -= 1
        l += 1      


print(ans)

Submission status: Wrong Answer

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

arr = []
cnt = 0
comp = S[0]
for s in S:
    if s==comp:
        cnt += 1
    else:
        arr.append(cnt)
        cnt = 1
        comp = s
arr.append(cnt)

if len(arr)<3:
    print(sum(arr))
else:
    L = (len(arr)+1)//2 - K
    if S[0]=='1':
        l = 0
        cand = sum(arr[2*l:2*(l+K)+1])
        cands = [cand]
        for l in range(1, L):
            cand = cand - (arr[2*(l-1)]+arr[2*l-1]) + (arr[2*(l-1+K)+1]+arr[2*(l+K)])
            cands.append(cand)
    else:
        l = 0
        cand = sum(arr[2*l:2*(l+K)])
        cands = [cand]
        for l in range(1, L):
            cand = cand - (arr[2*(l-1)]+arr[2*l-1]) + (arr[2*(l-1+K)]+arr[2*(l+K)-1])
    print(max(cands))

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
S = [int(i) for i in list(input())]
S.append(9)
z = []
o = []
tmp = 0
for i in range(N):
    tmp += 1
    if S[i] == 0 and S[i+1] != 0:
        z.append(tmp)
        tmp = 0
    elif S[i] == 1 and S[i+1] != 1:
        o.append(tmp)
        tmp = 0

ans = [0]
for i in range(len(o)):
    ans.append(sum(o[i:i+K+1]) + sum(z[i:i+K]))
print(max(ans))

Submission status: Wrong Answer

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

arr = []
cnt = 0
comp = S[0]
for s in S:
    if s==comp:
        cnt += 1
    else:
        arr.append(cnt)
        cnt = 1
        comp = s
arr.append(cnt)

if len(arr)<3:
    print(sum(arr))
else:
    cands = []
    L = len(arr)//2
    if S[0]=='1':
        for l in range(L):
            cands.append(sum(arr[2*l:min(2*(l+K)+1, len(arr))]))
    else:
        for l in range(L):
            cands.append(sum(arr[2*l:min(2*(l+K), len(arr))]))
    print(max(cands))

Submission status: Wrong Answer

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

arr = []
cnt = 0
comp = S[0]
for s in S:
    if s==comp:
        cnt += 1
    else:
        if comp=='0':
            arr.append(-cnt)
        else:
            arr.append(cnt)
        cnt = 1
        comp = s
arr.append(cnt)

if len(arr)<3:
    print(sum(arr))
else:
    cands = []
    L = len(arr)//2
    if S[0]=='1':
        for l in range(L):
            cands.append(sum(arr[2*l:min(2*(l+K)+1, len(arr))]))
    else:
        for l in range(L):
            cands.append(sum(arr[2*l:min(2*(l+K), len(arr))]))
    print(max(cands))

Submission status: Wrong Answer

Solution code:
import numpy as np

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

arr0 = np.array(list(map(len, s.split('1'))))
arr0 = arr0[arr0!=0]

arr1 = np.array(list(map(len, s.split('0'))))
arr1 = arr1[arr1!=0]
arr1 = np.concatenate(([0], arr1))

ans = 0
for i in range(len(arr0)):

    length = arr0[i:i+k].sum() + arr1[i:i+k+1].sum()
    ans = max(ans, length)

    if i+k > len(arr0):
        break

if len(s) == s.count('1'):
    print(len(s))
else:
    print(ans)

Submission status: Wrong Answer

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

arr = []
cnt = 0
comp = S[0]
for s in S:
    if s==comp:
        cnt += 1
    else:
        if comp=='0':
            arr.append(-cnt)
        else:
            arr.append(cnt)
        cnt = 1
        comp = s
if comp=='0':
    arr.append(-cnt)
else:
    arr.append(cnt)

if len(arr)==2:
    print(abs(arr[0]+abs(arr[1])))
elif len(arr)==1:
    print(arr[0])
else:

    if arr[0]>0:
        cands = []
        L = len(arr)//2
        for l in range(L):
            sub = arr[2*l:min(2*(l+K)+1, len(arr))]
            tot = sum([abs(su) for su in sub])
            cands.append(tot)
    else:
        cands = []
        L = len(arr)//2
        for l in range(L):
            sub = arr[2*l:min(2*(l+K), len(arr))]
            tot = sum([abs(su) for su in sub])
            cands.append(tot)
    print(max(cands))

Submission status: Wrong Answer

Solution code:
N, K = [int(x) for x in input().split()]
S = [int(x) for x in list(str(input()))]
 
S_list = []
 
s_b = S[0]
cnt = 1
for i in range(1,N):
  s_n = S[i]
  if s_n == s_b:
    cnt += 1
  else:
    S_list.append(cnt)
    cnt = 1
  s_b = s_n
  
S_list.append(cnt)
 
if len(S_list) - 2 * K <= 0:
  print(len(S))
  exit()
  
cand_list = []
if S[0] == 1:
  for idx in range(0, len(S_list)-K, 2):
    cand_list.append(sum(S_list[idx:idx+2*K+1]))

if S[0] == 0:
  for idx in range(1, len(S_list)-K, 2):
    cand_list.append(sum(S_list[idx:idx+2*K+1]))
    
print(max(cand_list))

Submission status: Wrong Answer

Solution code:
N, K = [int(x) for x in input().split()]
S = [int(x) for x in list(str(input()))]
 
S_list = []
 
s_b = S[0]
cnt = 1
for i in range(1,N):
  s_n = S[i]
  if s_n == s_b:
    cnt += 1
  else:
    S_list.append(cnt)
    cnt = 1
  s_b = s_n
  
S_list.append(cnt)
 
if len(S_list) - 2 * K <= 0:
  print(len(S))
  exit()
  
cand_list = []
if S[0] == 1:
  for idx in range(0, len(S_list)-K, 2):
    cand_list.append(sum(S_list[idx:idx+2*K+1]))

if S[0] == 0:
  for idx in range(0, len(S_list)-K, 2):
    cand_list.append(sum(S_list[idx:idx+2*K]))
    
print(max(cand_list))

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Your code here!
N, K = [int(_) for _ in input().split()]
S = str(input())

Nums = []
now, cnt = 1, 0
for i in range(N):
    if S[i] == str(now): cnt += 1
    else:
        Nums.append(cnt)
        now = 1 - now
        cnt = 1
if cnt != 0: Nums.append(cnt)
if len(Nums) % 2 == 0: Nums.append(0)


# add = 2 * K + 1
# ans = 0
# for i in range(0, len(Nums), 2):
#     tmp = 0
#     left = i
#     right = min(i+add, len(Nums))
#     j = left
#     while j < right:
#         tmp += Nums[j]
#         j += 1
#     ans = max(tmp, ans)
# print(ans)

#------------------------
add = 2 * K + 1
ans = 0
left = 0
right = 0
tmp = 0
for i in range(len(Nums)):
    nextLeft = i 
    nextRight = min(i + add, len(Nums))
    while nextLeft > left:
        tmp -= Nums[left]
        left += 1
    while nextRight > right:
        tmp += Nums[right]
        right += 1
    ans = max(ans, tmp)
        
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()

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()
elif len(positions) == K:
    print(N)
    exit()

ans = 0

for idx in range(len(positions)):
    if idx + K - 1 > len(positions) - 1:
        break
    if idx == 0:
        left = 0
    else:
        left = positions[idx-1][1]

    if idx + K - 1 == len(positions)-1:
        end = N
    else:
        end = positions[idx+K-1+1][0]
    #  print(end, left)
    ans = max(ans, end - left)
print(ans)

Submission status: Wrong Answer

Solution code:
import sys


def solve(lines):
    N, K = lines[0].split()
    N, K = int(N), int(K)
    S = lines[1]
    lens = []
    if S[0] == '0':
        lens.append(0)
        prev = '0'
    else:
        prev = '1'
    count = 0
    for c in S:
        if prev == c:
            count += 1
        else:
            prev = c
            lens.append(count)
            count = 1
    lens.append(count)

    L = 2 * K + 1
    max_sum = sum(lens[: L])
    cur_sum = max_sum
    for i in range(L, len(lens)):
        cur_sum -= lens[i - L]
        cur_sum += lens[i]
        if i % 2 == 0:
            max_sum = max(max_sum, cur_sum)

    print(max_sum)


if __name__ == '__main__':
    lines = sys.stdin.read()
    lines = lines.strip().split('\n')
    solve(lines)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
S = input()
Nums = [0]
now = 1 # 今見ている数
cnt = 0 # nowがいくつ並んでいるか
for i in range(N):
    if S[i] == str(now):
        cnt += 1
    else:
        Nums.append(cnt)
        now = 1 - now   # 0と1を切り替えるときの計算 now ^= 1
        cnt = 1
if cnt != 0:
    Nums.append(cnt)
# 1-0-1-0-1-0-1 って感じの配列が欲しい
# 1-0-1-0-1-0 みたいに0で終わっていたら適当に1つ足す
if len(Nums) % 2 == 0:
    Nums.append(0)

Add = 2 * K + 1
ans = 0
# 1-0-1... の1から始めるので、偶数番目だけ見る
for i in range(0, len(Nums), 2):
    tmp = 0
    left = i
    right = min(i + Add, len(Nums))
    for j in range(left, right):
        tmp += Nums[j]
    ans = max(tmp, 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()

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()
elif len(positions) == K:
    print(N)
    exit()

ans = 0

for idx in range(len(positions)):
    if idx + K - 1 > len(positions) - 1:
        break
    if idx == 0:
        left = 0
    elif idx == len(positions) - 1:
        left = N
    else:
        left = positions[idx-1][1]

    if idx + K - 1 == len(positions)-1:
        end = N
    else:
        end = positions[idx+K-1+1][0]
    #  print(end, left)
    ans = max(ans, end - left)
print(ans)

Submission status: Wrong Answer

Solution code:
import sys


def solve(lines):
    N, K = lines[0].split()
    N, K = int(N), int(K)
    S = lines[1]
    lens = []
    if S[0] == '0':
        lens.append(0)
        prev = '0'
    else:
        prev = '1'
    count = 0
    for c in S:
        if prev == c:
            count += 1
        else:
            prev = c
            lens.append(count)
            count = 1
    lens.append(count)

    L = 2 * K + 1
    max_sum = sum(lens[: L])
    cur_sum = max_sum
    for i in range(L, len(lens)):
        print(i, cur_sum)
        cur_sum -= lens[i - L]
        cur_sum += lens[i]
        if i % 2 == 0:
            max_sum = max(max_sum, cur_sum)

    print(max_sum)


if __name__ == '__main__':
    lines = sys.stdin.read()
    lines = lines.strip().split('\n')
    solve(lines)

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

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

ans = 0

for idx in range(len(positions)):
    if idx + K - 1 > len(positions):
        break
    if idx == 0:
        left = 0
    elif idx == len(positions) - 1:
        left = N
    else:
        left = positions[idx-1][1]

    if idx + K - 1 == len(positions)-1:
        end = N
        break
    else:
        end = positions[idx+K-1+1][0]
    ans = max(ans, end - left)
print(ans)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
S = [int(_) for _ in input()]

now = 1
cnt = 0
one_zero = []
for i in S:
    if i == now:
        cnt += 1
    else:
        one_zero.append(cnt)
        now = 1 - now
        cnt += 1
if (cnt != 0):
    one_zero.append(cnt)

if(len(one_zero) % 2 == 0):
    one_zero.append(cnt)

add = 2 * K + 1
ans = 0
for i in range(0,len(one_zero)+1,2):
    right = min(i+add, len(one_zero))-1
    ans = max(ans, one_zero[right] - one_zero[i])

print(ans)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
S = [int(_) for _ in input()]

now = 1
cnt = 0
one_zero = []
for i in S:
    if i == now:
        cnt += 1
    else:
        one_zero.append(cnt)
        now = 1 - now
        cnt = 1
if (cnt != 0):
    one_zero.append(cnt)

if(len(one_zero) % 2 == 0):
    one_zero.append(cnt)

add = 2 * K + 1
ans = 0
for i in range(0,len(one_zero)+1,2):
    tmp = 0
    right = min(i+add, len(one_zero))
    ans = max(ans, sum(one_zero[i:right]))

print(ans)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
S = [int(_) for _ in input()]

now = 1
cnt = 0
one_zero = []
for i in S:
    if i == now:
        cnt += 1
    else:
        one_zero.append(cnt)
        now = 1 - now
        cnt = 1
if (cnt != 0):
    one_zero.append(cnt)

if(len(one_zero) % 2 == 0):
    one_zero.append(cnt)

add = 2 * K + 1
ans = 0
for i in range(0,len(one_zero),2):
    tmp = 0
    right = min(i+add, len(one_zero))
    ans = max(ans, sum(one_zero[i:right]))

print(ans)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
S = [int(_) for _ in input()]

now = 1
cnt = 0
one_zero = []
for i in S:
    if i == now:
        cnt += 1
    else:
        one_zero.append(cnt)
        now = 1 - now
        cnt = 1
if (cnt != 0):
    one_zero.append(cnt)

if(len(one_zero) % 2 == 0):
    one_zero.append(cnt)

add = 2 * K + 1
ans = 0
for i in range(0,len(one_zero)-add+1,2):
    tmp = 0
    right = min(i+add, len(one_zero))
    ans = max(ans, sum(one_zero[i:right]))

print(ans)

Submission status: Wrong Answer

Solution code:
def main():
    n, k = map(int, input().split())
    s = input()
    
    arr = []
    if s[0] == '0':
        arr.append(0)
    cnt = 1
    i = 1
    while i != n:
        if s[i] != s[i - 1]:
            arr.append(cnt)
            cnt = 1
        else:
            cnt += 1
        i += 1
    arr.append(cnt)
    if s[-1] == '0':
        arr.append(0)
    
    ans = 0
    curr = arr[0]
    j = 1
    for i in range(1, len(arr), 2):
        curr += arr[i] + arr[i + 1]
        if i - j == 2 * k:
            curr -= arr[j] + arr[j - 1]
            j += 2
        ans = max(ans, curr)
    print(ans)
    return 0
main()

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
S = [int(_) for _ in input()]

now = 1
cnt = 0
one_zero = []
for i in S:
    if i == now:
        cnt += 1
    else:
        one_zero.append(cnt)
        now = 1 - now
        cnt = 1
if (cnt != 0):
    one_zero.append(cnt)

if(len(one_zero) % 2 == 0):
    one_zero.append(cnt)

add = 2 * K + 1
ans = 0
for i in range(0,len(one_zero),2):
    tmp = 0
    right = min(i+add, len(one_zero))
    for j in range(i,right):
        tmp += one_zero[j]

    ans = max(ans, tmp)

print(ans)

Submission status: Wrong Answer

Solution code:
def ret_arr(N, K, S):
    a0 = str(S).split('1')
    remove_specified_values(a0, '')
    a1 = str(S).split('0')
    remove_specified_values(a1, '')

    a = [2] * (len(a1) + len(a0))
    if int(S[0]) == 0:
        for i in range(0, len(a0)):
            if i > (len(a0) - 2) and int(S[-1]) == 0:
                a[i * 2] = a0[i]
            else:
                a[i * 2] = a0[i]
                a[i * 2 + 1] = a1[i]
    else:
        for i in range(0, len(a1)):
            if i > (len(a1) - 2) and int(S[-1]) == 1:
                a[i * 2] = a1[i]
            else:
                a[i * 2] = a1[i]
                a[i * 2 + 1] = a0[i]
    return a


def remove_specified_values(arr, value):
    while value in arr:
        arr.remove(value)


def ret_arr_len(arr):
    ret_a = []
    for i in arr:
        ret_a.append(len(i))
    return ret_a


def count(arr, K):
    k = K * 2 + 1
    c_a = []
    for i in range(0, len(arr) - (k - 1)):
        c = 0
        for _k in range(0, k):
            c = c + arr[(i + _k)]
        # print(c)
        c_a.append(c)
    try:
        m = max(c_a)
    except Exception as e:
        m = 0

    return m


def main(N, K, S):
    #print('~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~')
    a = ret_arr(N, K, S)
    len_a = ret_arr_len(a)

    c_a = []
    for i in range(0, K + 1):
        c = count(len_a, i)
        #print(f'{i} : {c}')
        c_a.append(c)

    return max(c_a)

Submission status: Wrong Answer

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

# Initial values
ans=1
l=0
r=0
edge=[]

# 立っているグループのカウント
# 最初の文字が0なら1
if S[l]=='0':
    count=1
else:
    count=0    

# ｒがNまでループ
while(r < N-1):
    r+=1
    #　もし1=>0の境目だと、countを追加
    if ( S[r-1]=='1') & (S[r]=='0'):
        count+=1

    #　countがKを超すとlのループに入る(Break)
    while(count>K):
        l+=1
        # lを更新したとき0=>1の境目だと、l~r間の0グループが減るので、countを減らす
        if (S[l-1]=='0') & (S[l]=='1'):
            count-=1
        #lがrを追い越さないように
        if l==r:
            r+=1
        print(r,l,count,ans)

    #何事もなく 進んだ場合は答えの更新
    ans=max(ans,r-l+1)
    print(r,l,count,ans) 

print(ans)

Submission status: Wrong Answer

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

# Initial values
ans=-1
l=0
r=0
edge=[]

# 立っているグループのカウント
# 最初の文字が0なら1
if S[l]=='0':
    count=1
else:
    count=0    

# ｒがNまでループ
while(r < N-1):
    r+=1
    #　もし1=>0の境目だと、countを追加
    if ( S[r-1]=='1') & (S[r]=='0'):
        count+=1

    #　countがKを超すとlのループに入る(Break)
    while(count>K):
        l+=1
        # lを更新したとき0=>1の境目だと、l~r間の0グループが減るので、countを減らす
        if (S[l-1]=='0') & (S[l]=='1'):
            count-=1
        #lがrを追い越さないように
        if l==r:
            r+=1
        #print(r,l,count,ans)

    #何事もなく 進んだ場合は答えの更新
    ans=max(ans,r-l+1)
    #print(r,l,count,ans) 

print(ans)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
S = "1" + input() + "1"
arr = []
cnt = 0
flg = 1
for s in S:
    if s == "1" and 1 - flg:
        flg = 1
        arr.append(cnt)
        cnt = 1
    elif s == "0" and flg:
        flg = 0
        arr.append(cnt)
        cnt = 1
    else:
        cnt += 1
arr.append(cnt)
csum = [0] * (len(arr)+1)
for i in range(len(arr)):
    csum[i + 1] = csum[i] + arr[i]
ans = 0
tmp = 0
for i in range(0, len(csum) - 2*K - 1, 2):
    l = i
    r = i + 2*K + 1
    tmp = csum[r] - csum[l]
    tmp -= (r == len(csum) - 1) + (l == 0) + (l == 1)
    ans = max(ans, tmp)
if len(csum) == 2:
    print(csum[1] - 2)
else:
    print(ans)

Submission status: Wrong Answer

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

# Initial values
ans=-1
l=0
r=1
edge=[]

# 立っているグループのカウント
# 最初の文字が0なら1
if S[l]=='0':
    count=1
else:
    count=0    

# ｒがNまでループ
while(r < N):

    #　もし1=>0の境目だと、countを追加
    if (S[r] != S[r-1]) & (S[r]=='0'):
        count+=1

    #　countがKを超すと次のrのループに入る(Break)
    while(count>K):
        l+=1
        # lを更新したとき0=>1の境目だと、l~r間の0グループが減るので、countを減らす
        if (S[l]!=S[l-1]) & (S[l]=='1'):
            count-=1
        if l==r:
            r+=1
        print(r,l,count)     

    #何事もなく 進んだ場合は答えの更新
    ans=max(ans,r-l+1)
    #print(r,l,count,ans) 
    r+=1        
       

    
print(ans)

Submission status: Wrong Answer

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

# Initial values
ans=-1
l=0
r=1
edge=[]

# 立っているグループのカウント
# 最初の文字が0なら1
if S[l]=='0':
    count=1
else:
    count=0    

# ｒがNまでループ
while(r < N+1):

    #　もし1=>0の境目だと、countを追加
    if (S[r] != S[r-1]) & (S[r]=='0'):
        count+=1

    #　countがKを超すと次のrのループに入る(Break)
    while(count>K):
        l+=1
        # lを更新したとき0=>1の境目だと、l~r間の0グループが減るので、countを減らす
        if (S[l]!=S[l-1]) & (S[l]=='1'):
            count-=1
        if l==r:
            r+=1
            

    #何事もなく 進んだ場合は答えの更新
    else:
        ans=max(ans,r-l)
        #print(r,l,count) 
        r+=1        
       

    
print(ans)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
S = "1" + input() + "1"
arr = []
cnt = 0
flg = 1
for s in S:
    if s == "1" and 1 - flg:
        flg = 1
        arr.append(cnt)
        cnt = 1
    elif s == "0" and flg:
        flg = 0
        arr.append(cnt)
        cnt = 1
    else:
        cnt += 1
arr.append(cnt)
csum = [0] * (len(arr)+1)
for i in range(len(arr)):
    csum[i + 1] = csum[i] + arr[i]
ans = 0
tmp = 0
for i in range(len(csum) - 2*K - 1):
    l = i
    r = i + 2*K + 1
    tmp = csum[r] - csum[l]
    tmp -= (r == len(csum) - 1) + (l == 0) + (l == 1)
    ans = max(ans, tmp)
if len(csum) == 2:
    print(csum[1] - 2)
else:
    print(ans)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
S = "1" + input() + "1"
arr = []
cnt = 0
flg = 1
for s in S:
    if s == "1" and 1 - flg:
        flg = 1
        arr.append(cnt)
        cnt = 1
    elif s == "0" and flg:
        flg = 0
        arr.append(cnt)
        cnt = 1
    else:
        cnt += 1
arr.append(cnt)
csum = [0] * (len(arr)+1)
for i in range(len(arr)):
    csum[i + 1] = csum[i] + arr[i]
ans = 0
for i in range(len(csum) - 2*K - 1):
    l = i
    r = i + 2*K + 1
    ans = max(ans, csum[r] - csum[l])
    ans -= (r == len(csum) - 1) + (l == 0) + (l == 1)
if len(csum) == 2:
    print(csum[1] - 2)
else:
    print(ans)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
S = "1" + input() + "1"
arr = []
cnt = 0
flg = 1
for s in S:
    if s == "1" and 1 - flg:
        flg = 1
        arr.append(cnt)
        cnt = 1
    elif s == "0" and flg:
        flg = 0
        arr.append(cnt)
        cnt = 1
    else:
        cnt += 1
arr.append(cnt)
csum = [0] * (len(arr)+1)
for i in range(len(arr)):
    csum[i + 1] = csum[i] + arr[i]
ans = 0
for i in range(len(csum) - 2*K - 1):
    l = i
    r = i + 2*K + 1
    ans = max(ans, csum[r] - csum[l])
    ans -= (r == len(csum) - 1) + (l == 0)
if len(csum) == 2:
    print(csum[1] - 2)
else:
    print(ans)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
S = "1" + input() + "1"
arr = []
cnt = 0
flg = 1
for s in S:
    if s == "1" and 1 - flg:
        flg = 1
        arr.append(cnt)
        cnt = 1
    elif s == "0" and flg:
        flg = 0
        arr.append(cnt)
        cnt = 1
    else:
        cnt += 1
arr.append(cnt)
csum = [0] * (len(arr)+1)
for i in range(len(arr)):
    csum[i + 1] = csum[i] + arr[i]
ans = 0
print(csum)
for i in range(len(csum) - 2*K - 1):
    l = i
    r = i + 2*K + 1
    ans = max(ans, csum[r] - csum[l])
    ans -= (r == len(csum) - 1) + (l == 0)
if len(csum) == 2:
    print(csum[1] - 2)
else:
    print(ans)

Submission status: Wrong Answer

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

# Initial values
ans=-1
l=0
r=1



for l in range(N):

    # 立っているグループのカウント
    # 最初の文字が0 => 1
    if S[l]=='0':
        count=1
    else:
        count=0    
    
    # ｒがlの次からNまでループ
    for r in range(l+1,N+1):

        #　もし1=>0の境目だと、countを追加
        if (S[r] != S[r-1]) & (S[r]=='0'):
            count+=1

            #　countがKを超すと次のrのループに入る(Break)
            if count>K:

                # lを更新したとき0=>1の境目だと、l~r間の0グループが減るので、countを減らす
                #if (S[l]!=S[l-1]) & (S[i]=='1'):
                #    count-=1
                break

        #何事もなく 進んだ場合は答えの更新
        #print(r,l)
        ans=max(ans,r-l)

print(ans)