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())
zeros=[0]
ones=[0]
S=list(input())
now=S[0]
one=0
zero=0
for i,s in enumerate(S):
  if s==now:
    if s=="0":
      zero+=1
    else:
      one+=1
  else:
    if s=="0":
      zero+=1
      ones.append(one)
      now=s
    else:
      one+=1
      zeros.append(zero)
      now=s
  if i==N-1:
    if s=="0":
      zeros.append(zero)
    else:
      ones.append(one)
#print(zeros, ones)
ans=0
lone=len(ones)
lzero=len(zeros)
if lzero==0:
  print(N)

if S[0]=="0":
  
  for i in range(lzero-K+1):
    idx=min(i+K, lzero-1)
    idx2=min(idx, lone-1)
    idx3=max(i-1, 0)
    ans=max(ans, zeros[idx]-zeros[i]+ones[idx2]-ones[idx3])
    
else:
  for i in range(lzero-K+1):
    idx=min(i+K, lzero-1)
    idx2=min(idx+1, lone-1)
    idx3=max(i-1, 0)
    ans=max(ans, zeros[idx]-zeros[i]+ones[idx2]-ones[idx3])

print(ans)

Submission status: Wrong Answer

Solution code:
N, K=map(int, input().split())
zeros=[0]
ones=[0]
S=list(input())
now=S[0]
one=0
zero=0
for i,s in enumerate(S):
  if s==now:
    if s=="0":
      zero+=1
    else:
      one+=1
  else:
    if s=="0":
      zero+=1
      ones.append(one)
      now=s
    else:
      one+=1
      zeros.append(zero)
      now=s
  if i==N-1:
    if s=="0":
      zeros.append(zero)
    else:
      ones.append(one)
#print(zeros, ones)
ans=0
lone=len(ones)
lzero=len(zeros)
if lzero==0:
  print(N)

if S[0]=="0":
  
  for i in range(lzero-K+1):
    idx=min(i+K, lzero-1)
    idx2=min(idx, lone-1)
    idx3=max(i-1, 0)
    ans=max(ans, zeros[idx]-zeros[i]+ones[idx2]-ones[idx3])
    
else:
  for i in range(lzero-K+1):
    idx=min(i+K, lzero-1)
    idx2=min(idx+1, lone-1)
    idx3=max(i-1, 0)
    ans=max(ans, zeros[idx]-zeros[i]+ones[idx2]-ones[i])

print(ans)

Submission status: Wrong Answer

Solution code:
N, K=map(int, input().split())
zeros=[0]
ones=[0]
S=list(input())
now=S[0]
one=0
zero=0
for i,s in enumerate(S):
  if s==now:
    if s=="0":
      zero+=1
    else:
      one+=1
  else:
    if s=="0":
      zero+=1
      ones.append(one)
      now=s
    else:
      one+=1
      zeros.append(zero)
      now=s
  if i==N-1:
    if s=="0":
      zeros.append(zero)
    else:
      ones.append(one)
print(zeros, ones)
ans=0
lone=len(ones)
lzero=len(zeros)
if lzero==0:
  print(N)

if S[0]=="0":
  
  for i in range(lzero-K+1):
    idx=min(i+K, lzero-1)
    idx2=min(idx, lone-1)
    idx3=max(i-1, 0)
    ans=max(ans, zeros[idx]-zeros[i]+ones[idx2]-ones[idx3])
    
else:
  for i in range(lzero-K+1):
    idx=min(i+K, lzero-1)
    idx2=min(idx+1, lone-1)
    idx3=max(i-1, 0)
    ans=max(ans, zeros[idx]-zeros[i]+ones[idx2]-ones[i])

print(ans)

Submission status: Wrong Answer

Solution code:
N, K=map(int, input().split())
zeros=[0]
ones=[0]
S=list(input())
now=S[0]
one=0
zero=0
for i,s in enumerate(S):
  if s==now:
    if s=="0":
      zero+=1
    else:
      one+=1
  else:
    if s=="0":
      zero+=1
      ones.append(one)
      now=s
    else:
      one+=1
      zeros.append(zero)
      now=s
  if i==N-1:
    if s=="0":
      zeros.append(zero)
    else:
      ones.append(one)
#print(zeros, ones)
ans=0
lone=len(ones)
lzero=len(zeros)
if lzero==0:
  print(N)

if S[0]=="0":
  
  for i in range(lzero-K+1):
    idx=min(i+K, lzero-1)
    idx2=min(idx, lone-1)
    idx3=max(i-1, 0)
    ans=max(ans, zeros[idx]-zeros[i]+ones[idx2]-ones[idx3])
    
else:
  for i in range(lzero-K+1):
    idx=min(i+K, lzero-1)
    idx2=min(idx+1, lone-1)
    ans=max(ans, zeros[idx]-zeros[i]+ones[idx2]-ones[i])

print(ans)

Submission status: Wrong Answer

Solution code:
N, K=map(int, input().split())
zeros=[0]
ones=[0]
S=list(input())
now=S[0]
one=0
zero=0
for i,s in enumerate(S):
  if s==now:
    if s=="0":
      zero+=1
    else:
      one+=1
  else:
    if s=="0":
      zero+=1
      ones.append(one)
      now=s
    else:
      one+=1
      zeros.append(zero)
      now=s
  if i==N-1:
    if s=="0":
      zeros.append(zero)
    else:
      ones.append(one)
#print(zeros, ones)
ans=0
lone=len(ones)
lzero=len(zeros)
if lzero==0:
  print(N)

if S[0]=="0":
  
  for i in range(lzero-K):
    idx=min(i+K, lzero-1)
    idx2=min(idx, lone-1)
    idx3=max(i-1, 0)
    ans=max(ans, zeros[idx]-zeros[i]+ones[idx2]-ones[idx3])
    
else:
  for i in range(lzero-K):
    idx=min(i+K, lzero-1)
    idx2=min(idx+1, lone-1)
    ans=max(ans, zeros[idx]-zeros[i]+ones[idx2]-ones[i])

print(ans)

Submission status: Wrong Answer

Solution code:
N, K=map(int, input().split())
zeros=[0]
ones=[0]
S=list(input())
now=S[0]
one=0
zero=0
for i,s in enumerate(S):
  if s==now:
    if s=="0":
      zero+=1
    else:
      one+=1
  else:
    if s=="0":
      zero+=1
      ones.append(one)
      now=s
    else:
      one+=1
      zeros.append(zero)
      now=s
  if i==N-1:
    if s=="0":
      zeros.append(zero)
    else:
      ones.append(one)
#print(zeros, ones)
ans=0
lone=len(ones)
lzero=len(zeros)
if lzero==0:
  print(N)

if S[0]=="0":
  
  for i in range(lzero-K):
    idx=min(i+K, lzero-1)
    idx2=min(idx, lone-1)
    idx3=min(i, lone-1)
    ans=max(ans, zeros[idx]-zeros[i]+ones[idx2]-ones[i])
    
else:
  for i in range(lzero-K):
    idx=min(i+K, lzero-1)
    idx2=min(idx+1, lone-1)
    ans=max(ans, zeros[idx]-zeros[i]+ones[idx2]-ones[i])

print(ans)

Submission status: Wrong Answer

Solution code:
N, K=map(int, input().split())
zeros=[0]
ones=[0]
S=list(input())
now=S[0]
one=0
zero=0
for i,s in enumerate(S):
  if s==now:
    if s=="0":
      zero+=1
    else:
      one+=1
  else:
    if s=="0":
      zero+=1
      ones.append(one)
      now=s
    else:
      one+=1
      zeros.append(zero)
      now=s
  if i==N-1:
    if s=="0":
      zeros.append(zero)
    else:
      ones.append(one)
print(zeros, ones)
ans=0
lone=len(ones)
lzero=len(zeros)
if lzero==0:
  print(N)

if S[0]=="0":
  
  for i in range(lzero-K):
    idx=min(i+K, lzero-1)
    idx2=min(idx, lone-1)
    idx3=min(i, lone-1)
    ans=max(ans, zeros[idx]-zeros[i]+ones[idx2]-ones[i])
    
else:
  for i in range(lzero-K):
    idx=min(i+K, lzero-1)
    idx2=min(idx+1, lone-1)
    ans=max(ans, zeros[idx]-zeros[i]+ones[idx2]-ones[i])

print(ans)

Submission status: Wrong Answer

Solution code:
N, K=map(int, input().split())
zeros=[0]
ones=[0]
S=list(input())
now=S[0]
one=0
zero=0
for i,s in enumerate(S):
  if s==now:
    if s=="0":
      zero+=1
    else:
      one+=1
  else:
    if s=="0":
      zero+=1
      ones.append(one)
      now=s
    else:
      one+=1
      zeros.append(zero)
      now=s
  if i==N-1:
    if s=="0":
      zeros.append(zero)
    else:
      ones.append(one)
#print(zeros, ones)
ans=0
lone=len(ones)
lzero=len(zeros)
if S[0]=="0":
  
  for i in range(lzero-K):
    idx=min(i+K, lzero-1)
    idx2=min(idx, lone-1)
    idx3=min(i, lone-1)
    ans=max(ans, zeros[idx]-zeros[i]+ones[idx2]-ones[i])
    
else:
  for i in range(lzero-K):
    idx=min(i+K, lzero-1)
    idx2=min(idx+1, lone-1)
    ans=max(ans, zeros[idx]-zeros[i]+ones[idx2]-ones[i])

print(ans)

Submission status: Wrong Answer

Solution code:
N, K=map(int, input().split())
zeros=[0]
ones=[0]
S=list(input())
now=S[0]
one=0
zero=0
for i,s in enumerate(S):
  if s==now:
    if s=="0":
      zero+=1
    else:
      one+=1
  else:
    if s=="0":
      zero+=1
      ones.append(one)
      now=s
    else:
      one+=1
      zeros.append(zero)
      now=s
  if i==N-1:
    if s=="0":
      zeros.append(zero)
    else:
      ones.append(one)
print(zeros, ones)
ans=0
lone=len(ones)
lzero=len(zeros)
if S[0]=="0":
  
  for i in range(lzero-K):
    idx=min(i+K, lzero-1)
    idx2=min(idx, lone-1)
    idx3=min(i, lone-1)
    ans=max(ans, zeros[idx]-zeros[i]+ones[idx2]-ones[i])
    
else:
  for i in range(lzero-K):
    idx=min(i+K, lzero-1)
    idx2=min(idx+1, lone-1)
    ans=max(ans, zeros[idx]-zeros[i]+ones[idx2]-ones[i])

print(ans)

Submission status: Wrong Answer

Solution code:
N, K=map(int, input().split())
zeros=[0]
ones=[0]
S=list(input())
now=S[0]
one=0
zero=0
for i,s in enumerate(S):
  if s==now:
    if s=="0":
      zero+=1
    else:
      one+=1
  else:
    if s=="0":
      zero+=1
      ones.append(one)
      now=s
    else:
      one+=1
      zeros.append(zero)
      now=s
  if i==N-1:
    if s=="0":
      zeros.append(zero)
    else:
      ones.append(one)
#print(zeros, ones)
ans=0
lone=len(ones)
lzero=len(zeros)
if S[0]=="0":
  
  for i in range(lzero):
    idx=min(i+K, lzero-1)
    idx2=min(idx, lone-1)
    idx3=min(i, lone-1)
    ans=max(ans, zeros[idx]-zeros[i]+ones[idx2]-ones[idx2-1])
    
else:
  for i in range(lzero):
    idx=min(i+K, lzero-1)
    idx2=min(idx+1, lone-1)
    ans=max(ans, zeros[idx]-zeros[i]+ones[idx2]-ones[i])

print(ans)

Submission status: Wrong Answer

Solution code:
N, K=map(int, input().split())
zeros=[0]
ones=[0]
S=list(input())
now=S[0]
one=0
zero=0
for i,s in enumerate(S):
  if s==now:
    if s=="0":
      zero+=1
    else:
      one+=1
  else:
    if s=="0":
      zero+=1
      ones.append(one)
      now=s
    else:
      one+=1
      zeros.append(zero)
      now=s
  if i==N-1:
    if s=="0":
      zeros.append(zero)
    else:
      ones.append(one)
#print(zeros, ones)
ans=0
lone=len(ones)
lzero=len(zeros)
if S[0]=="0":
  
  for i in range(lzero):
    idx=min(i+K, lzero-1)
    idx2=min(idx, lone-1)
    idx3=min(i, lone-1)
    ans=max(ans, zeros[idx]-zeros[i]+ones[idx2]-ones[idx3])
    
else:
  for i in range(lzero):
    idx=min(i+K, lzero-1)
    idx2=min(idx+1, lone-1)
    ans=max(ans, zeros[idx]-zeros[i]+ones[idx2]-ones[i])

print(ans)

Submission status: Wrong Answer

Solution code:
import sys
input = lambda: sys.stdin.readline().rstrip()
from itertools import accumulate


N, K = map(int, input().split())

S = input()

if len(S) == 1:
    print(1)
    exit()

cum_sum = [0]

tmp_S = S

if S[-1] == '0':
    tmp_S += '1'
else:
    tmp_S += '0'

cnt = 1
for i in range(len(tmp_S)-1):
    if tmp_S[i] == tmp_S[i+1]:
        cnt += 1
    else:
        cum_sum.append(cnt)
        cnt = 1

cum_sum = list(accumulate(cum_sum))

ans = 0
ran = K+K
if S[0] == '0':
    for i in range(0, len(cum_sum), 2):
        if i+ran < len(cum_sum):
            ans = max(ans, cum_sum[i+ran] - cum_sum[i])
else:
    for i in range(0, len(cum_sum), 2):
        if i+ran+1 < len(cum_sum):
            ans = max(ans, cum_sum[i+ran+1] - cum_sum[i])

print(ans)

Submission status: Wrong Answer

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

data = [[],[]]
key = 1
cnt = 0
for i in range(n):
    if s[i] == key:
        cnt += 1
        if i == n-1:
            data[key].append(cnt)
    else:
        data[key].append(cnt)
        key ^= 1
        cnt = 1
    
if len(data[0]) == len(data[1]):
    data[1].append(0)

m = len(data[0])

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

tmp = data[1][0]
for i in range(k):
    tmp += data[0][i]
    tmp += data[1][i+1]

ans = tmp
for i in range(k-1,m):
    tmp -= data[0][i-k+1]
    tmp -= data[1][i-k+1]
    tmp += data[0][i]
    tmp += data[1][i+1]
    ans = max(ans,tmp)

print(ans)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
s = str(input())
count = 1
v = []
for i in range(n-1):
  if s[i] != s[i+1]:
    v.append(count)
    count = 1
  else:
    count += 1
v.append(count)
if len(v) < 2*k+1:
  ans = n
else:
  ans = 0
  if s[0] == "0":
    ans = max(ans, sum(v[:2*k]))
    v.pop(0)
  if s[n-1] == "0":
    ans = max(ans, sum(v[len(v)-2*k:]))
    v.pop(-1)
  wa = sum(v[:2*k+1])
  ans = max(ans, wa)
  if len(v) > 1: 
    for i in range(0, len(v)//2 + 3-2*k, 2):
      wa -= (v[i] + v[i+1])
      wa += (v[i + 2*k+1]+v[i + 2*(k) +2])
      ans = max(ans, wa)
print(ans)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
s=input()
def r(a):
    ll,l=[],1
    for i in range(len(a)-1):
        if a[i]==a[i+1]:
            l+=1
        else:
            ll.append(l)
            l=1
    ll.append(l)
    return ll
a=r(s)
if s[0]=='0':
    a.insert(0,0)
if s[-1]=='0':
    a.insert(n,0)
b=[0]
c=0
for i in range(len(a)):
    c+=a[i]
    b.append(c)
c=0
for i in range(len(b)//2-k):
    c=max(b[2*(i+k)+1]-b[2*i],c)
print(c)

Submission status: Wrong Answer

Solution code:
import numpy as np
n,k = map(int,input().split())
s = list(input())
memory = s[0]
count = 1
lst = np.array([0])
M = 0
for i in range(1,n):
    if s[i] == memory:
        count += 1
    else:
        lst = np.append(lst,count)
        memory = s[i]
        count = 1
lst = np.append(lst,count)
cs = lst.cumsum()
if s[0] == '1':
    if (2*k+3) >= len(cs):
        print(cs[-1])
        exit()
    else:
        i = 0
        while i+2*k+2 <= len(cs):           
            M = max(M,cs[i+2*k+1]-cs[i])
            i += 2
        print(M)
        exit()
if s[0] == '0':
    if (2*k+2) >= len(cs):
        print(cs[-1])
        exit()
    else:
        i = 1
        while i+2*k+2 <= len(cs):
            M = max(M,cs[i+2*k+1]-cs[i])
            i += 2
        M = max(M,cs[2*k]-cs[0])
        print(M)
        exit()

Submission status: Wrong Answer

Solution code:
import numpy as np
n,k = map(int,input().split())
s = list(input())
memory = s[0]
count = 1
lst = np.array([0])
M = 0
for i in range(1,n):
    if s[i] == memory:
        count += 1
    else:
        lst = np.append(lst,count)
        memory = s[i]
        count = 1
lst = np.append(lst,count)
cs = lst.cumsum()
if s[0] == '1':
    if (2*k+2) >= len(cs):
        print(cs[-1])
        exit()
    else:
        i = 0
        while i+2*k+2 <= len(cs):           
            M = max(M,cs[i+2*k+1]-cs[i])
            i += 2
        print(M)
        exit()
if s[0] == '0':
    if (2*k+1) >= len(cs):
        print(cs[-1])
        exit()
    else:
        i = 1
        while i+2*k+2 <= len(cs):
            M = max(M,cs[i+2*k+1]-cs[i])
            i += 2
        M = max(M,cs[2*k]-cs[0])
        print(M)
        exit()

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 accumulate, permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
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()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

N, K = MAP()
S = [int(s) for s in input()] + [1]

r = 0
ans = 0
cnt = 0
used = [0]*(N + 1)
for l in range(N):
    if S[l] + S[l-1] == 0:
        continue
    while r < N and used[r] - used[l-1] <= K:
        if S[r-1] == 1 and S[r] == 0:
            used[r] = used[r - 1] + 1
        else:
            used[r] = used[r - 1]
        r += 1
    ans = max(ans, r-l)

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 accumulate, permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
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()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

N, K = MAP()
S = [int(s) for s in input()] + [1]

l = 0
r = -1
ans = 0
cnt = 0
tmp = ""
used = [0]*(N + 3)
while l < N:
    # while r < N and not (cnt==K and tmp == "1" and S[r] == "0"):
    #     if (tmp == "0" and S[r] == "1"): 
    #         cnt += 1
    #     tmp = S[r]
    #     r += 1
    while r < N and used[r] - used[l-1] <= K:
        r += 1
        if S[r]:
            used[r] = used[r - 1]
        else:
            if S[r-1]:
                used[r] = used[r - 1] + 1
            else:
                used[r] = used[r - 1]
    ans = max(ans, r-l)
    if l+1 == r:
        break
    l = r-1
    cnt = 0

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 accumulate, permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
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()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

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

l = 0
r = 0
ans = 0
cnt = 0
tmp = ""
while l < N:
    while r < N and not (cnt==K and tmp == "1" and S[r] == "0"):
        if (tmp == "0" and S[r] == "1"): 
            cnt += 1
        tmp = S[r]
        r += 1
    ans = max(ans, r-l)
    if l+1 == r:
        break
    l = r-1
    cnt = 0

print(ans)

Submission status: Wrong Answer

Solution code:
from sys import stdin
import heapq
def main():
    #入力
    readline=stdin.readline
    N,K=map(int,readline().split())
    S=readline().strip()

    li=[]
    now=0
    for i in range(N):
        if S[i]=="1":
            if now<=0:
                now-=1
            else:
                li.append(now)
                now=-1
            if i==N-1:
                li.append(now)
        else:
            if now>=0:
                now+=1
            else:
                li.append(now)
                now=+1
            if i==N-1:
                li.append(now)

    ps=0
    i=0
    max_count=0
    if (S[0]=="1" and len(li)>=2*K+1) or (len(li)>=2*K and S[1]=="0"):
        while i<len(li):
            if i==0:
                if li[0]<0:
                    for j in range(2*K+1):
                        ps+=abs(li[j])
                    max_count=ps
                    ps-=abs(li[0])
                    ps-=abs(li[1])
                    i=2*K+1
                else:
                    for j in range(2*K):
                        ps+=abs(li[j])
                    max_count=ps
                    ps-=abs(li[0])
                    i=2*K
            else:
                for j in range(i,i+2):
                    ps+=abs(li[j])
                    if j==len(li)-1:
                        max_count=max(max_count,ps)
                        i=j+1
                        break
                else:
                    i=j+1
                    max_count=max(max_count,ps)
                    ps-=abs(li[i-2*K-1])
                    ps-=abs(li[i-2*K])
    else:
        for i in range(len(li)):
            max_count+=abs(li[i])

    print(max_count)
    
if __name__=="__main__":
    main()

Submission status: Wrong Answer

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

ans = 0

check =[[0]]
i = 0
while i < n:
    if l[i] == "1":
        a = i
        while i < n and l[i] == "1":
            i += 1
        check.append([a,i-1])
    i += 1
check.append([n-1])

for i in range(len(check)-k):
       
    ans = max(ans,check[i+k][-1]-check[i][0]+1)
print(ans)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
S = input()
cnt = 1
counts = []

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

print(counts)
m = 0
K = min(K,len(counts)//2)
for i in range(len(counts)//2-K+1):
  m = max(m,sum(counts[2*i:2*(i+K)+1]))
print(m)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline

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

scomp = []

cnt = 0
for i in range(n):
    cnt += 1
    if s[i] != s[i+1]:
        scomp.append(cnt)
        cnt = 0

lens = len(scomp)
ans = 0

start = 0
if s[0] == '0':
    start = 1
    end = min(k*2,lens)
    tmp = sum(scomp[:end])
    ans = max(ans,tmp)

end = min(start + k*2+1,lens)
tmp = sum(scomp[start:end])
ans = max(ans,tmp)
for i in range(start,lens-k*2-2,2):
    tmp -= scomp[i]
    tmp -= scomp[i+1]
    tmp += scomp[i+k*2+1]
    tmp += scomp[i+k*2+2]
    ans = max(ans,tmp)

if s[-1] == '0':
    start = max(lens-k*2,0)
    tmp = sum(scomp[start:])
    ans = max(ans.tmp)

print(ans)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
s = input()
r = [0, 1]
prev = s[0]

for i in range(1, len(s)):
    if prev == s[i]:
        r[-1] += 1
    else:
        r.append(r[-1] + 1)
    prev = s[i]

lr = len(r)
import math
# 0の数よりkが大きい
if s[0] == '0' and math.ceil((lr-1) / 2) <= k:
    print(len(s))
    exit()
if s[0] == '1' and math.floor((lr-1) / 2) <= k:
    print(len(s))
    exit()

start = 0
if s[0] == '0':
    start = 1

ans = 0
for i in range(start, lr - (2 * k + 1), 2):
    ans = max(ans, r[i + (2 * k + 1)] - r[i])
    print(i, i+(2*k)+1, r[i+(2 * k)+1] - r[i])

print(ans)
# 0始まり
if s[0] == '0':
    ans = max(ans, r[2 * k] - r[0])
    print(ans)
# 0終わり
if s[-1] == '0':
    ans = max(ans, r[-1] - r[lr - 2 * k - 1])
    print(ans)
#print(r)
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline

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

scomp = []

cnt = 0
for i in range(n):
    cnt += 1
    if s[i] != s[i+1]:
        scomp.append(cnt)
        cnt = 0

lens = len(scomp)

ans = 0

start = 0
if s[0] == '0':
    start = 1
    end = min(k*2,lens)
    tmp = sum(scomp[:end])
    ans = max(ans,tmp)

end = min(start + k*2+1,lens)
tmp = sum(scomp[start:end])
ans = max(ans,tmp)

for i in range(start,lens-k*2-1,2):
    if i + k*2 +1 == lens-1:
        tmp -= scomp[i]
        tmp += scomp[i+k*2+1]
        ans = max(ans,tmp)
    else:
        tmp -= scomp[i]
        tmp -= scomp[i+1]
        tmp += scomp[i+k*2+1]
        tmp += scomp[i+k*2+2]
        ans = max(ans,tmp)


print(ans)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
s = input()
r = [0, 1]
prev = s[0]

for i in range(1, len(s)):
    if prev == s[i]:
        r[-1] += 1
    else:
        r.append(r[-1] + 1)
    prev = s[i]

lr = len(r)
import math
# 0の数よりkが大きい
if s[0] == '0' and math.ceil((lr-1) / 2) <= k:
    print(len(s))
    exit()
if s[0] == '1' and math.floor((lr-1) / 2) <= k:
    print(len(s))
    exit()

start = 0
if s[0] == '0':
    start = 1

ans = 0
for i in range(start, lr - (2 * k + 1), 2):
    ans = max(ans, r[i + (2 * k + 1)] - r[i])
    print(i, i+(2*k)+1, r[i+(2 * k)+1] - r[i])

# 0始まり
if s[0] == '0':
    ans = max(ans, r[2 * k] - r[0])
# 0終わり
if s[-1] == '0':
    ans = max(ans, r[-1] - r[lr - 2 * k])
#print(r)
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline

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

scomp = []

cnt = 0
for i in range(n):
    cnt += 1
    if s[i] != s[i+1]:
        scomp.append(cnt)
        cnt = 0

lens = len(scomp)

ans = 0

start = 0
if s[0] == '0':
    start = 1
    end = min(k*2,lens)
    tmp = sum(scomp[:end])
    ans = max(ans,tmp)

end = min(start + k*2+1,lens)
tmp = sum(scomp[start:end])
ans = max(ans,tmp)

for i in range(start,lens-k*2-2,2):
    if i + k*2 +1 == lens-1:
        tmp -= scomp[i]
        tmp += scomp[i+k*2+1]
        ans = max(ans,tmp)
    else:
        tmp -= scomp[i]
        tmp -= scomp[i+1]
        tmp += scomp[i+k*2+1]
        tmp += scomp[i+k*2+2]
        ans = max(ans,tmp)


print(ans)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline

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

scomp = []

cnt = 0
for i in range(n):
    cnt += 1
    if s[i] != s[i+1]:
        scomp.append(cnt)
        cnt = 0

lens = len(scomp)

ans = 0

start = 0
if s[0] == '0':
    start = 1
    end = min(k*2,lens)
    tmp = sum(scomp[:end])
    ans = max(ans,tmp)

end = min(start + k*2+1,lens)
tmp = sum(scomp[start:end])
ans = max(ans,tmp)

for i in range(start,lens-k*2-2,2):
    print(i,tmp)
    if i + k*2 +1 == lens-1:
        tmp -= scomp[i]
        tmp += scomp[i+k*2+1]
        ans = max(ans,tmp)
    else:
        tmp -= scomp[i]
        tmp -= scomp[i+1]
        tmp += scomp[i+k*2+1]
        tmp += scomp[i+k*2+2]
        ans = max(ans,tmp)


print(ans)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
s = input()
r = [0, 1]
prev = s[0]

for i in range(1, len(s)):
    if prev == s[i]:
        r[-1] += 1
    else:
        r.append(r[-1] + 1)
    prev = s[i]

lr = len(r)
import math
# 0の数よりkが大きい
if s[0] == '0' and math.ceil((lr-1) / 2) <= k:
    print(len(s))
    exit()
if s[0] == '1' and math.floor((lr-1) / 2) <= k:
    print(len(s))
    exit()

ans = 0
for i in range(lr - (2 * k)):
    ans = max(ans, r[i+(2 * k)] - r[i])
    #print(i, i+(2*k), r[i+(2 * k)] - r[i])

# 1始まり
if s[0] == '1':
    i = 0
    ans = max(ans, r[i + 1+(2 * k)] - r[i])
# 1終わり
if s[-1] == '1':
    i = lr - (2 * k) -1
    ans = max(ans, r[i+(2 * k)] - r[i-1])
#print(r)
print(ans)

Submission status: Wrong Answer

Solution code:
import math,itertools,fractions,heapq,collections,bisect,sys,queue,copy

sys.setrecursionlimit(10**7)
inf=10**20
mod=1000000007
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 I(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return input()

# Summarize count of factor within list -- START --
def summarize_list(sl):

  a=sl[0]
  c=1
  res=[]

  for x in sl[1:]:
    if x==a:
      c+=1
    else:
      res.append(c)
      a=x
      c=1
  res.append(c)

  return res
# Summarize count of factor within list --- END ---

def main():
  n,k=LI()
  s=list(S())

  l=summarize_list(s)
  # print(l)

  # 累積和
  for i in range(len(l)-1):
    l[i+1]+=l[i]

  # ...0 1 0 1 の形にする
  if s[-1]=='0':
    l+=[0]

  l=[0]+l

  # print(l)

  ans=0
  left=0
  while True:
    right=min(len(l)-1,left+2*k+1)
    if left>=len(l):
      return ans

    # print(right,left)
    ans=max(ans,l[right]-l[left])

    left+=2

# main()
print(main())

Submission status: Wrong Answer

Solution code:
import math,itertools,fractions,heapq,collections,bisect,sys,queue,copy

sys.setrecursionlimit(10**7)
inf=10**20
mod=1000000007
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 I(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return input()

# Summarize count of factor within list -- START --
def summarize_list(sl):

  a=sl[0]
  c=1
  res=[]

  for x in sl[1:]:
    if x==a:
      c+=1
    else:
      res.append(c)
      a=x
      c=1
  res.append(c)

  return res
# Summarize count of factor within list --- END ---

def main():
  n,k=LI()
  s=list(S())

  l=summarize_list(s)
  # print(l)

  # 累積和
  for i in range(len(l)-1):
    l[i+1]+=l[i]

  # 1 0 1 0... の形にする
  if s[0]=='0':
    l=[0]+l

  # ...0 1 0 1 の形にする
  if s[-1]=='0':
    l+=[l[-1]+1]

  l=[0]+l

  # print(l)

  ans=0
  left=0
  while True:
    right=min(len(l)-1,left+2*k+1)
    if left>=len(l):
      return ans

    # print(right,left)
    ans=max(ans,l[right]-l[left])

    left+=2

# main()
print(main())

Submission status: Wrong Answer

Solution code:
import math,itertools,fractions,heapq,collections,bisect,sys,queue,copy

sys.setrecursionlimit(10**7)
inf=10**20
mod=1000000007
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 I(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return input()

# Summarize count of factor within list -- START --
def summarize_list(sl):

  a=sl[0]
  c=1
  res=[]

  for x in sl[1:]:
    if x==a:
      c+=1
    else:
      res.append(c)
      a=x
      c=1
  res.append(c)

  return res
# Summarize count of factor within list --- END ---

def main():
  n,k=LI()
  s=list(S())

  l=summarize_list(s)
  # print(l)

  # 累積和
  for i in range(len(l)-1):
    l[i+1]+=l[i]

  # 1 0 1 0... の形にする
  if s[0]=='0':
    l=[0]+l

  # ...0 1 0 1 の形にする
  if s[-1]=='0':
    l+=[l[-1]+1]

  l=[0]+l

  ans=0
  left=0
  right=min(len(l)-1,2*k+1)
  
  while True:
    if right>=len(l):
      return ans

    # print(right,left)
    ans=max(ans,l[right]-l[left])

    left+=2
    right+=2

# main()
print(main())

Submission status: Wrong Answer

Solution code:
import math,itertools,fractions,heapq,collections,bisect,sys,queue,copy

sys.setrecursionlimit(10**7)
inf=10**20
mod=1000000007
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 I(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return input()

# Summarize count of factor within list -- START --
def summarize_list(sl):

  a=sl[0]
  c=1
  res=[]

  for x in sl[1:]:
    if x==a:
      c+=1
    else:
      res.append(c)
      a=x
      c=1
  res.append(c)

  return res
# Summarize count of factor within list --- END ---

def main():
  n,k=LI()
  s=list(S())

  l=summarize_list(s)
  # print(l)

  # 累積和
  for i in range(len(l)-1):
    l[i+1]+=l[i]

  # 1 0 1 0... の形にする
  if s[0]=='0':
    l=[0]+l

  # ...0 1 0 1 の形にする
  if s[-1]=='0':
    l+=[l[-1]+1]

  l=[0]+l

  ans=0
  left=0
  right=min(len(l)-1,2*k+1)
  
  while True:
    if right>=len(l):
      return ans

    print(right,left)
    ans=max(ans,l[right]-l[left])

    left+=2
    right+=2

# main()
print(main())