Problem:


Score : 400 points

Problem Statement

N people are arranged in a row from left to right.

You are given a string S of length N consisting of 0 and 1, and a positive integer K.

The i-th person from the left is standing on feet if the i-th character of S is 0, and standing on hands if that character is 1.

You will give the following direction at most K times (possibly zero):

Direction: Choose integers l and r satisfying 1 \leq l \leq r \leq N, and flip the l-th, (l+1)-th, ..., and r-th persons. That is, for each i = l, l+1, ..., r, the i-th person from the left now stands on hands if he/she was standing on feet, and stands on feet if he/she was standing on hands.

Find the maximum possible number of consecutive people standing on hands after at most K directions.

Constraints

N is an integer satisfying 1 \leq N \leq 10^5.

K is an integer satisfying 1 \leq K \leq 10^5.

The length of the string S is N.

Each character of the string S is 0 or 1.

Input

Input is given from Standard Input in the following format:

N K
S

Output

Print the maximum possible number of consecutive people standing on hands after at most K directions.

Sample Input 1

5 1
00010

Sample Output 1

4

We can have four consecutive people standing on hands, which is the maximum result, by giving the following direction:

Give the direction with l = 1, r = 3, which flips the first, second and third persons from the left.

Sample Input 2

14 2
11101010110011

Sample Output 2

8

Sample Input 3

1 1
1

Sample Output 3

1

No directions are necessary.



Sample input:
5 1
00010


Sample output:
4

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
s = input()
l = [0]*n
if int(s[0]) == 0:
    l[0] = 1
for i in range(1,n):
    if s[i] ==  "0" and s[i-1]=="0":
        l[i] = l[i-1]
    elif s[i] == "0" and s[i-1] =="1":
        l[i] = l[i-1] + 1
    else:
        l[i] = l[i-1]
def solve(x):
    for i in range(0,n-x+1):
        if i ==0:
            if l[x-1] <= k:
                return True
            else:
                continue
        elif s[i] == "0":
            if l[i+x-1] - l[i] + 1 <= k:
                return True
            else:
                continue
        else:
           if l[i+x-1] - l[i] <= k:
               return True
           else:
               continue
    return False


def binary_search(ok,ng):
    while abs(ok-ng) >1:
        mid = (ok+ng)//2
        if solve(mid):#判定のとこは別で
            ok = mid
        else:
            ng = mid
    return ok
print(binary_search(1,n))

Submission status: Wrong Answer

Solution code:
read =input
n,k = map(int, read().split())
a = read()
a = '1' + a + '1'
# -1 if use 0, -1 if use len(a) -1
pstart = 0
pcnt = 0
ptype = '1'
b = []
# (start, end, cnt)
for tp in enumerate(a):
    i, c = tp
    if ptype == c:
        pcnt = pcnt + 1
    else :
        b.append((pstart, i-1, pcnt))
        pstart = i
        ptype = c
        pcnt = 1
b.append((pstart, i, pcnt))
# print(b)
if k >= len(b)//2:
    print(sum(map(lambda tp:tp[2], b)) - 2)
else :
    start = 0
    end = k * 2
    ans = 0
    while(end <= len(b) - 1):
        tmp = sum(map(lambda tp:tp[2], b[start:(end+1)]))
        if b[start][0] == 0:
            tmp = tmp - 1
        if b[end][1] == len(a)-1:
            tmp = tmp -1
        print(start, end ,tmp)
        ans = max(ans, tmp)
        start = start + 2
        end = end + 2
    print(ans)

Submission status: Wrong Answer

Solution code:
import numpy as np

N, K = map(int, input().split(" "))
S = np.array([int(i) for i in input()])
pos1 = np.linspace(0,N-1,N)[S == 1]
lst = []
st =[0]
for i in pos1:
    if st[-1] + 1 == i:
        st.append(i)
    else:
        lst.append(st)
        st = [i]
    if i == pos1[-1]:
        lst.append(st)
time = len(lst) - K
if time <= 0:
    print(N)
else:
    print(int(max([max(lst[i+K]) - min(lst[i])+1 for i in range(time)])))

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
N, K = map(int, input().split())
S = input()
c0, c1 = [], []
if S[0] == '1':
    c0.append(0)
i = 0
while i < N:
    p = S[i]
    c = 1
    i += 1
    while i < N and S[i] == p:
        i += 1
        c += 1
    if p == '0':
        c0.append(c)
    else:
        c1.append(c)
if S[N-1] == '0':
    c1.append(0)
l0 = len(c0)
ans = sum(c0[0:K]) + sum(c1[0:K])
for i in range(1, l0):
    c0[i] += c0[i-1]
    c1[i] += c1[i-1]
l = l0 - K + 1
if l0 == 2 and S[0] == '1':
    ans = N
else:
    for i in range(1, l):
        tmp = c0[i+K-1] - c0[i-1] + c1[i+K-1] - c1[i-2]
        if ans < tmp:
            ans = tmp
print(ans)

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
S=input()
l=[]
count=[0,0] #count[0]が0,count[1]が1
if S[0]=="0":
    now=0
    a=0
else:
    now=1
    a=1
for i in range(len(S)):
    if int(S[i])==now:
        count[now]+=1
    else:
        if now==0:
            l+=[count[now]]
        else:
            l+=[count[now]]
        count[now]=0
        now=int(S[i])
        count[now]+=1
if now==0:
    l+=[(-1)*count[now]]
else:
    l+=[count[now]]

mx=0
for i in range(len(l)-2*K):
    if a==1:
        if mx<sum(l[i:i+2*K+1]) and i%2==0:
            mx=sum(l[i:i+2*K+1])
    elif a==0:
        if i==0:
            mx=sum(l[0:2*K])
        else:
            if mx<sum(l[i:i+2*K+1]) and i%2==1:
                mx=sum(l[i:i+2*K+1])

if len(l)==1 and a==1:
    print(l[0])
else:
    print(mx)

Submission status: Wrong Answer

Solution code:
#!usr/bin/env python3
from collections import defaultdict
from collections import deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
import itertools
sys.setrecursionlimit(10**5)
stdin = sys.stdin
def LI(): return list(map(int, stdin.readline().split()))
def LF(): return list(map(float, stdin.readline().split()))
def LI_(): return list(map(lambda x: int(x)-1, stdin.readline().split()))
def II(): return int(stdin.readline())
def IF(): return float(stdin.readline())
def LS(): return list(map(list, stdin.readline().split()))
def S(): return list(stdin.readline().rstrip())
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = float('INF')

#A
def A():
    a, b = LI()
    if a < b:
        a, b = b, a
    ans = a
    a -= 1
    ans += max(a, b)
    print(ans)
    return

#B
def B():
    n = II()
    h = LI()
    dp = [0 for i in range(n)]
    ans = 0
    for i in range(n):
        dp[i] = max(dp[i - 1], h[i])
        if dp[i - 1] <= h[i]:
            ans += 1
    print(ans)
    return

#C
def C():
    s = S()
    ans = 0
    for num, i in enumerate(s):
        if num % 2:
            if i == "0":
                ans += 1
        else:
            if i == "1":
                ans += 1
    ansb = 0
    for num, i in enumerate(s):
        if not num % 2:
            if i == "0":
                ansb += 1
        else:
            if i == "1":
                ansb += 1
    print(min(ans, ansb))
    return

#D
def D():
    n, K = LI()
    s = S()
    ans = []
    i = 0
    while i < n:
        l = 1
        for x in range(i+1, n):
            if s[x] == s[i]:
                l = x + 1 - i
                continue
            l = x - i
            break
        ans.append(l)
        i += l
    dp = [0 for i in range(len(ans))]
   # print(ans)
    if s[0] == "0":
        i = 2*K
        dp[0] = sum(ans[:i])
        if len(ans) - i == 1:
            k = 1
            dp[k] = dp[k - 1] + ans[i] - ans[k - 1]
        for k in range(1,len(ans) - i):
            dp[k] = dp[k - 1] + ans[i + k] - ans[k - 1]
    if s[0] == "1":
        i = 2*K+1
        dp[0] = sum(ans[:i])
        if len(ans) - i == 1:
            k = 1
            dp[k] = dp[k - 1] + ans[i] - ans[k-1]
        for k in range(1,len(ans) - i):
            dp[k] = dp[k - 1] + ans[i + k] - ans[k-1]
    print(max(dp))
        

    return

#E
def E():
    return

#F
def F():
    return

#G
def G():
    return

#H
def H():
    return

#Solve
if __name__ == '__main__':
    D()

Submission status: Wrong Answer

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

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

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

ss = array_count(s)

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

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

ss_sum = []

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

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

print(ans)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
s=input()
s+='a'
l=[]
a=s[0]

if a=='0':
  f=0
else:
  f=1

for x in s[1:]:
  if a[0]!=x:
    l.append(len(a))
    a=x
  else:
    a+=x

mx=0
if f==1:
  l2=l[0:2*k+1]
  sm=sum(l2)
  mx=sm
  ff=0
  b=0
  for i in range(2*k+1,len(l)):
    a=l2[0]+l2[1]
    if ff==0:
      for j in range(len(l2)-2):
        l2[j]=l2[j+2]
      ff=1
      l2[-2]=l[i]
      b=l[i]
    else:
      ff=0
      l2[-1]=l[i]
      b+=l[i]
    if b>a:
      mx=sum(l2)
else:
  l2=l[0:2*k]
  l2=[0]+l2
  sm=sum(l2)
  mx=sm
  ff=0
  b=0
  for i in range(2*k+1,len(l)):
    a=l2[0]+l2[1]
    if ff==0:
      for j in range(len(l2)-2):
        l2[j]=l2[j+2]
      ff=1
      l2[-2]=l[i]
      b=l[i]
    else:
      ff=0
      l2[-1]=l[i]
      b+=l[i]
    if b>a:
      mx=sum(l2)
print(mx)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split());s=input();aa,b=[],[]
r,l=0,s[0]
for i in range(n):
  if s[i]!=l:
    aa.append(r)
    b.append(l)
    l=s[i]
    r=1
  else:r+=1
aa.append(r);b.append(l)
a=[aa[0]]
l=len(aa)
for i in range(1,l):a.append(a[-1]+aa[i])
x=max(l-k*2-1,1)
ans=0
for i in range(x):
  ans=max(ans,a[l-x-i]-a[i]+aa[i])
print(ans)

Submission status: Wrong Answer

Solution code:
import bisect
from collections import deque
from collections import defaultdict
from fractions import gcd
#from math import factorial
from math import sqrt
from math import ceil
from itertools import permutations
from heapq import *

N,K=map(int,input().split())
S=input()
if S[-1]=="0":
    S+="1"
else:
    S+="0"
List=[]
ini=S[0]
now=0
zeronum=0
for i in range(N+1):
    if S[i]==ini:
        now+=1
        continue
    else:
        if ini=="1":
            List.append(now)
        else:
            List.append(now*(-1))
            zeronum+=1
        ini=S[i]
        now=1
if List[0]<0:
    List.insert(0,0)
N=len(List)
sum=0
SUM=0

if zeronum<=K:
    print(N)
    quit()
for i in range(2*K+1):
    if i%2==1:
        sum+=List[i]*(-1)
    else:
        sum+=List[i]
SUM=max(sum,SUM)
List.append(0)
ind=0
for i in range(K+1,N//2+1):
    sum+=-List[ind]+List[ind+1]-List[2*i-1]+List[2*i]
    ind+=2

    SUM=max(sum,SUM)
print(SUM)

Submission status: Wrong Answer

Solution code:
n,k=list(map(int,input().split()))
t=input()
z=[]
p=t[0]
if t[0]=="0":
  z.append(0)
z.append(0)
for i in t:
  if i == p:
    z[-1]+=1
  else:
    p=i
    z.append(1)
if k*2+1 >= len(z):
  r=n
else:
  p=sum(z[:2*k+1])
  r=p
  for i in range(0,len(z)-2*k-2,2):
    e=z[i]+z[i+1]
    p-=e
    f=z[2*k+i+1]+z[2*k+i+2]
    p+=f
    r=max(r,p)
print(r)

Submission status: Wrong Answer

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

L = []
cnt = 0
for i in range(N) :
    cnt += 1
    if S[i] != S[i+1] :
        L.append(cnt)
        cnt = 0
#print(L)

A = [] #1
B = [] #0

if S[0] == "1" :
    for i in range(len(L)) :
        if i % 2 == 0 :
            A.append(L[i])
        else :
            B.append(L[i])
else :
    for i in range(len(L)) :
        if i % 2 == 0 :
            B.append(L[i])
        else :
            A.append(L[i])

#print(A)
#print(B)

ans = sum(A[:K+1]) + sum(B[:K])
for i in range(len(B)-K+1) :
    
    #print(i,K+i,"q")
    calc = sum(B[i:K+i]) + sum(A[i:K+i+1])
    ans = max(ans,calc)
print(ans)

Submission status: Wrong Answer

Solution code:
n,m=map(int,input().split())
l1=list(input())
l=[]
lm=[]
c=0
d=l1[0]
for i in l1:
  if i==d:
    c+=1
  else:
    d=i
    l.append(c)
    c=1
l.append(c)
if l1[0]=="1":
  if len(l)//2<=m:
    print(n)
  else:
    j=0
    for i in range(m+1):
      lm.append(sum(l[j:j+2*m])+l[j+2*m])
      j+=2
      if j+2*m+1>len(l):
        break
    print(max(lm))
else:
  if (len(l)+1)//2<=m:
    print(n)
  else:
    j=1
    for i in range(m):
      lm.append(sum(l[j:j+2*m]))
      j+=2
      if j+2*m+1>len(l):
        break
    print(max(lm))

Submission status: Wrong Answer

Solution code:
import numpy as np
N, K = map(int, input().split())
x = list(input())
xn = "1"
c = 0
y = []
for n in range(N):
    if x[n] == xn:
        c += 1
    else:
        xn = x[n]
        y.append(c)
        c = 1
y.append(c)
if len(y) % 2 == 0:
    y.append(0)
y = np.array(y)
print(y)
l = 2 * K + 1
print(l)
if l >= len(y):
    print(np.sum(y))
else:
    z = [np.sum(y[2 * i:2 * i + l]) for i in range((len(y) + 3 - l) // 2)]
    print(z)
    print(max(z))

Submission status: Wrong Answer

Solution code:
n, k = map(int,input().split())
s = input()
a = []
i = 0
cnt = 0
now = s[0]
while i < n:
    if s[i] == now:
        cnt += 1
    else:
        a.append(cnt)
        now = s[i]
        cnt = 1
    if i == n-1:
        a.append(cnt)
    i += 1

if len(a) == 1 and s[0] == '1':
    print(n)
    exit()

if s[0] == '0':
    a = [0] + a
    n += 1
if s[-1] == '0':
    a.append(0)
    n += 1

la = len(a)
ans = -float('inf')
for i in range(0,la-2*k,2):
    l = i
    r = i + 2*k+1
    sa = sum(a[l:r])
    if sa > ans:
        ans = sa
print(ans)

Submission status: Wrong Answer

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

l=[0]
ctr=1
if s[0]=="0":
	l.append(1)
	ctr+=1
else:
	l.append(-1)

for i in range(1,n):
	if s[i-1]=="1" and s[i]=="0":
		l.append(ctr)
		ctr+=1
	else:
		l.append(-1)
l.append(ctr)

ans=[]
m=ctr
if k>=m:
	print n
else:
	for j in range(m-k):
		start=j
		end=j+k+1
		ans.append(l.index(end)-l.index(start)-1)

	if len(ans)==0:
		print n
	else:
		print max(ans)

Submission status: Wrong Answer

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

def main():
    n, k = [int(i) for i in input().split()]
    s = input() + "3"
    s0 = []
    s1 = []
    cnt = 0
    start = 1
    for i in range(n+1):
        if start == 1 and s[i] != "1":
            s1.append(cnt)
            cnt = 1
            start = 0
        elif start == 0 and s[i] != "0":
            s0.append(cnt)
            cnt = 1
            start = 1
        else:
            cnt += 1

    #ans = max(s1)
    #for i in range(n // 2 +1):
    #    ans = max(ans, sum(s1[i:i+k+1]) + sum(s0[i:i+k]))

    ans = sum(s1[0:0+k+1]) + sum(s0[0:0+k])
    n_ans = ans

    for i in range(n):
        try:
            n_ans = n_ans + s1[k+1+i] + s0[k+i] - s1[i] - s0[i]
            ans = max(ans, n_ans)
        except:
            pass

    print(ans)
    #print(s1, s0)

main()

Submission status: Wrong Answer

Solution code:
N , K = map(int,input().split())
S = list(input())
row = []
for i in range(N):
	if i == 0:
		judge = S[i]
		row.append(1)
	else:
		if judge == S[i]:
			row[-1] += 1
		else:
			judge = S[i]
			row.append(1)

max_row = 2 * K + 1


if max_row >= len(row):
	print(N)
else:
	if S[-1] == "0":
		row.append(0)
	if S[0] == "0":
		row.insert(0,0)
	sum_count = []
	temp_sum = 0
	for i in range(max_row):
		temp_sum += row[i]
	sum_count.append(temp_sum)
	i = max_row
	while i < len(row):
		sum_count.append(sum_count[-1] + row[i] + row[i+1] - row[i-max_row] - row[i-max_row+1])
		i += 2
	print(max(sum_count))

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
s=input()
a0=[]
a1=[]
count=0
if s[0]=='0':
  tmp='0'
else:
  tmp='1'
for i in range(len(s)):
  if s[i]==tmp:
    count+=1
  else:
    if tmp=='0':
      a0.append(count)
      count=1
      tmp='1'
    else:
      a1.append(count)
      count=1
      tmp='0'
if tmp=='0':
  a0.append(count)
else:
  a1.append(count)
if len(a0)<=k:
  print(n)
else:
  ans=0
  if s[0]=='0':
    for i in range(len(a0)-k+1):
      tmp=sum(a0[i:i+k])+sum(a1[i-1:i+k])
      if ans<tmp:
        ans=tmp
  else:
    for i in range(len(a0)-k+1):
      tmp=sum(a0[i:i+k])+sum(a1[i:i+k+1])
      if ans<tmp:
        ans=tmp
  print(ans)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
s=input()
s='0'+s+'0'
S=[]
E=[]
ans=[]
for i in range(n+1):
  if s[i:i+2]=='01':
    S.append(i+1)
  if s[i:i+2]=='10':
    E.append(i)
if S==[] and E==[]:
  print(n)
else:
  for j in range(len(E)-k):
    ans.append(abs(S[j]-E[j+k]))
  if s[1]=='0':
    if len(E)>=k:
      ans.append(E[k-1]-1)
    else:
      ans.append(n-1)
  if s[n]=='0':
    if len(S)<=len(S)-k+1:
      ans.append(S[len(S)-k]-1)
    else:
      ans.append(n-1)
  if ans==[]:
    print(n)
  else:
    print(max(ans)+1)

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
S = input()
l = 0
r = 1
ans = 0
k = 0
if S[0] == '0':
    k = 1
while r < N:
    #print(l,r)
    if S[r] == '1':
        r += 1
        ans = max(ans,r-l)
        continue
    if S[r-1] == '1' and S[r] == '0':
        if k == K:
            while True:
                l += 1
                if S[l] == '0':
                    l += 1
                    #k -= 1
                    break
            r += 1
        else:
            k += 1
            r += 1
    else:
        r += 1
print(ans)

Submission status: Wrong Answer

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

first = S[0]
sakadachi = [ len(x) for x in S.split('0') if x != '' ]
normal = [ len(x) for x in S.split('1') if x != '' ]

# normal[0] sakadachi[0] normal[1] ... to sakadachi[0] normal[0] sakadachi[1] ...
if first == '0':
    sakadachi = [0] + sakadachi

n_normal = K
n_sakadachi = K+1
people = sum(sakadachi[0:n_sakadachi]) + sum(normal[0:n_normal])
res = people
for i in range(n_sakadachi, len(sakadachi)):
#    people = sum(sakadachi[i:i+n_sakadachi]) + sum(normal[i:i+n_normal])
    people = people - sakadachi[i-n_sakadachi] - normal[i-n_normal] + sakadachi[i] + normal[i-1]
    if res < people:
        res = people
print(res)

Submission status: Wrong Answer

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

wl = []
wc = 0
prevs = None
for i, _s in enumerate(s):
  if prevs is None:
    wl.append(i)
    prevs = _s
    if _s == '0':
      wc += 1
  elif prevs != _s:
    wl.append(i)
    prevs = _s
    if _s == '0':
      wc += 1
wl.append(n)

if k >= wc:
  print(n)
elif s[0] == '0':
  if s[-1] == '0':
    maxl = max(wl[2*k] - wl[0], wl[-1] - wl[-2*k-1])
    for l, r in zip(wl[1:], wl[2*k+2:-1]):
      if r - l > maxl:
        maxl = r - l
  else:
    maxl = wl[2*k] - wl[0]
    for l, r in zip(wl[1:], wl[2*k+2:]):
      if r - l > maxl:
        maxl = r - l
  print(maxl)
else:
  if s[-1] == '0':
    maxl = wl[-1] - wl[-2*k-1]
    for l, r in zip(wl, wl[2*k+1:-1]):
      if r - l > maxl:
        maxl = r - l
  else:
    maxl = 0
    for l, r in zip(wl, wl[2*k+1:]):
      if r - l > maxl:
        maxl = r - l
  print(maxl)

Submission status: Wrong Answer

Solution code:
read = input
n,k = map(int, read().split())
a = read()
a = '1' + a + '1'
# -1 if use 0, -1 if use len(a) -1
pstart = 0
pcnt = 0
ptype = '1'
b = []
# (start, end, cnt)
for tp in enumerate(a):
    i, c = tp
    if ptype == c:
        pcnt = pcnt + 1
    else :
        b.append((pstart, i-1, pcnt))
        pstart = i
        ptype = c
        pcnt = 1
b.append((pstart, i, pcnt))
# print(b)
if k > len(b)//2:
    print(sum(map(lambda tp:tp[2], b)) - 2)
else :
    start = 0
    end = k * 2
    ans = 0
    while(end <= len(b) - 1):
        tmp = sum(map(lambda tp:tp[2], b[start:(end+1)]))
        if b[start][0] == 0:
            tmp = tmp - 1
        if b[end][1] == len(a)-1:
            tmp = tmp -1
        print(start, end ,tmp)
        ans = max(ans, tmp)
        start = start + 2
        end = end + 2
    print(ans)

Submission status: Wrong Answer

Solution code:
N,K=(int(x) for x in input().split())
S=input()

def Solve(S,N,K):
    if S.count("0")<=K:
        return len(S)
    if S[0]=="1":
        S="0"+S
    

    LA=[]#立ってる人 = 0
    LB=[]#逆立ちしてる人 = 1
    k=-1
    current="0"
    for i,s in enumerate(S):
        k+=1
        if s!=current:
            if s=="0":
                LB.append(k)#1の個数
                current="0"
                k=0
            else:
                LA.append(k)#0の個数
                current="1"
                k=0

    if current=="1":
        LB.append(k+1)
    else:
        LA.append(k+1)
    
    
    LB.append(0)#0で終わる場合、LAが1つ多くなるので、LBに0を足して調整
    mx=0
    for i in range(len(LA)-K+1):
        result=sum(LA[i:i+K])+sum(LB[i-1:i+K])
        mx=max(mx,result)

    return mx

print(Solve(S,N,K))

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
N, K = map(int, input().split())
S = input()
c0, c1 = [], []
if S[0] == '1':
    c0.append(0)
i = 0
while i < N:
    p = S[i]
    c = 1
    i += 1
    while i < N and S[i] == p:
        i += 1
        c += 1
    if p == '0':
        c0.append(c)
    else:
        c1.append(c)
if S[N-1] == '0':
    c1.append(0)
l0 = len(c0)
for i in range(1, l0):
    c0[i] += c0[i-1]
    c1[i] += c1[i-1]
l = l0 - K + 1
if K <= len(c0):
    k = K
else:
    k = len(c0)
ans = c0[k-1] + c1[k-1]
if l0 == 2 and S[0] == '1':
    ans = N
else:
    for i in range(1, l):
        tmp = c0[i+K-1] - c0[i-1] + c1[i+K-1] - c1[i-2]
        print(i, tmp)
        if ans < tmp:
            ans = tmp
print(ans)

Submission status: Wrong Answer

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

people = []
prev = s[0]
if prev == "0":
	count = -1
else:
	count = 1

for i in range(1, len(s)):
	if prev != s[i]:
		people.append(count)
		count = 0
		prev = s[i]
	if s[i] == "0":
		count -= 1
	else:
		count += 1
people.append(count)

if len(people) == 1:
	print(n)
	quit()

if len(people)/2 <= k:
	print(n)
	quit()

max_people = 0
for i in range(0, len(people)-(k*2), 2):
	summed = 0
	for j in range(i, i+(k*2)):
		current = people[j]
		if current < 0:
			current = -current
		summed += current
	if people[i+(k*2)] > 0:
		summed += people[i+(k*2)]

	max_people = max(max_people, summed)

print(max_people)

Submission status: Wrong Answer

Solution code:
import numpy as np

N, K = map(int, input().split(" "))
S = np.array([int(i) for i in input()])
pos1 = np.linspace(0,N-1,N)[S == 1]
print(pos1)
lst = []
st =[0]
for i in pos1:
    if st[-1] + 1 == i:
        st.append(i)
    else:
        lst.append(st)
        st = [i]
    if i == pos1[-1]:
        lst.append(st)
print(lst)
time = len(lst) - K
if time <= 0:
    print(N)
else:
    print(int(max([max(lst[i+K]) - min(lst[i])+1 for i in range(time)])))

Submission status: Wrong Answer

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

sl = []
c = 1
for i in range(n):
    if (i == 0):
        if (s[i] == "1"):
            sl.append(0)
    if (i != 0):
        if (s[i] == s[i-1]):
            c += 1
        else:
            sl.append(c)
            c = 1
sl.append(c)

cl = []
for i in range(n):
    if(k % 2 == 1):
        if (i % 2 == 0):
            cl.append(sum(sl[i - k:i + k + 1]))
    else:
        if (i % 2 == 1):
            cl.append(sum(sl[i - k:i + k + 1]))

print(max(cl))

Submission status: Wrong Answer

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

last = "1"
cnt = 1

for i, l in enumerate(s):
  if l == last:
    cnt += 1
    if i+1 == len(s):
      patterns.append(cnt)
  else:
    last = l
    patterns.append(cnt)
    cnt = 1

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

Submission status: Wrong Answer

Solution code:
# coding: utf-8

n, k = map(int, input().split())
s = input()
s += "2"
n += 1

nums = []
if s[0] == "0":
    nums.append(0)

fin = False
i = 0
while i < n:
    if fin:
        break
    for j in range(i, n):
        if s[j] != s[i]:
            nums.append(j-i)
            i = j
            break
        if i == n-1:
            fin = True
            break

if s[-1] == "0":
    nums.append(0)
# print(nums)
length = min([2*k+1, len(nums)])

ans = 0
for i in range(0, length):
    ans += nums[i]

oldsum = ans
for i in range(0, length, 2):
    if i + length + 1 < len(nums):
        newsum = oldsum - (nums[i] + nums[i+1]) + (nums[length+i] + nums[length+2])
        if newsum > ans:
            ans = newsum
        oldsum = newsum
    else:
        break

print(ans)

Submission status: Wrong Answer

Solution code:
n, k = [int(x) for x in input().split()]
s = input()

if len(s) == 1:
    print(1)
else:
    # 最初に塊を把握
    chunk_len = []
    prev = s[0]
    cnt = 1
    for x in s[1:]:
        if x == prev:
            cnt += 1
        else:
            chunk_len.append(cnt)
            cnt = 1
            prev = '1' if prev == '0' else '0'
    chunk_len.append(cnt)
    
    n_chunk = len(chunk_len)
    
    max_score = 0
    if s[0] == '1':
        # 偶数番目を順番に入れ替えていく
        for i in range(int(n_chunk / 2) - k + 1):
            score = sum(chunk_len[2*i:2*(i+k)+1])
            if max_score < score:
                max_score = score
    else:
        # 奇数番目を順番に入れ替えていく
        for i in range(int(n_chunk / 2) - k + 1):
            if i == 0:
                score = sum(chunk_len[:2*(i+k)])
            else:
                score = sum(chunk_len[2*i-1:2*(i+k)])
            
            if max_score < score:
                max_score = score
        
    print(max_score)

Submission status: Wrong Answer

Solution code:
N,K = [int(x) for x in input().split()]
S = input()
li = []
ans = 0
l = 0
for index,x in enumerate(S):
    if(index==0):
        if(x=='0'):
            li+=[0]
            li+=[1]
        else:
            li+=[1]
    else:
        if(S[index-1]!=x):
            li+=[1]
        else:
            li[-1]+=1

    if(index==N-1):
        if(x!='0'):
            li+=[0]
        li+=[0]

while(l+2*K<len(li)):
    ans = max(sum(li[l:l+2*K+1]),ans)
    l+=2

print(ans)

Submission status: Wrong Answer

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

last = "1"
cnt = 1

for i, l in enumerate(s):
  if l == last:
    cnt += 1
    if i+1 == len(s):
      patterns.append(cnt)
  else:
    last = l
    patterns.append(cnt)
    cnt = 1

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

Submission status: Wrong Answer

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


status = S[0]
temp = 0
values = []

for i in range(N):
    if S[i] == status:
        temp += 1
    else:
        if status == '1':
            temp *= -1
        values.append(temp)
        status = S[i]
        temp = 1

if status == '1':
    temp *= -1
values.append(temp)

if len(values) == 1:
    if values[0] < 0:
        print(abs(values[0]))
    elif K >= 1:
        print(abs(values[0]))
    exit()

ans = 0

for i in range(len(values)):
    temp = 0
    renzoku = 0

    if values[i] < 0:
        renzoku = (2 * K + 1)
    else:
        renzoku = 2 * K

    if i + renzoku > len(values):
        break

    for x in range(renzoku):
        temp += abs(values[i + x])

    ans = max(ans, temp)

print(ans)

Submission status: Wrong Answer

Solution code:
n, k=map(int, input().split())
s=list(map(int, list(input())))
renzoku_list=[]
sum=1
before=s[0]
for i in range(1, n):
    if s[i]==before:
        sum=sum+1
        before=s[i]
    else:
        renzoku_list.append(sum)
        sum=1
        before=s[i]
renzoku_list.append(sum)

print(renzoku_list)
"""
result_list=[]
length=len(renzoku_list)
if k*2+1>=length:
    print(n)
else:
    for i in range(length-(2*k+1-1)):
        result=0
        if i==0 and s[i]==0:
            for j in range(2*k):
                result=result+renzoku_list[j]
        elif i==length-(2*k+1-1)-1 and s[i]==0:
            for j in range(length-2*k, n-1):
                result=result+renzoku_list[j]
        else:
            for j in range(i, i+2*k+1):
                result=result+renzoku_list[j]
            result_list.append(result)
    print(max(result_list))
"""