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:
from collections import Counter,defaultdict,deque
import sys
from itertools import permutations, combinations
from heapq import heappop, heappush
# input = sys.stdin.readline
sys.setrecursionlimit(10**8)
mod = 10**9+7
    
def inp(): # n=1
    return int(input())
def inpm(): # x=1,y=2
    return map(int,input().split())
def inpl(): # a=[1,2,3,4,5,...,n]
    return list(map(int, input().split()))
def inpls(): # a=['1','2','3',...,'n']
    return list(input().split())
def inplm(n): # x=[] 複数行
    return list(int(input()) for _ in range(n))
def inpll(n): # [[1,1,1,1],[2,2,2,2],[3,3,3,3]]
    return sorted([list(map(int, input().split())) for _ in range(n)])


def main():
    n,k = inpm()
    s = list(input())
    cnt = []
    be = ''
    c = 0
    for i in range(n):
        if i == 0:
            be = s[i]
            c = 1                        
        elif s[i] != be:
            if be == '0':
                cnt.append((c,0))
            else:
                cnt.append((c,1))
            be = s[i]
            c = 1
        else:
            c += 1
    cnt.append((c,int(be)))
    ans = []
    length = len(cnt)
    if length//2 + (1-cnt[0][1]) <= k:
        print(n)
        return
    l = 0
    r = 2*k + cnt[0][1]
    s = 0
    for i in range(l,r):
        s += cnt[i][0]
    ans.append(s)
    l += 1
    bl = l
    br = r
    while r < length:
        if cnt[l][1] == 0:
            l += 1
            r += 1
        else:
            if l-2 >= 0:
                s -= cnt[l-2][0]
            s -= cnt[l-1][0]
            s += cnt[r-1][0]
            s += cnt[r][0]
            ans.append(s)
            l += 2
            r += 2
            if r == length:
                r -= 1
        bl = l
        br = r
    print(max(ans))

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
from collections import Counter,defaultdict,deque
import sys
from itertools import permutations, combinations
from heapq import heappop, heappush
# input = sys.stdin.readline
sys.setrecursionlimit(10**8)
mod = 10**9+7
    
def inp(): # n=1
    return int(input())
def inpm(): # x=1,y=2
    return map(int,input().split())
def inpl(): # a=[1,2,3,4,5,...,n]
    return list(map(int, input().split()))
def inpls(): # a=['1','2','3',...,'n']
    return list(input().split())
def inplm(n): # x=[] 複数行
    return list(int(input()) for _ in range(n))
def inpll(n): # [[1,1,1,1],[2,2,2,2],[3,3,3,3]]
    return sorted([list(map(int, input().split())) for _ in range(n)])


def main():
    n,k = inpm()
    s = list(input())
    cnt = []
    be = ''
    c = 0
    for i in range(n):
        if i == 0:
            be = s[i]
            c = 1                        
        elif s[i] != be:
            if be == '0':
                cnt.append((c,0))
            else:
                cnt.append((c,1))
            be = s[i]
            c = 1
        else:
            c += 1
    cnt.append((c,int(be)))
    ans = []
    length = len(cnt)
    if length//2 + (1-cnt[0][1]) <= k:
        print(n)
        return
    l = 0
    r = 2*k + cnt[0][1]
    s = 0
    for i in range(l,r):
        s += cnt[i][0]
    ans.append(s)
    l += 1
    bl = l
    br = r
    while r < length:
        if cnt[l][1] == 0:
            l += 1
            r += 1
        else:
            if l-2 >= 0:
                s -= cnt[l-2][0]
            s -= cnt[l-1][0]
            s += cnt[r-1][0]
            s += cnt[r][0]
            ans.append(s)
            l += 2
            r += 2
            if r == length:
                r -= 1
        bl = l
        br = r
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
N,K=[int(s) for s in input().split()]
S=input()
ls01=[0]
ls10=[0]
for i in range(1,N):
  if S[i-1]=='0' and S[i]=='1':
    ls01.append(i)
  elif S[i-1]=='1' and S[i]=='0':
    ls10.append(i)
ls10.append(N)
ls01.append(N)
if K>=len(ls10):
  print(N)
else:
  ls=[ls10[i+K]-ls01[i] for i in range(len(ls10)-K)]
  print(max(ls))

Submission status: Wrong Answer

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

if not "0" in S:
    print(N)
    exit()

if N == 1:
    if K > 0:
        print(N)
    else:
        print(0)
    exit()

key = [S[0]]
count = []
now = S[0]
cnt = 1
for i,x in enumerate(S[1:],1):
    if now != x:
        count.append(cnt)
        cnt,now = 1,x
        key.append(x)
    else:
        cnt += 1
count.append(cnt)

left = 0
answer = count[0]
for right in range(1,len(key)):
    while key[left:right+1].count("0") > K:
        left += 1
    if right < len(key)-1 and key[right] == "0":
            answer = max(answer, count[right+1] + sum(count[left:right+1]))
    answer = max(answer, sum(count[left:right+1]))
    print(left,right,answer)

print(answer)

Submission status: Wrong Answer

Solution code:
def solve():
    N, K = map(int, input().split())
    S = input()
    
    if all([char == '1' for char in S]):
        print(len(S))
        exit()
    
    cur = S[0]
    start = [0]
    end = []
    for i, char in enumerate(S):
        if cur == '0' and char == '1':
            start.append(i)
        elif cur == '1' and char == '0':
            end.append(i)
        cur = char
    end.append(len(S))
    
    ans = 0
    for s, e in zip(start, end[K+int(S[0])-1:]):
        ans = max(ans, e - s)
    if ans == 0:
        if not all([char == '0' for char in S]):
            ans = len(S)
    print(ans)


if __name__ == '__main__':
    solve()

Submission status: Wrong Answer

Solution code:
def solve():
    N, K = map(int, input().split())
    S = input()
    
    if all([char == '1' for char in S]):
        print(len(S))
        exit()
    
    cur = S[0]
    start = [0]
    end = []
    for i, char in enumerate(S):
        if cur == '0' and char == '1':
            start.append(i)
        elif cur == '1' and char == '0':
            end.append(i)
        cur = char
    end.append(len(S))
    
    ans = 0
    for s, e in zip(start, end[K+int(S[0])-1:]):
        ans = max(ans, e - s)
    print(ans)


if __name__ == '__main__':
    solve()

Submission status: Wrong Answer

Solution code:
# 全然わからなかった、もう一度解くべき
# 解説動画がわかりやすい
# https://www.youtube.com/watch?v=FRzpDCx17vw&feature=youtu.be

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

Nums = [] 
now = 1 # 今見ている数
cnt = 0 # nowがいくつ並んでいるか

for i in range(N):
  if S[i] == str(now):
    cnt += 1
  else:
    Nums.append(cnt)
    cnt = 1
    now = 1 - now

if cnt != 0:
  Nums.append(cnt)

# 1-0-1-0-1-0-1 って感じの配列が欲しい
# 1-0-1-0-1-0 みたいに0で終わっていたら適当に1つ足す
if len(Nums) % 2 == 0:
    Nums.append(0)

Add = 2 * K + 1
# 累積和を作る
# 0 1 2 3 4 5 6
#  0 1 2 3 4 5
sum = [0] * (len(Nums) + 1)
for i in range(len(Nums)):
    sum[i + 1] = sum[i] + Nums[i]

print(Nums ,sum)
ans = 0
# 1-0-1... の1から始めるので、偶数番目だけ見る
for i in range(0, len(Nums), 2):
    print(i, sum[i])
    # 次のleft, rightを計算する [left, right)
    left = i
    right = min(i + Add, len(Nums))
    tmp = sum[right] - sum[left]
    ans = max(tmp, ans)

print(ans)

Submission status: Wrong Answer

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

#K=2
#S="11101010110011"
#N=len(S)

LIST=[[[0] for j in range (2)]for i in range(N)]

t=S[0]
c=0
k=0
for i in range(N):
    if(t!=S[i]):
        LIST[k][0]=c
        LIST[k][1]=t
        t=S[i]
        k+=1
        c=0
    c+=1

LIST[k][0]=c
LIST[k][1]=t
LIST=LIST[0:k+1]
MAX=0

s=0
l=0
r=0

if(LIST[r][1]=="1"):
    s+=LIST[r][0]
    r+=1

while(K>0 and len(LIST)>r):
    s+=LIST[r][0]
    K-=1
    if(len(LIST)-1==r):
        break
    r+=1
    s+=LIST[r][0]
    if(len(LIST)-1==r):
        break
    r+=1


MAX=max(s,MAX)

f=0
if(r<(len(LIST)-1)):
    while(r<len(LIST)):
        if(l< len(LIST)-1):
            s-=LIST[l][0]
            l+=1
            f=1
        if(l<len(LIST)-1 and LIST[l][1]=="0"):
            s-=LIST[l][0]
            l+=1

        if(f==0):
            break
        

        s+=LIST[r][0]
        MAX=max(s,MAX)
        r+=1
        if(len(LIST)==r):
            break
        
        s+=LIST[r][0]
        MAX=max(s,MAX)
        r+=1
        if(len(LIST)==r):
            break

print(MAX)

Submission status: Wrong Answer

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

#K=4
#S="010101000"
#N=len(S)

LIST=[[[0] for j in range (2)]for i in range(N)]

t=S[0]
c=0
k=0
for i in range(N):
    if(t!=S[i]):
        LIST[k][0]=c
        LIST[k][1]=t
        t=S[i]
        k+=1
        c=0
    c+=1

LIST[k][0]=c
LIST[k][1]=t
LIST=LIST[0:k+1]
MAX=0

s=0
l=0
r=0

if(LIST[r][1]=="1"):
    s+=LIST[r][0]
    r+=1

while(K>0 and len(LIST)>r):
    s+=LIST[r][0]
    K-=1
    if(len(LIST)-1==r):
        break
    r+=1
    s+=LIST[r][0]
    if(len(LIST)-1==r):
        break
    r+=1


MAX=max(s,MAX)

f=0
while(r<(len(LIST)-1)):
    if(l< len(LIST)-1):
        s-=LIST[l][0]
        l+=1
        f=1
    if(l<len(LIST)-1 and LIST[l][1]=="0"):
        s-=LIST[l][0]
        l+=1

    if(f==0):
        break

    s+=LIST[r][0]
    MAX=max(s,MAX)
    if(len(LIST)-1==r):
        break
    r+=1
    s+=LIST[r][0]
    MAX=max(s,MAX)
    if(len(LIST)-1==r):
        break
    r+=1

print(MAX)

Submission status: Wrong Answer

Solution code:
print(0)

Submission status: Wrong Answer

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

#K=1
#S="000"
#N=len(S)

LIST=[[[0] for j in range (2)]for i in range(N)]

t=S[0]
c=0
k=0
for i in range(N):
    if(t!=S[i]):
        LIST[k][0]=c
        LIST[k][1]=t
        t=S[i]
        k+=1
        c=0
    c+=1

LIST[k][0]=c
LIST[k][1]=t
LIST=LIST[0:k+1]
MAX=0

s=0
l=0
r=0

if(LIST[r][1]=="1"):
    s+=LIST[r][0]
    r+=1

while(K>0 and len(LIST)>r):
    s+=LIST[r][0]
    K-=1
    if(len(LIST)-1==r):
        break
    r+=1
    s+=LIST[r][0]
    if(len(LIST)-1==r):
        break
    r+=1


MAX=max(s,MAX)

f=0
while(r<len(LIST)):
    if(l< len(LIST)-1):
        s-=LIST[l][0]
        l+=1
        f=1
    if(l<len(LIST)-1 and LIST[l][1]=="0"):
        s-=LIST[l][0]
        l+=1

    if(f==0):
        break

    s+=LIST[r][0]
    MAX=max(s,MAX)
    if(len(LIST)-1==r):
        break
    r+=1
    s+=LIST[r][0]
    MAX=max(s,MAX)
    if(len(LIST)-1==r):
        break
    r+=1

print(MAX)

Submission status: Wrong Answer

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

#K=0
#S="0"
#N=len(S)

LIST=[[[0] for j in range (2)]for i in range(N)]

t=S[0]
c=0
k=0
for i in range(N):
    if(t!=S[i]):
        LIST[k][0]=c
        LIST[k][1]=t
        t=S[i]
        k+=1
        c=0
    c+=1

LIST[k][0]=c
LIST[k][1]=t
LIST=LIST[0:k+1]
MAX=0

s=0
l=0
r=0
f=0

while(K>0 and len(LIST)>r):
    s+=LIST[r][0]
    K-=1
    if(len(LIST)-1==r):
        break
    r+=1
    s+=LIST[r][0]
    if(len(LIST)-1==r):
        break
    r+=1
MAX=max(s,MAX)

f=0
while(r<len(LIST)):
    if(l< len(LIST)-1):
        s-=LIST[l][0]
        l+=1
        f=1
    if(l<len(LIST)-1 and LIST[l][1]=="0"):
        s-=LIST[l][0]
        l+=1
        f=1

    if(f==0):
        break

    s+=LIST[r][0]
    MAX=max(s,MAX)
    if(len(LIST)-1==r):
        break
    r+=1
    s+=LIST[r][0]
    MAX=max(s,MAX)
    if(len(LIST)-1==r):
        break
    r+=1
print(MAX)

Submission status: Wrong Answer

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

K=0
S="0"
N=len(S)

LIST=[[[0] for j in range (2)]for i in range(N)]

t=S[0]
c=0
k=0
for i in range(N):
    if(t!=S[i]):
        LIST[k][0]=c
        LIST[k][1]=t
        t=S[i]
        k+=1
        c=0
    c+=1

LIST[k][0]=c
LIST[k][1]=t
LIST=LIST[0:k+1]
MAX=0

s=0
l=0
r=0
f=0

while(K>0 and len(LIST)>r):
    s+=LIST[r][0]
    K-=1
    if(len(LIST)-1==r):
        break
    r+=1
    s+=LIST[r][0]
    if(len(LIST)-1==r):
        break
    r+=1
MAX=max(s,MAX)

f=0
while(r<len(LIST)):
    if(l< len(LIST)-1):
        s-=LIST[l][0]
        l+=1
        f=1
    if(l<len(LIST)-1 and LIST[l][1]=="0"):
        s-=LIST[l][0]
        l+=1
        f=1

    if(f==0):
        break

    s+=LIST[r][0]
    MAX=max(s,MAX)
    if(len(LIST)-1==r):
        break
    r+=1
    s+=LIST[r][0]
    MAX=max(s,MAX)
    if(len(LIST)-1==r):
        break
    r+=1
print(MAX)

Submission status: Wrong Answer

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

#K=1
#S="11111"
#N=len(S)

LIST=[[[0] for j in range (2)]for i in range(N)]

t=S[0]
c=0
k=0
for i in range(N):
    if(t!=S[i]):
        LIST[k][0]=c
        LIST[k][1]=t
        t=S[i]
        k+=1
        c=0
    c+=1

LIST[k][0]=c
LIST[k][1]=t
LIST=LIST[0:k+1]
MAX=0

s=0
l=0
r=0
f=0
if(LIST[0][1]=="1"):
    s+=LIST[0][0]
    l=0
    r=1
    f=1
MAX=0

while(K>0 and len(LIST)>r ):
    s+=LIST[r][0]
    K-=1
    if(len(LIST)-1==r):
        break
    r+=1
    s+=LIST[r][0]
    if(len(LIST)-1==r):
        break
    r+=1
MAX=max(s,MAX)

if f==1:
    while(r<len(LIST)):
        if(l<len(LIST)):
            s-=LIST[l][0]
            l+=1
        if(l<len(LIST)):
            s-=LIST[l][0]
            l+=1

        s+=LIST[r][0]
        MAX=max(s,MAX)
        if(len(LIST)-1==r):
            break
        r+=1
        s+=LIST[r][0]
        MAX=max(s,MAX)
        if(len(LIST)-1==r):
            break
        r+=1
else:
    s-=LIST[l][0]
    l+=1

    s+=LIST[r][0]
    MAX=max(s,MAX)
    if(len(LIST)-1==r):
        print(MAX)
        exit()
    r+=1
    s+=LIST[r][0]
    MAX=max(s,MAX)
    if(len(LIST)-1==r):
        print(MAX)
        exit()
    r+=1
    while(r<len(LIST)):
        s-=LIST[l][0]
        l+=1
        s-=LIST[l][0]
        l+=1

        s+=LIST[r][0]
        MAX=max(s,MAX)
        if(len(LIST)-1==r):
            break
        r+=1
        s+=LIST[r][0]
        MAX=max(s,MAX)
        if(len(LIST)-1==r):
            break
        r+=1

print(MAX)

Submission status: Wrong Answer

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

K=0
S="1111111"
N=len(S)

LIST=[[[0] for j in range (2)]for i in range(N)]

t=S[0]
c=0
k=0
for i in range(N):
    if(t!=S[i]):
        LIST[k][0]=c
        LIST[k][1]=t
        t=S[i]
        k+=1
        c=0
    c+=1

LIST[k][0]=c
LIST[k][1]=t
LIST=LIST[0:k+1]
MAX=0

s=0
l=0
r=0
f=0
if(LIST[0][1]=="1"):
    s+=LIST[0][0]
    l=0
    r=1
    f=1
MAX=0

while(K>0 and N>r ):
    s+=LIST[r][0]
    K-=1
    if(len(LIST)-1==r):
        break
    r+=1
    s+=LIST[r][0]
    if(len(LIST)-1==r):
        break
    r+=1
MAX=max(s,MAX)

if f==1:
    while(r<len(LIST)):
        if(l<len(LIST)):
            s-=LIST[l][0]
            l+=1
        if(l<len(LIST)):
            s-=LIST[l][0]
            l+=1

        s+=LIST[r][0]
        MAX=max(s,MAX)
        if(len(LIST)-1==r):
            break
        r+=1
        s+=LIST[r][0]
        MAX=max(s,MAX)
        if(len(LIST)-1==r):
            break
        r+=1
else:
    s-=LIST[l][0]
    l+=1

    s+=LIST[r][0]
    MAX=max(s,MAX)
    if(len(LIST)-1==r):
        print(MAX)
        exit()
    r+=1
    s+=LIST[r][0]
    MAX=max(s,MAX)
    if(len(LIST)-1==r):
        print(MAX)
        exit()
    r+=1
    while(r<len(LIST)):
        s-=LIST[l][0]
        l+=1
        s-=LIST[l][0]
        l+=1

        s+=LIST[r][0]
        MAX=max(s,MAX)
        if(len(LIST)-1==r):
            break
        r+=1
        s+=LIST[r][0]
        MAX=max(s,MAX)
        if(len(LIST)-1==r):
            break
        r+=1

print(MAX)

Submission status: Wrong Answer

Solution code:
import sys
if sys.platform in ['ios', 'win32', 'darwin']:
	sys.stdin=open('Untitled.txt')
input = sys.stdin.readline
def INT(): return int(input())
def MAP(): return [int(x) for x in input().split()]

N, K = MAP()
S = input().rstrip()
S = [int(s) for s in S]

A = []
now = -1
l = -1
for i,s in enumerate(S):
	if now == -1:
		l = i
		now = s
	elif now == s: continue
	elif now != s:
		A.append([int(now), l, i, i-l])
		now = s
		l = i
else:
	A.append([int(now), l, i+1, i-l+1])
# print(A)

if S[0] == 1:
	left, right = 0, 2*K+1
else:
	left, right = 0, 2*K
sum_A = sum(a[3] for a in A[left:right])
# print(sum_A)

# print(A)
ans = sum_A
for i in range(1, len(A)):
	if i+1 > len(A)-2: continue
	# print(i)
	now, fr, to, ln = A[i]
	if now:
		sum_A -= A[i-1][3]
		sum_A += A[i+1][3]
		sum_A += A[i+2][3]
		ans = max(ans, sum_A)
	else:
		sum_A -= A[i-1][3]
		ans = max(ans, sum_A)
	# print(now, fr, to, sum_A)
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
n,k=map(int,input().split())
s=input()
a=[(s[0],0)]
for i in range(n-1):
  if s[i]!=s[i+1]:
    a.append((s[i+1],i+1))

ans=0
r=len(a)
if a[0][0]=="0":
  tmp=2*k if 2*k<r else r-1
  ans=a[tmp][1]
else:
  tmp=2*k+1 if 2*k+1<r else r-1
  ans=a[tmp][1]
for i in range(r):
  if a[0][0]=="0":
    tmp=i+2*k if i+2*k<r else r-1
    ans=max(ans,a[tmp][1]-a[i-1][1]+1)
  else:
    tmp=i+2*k+1 if i+2*k+1<r else r-1
    ans=max(ans,a[tmp][1]-a[i][1]+1)
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
n,k=map(int,input().split())
s=input()
a=[(s[0],0)]
for i in range(n-1):
  if s[i]!=s[i+1]:
    a.append((s[i+1],i+1))

ans=0
r=len(a)
if a[0][0]=="0":
  tmp=2*k if 2*k<r else r-1
  ans=a[tmp][1]
else:
  tmp=2*k+1 if 2*k+1<r else r-1
  ans=a[tmp][1]
for i in range(r):
  if a[0][0]=="0":
    tmp=i+2*k if i+2*k<r else r-1
  else:
    tmp=i+2*k+1 if i+2*k+1<r else r-1
  ans=max(ans,a[tmp][1]-a[i][1]+1)
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
import re
n,k=map(int,input().split())
s=input()
a=[]
tmp=1
for i,j in zip(s[:n-1],s[1:]):
  if i==j:
    tmp+=1
  else:
    a.append((i,tmp))
    tmp=1
if tmp!=1:
  a.append((s[-1],tmp))
tmp=sum(1 for i in a if i[0]=="0")
if tmp<=k:
  print(n)
  sys.exit()

Submission status: Wrong Answer

Solution code:
def main():
    from itertools import groupby
    from collections import deque
    N, K = (int(i) for i in input().split())
    S = input()
    ans = 0
    cur_0_val = 0
    cur_0 = deque()
    cur_0_sum = 0
    cur_1 = deque()
    cur_1_sum = 0
    for k, g in groupby(S):
        if k == "0":
            g = list(g)
            cur_0_val += 1
            cur_0.append(len(g))
            cur_0_sum += len(g)
            if cur_0_val > K:
                cur_0_sum -= cur_0.popleft()
                cur_0_val -= 1
                if len(cur_1) >= K:
                    cur_1_sum -= cur_1.popleft()
        elif k == "1":
            g = list(g)
            cur_1.append(len(g))
            cur_1_sum += len(g)
        ans = max(ans, cur_1_sum + cur_0_sum)
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
from collections import deque
n, k = map(int, input().split())
s = input() + "#"
now = 1
c = 0
L = deque()
ans = 0
for i in range(n):
  if s[i] == s[i+1]:
    now += 1
  else:
    if not L and s[i]=="0":
      L.append(0)
    L.append(now)
    c += now
    if len(L) == 2*k+1:
      ans = max(ans, c)
      c -= L.popleft()
      c -= L.popleft()
    now = 1
if ans == 0:
  ans = sum(L)
print(ans)

Submission status: Wrong Answer

Solution code:
from itertools import accumulate

def length(S):
    cnt = 1
    A = []
    for i in range(len(S)-1):
        if S[i] == S[i+1]:
            cnt += 1
        else:
            A.append(cnt)
            cnt = 1
    else:
        A.append(cnt)

    return A

def main():
    N, K = map(int, input().split())
    S = input()
    lens = length(S)
    A = [0] + list(accumulate(lens))
    ans = 0
    if S[0] == '0':
        for i in range(0,len(A),2):
            right = i + 2 * K
            if right >= len(A):
                right = len(A) - 1
            tmp = A[right] - A[i]
            ans = max(ans, tmp)
    else:
        for i in range(1,len(A)+1,2):
            right = i + 2 * K
            if right >= len(A):
                right = len(A) - 1
            tmp = A[right] - A[i-1]
            ans = max(ans, tmp)

    print(ans)



if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

run=[]
import itertools
for key,g in itertools.groupby(S):
  run.append(len(list(g)))
  
import numpy as np
run=np.cumsum(run)

ans=0
if S[0]=="0":
  left=0
  while left<len(run):
    if left==0:
      zone=min(2*K-1,len(run)-1)
      val=run[zone]
      left+=1
    else:
      zone=min(left+2*K,len(run)-1)
      val=run[zone]-run[left-1]
      left+=2
    ans=max(ans,val)
elif S[0]=="1":
  left=0
  while left<len(run):
    if left==0:
      zone=min(2*K,len(run)-1)
      val=run[zone]-run[left-1]
      left+=2
    else:
      zone=min(left+2*K,len(run)-1)
      val=run[zone]-run[left-1]
      left+=2
    ans=max(ans,val)
print(ans)

Submission status: Wrong Answer

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

zero = []
one = []
from itertools import groupby
fo = None
for k, g in groupby(S):
    if k == '0':
        if fo is None:
            fo = True
        zero.append(len(list(g)))
    else:
        if fo is None:
            fo = False
        one.append(len(list(g)))

if len(zero) <= K:
    print(N)
    exit()

zerob = [0]
oneb = [0]
for i in range(len(zero)):
    zerob.append(zerob[i] + zero[i])
for i in range(len(one)):
    oneb.append(oneb[i] + one[i])

l = len(oneb)
ans = 0
for i in range(K, len(zerob)):
    z = zerob[i] - zerob[i-K]
    if fo:
        if i-K-1 < 0:
            a = 0
        else:
            a = i-K-1
    else:
        a = i-K
    if i+1 < l:
        o = oneb[i+1] - oneb[a]
    elif i+1 == l:
        o = oneb[i] - oneb[a]
    else:
        o = oneb[i-1] - oneb[a]
    ans = max(ans, z+o)

print(ans)

Submission status: Wrong Answer

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

zero = []
one = []
from itertools import groupby
for k, g in groupby(S):
    if k == '0':
        zero.append(len(list(g)))
    else:
        one.append(len(list(g)))

if len(zero) <= K:
    print(N)
    exit()

zerob = [0]
oneb = [0]
for i in range(len(zero)):
    zerob.append(zerob[i] + zero[i])
for i in range(len(one)):
    oneb.append(oneb[i] + one[i])

l = len(oneb)
ans = 0
for i in range(K, len(zerob)):
    z = zerob[i] - zerob[i-K]
    if i+1 < l:
        o = oneb[i+1] - oneb[i-K]
    elif i+1 == l:
        o = oneb[i] - oneb[i-K]
    else:
        o = oneb[i-1] - oneb[i-K]
    ans = max(ans, z+o)

print(ans)

Submission status: Wrong Answer

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

zero = []
one = []
from itertools import groupby
for k, g in groupby(S):
    if k == '0':
        zero.append(len(list(g)))
    else:
        one.append(len(list(g)))

if len(zero) < K:
    print(N)
    exit()

zerob = [0]
oneb = [0]
for i in range(len(zero)):
    zerob.append(zerob[i] + zero[i])
for i in range(len(one)):
    oneb.append(oneb[i] + one[i])

l = len(oneb)
ans = 0
for i in range(K, len(zerob)):
    z = zerob[i] - zerob[i-K]
    if i+1 < l:
        o = oneb[i+1] - oneb[i-K]
    elif i+1 == l:
        o = oneb[i] - oneb[i-K]
    else:
        o = oneb[i-1] - oneb[i-K]
    ans = max(ans, z+o)

print(ans)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
s=list(str(input()))
r=[]
l=[]
c=0
d=1
for i in range(n):
	if s[i]=='1' and d==1:
		r.append(i)
		c=1
		d=0
	elif s[i]=='0' and c==1:
		l.append(i-1)
		c=0
		d=1
if c>0:
	l.append(i)
if len(r)<=k+1:
	r=[0]*(k+1-len(r))+r+[n-1]*(k+1-len(r))
	l=[0]*(k+1-len(l))+l+[n-1]*(k+1-len(l))
#print(r)
#print(l)
ans=0
for i in range(len(r)-k):
	ans=max(ans,l[i+k]-r[i]+1)
print(ans)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
s=input()
num=[0]
c=1
for i in range(1,n):

    if s[i]==s[i-1]:
        c+=1
    else:
        num.append(c)
        c=1
num.append(c)
num.append(0)
#print(num)

ans=0
#print(num)
if s[0]=="0":
    if len(num)-2*k+1<=0:
        ans=sum(num)
    else:
        asum=sum(num[0:2*k+1])
        ans=asum
        #print(asum)
        for i in range(2,len(num)-2*k,2):
            #print(asum,num[i+2*k],num[i+2*k-1],num[i-1],num[i-2])
            asum=asum+num[i+2*k]+num[i+2*k-1]-num[i-1]-num[i-2]
            ans=max(ans,asum)
if s[0]=="1":
    if len(num)-2*k+2<=0:
        ans=sum(num)
    else:
        asum=sum(num[1:2*k+2])
        ans=asum
        for i in range(3,len(num)-2*k-1,2):
            #print(asum,num[i+2*k],num[i+2*k-1],num[i-1],num[i-2])
            asum=asum+num[i+2*k]+num[i+2*k-1]-num[i-1]-num[i-2]
            ans=max(ans,asum)
print(ans)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
s=input()
num=[0]
c=1
for i in range(1,n):

    if s[i]==s[i-1]:
        c+=1
    else:
        num.append(c)
        c=1
num.append(c)
num.append(0)
#print(num)

ans=0
print(num)
if s[0]=="0":
    if len(num)-2*k+1<=0:
        ans=sum(num)
    else:
        asum=sum(num[0:2*k+1])
        ans=asum
        #print(asum)
        for i in range(2,len(num)-2*k,2):
            print(asum,num[i+2*k],num[i+2*k-1],num[i-1],num[i-2])
            asum=asum+num[i+2*k]+num[i+2*k-1]-num[i-1]-num[i-2]
            ans=max(ans,asum)
if s[0]=="1":
    if len(num)-2*k+2<=0:
        ans=sum(num)
    else:
        asum=sum(num[1:2*k+2])
        ans=asum
        for i in range(3,len(num)-2*k,2):
            #print(asum,num[i+2*k],num[i+2*k-1],num[i-1],num[i-2])
            asum=asum+num[i+2*k]+num[i+2*k-1]-num[i-1]-num[i-2]
            ans=max(ans,asum)
print(ans)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
s=input()
num=[0]
c=1
for i in range(1,n):

    if s[i]==s[i-1]:
        c+=1
    else:
        num.append(c)
        c=1
num.append(c)
num.append(0)
#print(num)

ans=0
#print(num)
if s[0]=="0":
    if len(num)-2*k+1<=0:
        ans=sum(num)
    else:
        asum=sum(num[0:2*k+1])
        ans=asum
        #print(asum)
        for i in range(2,len(num)-2*k-1,2):
            #print(asum,num[i+2*k],num[i+2*k-1],num[i-1],num[i-2])
            asum=asum+num[i+2*k]+num[i+2*k-1]-num[i-1]-num[i-2]
            ans=max(ans,asum)
if s[0]=="1":
    if len(num)-2*k+2<=0:
        ans=sum(num)
    else:
        asum=sum(num[1:2*k+2])
        ans=asum
        for i in range(3,len(num)-2*k-1,2):
            #print(asum,num[i+2*k],num[i+2*k-1],num[i-1],num[i-2])
            asum=asum+num[i+2*k]+num[i+2*k-1]-num[i-1]-num[i-2]
            ans=max(ans,asum)
print(ans)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
s=input()
num=[0]
c=1
for i in range(1,n):

    if s[i]==s[i-1]:
        c+=1
    else:
        num.append(c)
        c=1
num.append(c)
num.append(0)
#print(num)

ans=0
#print(num)
if s[0]=="0":
    if len(num)-2*k+1<0:
        ans=sum(num)
    else:
        asum=sum(num[0:2*k+1])
        ans=asum
        #print(asum)
        for i in range(2,len(num)-2*k-1,2):
            #print(asum,num[i+2*k],num[i+2*k-1],num[i-1],num[i-2])
            asum=asum+num[i+2*k]+num[i+2*k-1]-num[i-1]-num[i-2]
            ans=max(ans,asum)
if s[0]=="1":
    if len(num)-2*k+2<0:
        ans=sum(num)
    else:
        asum=sum(num[1:2*k+2])
        ans=asum
        for i in range(3,len(num)-2*k-1,2):
            #print(asum,num[i+2*k],num[i+2*k-1],num[i-1],num[i-2])
            asum=asum+num[i+2*k]+num[i+2*k-1]-num[i-1]-num[i-2]
            ans=max(ans,asum)
print(ans)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
s=input()
num=[0]
c=1
for i in range(1,n):

    if s[i]==s[i-1]:
        c+=1
    else:
        num.append(c)
        c=1
num.append(c)
num.append(0)
#print(num)

ans=0
#print(num)
if s[0]=="0":
    if len(num)-2*k-1<2:
        ans=sum(num)
    else:
        asum=sum(num[0:2*k+1])
        ans=asum
        #print(asum)
        for i in range(2,len(num)-2*k-1,2):
            #print(asum,num[i+2*k],num[i+2*k-1],num[i-1],num[i-2])
            asum=asum+num[i+2*k]+num[i+2*k-1]-num[i-1]-num[i-2]
            ans=max(ans,asum)
if s[0]=="1":
    if len(num)-2*k-1<3:
        ans=sum(num)
    else:
        asum=sum(num[1:2*k+2])
        ans=asum
        for i in range(3,len(num)-2*k-1,2):
            #print(asum,num[i+2*k],num[i+2*k-1],num[i-1],num[i-2])
            asum=asum+num[i+2*k]+num[i+2*k-1]-num[i-1]-num[i-2]
            ans=max(ans,asum)
print(ans)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
s=input()
num=[0]
c=1
for i in range(1,n):

    if s[i]==s[i-1]:
        c+=1
    else:
        num.append(c)
        c=1
num.append(c)
num.append(0)
#print(num)

ans=0
#print(num)
if s[0]=="0":
    if len(num)-2*k+1<=2:
        ans=sum(num)
    else:
        asum=sum(num[0:2*k+1])
        for i in range(2,len(num)-2*k-1,2):
            asum=asum+num[i+2*k]+num[i+2*k-1]-num[i-1]-num[i-2]
            ans=max(ans,asum)
if s[0]=="1":
    if len(num)-2*k+1<=3:
        ans=sum(num)
    else:
        asum=sum(num[1:2*k+2])
        for i in range(3,len(num)-2*k-1,2):
            #print(asum,num[i+2*k],num[i+2*k-1],num[i-1],num[i-2])
            asum=asum+num[i+2*k]+num[i+2*k-1]-num[i-1]-num[i-2]
            ans=max(ans,asum)
print(ans)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
s=input()
num=[0]
c=1
for i in range(1,n):

    if s[i]==s[i-1]:
        c+=1
    else:
        num.append(c)
        c=1
num.append(c)
num.append(0)
#print(num)

ans=0
#print(num)
if s[0]=="0":
    if (len(num)-2+1)//2<=k:
        ans=sum(num)
    else:
        asum=sum(num[0:2*k+1])
        for i in range(2,len(num)-2*k-1,2):
            asum=asum+num[i+2*k]+num[i+2*k-1]-num[i-1]-num[i-2]
            ans=max(ans,asum)
if s[0]=="1":
    if (len(num)-2)//2<=k:
        ans=sum(num)
    else:
        asum=sum(num[1:2*k+2])
        for i in range(3,len(num)-2*k-1,2):
            #print(asum,num[i+2*k],num[i+2*k-1],num[i-1],num[i-2])
            asum=asum+num[i+2*k]+num[i+2*k-1]-num[i-1]-num[i-2]
            ans=max(ans,asum)
print(ans)