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 -*-

#############
# Libraries #
#############

import sys
input = sys.stdin.readline

import math
#from math import gcd
import bisect
from collections import defaultdict
from collections import deque
from functools import lru_cache


#############
# Constants #
#############

MOD = 10**9+7
INF = float('inf')

#############
# Functions #
#############

######INPUT######
def I(): return int(input().strip())
def S(): return input().strip()
def IL(): return list(map(int,input().split()))
def SL(): return list(map(str,input().split()))
def ILs(n): return list(int(input()) for _ in range(n))
def SLs(n): return list(input().strip() for _ in range(n))
def ILL(n): return [list(map(int, input().split())) for _ in range(n)]
def SLL(n): return [list(map(str, input().split())) for _ in range(n)]

######OUTPUT######
def P(arg): print(arg); return
def Y(): print("Yes"); return
def N(): print("No"); return
def E(): exit()
def PE(arg): print(arg); exit()
def YE(): print("Yes"); exit()
def NE(): print("No"); exit()

#####Shorten#####
def DD(arg): return defaultdict(arg)


#####Inverse#####
def inv(n): return pow(n, MOD-2, MOD)

######Combination######
kaijo_memo = []
def kaijo(n):
  if(len(kaijo_memo) > n):
    return kaijo_memo[n]
  if(len(kaijo_memo) == 0):
    kaijo_memo.append(1)
  while(len(kaijo_memo) <= n):
    kaijo_memo.append(kaijo_memo[-1] * len(kaijo_memo) % MOD)
  return kaijo_memo[n]

gyaku_kaijo_memo = []
def gyaku_kaijo(n):
  if(len(gyaku_kaijo_memo) > n):
    return gyaku_kaijo_memo[n]
  if(len(gyaku_kaijo_memo) == 0):
    gyaku_kaijo_memo.append(1)
  while(len(gyaku_kaijo_memo) <= n):
    gyaku_kaijo_memo.append(gyaku_kaijo_memo[-1] * pow(len(gyaku_kaijo_memo),MOD-2,MOD) % MOD)
  return gyaku_kaijo_memo[n]

def nCr(n,r):
  if(n == r):
    return 1
  if(n < r or r < 0):
    return 0
  ret = 1
  ret = ret * kaijo(n) % MOD
  ret = ret * gyaku_kaijo(r) % MOD
  ret = ret * gyaku_kaijo(n-r) % MOD
  return ret

######Factorization######
def factorization(n):
  arr = []
  temp = n
  for i in range(2, int(-(-n**0.5//1))+1):
    if temp%i==0:
      cnt=0
      while temp%i==0: 
        cnt+=1 
        temp //= i
      arr.append([i, cnt])
  if temp!=1:
    arr.append([temp, 1])
  if arr==[]:
    arr.append([n, 1])
  return arr

#####MakeDivisors######
def make_divisors(n):
  divisors = []
  for i in range(1, int(n**0.5)+1):
    if n % i == 0:
      divisors.append(i)
      if i != n // i: 
        divisors.append(n//i)
  return divisors

#####GCD#####
def gcd(a, b):
    while b:
        a, b = b, a % b
    return a

#####LCM#####
def lcm(a, b):
    return a * b // gcd (a, b)

#####BitCount#####
def count_bit(n):
  count = 0
  while n:
    n &= n -1
    count += 1
  return count

#####ChangeBase#####
def base_10_to_n(X, n):
  if X//n:
    return base_10_to_n(X//n, n)+[X%n]
  return [X%n]

def base_n_to_10(X, n):
  return sum(int(str(X)[-i-1])*n**i for i in range(len(str(X))))

#####IntLog#####
def int_log(n, a):
  count = 0
  while n>=a:
    n //= a
    count += 1
  return count

#############
# Main Code #
#############

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

zero = []
one = []

prev = None
count = 1
for s in S:
  if s == prev:
    count += 1
  else:
    if prev == "0":
      zero.append(count)
    if prev == "1":
      one.append(count)
    count = 1
  prev = s
if prev == "0":
  zero.append(count)
if prev == "1":
  one.append(count)
  
if len(zero) <= K:
  print(N)
  exit()

ruiseki_zero = [0]
ruiseki_one = [0]
for i in range(len(zero)):
  ruiseki_zero.append(ruiseki_zero[-1]+zero[i])
for i in range(len(one)):
  ruiseki_one.append(ruiseki_one[-1]+one[i])

MAX = 0

for i in range(len(ruiseki_zero)-K):
  MAX = max(MAX,ruiseki_zero[i+K]-ruiseki_zero[i]+ruiseki_one[min(i+K+int(S[0]),len(ruiseki_one)-1)]-ruiseki_one[max(i-1,0)])

print(MAX)

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
s=input()
g=[]
l=0
for i in range(1,N):
    if s[i]!=s[i-1]:
        g.append(i-l)
        l=i
g.append(N-l)

M=len(g)
ans=0
S=[0]
for h in range(M):
    S.append(S[h]+g[h])

if s[0]=="0":
    if K>M//2:
        print(N)
    else:
        for j in range(M):
            if j+2*K>M:
                break
            elif j==0:
                ans=max(ans,S[j+2*K]-S[j])
            elif j%2==1:
                ans=max(ans,S[j+2*K]-S[j])
        print(ans)
    
else:
    if K>=M//2+M%2:
        print(N)
    else:
        for j in range(M):
            if j+2*K+1>M:
                break
            elif j%2==0:
                ans=max(ans,S[j+2*K+1]-S[j])
        print(ans)

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
s=input()
g=[]
l=0
for i in range(1,N):
    if s[i]!=s[i-1]:
        g.append(i-l)
        l=i
g.append(N-l)

M=len(g)
ans=0
S=[0]
for h in range(M):
    S.append(S[h]+g[h])

if s[0]=="0":
    if K>M//2:
        print(N)
    else:
        for j in range(M):
            if j+2*K+1>M:
                break
            elif j==0:
                ans=max(ans,S[j+2*K]-S[j])
            elif j%2==1:
                ans=max(ans,S[j+2*K+1]-S[j])
        print(ans)
    
else:
    if K>=M//2+M%2:
        print(N)
    else:
        for j in range(M):
            if j+2*K+1>M:
                break
            elif j%2==0:
                ans=max(ans,S[j+2*K+1]-S[j])
        print(ans)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import numpy as np
from collections import deque
from collections import defaultdict
import heapq
import collections
import itertools
import bisect


mod = 10 ** 9 + 7
n, k = list(map(int, input().split()))
s = input()
dp = [1] * n
# 1の人をわざわざ0にする必要はない
# i番目の人から数えて何人いけるかを考える
ans = 0
prev = s[0]
change_points = [0]
for i in range(n):
    if prev != s[i]:
        change_points.append(i)
    prev = s[i]
# 全部ひっくり返せる時
if ((len(change_points))//2 <= k and s[0] == '1'):
    print(n)
    exit()
change_points.append(n)
ans = 0
# print(change_points)
for i in range(len(change_points)-1-(k*2)):
    if (s[i] == '1'):
        ans_kouho = change_points[i+2*k+1] - change_points[i]
        ans = max(ans, ans_kouho)
    else:
        ans_kouho = change_points[i+k*2] - change_points[i]
        ans = max(ans, ans_kouho)
    # print(ans_kouho)
print(ans)
# dp[i] = dp[i-1]

Submission status: Wrong Answer

Solution code:
 #ランレングス圧縮とは RLE(Run Length Encoding)とも言われます。
#連続したデータを、ひとつ分のデータと連続した長さで表現します。
def rle(s):
    tmp, count, ans_c,ans_n = int(s[0]),1,[] , []
    for i in range(1,len(s)):
        if tmp == int(s[i]):
            count += 1
        else:
            ans_c.append(tmp)
            ans_n.append(count)
            tmp = int(s[i])
            count = 1
    ans_c.append(tmp)
    ans_n.append(count)
    return ans_c,ans_n

   # AAABBCCCCA ⇒ A3B2C4A1

# 初期入力
import sys
input = sys.stdin.readline
N,K = (int(x) for x in input().split())
S =input().strip()

#0と1が何回続くか
a_c,a_n =rle(S)
a_len =len(a_c)
count =[0]*a_len
ans =0
if a_c[-1] ==1:
    f=1 
if a_c[-1] ==0:
    f=0 

if K >= (a_len +1)//2:
    ans =N
elif a_len==2:
    ans =N
elif a_len ==3 and a_c[0]==1:
    ans=N
else:
    if a_c[0] ==1:
        i=0
        while i +2*K +f <=a_len:
            count[i] =sum(a_n[i:i+2*K+f])
            i +=2
    else:
        count[0] =sum(a_n[:2*K+f])
        i=1
        while i +2*K +f <=a_len:
            count[i] =sum(a_n[i:i+2*K+f])
            i +=2      

    ans =max(count)
print(ans)

Submission status: Wrong Answer

Solution code:
 #ランレングス圧縮とは RLE(Run Length Encoding)とも言われます。
#連続したデータを、ひとつ分のデータと連続した長さで表現します。
def rle(s):
    tmp, count, ans_c,ans_n = int(s[0]),1,[] , []
    for i in range(1,len(s)):
        if tmp == int(s[i]):
            count += 1
        else:
            ans_c.append(tmp)
            ans_n.append(count)
            tmp = int(s[i])
            count = 1
    ans_c.append(tmp)
    ans_n.append(count)
    return ans_c,ans_n

   # AAABBCCCCA ⇒ A3B2C4A1

# 初期入力
import sys
input = sys.stdin.readline
N,K = (int(x) for x in input().split())
S =input().strip()

#0と1が何回続くか
a_c,a_n =rle(S)
a_len =len(a_c)
count =[0]*a_len
ans =0
if a_c[0] ==1:
    f=1 ;i=0
if a_c[0] ==0:
    f=0 ;i=1

if K >= (a_len +1)//2:
    ans =N
elif a_len==2:
    ans =N
elif a_len ==3 and a_c[0]==1:
    ans=N
else:
    if a_c[0] ==1:
        while i +2*K +f <=a_len:
            count[i] =sum(a_n[i:i+2*K+f])
            i +=2
    else:
        count[0] =sum(a_n[:2*K+f])
        while i +2*K +f <=a_len:
            count[i] =sum(a_n[i:i+2*K+f])
            i +=2      

    ans =max(count)
print(ans)

Submission status: Wrong Answer

Solution code:
 #ランレングス圧縮とは RLE(Run Length Encoding)とも言われます。
#連続したデータを、ひとつ分のデータと連続した長さで表現します。
def rle(s):
    tmp, count, ans_c,ans_n = int(s[0]),1,[] , []
    for i in range(1,len(s)):
        if tmp == int(s[i]):
            count += 1
        else:
            ans_c.append(tmp)
            ans_n.append(count)
            tmp = int(s[i])
            count = 1
    ans_c.append(tmp)
    ans_n.append(count)
    return ans_c,ans_n

   # AAABBCCCCA ⇒ A3B2C4A1

# 初期入力
import sys
input = sys.stdin.readline
N,K = (int(x) for x in input().split())
S =input().strip()

#0と1が何回続くか
a_c,a_n =rle(S)
a_len =len(a_c)
count =[0]*a_len
ans =0
if a_c[0] ==0:
    i=0
if a_c[0] ==1:
    i=0
if a_c[-1] ==1:
    f=1
if a_c[-1] ==0:
    f=0

if K >= (a_len +1)//2:
    ans =N
elif a_len==2:
    ans =N
elif a_len ==3 and a_c[0]==1:
    ans=N
else:
    while i +2*K+f <=a_len:
        count[i] =sum(a_n[i:i+2*K+f])
        i +=2

    ans =max(count)
print(ans)

Submission status: Wrong Answer

Solution code:
 #ランレングス圧縮とは RLE(Run Length Encoding)とも言われます。
#連続したデータを、ひとつ分のデータと連続した長さで表現します。
def rle(s):
    tmp, count, ans_c,ans_n = int(s[0]),1,[] , []
    for i in range(1,len(s)):
        if tmp == int(s[i]):
            count += 1
        else:
            ans_c.append(tmp)
            ans_n.append(count)
            tmp = int(s[i])
            count = 1
    ans_c.append(tmp)
    ans_n.append(count)
    return ans_c,ans_n

   # AAABBCCCCA ⇒ A3B2C4A1

# 初期入力
import sys
input = sys.stdin.readline
N,K = (int(x) for x in input().split())
S =input().strip()

#0と1が何回続くか
a_c,a_n =rle(S)
a_len =len(a_c)
count =[0]*a_len
ans =0
if a_c[0] ==0:
    i=1
if a_c[0] ==1:
    i=0
if a_c[-1] ==1:
    f=1
if a_c[-1] ==0:
    f=0

if K >= (a_len +1)//2:
    ans =N
elif a_len==2:
    ans =N
elif a_len ==3 and a_c[0]==1:
    ans=N
else:
    while i +2*K+f <=a_len:
        count[i] =sum(a_n[i:i+2*K+f])
        i +=2

    ans =max(count)
print(ans)

Submission status: Wrong Answer

Solution code:
 #ランレングス圧縮とは RLE(Run Length Encoding)とも言われます。
#連続したデータを、ひとつ分のデータと連続した長さで表現します。
def rle(s):
    tmp, count, ans = s[0], 1, []
    for i in range(1,len(s)):
        if tmp == s[i]:
            count += 1
        else:
            ans.append((tmp,count))
            tmp = s[i]
            count = 1
    ans.append((tmp,count))
    return ans

   # AAABBCCCCA ⇒ A3B2C4A1

# 初期入力
import sys
input = sys.stdin.readline
N,K = (int(x) for x in input().split())
S =input().strip()

#0と1が何回続くか
a =rle(S)
a_len =len(a)
count =[0]*a_len
ans =0

if K >= (a_len +1)//2:
    ans =N
else:
    for i in range(a_len):
        j =0
        count_0 =0
        while count_0 <K and i+j+1 <a_len and j <a_len:
            count[i] +=a[i+j][1]
            if a[i+j][0]=="0":
                count_0 +=1
                if count_0 ==K :
                    count[i] +=a[i+j+1][1]
            j +=1
    ans =max(count)
print(ans)

Submission status: Wrong Answer

Solution code:
 #ランレングス圧縮とは RLE(Run Length Encoding)とも言われます。
#連続したデータを、ひとつ分のデータと連続した長さで表現します。
def rle(s):
    tmp, count, ans = s[0], 1, []
    for i in range(1,len(s)):
        if tmp == s[i]:
            count += 1
        else:
            ans.append((tmp,count))
            tmp = s[i]
            count = 1
    ans.append((tmp,count))
    return ans

   # AAABBCCCCA ⇒ A3B2C4A1

# 初期入力
import sys
input = sys.stdin.readline
N,K = (int(x) for x in input().split())
S =input().strip()

#0と1が何回続くか
a =rle(S)
a_len =len(a)
count =[0]*a_len
ans =0

if K >= (a_len +1)//2:
    ans =N
else:
    for i in range(a_len):
        j =0
        count_0 =0
        while count_0 <K and i+j+1 <a_len and j <a_len:
            count[i] +=a[i+j][1]
            if a[j][0]=="0":
                count_0 +=1
                if count_0 ==K :
                    count[i] +=a[i+j+1][1]
            j +=1
    ans =max(count)
print(ans)

Submission status: Wrong Answer

Solution code:
 #ランレングス圧縮とは RLE(Run Length Encoding)とも言われます。
#連続したデータを、ひとつ分のデータと連続した長さで表現します。
def rle(s):
    tmp, count, ans = s[0], 1, []
    for i in range(1,len(s)):
        if tmp == s[i]:
            count += 1
        else:
            ans.append((tmp,count))
            tmp = s[i]
            count = 1
    ans.append((tmp,count))
    return ans

   # AAABBCCCCA ⇒ A3B2C4A1

# 初期入力
import sys
input = sys.stdin.readline
N,K = (int(x) for x in input().split())
S =input().strip()

#0と1が何回続くか
a =rle(S)
a_len =len(a)
count =[0]*a_len
ans =0

if K >= (a_len +1)//2:
    ans =N
else:
    for i in range(a_len):
        j =0
        count_0 =0
        while count_0 <K and i+j+1 <a_len and j <a_len:
            count[i] +=a[i+j][1]
            if a[j][0]=="0":
                count_0 +=1
                if count_0 ==K :
                    count[i] +=a[i+j+1][1]
            j +=1
    ans =max(count)
print(ans)
print(a)

Submission status: Wrong Answer

Solution code:
 #ランレングス圧縮とは RLE(Run Length Encoding)とも言われます。
#連続したデータを、ひとつ分のデータと連続した長さで表現します。
def rle(s):
    tmp, count, ans = s[0], 1, []
    for i in range(1,len(s)):
        if tmp == s[i]:
            count += 1
        else:
            ans.append((tmp,count))
            tmp = s[i]
            count = 1
    ans.append((tmp,count))
    return ans

   # AAABBCCCCA ⇒ A3B2C4A1

# 初期入力
import sys
input = sys.stdin.readline
N,K = (int(x) for x in input().split())
S =input().strip()

#0と1が何回続くか
a =rle(S)
a_len =len(a)
count =[0]*a_len
ans =0

if K >= (a_len +1)//2:
    ans =N
else:
    for i in range(a_len):
        j =0
        count_0 =0
        while count_0 <K and i+j+1 <a_len and j <a_len:
            count[i] +=a[i+j][1]
            if a[j][0]=="0":
                if count_0 ==K-1 and a[i+j+1][0] =="1":
                    count[i] +=a[i+j+1][1]
                count_0 +=1
            j +=1
    ans =max(count)
print(ans)

Submission status: Wrong Answer

Solution code:
 #ランレングス圧縮とは RLE(Run Length Encoding)とも言われます。
#連続したデータを、ひとつ分のデータと連続した長さで表現します。
def rle(s):
    tmp, count, ans = s[0], 1, []
    for i in range(1,len(s)):
        if tmp == s[i]:
            count += 1
        else:
            ans.append((tmp,count))
            tmp = s[i]
            count = 1
    ans.append((tmp,count))
    return ans

   # AAABBCCCCA ⇒ A3B2C4A1

# 初期入力
import sys
input = sys.stdin.readline
N,K = (int(x) for x in input().split())
S =input().strip()

#0と1が何回続くか
a =rle(S)
a_len =len(a)
count =[0]*a_len
ans =0

if K >= (a_len +1)//2:
    ans =N
else:
    for i in range(a_len):
        j =0
        count_0 =0
        while count_0 <K and i+j+1 <a_len and j <a_len:
            count[i] +=a[i+j][1]
            if a[j][0]=="0":
                if count_0 ==K-1 and a[i+j+1][0] ==1:
                    count[i] +=a[i+j+1][1]
                count_0 +=1
            j +=1
    ans =max(count)
print(ans)

Submission status: Wrong Answer

Solution code:
 #ランレングス圧縮とは RLE(Run Length Encoding)とも言われます。
#連続したデータを、ひとつ分のデータと連続した長さで表現します。
def rle(s):
    tmp, count, ans = s[0], 1, []
    for i in range(1,len(s)):
        if tmp == s[i]:
            count += 1
        else:
            ans.append((tmp,count))
            tmp = s[i]
            count = 1
    ans.append((tmp,count))
    return ans

   # AAABBCCCCA ⇒ A3B2C4A1

# 初期入力
import sys
input = sys.stdin.readline
N,K = (int(x) for x in input().split())
S =input().strip()

#0と1が何回続くか
a =rle(S)
a_len =len(a)
count =[0]*a_len
ans =0

if K >= (a_len +1)//2:
    ans =N
else:
    for i in range(a_len):
        j =0
        count_0 =0
        while count_0 <K and i+j+1 <a_len and j <a_len:
            count[i] +=a[i+j][1]
            if a[j][0]=="0":
                if count_0 ==K-1:
                    count[i] +=a[i+j+1][1]
                count_0 +=1
            j +=1
    ans =max(count)
print(ans)

Submission status: Wrong Answer

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

def cal(_S):
    cum = [0]
    flg = False
    cur_zero = 0
    cur_one = 0
    for i, s in enumerate(_S):
        if s == "0":
            if flg:
                cum.append(cum[-1] + cur_zero + cur_one)
                flg = False
                cur_zero = 0
                cur_one = 0
            cur_zero += 1
        else:
            if cur_zero != 0:
                flg = True
            cur_one += 1

    cum.append(cum[-1] + cur_zero + cur_one)
    return cum

def main():
    cum_l = cal(S)
    cum_r = cal(S[::-1])


    if K - 1 > len(cum_l):
        print(len(S))
    else:
        longest = 0
        for i in range(len(cum_l) - K):
            total = cum_l[i + K] - cum_l[i]
            if total > longest:
                longest = total
        for i in range(len(cum_r) - K):
            total = cum_r[i + K] - cum_r[i]
            if total > longest:
                longest = total

        print(longest)

main()

Submission status: Wrong Answer

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

cum = [0]
flg = False
cur_zero = 0
cur_one = 0
for i, s in enumerate(S):
    if s == "0":
        if flg:
            cum.append(cum[-1] + cur_zero + cur_one)
            flg = False
            cur_zero = 0
            cur_one = 0
        cur_zero += 1
    else:
        if cur_zero != 0:
            flg = True
        cur_one += 1

cum.append(cum[-1] + cur_zero + cur_one)

if K - 1 > len(cum):
    print(len(S))
else:
    longest = 0
    for i in range(len(cum) - K):
        total = cum[i + K] - cum[i]
        if total > longest:
            longest = total

    print(longest)

Submission status: Wrong Answer

Solution code:
from itertools import accumulate, permutations, combinations, combinations_with_replacement, groupby, product
# import math
# import numpy as np  # Pythonのみ！
# from operator import xor
# import re
# from scipy.sparse.csgraph import connected_components  # Pythonのみ！
# ↑cf.  https://note.nkmk.me/python-scipy-connected-components/
# from scipy.sparse import csr_matrix
# import statistics # Pythonのみ
# import string
import sys
sys.setrecursionlimit(10 ** 5 + 10)


def input(): return sys.stdin.readline().strip()


def resolve():
    def main():

        N, K = map(int, input().split())
        s = input()
        S = [int(c) for c in s]
        runlen = []
        if S[0] != 1:
            runlen.append(0)
        flag = False
        if S[-1] != 1:
            flag = True
        gr = groupby(S)
        cnt=0
        for key, group in gr:  # groupはイテレータ。list(group)でリスト化
            runlen.append(len(list(group)))
            cnt+=1
        if flag:
            runlen.append(0)
        if cnt==1: return N

        runlen = [0] + runlen
        # 累積和を格納したリスト．A[l:r]の総和はB[r] - B[l]
        runlen = list(accumulate(runlen))

        ans = 0
        i = 0
        while K*2+i+1 < len(runlen):
            r = K*2+i+1
            l = i
            ans = max(ans, runlen[r]-runlen[l])
            i += 2

        return ans
    print(main())


resolve()

Submission status: Wrong Answer

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

one = 0
zero = 0
z = 0
flg = False
num_list = []

if a[0] == '0':
    flg = True
for i in a:
    if one >= 0 and zero == 0 and i == '1':
        one += 1
    elif one == 0 and zero >= 0 and i == '0':
        zero += 1
        z += 1
    elif zero > 0 and i == '1':
        num_list.append(zero)
        one += 1
        zero = 0
    elif one > 0 and i == '0':
        num_list.append(one)
        zero += 1
        one = 0
        z += 1
num_list.append(max(one, zero))

if a[0] == '0':
    num_list.insert(0,0)
num_list.insert(0,0)
num_list.append(0)

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

n_list = []

sum = 0
for i in range(len(num_list)):
    sum += num_list[i]
    n_list.append(sum)

width = 2*k
if width >= len(num_list):
    print(n)
    exit()

max_num = 0

for i in range(1, len(n_list)-width, 2):
    print(i)
    num = n_list[i+width] - n_list[i-1]
    if num > max_num:
        max_num = num

print(max_num)

Submission status: Wrong Answer

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

one = 0
zero = 0
z = 0
flg = False
num_list = []

if a[0] == '0':
    flg = True
for i in a:
    if one >= 0 and zero == 0 and i == '1':
        one += 1
    elif one == 0 and zero >= 0 and i == '0':
        zero += 1
        z += 1
    elif zero > 0 and i == '1':
        num_list.append(zero)
        one += 1
        zero = 0
    elif one > 0 and i == '0':
        num_list.append(one)
        zero += 1
        one = 0
        z += 1
num_list.append(max(one, zero))

if a[0] == '0':
    num_list.insert(0,0)
num_list.insert(0,0)
num_list.append(0)

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

n_list = []

sum = 0
for i in range(len(num_list)):
    sum += num_list[i]
    n_list.append(sum)

width = 2*k

max_num = 0
for i in range(1, len(n_list)-width, 2):
    num = n_list[i+width] - n_list[i-1]
    if num > max_num:
        max_num = num

print(max_num)

Submission status: Wrong Answer

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

one = 0
zero = 0
z = 0
flg = False
num_list = []

if a[0] == '0':
    flg = True
for i in a:
    if one >= 0 and zero == 0 and i == '1':
        one += 1
    elif one == 0 and zero >= 0 and i == '0':
        zero += 1
        z += 1
    elif zero > 0 and i == '1':
        num_list.append(zero)
        one += 1
        zero = 0
    elif one > 0 and i == '0':
        num_list.append(one)
        zero += 1
        one = 0
        z += 1
num_list.append(max(one, zero))

if a[0] == '0':
    num_list.insert(0,0)
num_list.insert(0,0)
num_list.append(0)

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

n_list = []

sum = 0
for i in range(len(num_list)):
    sum += num_list[i]
    n_list.append(sum)

width = 2*k+1

max_num = 0
for i in range(1, len(n_list)-width, 2):
    num = n_list[i+width-1] - n_list[i-1]
    if num > max_num:
        max_num = num

print(max_num)

Submission status: Wrong Answer

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

one = 0
zero = 0
z = 0
flg = False
num_list = []

if a[0] == '0':
    flg = True
for i in a:
    if one >= 0 and zero == 0 and i == '1':
        one += 1
    elif one == 0 and zero >= 0 and i == '0':
        zero += 1
        z += 1
    elif zero > 0 and i == '1':
        num_list.append(zero)
        one += 1
        zero = 0
    elif one > 0 and i == '0':
        num_list.append(one)
        zero += 1
        one = 0
        z += 1
num_list.append(max(one, zero))
num_list.insert(0,0)
num_list.append(0)
if flg is True:
    num_list.insert(0, 0)
if z <= k:
    print(n)
    exit()

n_list = []

sum = 0
for i in range(len(num_list)):
    sum += num_list[i]
    n_list.append(sum)

width = 2*k+1

max_num = 0
for i in range(1, len(n_list)-width, 2):
    num = n_list[i+width-1] - n_list[i-1]
    if num > max_num:
        max_num = num

print(max_num)

Submission status: Wrong Answer

Solution code:
import sys, itertools

N, K = map(int, input().split())
S = sys.stdin.readline()[:-1]
A = []
if S[0] == '0':
    A.append(0)

num1 = 0
num0 = 0
for s in S:
    if s == '1':
        if num0 != 0:
            A.append(num0)
        num1 += 1
        num0 = 0
    else:
        if num1 != 0:
            A.append(num1)
        num1 = 0
        num0 += 1
if num1 != 0:
    A.append(num1)
if num0 != 0:
    A.append(num0)

ans = 0
n = len(A)
if n // 2 <= K:
    ans = N
else:
    A = [0] + list(itertools.accumulate(A))
    for i in range(0, n-2*K, 2):
        ans = max(A[i+2*K+1] - A[i], ans)

print(ans)

Submission status: Wrong Answer

Solution code:
import sys

N, K = map(int, input().split())
S = sys.stdin.readline()[:-1]
A = []
if S[0] == '0':
    A.append(0)

num1 = 0
num0 = 0
for s in S:
    if s == '1':
        if num0 != 0:
            A.append(num0)
        num1 += 1
        num0 = 0
    else:
        if num1 != 0:
            A.append(num1)
        num1 = 0
        num0 += 1
if num1 != 0:
    A.append(num1)
if num0 != 0:
    A.append(num0)

ans = 0
n = len(A)
if n // 2 <= K:
    ans = N
else:
    for i in range(0, n-2*K, 2):
        ans = max(sum(A[i:i+2*K+1]), ans)

print(ans)

Submission status: Wrong Answer

Solution code:
import sys
input=sys.stdin.buffer.readline

N, K = map(int, input().split())
S = input().split()
A = []
if S[0] == '0':
    A.append(0)

num1 = 0
num0 = 0
for s in S:
    if s == '1':
        if num0 != 0:
            A.append(num0)
        num1 += 1
        num0 = 0
    else:
        if num1 != 0:
            A.append(num1)
        num1 = 0
        num0 += 1
if num1 != 0:
    A.append(num1)
if num0 != 0:
    A.append(num0)

ans = 0
n = len(A)
if n // 2 <= K:
    ans = N
else:
    for i in range(0, n-2*K, 2):
        ans = max(sum(A[i:i+2*K+1]), ans)

print(ans)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
S = input()
A = []
if S[0] == '0':
    A.append(0)

num1 = 0
num0 = 0
for s in S:
    if s == '1':
        if num0 != 0:
            A.append(num0)
        num1 += 1
        num0 = 0
    else:
        if num1 != 0:
            A.append(num1)
        num1 = 0
        num0 += 1
if num1 != 0:
    A.append(num1)
if num0 != 0:
    A.append(num0)

ans = 0
if len(A) // 2 <= K:
    ans = sum(A)
else:
    for i in range(0, len(A)-2*K, 2):
        ans = max(sum(A[i:i+2*K+1]), ans)

print(ans)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
S = input()
A = []
mnum1 = 0
num1 = 0
num0 = 0
for s in S:
    if s == '1':
        if num0 != 0:
            A.append(num0)
        num1 += 1
        num0 = 0
    else:
        if num1 != 0:
            A.append(num1)
            mnum1 = max(mnum1, num1)
        num1 = 0
        num0 -= 1
if num1 != 0:
    A.append(num1)
    mnum1 = max(mnum1, num1)
if num0 != 0:
    A.append(num0)
if A[0] < 0:
    A = [0] + A
ans = mnum1

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

print(ans)

Submission status: Wrong Answer

Solution code:
N,K = list(map(int, input().split()))
S = input()
Sa = [0]
pre = S[0]
for i in range(1,N):
  if pre != S[i]:
    Sa.append(i)
    pre = S[i]

Sa.append(N)
#print(Sa)

# Sa[j+K*2+1] - Sa[j] のmaxかな？

if K*2 + 1 > len(Sa):
  print(N)
else:
  ans = -1
  offs = 0 #if S[0] != '0' else 1
  wid = 2*K+1 if S[0] != '0' else 2*K
  for j in range(offs,N,2):
    if j + wid >= len(Sa):
      break
    tmp = Sa[j + wid] - Sa[j]
    #print(j,ans,tmp)
    ans = max(ans, tmp)
  print(ans)

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
S = input()
cnt01 = []
for i,s in enumerate(S):
    if i == 0:
        if s == "0":
            cnt01.append([1,0])
        cnt01.append([s,1])
    else:
        if cnt01[-1][0] == s:
            cnt01[-1][1] += 1
        else:
            cnt01.append([s,1])
if len(cnt01) <= 2*K+1:
    ans = [sum([cnt[1] for cnt in cnt01])]
    print(ans)
else:
    ans = [sum([cnt[1] for cnt in cnt01[0:2*K+1]])]
    pos = 2
    while True:
        temp = ans[-1] - cnt01[pos-2][1] - cnt01[pos-1][1]
        if len(cnt01)-1 >= (pos + 2*K -1):
            temp += cnt01[pos+2*K-1][1]
        else:
            break
        if len(cnt01)-1 >= (pos + 2*K):
            temp += cnt01[pos+2*K][1]
        else:
            ans.append(temp)
            break
        ans.append(temp)
        pos += 2
print(max(ans))

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
s=input()
d=[]
f=True

if "0" not in s:
  print(n)
  exit()
if s[0]=="0":
    d.append(0)
    f=False

seq=1
for i in range(1,n):
    if s[i]=="0" and not f:
        seq+=1
    elif s[i]=="0" and f:
        f=False
        d.append(seq)
        seq=1
    elif s[i]=="1" and f:
        seq+=1
    else:
        f=True
        d.append(seq)
        seq=1
    if i==n-1:
      d.append(seq)
if s[-1]=="0":
    d.append(0)
l=len(d)
res=[0]*(l+1)
res[0]=0
for i in range(1,l+1):
    res[i]=res[i-1]+d[i-1]
ans=1
for i in range(1,n):
    if i%2==1:
        continue
    if 2*k+i-1>l:
      break
    a=res[2*k+i-1]-res[i-2]
    if a>ans:
        ans=a
print(ans)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
s=input()
d=[]
f=True

if s[0]=="0":
    d.append(0)
    f=False

seq=1
for i in range(1,n):
    if s[i]=="0" and not f:
        seq+=1
    elif s[i]=="0" and f:
        f=False
        d.append(seq)
        seq=1
    elif s[i]=="1" and f:
        seq+=1
    else:
        f=True
        d.append(seq)
        seq=1
    if i==n-1:
      d.append(seq)
if s[-1]=="0":
    d.append(0)
l=len(d)
res=[0]*(l+1)
res[0]=0
for i in range(1,l+1):
    res[i]=res[i-1]+d[i-1]
ans=1
for i in range(1,n):
    if i%2==1:
        continue
    if 2*k+i-1>l:
      break
    a=res[2*k+i-1]-res[i-2]
    if a>ans:
        ans=a
print(ans)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
s=input()
d=[]
f=True

if s[0]=="0":
    d.append(0)
    f=False

seq=1
for i in range(1,n):
    if s[i]=="0" and not f:
        seq+=1
    elif s[i]=="0" and f:
        f=False
        d.append(seq)
        seq=1
    elif s[i]=="1" and f:
        seq+=1
    else:
        f=True
        d.append(seq)
        seq=1
    if i==n-1:
      d.append(seq)
if s[-1]=="0":
    d.append(0)
l=len(d)
res=[0]*(l+1)
res[0]=0
for i in range(1,l+1):
    res[i]=res[i-1]+d[i-1]
ans=0
for i in range(1,n):
    if i%2==1:
        continue
    if 2*k+i-1>l:
      break
    a=res[2*k+i-1]-res[i-2]
    if a>ans:
        ans=a
print(ans)