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: Accepted

Solution code:
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
    if len(s0) == len(s1):
        s1.append(0)

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

main()

Submission status: Accepted

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

S = input()
para = [0]
for i, s in enumerate(S):
    if i == 0:
        para.append(1)
    else:
        if S[i] == S[i-1]:
            para[-1] += 1
        else:
            para.append(1+para[-1])
i = 0
if (S[-1] == '0'):
    para.append(para[-1])

#print(para)
tmpmax = 0

if S[0] == '1':
    if i + 2*k + 1 >= len(para) - 1:
        print(n)
    else:
        while i + 2*k + 1 <= len(para) - 1:
            j = i + 2*k + 1
            tmpmax = max(tmpmax, para[j] - para[i])
            #if para[j] - para[i-1] > tmpmax:
            #    tmpmax = para[j] - para[i]

            i += 2
        print(tmpmax)
else:
    if i + 2*k >= len(para) - 1:
        print(n)
    else:
        while i + 2*k <= len(para) - 1:
            j = i + 2*k
            if i == 0:
                tmpmax = max(tmpmax, para[j] - para[i])
                #if para[j] - para[i] > tmpmax:
                #    tmpmax = para[j] - para[i]
            else:
                tmpmax = max(tmpmax, para[j] - para[i-1])
                #if para[j] - para[i-1] > tmpmax:
                #    tmpmax = para[j] - para[i-1]

            i += 2
        print(tmpmax)

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
s=list(map(int,list(input())))
t=[0]+[1 if s[0]==1 else -1]
ans=0
for i in range(1,n):
    if s[i]==s[i-1]:
        t[-1]=i+1 if s[i]==1 else -i-1
    else:
        t+=[i+1 if s[i]==1 else -i-1]
for i in range(len(t)-1):
    temp=0
    l=k*2+1 if t[i+1]>0 else k*2
    temp=abs(t[min(len(t)-1,i+l)])-abs(t[i])
    ans=max(ans,temp)
print(ans)

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
s=str(input())
s=s+'e'
arr=[[] for _ in range(2)]
count=1
for i in range(n):
  if s[i]==s[i+1]:
    count+=1
  else:
    if s[i]=='1':
      arr[0].append(count)
      count=1
    else:
      arr[1].append(count)
      count=1
if len(arr[1])<=k:
  print(n)
else:
  if s[0]=='0':
    arr2=[0]
    arr2.extend(arr[0])
    arr[0]=arr2
  arr[0].append(0)
  arr[1].append(0)
  tmp=0
  sum=0
  count=0
  max=0
  flag=False
  for i in range(len(arr[1])-1):
    sum+=arr[0][i]
    sum+=arr[1][i]
    count+=1
    if flag:      
      sum-=arr[0][i-k]
      sum-=arr[1][i-k]
    if count==k:
      tmp=sum
      tmp+=arr[0][i+1]
      if tmp>max:
        max=tmp
      count-=1
      flag=True
  print(max)

Submission status: Accepted

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

start = [0]
end = []

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

for i, (c0, c1) in enumerate(zip(s, s[1:])):
    if c0 == "1" and c1 == "0":
        end.append(i + 1)
    elif c0 == "0" and c1 == "1":
        start.append(i + 1)

if s[-1] == "1":
    end.append(n)
if n > 1 and s[-1] == "0":
    end.append(n)
    start.append(n)

k = min(k, len(start) - 1)

print(max(en - st for st, en in zip(start, end[k:])))

Submission status: Accepted

Solution code:
def main():
  from itertools import groupby

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

  num = []
  cot = []
  for k, n in groupby(S):
    num.append(k)
    cot.append(len(list(n)))

  if num[-1] == "0":
    cot += [0]
    num += [0]

  X = [0]*(len(cot)+1)
  for i in range(len(cot)):
    X[i+1] = X[i]+cot[i]

  ans = 0
  for i in range(len(X)):
    x = K*2
    if num[i] == "1":
      x += 1
    try:
      ans = max(ans, X[i+x]-X[i])
    except:
      break
  if ans == 0:
    ans = len(S)
  print(ans)

if __name__ == '__main__':
  main()

Submission status: Accepted

Solution code:
t,s=open(0)
n,k=map(int,t.split())
l=2*k+1
x=[0]*(2+int(s[0]))+[i+1for i in range(n-1)if s[i]!=s[i+1]]+[n]*l
print(max(x[i+l]-x[i]for i in range(0,len(x)-l,2)))

Submission status: Accepted

Solution code:
#!/usr/bin/env python
N, K = map(int, input().split())
S = input()
L=[0]
#even:1
#odd:0
tmp="1"
for i in range(N):
    if tmp!=S[i]:
        L.append(1)
    else:
        L[-1]+=1
    tmp=S[i]
if tmp=="0":
    L.append(0)
M=len(L)
if M<=2*K+1:
    print(N)
    exit()
X=[0 for i in range(M+1)]
for i in range(M):
    X[i+1]=L[i]+X[i]
ans=0
for i in range(0,M-2*K,2):
    ans=max(ans,X[i+2*K+1]-X[i])
print(ans)

Submission status: Accepted

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

l = list(map(int,input()))
ll = []
p = l[0]
co = 1
ppp = 0
su = 0
lll = []
for i in range(n-1):
    if p == l[i+1]:
        co += 1
    else:
        if p == 0:
            ppp += 1
        ll.append([p,co])
        p = l[i+1]
        co = 1
if p == 0:
    ppp += 1
ll.append([p,co])
co = 0
if ll[0][0] == 0:
    ll.insert(0,[1,0])
if ll[-1][0] == 0:
    ll.append([1,0])
if len(ll) == 1:
    print(n)
    exit(0)
if ppp <= k:
    print(n)
    exit(0)
co = 0
for i in range(2*k+1):
    co += ll[i][1]
lll.append(co)
for i in range(ppp-k):
    co += ll[i*2+1+2*k][1] + ll[i*2+2+2*k][1] - (ll[i*2][1] + ll[i*2+1][1])
    lll.append(co)
print(max(lll))

Submission status: Accepted

Solution code:
from itertools import groupby
n, k = map(int, input().split())
s = input()

t = []
if s[0] == "0":
    t.append(("1", 0))
for c, v in groupby(s):
    t.append((c, sum(1 for _ in v)))

if s[-1] == "0":
    t.append(("1", 0))

S = [t[0][1]]
rm = [0]
m = len(t)
for i in range(m//2):
    tmp = S[-1]
    S.append(tmp+t[2*i+1][1]+t[2*i+2][1])
    rm.append(tmp+t[2*i+1][1])

ans = []
for i in range(m//2+1):
    if i - k < 0:
        ans.append(S[i])
    else:
        ans.append(S[i]-rm[i-k])
print(max(ans))

Submission status: Accepted

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

index_lst = []
num_lst = []
before = '-1'
for K, i in enumerate(s):
    if i != before:
        before = i
        index_lst.append(K)
        num_lst.append(int(i))
N = len(index_lst)
index_lst += [n] * (2*k+10)

S = 0
for i in range(N):
    S = max(S, index_lst[i+2*k] - index_lst[i]) if num_lst[i] == 0 else max(S, index_lst[i+2*k+1] - index_lst[i])
print(S)

Submission status: Accepted

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

index_lst = []
num_lst = []
before = '-1'
for K, i in enumerate(s):
    if i != before:
        before = i
        index_lst.append(K)
        num_lst.append(int(i))
N = len(index_lst)
index_lst += [n] * (2*k+10)
#print('index_lst: ', index_lst)
#print('num_lst: ', num_lst)

S = 0
for i in range(N):
    if num_lst[i] == 0:
        S = max(S, index_lst[i+2*k] - index_lst[i])
    else:
        S = max(S, index_lst[i+2*k+1] - index_lst[i])
print(S)

Submission status: Accepted

Solution code:
n, k = map(int, input().rstrip().split())
s = input()
li1 = []
li2 = []
A = []
if n == 1:
    ans = 1
else:
    for i in range(n - 1):
        if s[i] == "0" and s[i + 1] == "1":
            li1.append(i)
        elif s[i] == "1" and s[i + 1] == "0":
            li2.append(i)
    if s[n - 1] == "0":
        li1.append(n - 1)
    if len(li1) <= k:
        ans = n
    else:
        if s[0] == "1":
            for j in range(len(li1) - k + 1):
                if j == 0:
                    A.append(li2[k] + 1)
                elif j == len(li1) - k:
                    A.append(n - li1[j - 1] - 1)
                else:
                    A.append(li2[k + j] - li1[j - 1])
        else:
            for j in range(len(li1) - k + 1):
                if j == 0:
                    A.append(li2[k - 1] + 1)
                elif j == len(li1) - k:
                    A.append(n - li1[j - 1] - 1)
                else:
                    A.append(li2[k + j - 1] - li1[j - 1])
        ans = max(A)
print(ans)

Submission status: Accepted

Solution code:
def solve():
  N, K = map(int, input().split())
  s = input()
  S = []
  look = s[0]
  cnt = 0

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

  num = min(len(S), 2 * K + 1)
  part = sum(S[0:num])
  ans = part

  for i in range(0, len(S) - num, 2):
    part += - sum(S[i:i+2]) + sum(S[i + num:i + num + 2])
    ans = max(ans, part)
  return ans

print(solve())

Submission status: Accepted

Solution code:
import bisect;n,k=map(int,input().split());s=input();f=[0]*n;f[0]=1-int(s[0])
for i in range(1,n):f[i]=(1-int(s[i]))*int(s[i-1])
for i in range(n-1):f[i+1]+=f[i]
a=0
for l in range(n):r=bisect.bisect_right(f,k+f[l]*int(s[l]));a=max(a,r-l)
print(a)

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!

import numpy as np

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

s = input()

now = s[0]
tmp = 0

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

for i in s:
    if i != now:
        now = i
        h.append(tmp)
        tmp=0
    tmp+=1
h.append(tmp)

h = np.array(h)

ans = 0

for i in range(0,len(h),2):
    ans = max(ans,np.sum(h[i:i+k*2+1]))
    
print(ans)

Submission status: Accepted

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

l = list(map(int,input()))
ll = []
p = l[0]
co = 1
ppp = 0
su = 0
lll = []
for i in range(n-1):
    if p == l[i+1]:
        co += 1
    else:
        if p == 0:
            ppp += 1
        ll.append([p,co])
        p = l[i+1]
        co = 1
if p == 0:
    ppp += 1
ll.append([p,co])
co = 0
if ll[0][0] == 0:
    ll.insert(0,[1,0])
if len(ll) == 1:
    print(n)
    exit(0)
if ppp <= k:
    print(n)
    exit(0)
co = 0
if ll[-1][0] == 1:
    for i in range(2*k+1):
        co += ll[i][1]
    lll.append(co)
    co -= ll[0][1] + ll[1][1]
    for i in range(ppp-k):
        if i == 0:
            co += ll[i*2+1+2*k][1] + ll[i*2+2+2*k][1]
            lll.append(co)
        else:
            co += ll[i*2+1+2*k][1] + ll[i*2+2+2*k][1] - (ll[i*2][1] + ll[i*2+1][1])
            lll.append(co)
else:
    for i in range(2*k+1):
        co += ll[i][1]
    lll.append(co)
    co -= ll[0][1] + ll[1][1]
    for i in range(ppp-k):
        if ppp-k-1 == i and i == 0:
            co += ll[-1][1]
            lll.append(co)
        elif ppp-k-1 == i:
            co += ll[-1][1] - (ll[i*2][1] + ll[i*2+1][1])
            lll.append(co)
        elif i == 0:
            co += ll[i*2+1+2*k][1] + ll[i*2+2+2*k][1]
            lll.append(co)
        else:
            co += ll[i*2+1+2*k][1] + ll[i*2+2+2*k][1] - (ll[i*2][1] + ll[i*2+1][1])
            lll.append(co)
print(max(lll))

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
S = input()
last_s = "0"
count = 0
A = []
for i, s in enumerate(S):
	if i == 0:
		count += 1
	else:
		if s != last_s:
			A.append(count)
			count = 0
		count += 1
	last_s = s

if count != 0:
	A.append(count)

A.append(0)
A.append(0)

val = 0

if S[0] == "0":
	val = sum(A[:2*K])
	currentMax = val
	old = A[0]
	new = sum(A[2*K:2*K+2])
	val += new - old
	if val > currentMax:
		currentMax = val
	
	for i in range(1, len(A)-2*K, 2):
		old_sum_of_two = sum(A[i:i+2])
		sum_of_two = sum(A[i+2*K+1:i+2*K+3])
		val += sum_of_two - old_sum_of_two
		if val > currentMax:
			currentMax = val
	print(currentMax)
else:
	val = sum(A[:2*K+1])
	currentMax = val
	for i in range(0, len(A)-2*K, 2):
		old_sum_of_two = sum(A[i:i+2])
		sum_of_two = sum(A[i+2*K+1:i+2*K+3])
		val += sum_of_two - old_sum_of_two
		if val > currentMax:
			currentMax = val
	print(currentMax)

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
S = list(map(int, input()))
 
Z = [j for j, s in enumerate(S) if (j > 0 and s == 0 and S[j] != S[j-1]) or (j == 0 and s == 0)] + [N]
Y = [j for j, s in enumerate(S) if (j > 0 and s == 1 and S[j] != S[j-1]) or (j == 0 and s == 1)]
result = 0
if len(Z) <= K:
    print(N)
else:
    for i in range(len(Z)-K):
        if i == 0:
            result = Z[i+K]
        else:
            result = max(result, Z[i+K] - Y[i if Y[0] < Z[0] else i-1])
    print(result)

Submission status: Accepted

Solution code:
import bisect
n,k=map(int,input().split())
s=list(map(int,input()))
f=[0]*n
f[0]=1-s[0]
for i in range(1,n):f[i]=(1-s[i])*s[i-1]
for i in range(n-1):f[i+1]+=f[i]
a=0
for l in range(n):
    r=bisect.bisect_right(f,k+f[l]*int(s[l]))
    a=max(a,r-l)
print(a)

Submission status: Accepted

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

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



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

Submission status: Accepted

Solution code:
N,K=map(int,input().split());s=input();L=2*K+1;X=[0]*(2-int(s[0]))+[i+1 for i in range(N-1) if s[i]!=s[i+1]]+[N]*L;print(max([X[i+L]-X[i]for i in range(0,len(X)-L,2)]))

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!

import numpy as np

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

s = input()

now = s[0]
tmp = 0

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

for i in s:
    if i != now:
        now = i
        h.append(tmp)
        tmp=0
    tmp+=1
h.append(tmp)

h = np.array(h)

ans = 0

for i in range(0,max(len(h),len(h)-k),2):
    ans = max(ans,np.sum(h[i:i+k*2+1]))
    
print(ans)

Submission status: Accepted

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

l = list(map(int,input()))
ll = []
p = l[0]
co = 1
ppp = 0
su = 0
lll = []
for i in range(n-1):
    if p == l[i+1]:
        co += 1
    else:
        if p == 0:
            ppp += 1
        ll.append([p,co])
        p = l[i+1]
        co = 1
if p == 0:
    ppp += 1
ll.append([p,co])
co = 0

if len(ll) == 1:
    print(n)
    exit(0)
if ppp <= k:
    print(n)
    exit(0)
if ll[0][0] == 0:
    if ll[-1][0] == 0:
        for i in range(2*k):
            co += ll[i][1]
        lll.append(co)
        co -= ll[0][1]
        for i in range(ppp-k):
            if ppp-k-1 == i and i == 0:
                co += ll[-1][1]
                lll.append(co)
            elif ppp-k-1 == i:
                co += ll[-1][1] - (ll[i*2-1][1] + ll[i*2][1])
                lll.append(co)
            elif i == 0:
                co += ll[i*2+1+2*k][1] + ll[i*2+2*k][1]
                lll.append(co)
            else:
                co += ll[i*2+1+2*k][1] + ll[i*2+2*k][1] - (ll[i*2-1][1] + ll[i*2][1])
                lll.append(co)

    else:
        for i in range(2*k):
            co += ll[i][1]
        lll.append(co)
        co -= ll[0][1]
        for i in range(ppp-k):
            if i == 0:
                co += ll[i*2+1+2*k][1] + ll[i*2+2*k][1]
                lll.append(co)
            else:
                co += ll[i*2+1+2*k][1] + ll[i*2+2*k][1] - (ll[i*2-1][1] + ll[i*2][1])
                lll.append(co)
else:
    if ll[-1][0] == 1:
        for i in range(2*k+1):
            co += ll[i][1]
        lll.append(co)
        co -= ll[0][1] + ll[1][1]
        for i in range(ppp-k):
            if i == 0:
                co += ll[i*2+1+2*k][1] + ll[i*2+2+2*k][1]
                lll.append(co)
            else:
                co += ll[i*2+1+2*k][1] + ll[i*2+2+2*k][1] - (ll[i*2][1] + ll[i*2+1][1])
                lll.append(co)
    else:
        for i in range(2*k+1):
            co += ll[i][1]
        lll.append(co)
        co -= ll[0][1] + ll[1][1]
        for i in range(ppp-k):
            if ppp-k-1 == i and i == 0:
                co += ll[-1][1]
                lll.append(co)
            elif ppp-k-1 == i:
                co += ll[-1][1] - (ll[i*2][1] + ll[i*2+1][1])
                lll.append(co)
            elif i == 0:
                co += ll[i*2+1+2*k][1] + ll[i*2+2+2*k][1]
                lll.append(co)
            else:
                co += ll[i*2+1+2*k][1] + ll[i*2+2+2*k][1] - (ll[i*2][1] + ll[i*2+1][1])
                lll.append(co)
print(max(lll))

Submission status: Accepted

Solution code:
import bisect
n,k=map(int,input().split())
s=list(map(int,input()))
f=[0 for i in range(n)]
f[0]=1-s[0]
for i in range(1,n):f[i]=(1-s[i])*(s[i-1]^s[i])
for i in range(n-1):f[i+1]+=f[i]
ans=0
for l in range(n):
    r=bisect.bisect_right(f,k+f[l]*s[l])
    ans=max(ans,r-l)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def binarySearch(alist, item):
    first = 0
    last = len(alist) - 1

    while first <= last:
        midpoint = (first + last)//2
        if item < alist[midpoint] and alist[midpoint - 1] < item or midpoint == 0 and item < alist[midpoint]:
            return midpoint
        else:
            if item < alist[midpoint]:
                last = midpoint - 1
            else:
                first = midpoint + 1
    return -1


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


def main():
    n, k, s = load_input()

    starts = [0]
    ends = []
    for i in range(n):
        if 0 < i and s[i - 1] == '1' and s[i] == '0':
            ends.append(i)

        if 0 < i and s[i - 1] == '0' and s[i] == '1':
            starts.append(i)
    ends.append(n)

    ml = 0
    for start in starts:
        order = k
        if start == 0 and s[0] == '0':
            order -= 1
        i = binarySearch(ends, start)
        if i + order < len(ends):
            end = ends[i + order]
        else:
            end = ends[-1]
        if ml < end - start:
            ml = end - start
    print(ml)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import numpy as np

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

l = [[], []]
ans = []
k = [int(s) for s in S]
if k[0] == 0:
    l[1].append(0)
v0 = 1 if k[0] == 0 else 0
v1 = 1 if k[0] == 1 else 0
for i in range(1, N):
    if (k[i]==0) & (k[i-1]==0):
        v0 += 1
    elif (k[i]==1) & (k[i-1]==0):
        l[0].append(v0)
        v0 = 0
        v1 += 1
    elif (k[i]==0) & (k[i-1]==1):
        l[1].append(v1)
        v0 += 1
        v1 = 0 
    else:
        v1 += 1
        
for i, v in enumerate([v0, v1]):
    if v > 0:
        l[i].append(v)
        
if k[0] == 0:
    l[1].append(0)

if len(l[0]) <= K:
    print(N)

else:
    l[0].extend(np.repeat(0,K+1))
    l[1].extend(np.repeat(0,K+1))
    a = sum(l[0][:K]) + sum(l[1][:K+1])
    ans.append(a)
    for i in range(1, len(l[0])-K-1):
        a -= l[0][i-1] + l[1][i-1]
        a += l[0][i+K-1] + l[1][i+K]
        ans.append(a)
    print(max(ans))

Submission status: Accepted

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

S = S + 'E'   # 終端処理
ans = -1    # 答えの初期値（十分に小さい値）

# 立った人の連続の数
if (S[0] == '0'):
    stand = 1
else:
    stand = 0

L = 0
R = 0

# 尺取り法
while (R < N):
    # 右端を進めることが可能（立った人の連続の数がK以下）なら、右端を進める
    if (stand <= K):
        # 立った人の連続の集合数が増えるなら1足す
        if (S[R] == '1' and S[R + 1] == '0'):
            stand += 1
        # 答えの更新
        ans = max(ans, R - L + 1)
        R += 1
    # 進められない場合、左端と右端が同じなら両者を進める
    # 左端が右端を追い越さないための処理
    elif (L == R):
        L += 1
        R += 1
    # それ以外は左端のみを進める
    else:
        # 立った人の連続の数が減るなら1減らす
        if (S[L] == '0' and S[L + 1] == '1'):
            stand -= 1
        L += 1
        # 左端のみを進めた場合は答えを更新し得ない

print(ans)

Submission status: Accepted

Solution code:
n,k=map(int, input().split())
s=input()
st=[0]
for i in range(1,n):
    if(s[i]!=s[i-1]):
        st.append(i)
st.append(n)
a=len(st)-1
if(s[0]=="0"):
    ans=st[min(2*k,a)]
    for i in range(1,a+1,2):
        ans=max(ans,st[min(2*k+1+i,a)]-st[i])
elif(s[0]=="1"):
    ans=0
    for i in range(0,a+1,2):
        ans=max(ans,st[min(2*k+1+i,a)]-st[i])
print(ans)

Submission status: Accepted

Solution code:
import math                                                                                            

n,k = raw_input().split()
s = list(raw_input())


cnt = 0

prev = ''
tmp = ''
ar = []
for i in s:
    if i == '0' and prev !=  1:
        tmp += '0'
        prev = 0
    elif i == '0' and prev == 1:
        prev = 0
        ar.append(tmp)
        tmp = '0'
    elif i == '1' and prev != 0:
        tmp += '1'
        prev = 1
    elif i == '1' and prev == 0:
        prev = 1
        ar.append(tmp)
        tmp = '1'

ar.append(tmp)
k = int(k)


j = 0
cnt = 0
total = 0
m = 0

for i in ar:
    total += len(i)
    if '0' in i:
        cnt += 1
        if cnt == k + 1:                           
            total -= len(ar[j-2*k]) 
            if j >= 2*k + 1:                       
                total -= len(ar[j-2*k-1])
            cnt = k
    m = max(m, total)
    j += 1

print m

Submission status: Accepted

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

if s[0] == "1":
    retu = "whiteblack"
else:
    retu = "blackwhite"


count = 0
pre_person = s[0]
groups = []
for i in range(n):
    person = s[i]

    if person != pre_person:
        groups.append(count)
        count = 0
        pre_person = person
    count += 1
groups.append(count)


maxscore1 = 0
if retu == "whiteblack":
    score1 = 0
    blackgroups = len(groups)//2
    if k < blackgroups:
        start_index = 1
        score1 = groups[start_index-1]
        for j in range(k):
            score1 += groups[start_index+2*j]
            if start_index + 2*j+1 < len(groups):
                score1 += groups[start_index+2*j+1]
        maxscore1 = max(score1, maxscore1)
        for i in range(blackgroups - k):
            score1 -= groups[start_index + 2*i - 1]
            score1 -= groups[start_index + 2*i]
            score1 += groups[start_index + 2*k +2*i]
            if start_index+2*k+1+2*i < len(groups):
                score1 += groups[start_index + 2 * k +1 + 2 * i]
            maxscore1 = max(score1, maxscore1)
    else:
        maxscore1 = n

else:
    score1 = 0
    blackgroups = len(groups) - len(groups)//2
    if k < blackgroups:
        start_index = 0
        score1 = 0
        for j in range(k):
            score1 += groups[start_index+2*j]
            if start_index + 2*j+1 < len(groups):
                score1 += groups[start_index+2*j+1]

        maxscore1 = max(score1, maxscore1)
        for i in range(blackgroups - k):
            if i >=1:
                score1 -= groups[start_index + 2*i - 1]
            score1 -= groups[start_index + 2*i]
            score1 += groups[start_index + 2*k +2*i]
            if start_index+2*k+1+2*i < len(groups):
                score1 += groups[start_index + 2 * k + 1 + 2 * i]

            maxscore1 = max(score1, maxscore1)
    else:
        maxscore1 = n

print(maxscore1)

Submission status: Accepted

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

mans = [];
seq = 0;
now = S[0];
if(now == "0"):
    mans.append(0);

for i in S:
    if(i == now):
        seq += 1;
    else:
        mans.append(seq);
        seq = 1;
        if(now=="1"):
            now="0"
        else:
            now="1"

if(seq!=0):
    mans.append(seq);
    if(now=="1"):
        now="0"
    else:
        now="1"
if(now=="0"):
    mans.append(0);

mans.append(0);

shakutori = [0]
shakunum = 0;

for i in mans:
    shakunum += i;
    shakutori.append(shakunum);

if(len(shakutori) <= 2*K+1):
    print(shakutori[-1]);
else:
    answer = 0;
    for i in range(1,len(shakutori),2):
        if(len(shakutori) <= i+2*K):
            break;
        else:
            if(shakutori[i+2*K]-shakutori[i-1] > answer):
                answer = shakutori[i+2*K]-shakutori[i-1];
    print(answer);

Submission status: Accepted

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

cum = [0]
last = S[0]
cnt = 0
for s in S:
    if s == last:
        cnt += 1
    else:
        cum.append(cum[-1] + cnt)
        cnt = 1
        last = s
cum.append(cum[-1] + cnt)

ans = 0
start = 0 if S[0] == '1' else 1
for i in range(start, len(cum), 2):
    ans = max(ans, cum[min(i + 2 * k + 1, len(cum) - 1)] - cum[i])

if S[0] == '0':
    ans = max(ans, cum[min(2 * k, len(cum) - 1)])

print(ans)

Submission status: Accepted

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

S = S + 'E'   # 終端処理
ans = -1    # 答えの初期値（十分に小さい値）

# 立った人の連続の数
if (S[0] == '0'):
    stand = 1
else:
    stand = 0

L = 0
R = 0

# 尺取り法
while (R < N):
    # 右端を進めることが可能（立った人の連続の数がK以下）なら、右端を進める
    if (stand <= K):
        # 立った人の連続の集合数が増えるなら1足す
        if (S[R] == '1' and S[R + 1] == '0'):
            stand += 1
        ans = max(ans, R - L + 1)
        R += 1
    # 進められない場合、左端と右端が同じなら両者を進める
    # 左端が右端を追い越さないための処理
    elif (L == R):
        L += 1
        R += 1
    # それ以外は左端のみを進める
    else:
        # 立った人の連続の数が減るなら1減らす
        if (S[L] == '0' and S[L + 1] == '1'):
            stand -= 1
        L += 1

print(ans)

Submission status: Accepted

Solution code:
def solve():
  N, K = map(int, input().split())
  s = input()
  S = []
  b = s[0]
  cnt = 0

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

  num = min(len(S), 2 * K + 1)
  part = sum(S[0:num])
  ans = part

  for i in range(0, len(S) - num, 2):
    part = part - sum(S[i:i+2]) + sum(S[i + num:i + num + 2])
    ans = max(ans, part)

  return ans

print(solve())

Submission status: Accepted

Solution code:
n, k = list(map(int, input().split()))
s = str(input())
prev = '1'
r = []
for i in range(n):
  if s[i] != prev:
    if s[i] == '0':
      f = i
    else:
      r.append((f, i))
  prev = s[i]
if s[-1] == '0':
  r.append((f, n))
if k >= len(r):
  print(n)
else:
  a = 0
  for i in range(len(r) - k + 1):
    if i == 0:
      begin = 0
    else:
      begin = r[i-1][1]
    if i == len(r) - k:
      end = n
    else:
      end = r[i+k][0]
    a = max(a, end - begin)
  print(a)

Submission status: Accepted

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

def Solve(S,K):

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

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

    sum_LA = list(numpy.cumsum(LA))
    sum_LB=list(numpy.cumsum(LB))
    

    for i in range(len(LA)-K+1):
        if i==0:
            result=sum_LA[K-1]+sum_LB[K-1]
        elif i==1:
            result=sum_LA[i+K-1]+sum_LB[i+K-1]-sum_LB[i-1]
        else:
            result=sum_LA[i+K-1]-sum_LA[i-2]+sum_LB[i+K-1]-sum_LB[i-1]
        mx=max(mx,result)

    return mx

print(Solve(S,K))

Submission status: Accepted

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

zero_positions = [0] + [i for i, c in enumerate(S) if c==0]
count0 = 0
start = 0
end = 0
numbers = 0
index = 0
load_0 = False
l_0 = False
scores = []
while True:
    if (not load_0) and S[index] == '0':
        count0 += 1
        load_0 = True
    elif load_0 == True and S[index] == '1':
        load_0 = False
    if count0 == K+1:
        scores.append((index-1) - start + 1)
        if (not l_0) and S[start] == '0':
            l_0 = True
        while count0 > K:
            start += 1
            if (not l_0) and S[start] == '0':
                l_0 = True
            elif l_0 == True and S[start] == '1':
                count0 -= 1
                l_0 = False
    if index == N - 1 :
        scores.append(index-start + 1)
        break
    index += 1
        
print(max(scores))

Submission status: Accepted

Solution code:
#
import sys
import math
import numpy as np
import itertools

# いくつか入力
n,k = (int(i) for i in input().split())  

# リストに入れる
s = [int(i) for i in list(input())] 

j=0
t = [ [s[0],1]]

for i in range(1,n):
    if s[i] == t[j][0]:
        t[j][1] += 1
    else:
        j += 1
        t.append([s[i],1])
#print(t)

if t[0][0] == 0:
    t.insert(0,[1,0])
if t[-1][0] == 0:
    t.append([1,0])
#print(t)

z = len(t) // 2

if z <= k:
    print(n)
    exit()
m = 0
for i in range(k*2+1):
    m += t[i][1]
ma = m
#print(z,ma)
t.append([1,0])
#print(t)
for i in range(z-k):
    m += (t[k*2+i*2+1][1]+t[k*2+i*2+2][1])
    m -= (t[i*2][1]+t[i*2+1][1])
    ma = max(ma,m)
#    print(t[i*2][1],t[i*2+1][1],t[k*2+i*2+1][1],t[k*2+i*2+2][1],ma)

print(ma)

Submission status: Accepted

Solution code:
def solve():
  N, K = map(int, input().split())
  _S = input()
  S = []
  b = _S[0]
  s = 0
  if b == '0': S.append(0)
  for _s in _S:
    if b == _s:
      s = s + 1
    else:
      b = _s
      S.append(s)
      s = 1
  S.append(s)
  if b == '0': S.append(0)
 
  num = min(len(S), 2 * K + 1)
  # n = sum(S[0:num if _S[0] == '1' else (num - 1)])
  n = sum(S[0:num])
  m = n
  # for i in range(0 if _S[0] == '1' else -1, len(S) - num, 2):
  for i in range(0, len(S) - num, 2):
    n = n - sum(S[i:i+2])
    n = n + sum(S[i + num:i + num + 2])
    m = max(m, n)
 
  return m
 
print(solve())