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()

a = []
j = 0
temp = 1
prev = s[0]
for i in s[1:]:
    if i == prev:
        prev = i
        temp += 1
    else:
        a.append(temp)
        prev = i        
        temp = 1
a.append(temp)

b = []
if s[0] == "1":
    b.append(sum(a[:2*k+1]))
    for i in range(len(a) - 2*k + 1):
        if i % 2 == 0:
            try:
                b.append(b[-1] - a[i] + a[2*k+i+1])
            except:
                continue
    print(max(b))

else:
    t = a[1:]
    b.append(sum(t[:2*k+1]))
    for i in range(len(t) - 2*k + 1):
        if i % 2 == 0:
            try:
                b.append(b[-1] - t[i] + t[2*k+i+1])
            except:
                continue
    b.append(sum(a[:2*k]))
    print(max(b))

Submission status: Wrong Answer

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

a = []
j = 0
temp = 1
prev = s[0]
for i in s[1:]:
    if i == prev:
        prev = i
        temp += 1
    else:
        a.append(temp)
        prev = i        
        temp = 1
a.append(temp)

b = []
if s[0] == "1":
    b.append(sum(a[:2*k+1]))
    for i in range(len(a) - 2*k):
        if i % 2 == 0:
            try:
                b.append(b[-1] - a[i] + a[2*k+i+1])
            except:
                continue
    print(max(b))

else:
    t = a[1:]
    b.append(sum(t[:2*k+1]))
    for i in range(len(a) - 2*k):
        if i % 2 == 0:
            try:
                b.append(b[-1] - t[i] + t[2*k+i+1])
            except:
                continue
        b.append(sum(a[:2*k]))
    print(max(b))

Submission status: Wrong Answer

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

a = []
j = 0
temp = 1
prev = s[0]
for i in s[1:]:
    if i == prev:
        prev = i
        temp += 1
    else:
        a.append(temp)
        prev = i        
        temp = 1
a.append(temp)

b = []
if s[0] == "1":
    b.append(sum(a[:2*k+1]))
    for i in range(len(a) - 2*k - 1):
        try:
            b.append(b[-1] - a[i] + a[2*k+i+1])
        except:
            continue
    print(max(b))

else:
    t = a[1:]
    b.append(sum(t[:2*k+1]))
    for i in range(len(a) - 2*k - 1):
        try:
            b.append(b[-1] - t[i] + t[2*k+i+1])
        except:
            continue
    b.append(sum(a[:2*k]))
    print(max(b))

Submission status: Wrong Answer

Solution code:
def solve(N, K, S):
    X = [0]
    for i in range(1, N):
        if S[i-1] != S[i]:
            X.append(i)
    X.append(N)

    #print(X)
    m = None
    for i in range(len(X)-1):
        j = i + 2 * K
        if S[X[i]] == '1':
            j += 1
        if j < len(X):
            #print(i, i, repr(S[X[i]]), j, "m=%d"%(X[j]-X[i]),"X[%d]:%d-X[%d]"%(j,X[j],i))
            m = max(0 if m is None else m, X[j] - X[i])
    if m is None:
        return N
    return m

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

Submission status: Wrong Answer

Solution code:
def solve(N, K, S):
    X = [0]
    for i in range(1, N):
        if S[i-1] != S[i]:
            X.append(i)
    X.append(N)

    #print(X)
    m = None
    for i in range(len(X)-1):
        j = i + 2 * K
        if S[i] == '1':
            j += 1
        if j < len(X):
            #print(i, repr(S[i]), j, "m=%d"%(X[j]-X[i]),"X[%d]:%d-X[%d]"%(j,X[j],i))
            m = max(0 if m is None else m, X[j] - X[i])
    if m is None:
        return N
    return m

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

Submission status: Wrong Answer

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

count = 1
block = []
len_block = 0
ans = 1

for i in range(n):
    if s[i] != s[i+1]:
        block.append(count)
        len_block += 1
        if len_block == (k * 2) + 1:
            ans = max(ans, sum(block))
            block = block[1:]
            len_block -= 1
        count = 1

    else:
        count += 1

print(ans)

Submission status: Wrong Answer

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

li=[]
if s[0]=='0':
    num=0
    for i in range(n-1):
        if s[i]==s[i+1]:
            num+=1
        else:
            num+=1
            li.append(num)
            num=0
    li.append(num+1)
    
    maxi=0
    N=len(li)
    for i in range(-(-N//2)-k+1):
        maxi=max(maxi,sum(li[i*2:i*2+k*2]))
        
if s[0]=='1':
    if s.count('0')==0:
        print(len(s))
        exit()
    num=0
    for i in range(n-1):
        if s[i]==s[i+1]:
            num+=1
        else:
            num+=1
            li.append(num)
            num=0
    li.append(num+1)
    
    maxi=0
    N=len(li)
    for i in range(N//2-k+1):
        maxi=max(maxi,sum(li[i*2:i*2+k*2+1]))

print(maxi)

Submission status: Wrong Answer

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

li=[]
if s[0]=='0':
    num=0
    for i in range(n-1):
        if s[i]==s[i+1]:
            num+=1
        else:
            num+=1
            li.append(num)
            num=0
    li.append(num+1)
    
    maxi=0
    N=len(li)
    for i in range(-(-N//2)-k+1):
        maxi=max(maxi,sum(li[i*2:i*2+k*2]))
        
if s[0]=='1':
    num=0
    for i in range(n-1):
        if s[i]==s[i+1]:
            num+=1
        else:
            num+=1
            li.append(num)
            num=0
    li.append(num+1)
    
    maxi=0
    N=len(li)
    for i in range(N//2-k+1):
        maxi=max(maxi,sum(li[i*2:i*2+k*2+1]))

print(maxi)

Submission status: Wrong Answer

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

l = []
now = 1 # 今見ている数(1から見る配列を作りたいので1で初期化)
cnt = 0

# lには(1の個数),(0の個数),(1の個数),(0の個数),...(1の個数)という感じのリストを入れたい(両端が0から始まるとしても1が0こあるとしてカウントしたい)
for i in range(N):
    if int(S[i]) == now:
        cnt += 1
    else:
        l.append(cnt)
        cnt = 1
        now = 1 - now # 0と1を切り替える(now ^= 1でも良い)
if cnt != 0:
    l.append(cnt)
if len(l) % 2 == 0: # (1の個数),(0の個数),(1の個数),(0の個数),...(0の個数)となってしまっていたら最後に0を入れておく
    l.append(0)
print(l)

ans = 0
width = 2 * K + 1

# 累積和
# 元のリストが[0, 1, 2, 3, 4, 5]の時に[0, 1, 2, 3, 4, 5, 6]のように個数は+1になる(つまり最初は0)
lsum = [0 for _ in range(len(l) + 1)] # len(l)+1個の要素
for i in range(len(l)): # 0からlen(l)まで
    lsum[i + 1] = lsum[i] + l[i]
print(lsum)

for i in range(0, len(l), 2): # ここを2ごとにすることで1の個数を数えられる
    left = i
    right = min(i + width, len(l))
    tmp = lsum[right] - lsum[left] # [left, right)
    ans = max(ans, tmp)

"""
# しゃくとり法

# ①forループの外側にleft,rightを持っておく
left = 0
right = 0
tmp = 0 # [left, right)の総和

for i in range(0, len(l), 2):
    # ②次のleft,rightを計算する
    nextleft = i # 0,2,4,...
    nextright = min(i + width, len(l)) # 0+width,2+width,...

    # ③左端の移動
    while nextleft > left:
        tmp -= l[left]
        left += 1

    # ③右端の移動
    while nextright > right:
        tmp += l[right]
        right += 1

    ans = max(ans, tmp)
"""

"""
# 愚直に足し算を繰り返すとTLE

for i in range(0, len(l), 2): # ここを2ごとにすることで1の個数を数えられる
    tmp = 0
    left = i
    right = min(i + width, len(l))
    for j in range(left, right): # leftからright-1まで
        tmp += l[j]
    ans = max(ans, tmp)
"""

print(ans)

Submission status: Wrong Answer

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

l = []
now = 1 # 今見ている数(1から見る配列を作りたいので1で初期化)
cnt = 0

# lには(1の個数),(0の個数),(1の個数),(0の個数),...(1の個数)という感じのリストを入れたい(両端が0から始まるとしても1が0こあるとしてカウントしたい)
for i in range(N):
    if int(S[i]) == now:
        cnt += 1
    else:
        l.append(cnt)
        cnt = 1
        now = 1 - now # 0と1を切り替える(now ^= 1でも良い)
if cnt != 0:
    l.append(cnt)
if len(l) % 2 == 0: # (1の個数),(0の個数),(1の個数),(0の個数),...(0の個数)となってしまっていたら最後に0を入れておく
    l.append(0)
print(l)

ans = 0
width = 2 * K + 1

for i in range(0, len(l), 2):
    tmp = 0
    left = i
    right = min(i + width, len(l))
    for j in range(left, right): # leftからright-1まで
        tmp += l[j]
    ans = max(ans, tmp)

print(ans)

Submission status: Wrong Answer

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

ones=[0] if s[0]=="0" else [0, 0]
cnt=1
for i in range(n):
    if s[i]==s[i+1]:
        cnt+=1
    else:
        ones.append(cnt)
        cnt=1
if s[-2]=="1":
    ones.append(0)
    ones.append(0)
elif s[-2]=="0":
    ones.append(0)

ans=0
for i in range(0, len(ones)-(2*k+1)+1, 2):
    if i==0:
        tmp=sum(ones[0:2*k+1])
    else:
        tmp-=ones[i-2]+ones[i-1]
        tmp+=ones[i+2*k-1]+ones[i+2*k]
    ans=max(ans, tmp)

print(ans)

Submission status: Wrong Answer

Solution code:
from collections import deque

def main():
    n,k = map(int,input().split())
    ls = list(input())

    
    que_value = deque([])
    que_key = deque([])
    i = 1
    now = ls[0]
    cnt = 1
    while 1:
        if i == n:
            que_value.append(cnt)
            que_key.append(now)
            break

        if ls[i]==now:
            cnt += 1
            i += 1
        else:
            que_value.append(cnt)
            que_key.append(now)
            cnt = 1
            now = ls[i]
            i += 1

    que_value.appendleft(0)
    for i in range(1,len(que_value)):
        que_value[i] += que_value[i-1]
    ans = 0
    
    for i in range(len(que_key)):
        if que_key[i]=="1":
            try:
                ans = max(ans,que_value[i+2*k+1]-que_value[i]) 
            except:
                pass
        else:
            try:
                ans = max(ans,que_value[i+2*k]-que_value[i])
            except:
                pass
    if ans == 0:
        ans = que_value[-1]
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
S = input()
Is = []
i = 0
while i < N:
  j = S.find("0", i)
  if j < 0: j = N
  k = S.find("1", j)
  if k < 0: k = N
  Is.append((i, j, k))
  i = k
#print(Is)
if len(Is) <= K:
  r = N
  print(r)
  exit()
rs = []
for i in range(len(Is)-K):
  r = Is[i+K][1]-Is[i][0]
  rs.append(r)
#print(rs)
r = max(rs)
print(r)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
S = input()
As = [0]
for i in range(N-1):
    if S[i] != S[i+1]:
        As.append(i+1)
As.append(N)
ans = 0
now = 0
for i in range(len(As)-1):
    if S[i] == "0":
        if i+2*K > len(As)-1:
            now = As[len(As)-1] - As[i]
        else:
            now = As[i+2*K] - As[i]
    else:
        if i+2*K+1 > len(As)-1:
            now = As[len(As)-1] - As[i]
        else:
            now = As[i+2*K+1] - As[i]
    if now > ans:
        ans = now

print(ans)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
S = input()
As = []
Cs = []
count = 0
now = S[0]
ans = 0
for i in range(N):
    if now == S[i]:
        count += 1
    else:
        As.append(now)
        Cs.append(count)
        now = S[i]
        count = 1
        k = 2*K if len(As) - 1 - K*2 >= 0 else len(As) - 1
        if As[len(As)-1] == "1":
            if ans < sum(Cs[len(As)-k-1:]):
                ans = sum(Cs[len(As)-k-1:])
        else:
            if ans < sum(Cs[len(As)-k:]):
                ans = sum(Cs[len(As)-k:])
As.append(now)
Cs.append(count)
k = 2*K if len(As) - 1 - K*2 >= 0 else len(As) - 1
if As[len(As)-1] == "1":
    if ans < sum(Cs[len(As)-k-1:]):
        ans = sum(Cs[len(As)-k-1:])
else:
    if ans < sum(Cs[len(As)-k:]):
        ans = sum(Cs[len(As)-k:])
        
print(ans)

Submission status: Wrong Answer

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

a = [0]
tmp = 1
for i in range(n-1):
    if s[i] == s[i+1]:
        tmp += 1
    else:
        a.append(tmp)
        tmp += 1
a.append(tmp)
m = len(a)
ans = 1
if len(a) == 2 or m-2*k < 1:
    ans = a[m-1]
elif s[0] == "1":
    for i in range(1,m-2*k):
        if i % 2 != 0:        
            ans = max(ans,a[2*k+i]-a[i-1])
        elif 2*k+i == m - 1:
            ans = max(ans,a[m-1]-a[i-1])
        else:
            ans = max(ans,a[2*k+i-1]-a[i-1])
else:
    for i in range(1,m-2*k):
        if i % 2 == 0:        
            ans = max(ans,a[2*k+i]-a[i-1])
        elif 2*k+i == m - 1:
            ans = max(ans,a[m-1]-a[i-1])
        else:
            ans = max(ans,a[2*k+i-1]-a[i-1])
print(ans)

Submission status: Wrong Answer

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

a = [0]
tmp = 1
for i in range(n-1):
    if s[i] == s[i+1]:
        tmp += 1
    else:
        a.append(tmp)
        tmp += 1
a.append(tmp)
m = len(a)
ans = 1
if len(a) == 2 or m-2*k < 1:
    ans = a[m-1]
elif s[0] == "1":
    for i in range(1,m-2*k):
        if i % 2 != 0:        
            ans = max(ans,a[2*k+i]-a[i-1])
        else:
            ans = max(ans,a[min(2*k+i-1,m-1)]-a[i-1])
else:
    for i in range(1,m-2*k):
        if i % 2 == 0:        
            ans = max(ans,a[2*k+i]-a[i-1])
        else:
            ans = max(ans,a[min(2*k+i-1,m-1)]-a[i-1])

print(ans)

Submission status: Wrong Answer

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

a = [0]
tmp = 1
for i in range(n-1):
    if s[i] == s[i+1]:
        tmp += 1
    else:
        a.append(tmp)
        tmp += 1
a.append(tmp)
m = len(a)
ans = 1
if len(a) == 2 or m-2*k < 1:
    ans = a[m-1]
elif s[0] == "1":
    for i in range(1,m-2*k):
        if i % 2 != 0:        
            ans = max(ans,a[2*k+i]-a[i-1])
        else:
            ans = max(ans,a[2*k+i-1]-a[i-1])
else:
    for i in range(1,m-2*k):
        if i % 2 == 0:        
            ans = max(ans,a[2*k+i]-a[i-1])
        else:
            ans = max(ans,a[2*k+i-1]-a[i-1])

print(ans)

Submission status: Wrong Answer

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

a = [0]
tmp = 1
for i in range(n-1):
    if s[i] == s[i+1]:
        tmp += 1
    else:
        a.append(tmp)
        tmp += 1
a.append(tmp)
m = len(a)
ans = 1
if len(a) == 2:
    ans = a[1]
elif s[0] == "1":
    for i in range(1,m-2*k):
        if i % 2 != 0:        
            ans = max(ans,a[2*k+i]-a[i-1])
        else:
            ans = max(ans,a[2*k+i-1]-a[i-1])
else:
    for i in range(1,m-2*k):
        if i % 2 == 0:        
            ans = max(ans,a[2*k+i]-a[i-1])
        else:
            ans = max(ans,a[2*k+i-1]-a[i-1])

print(ans)

Submission status: Wrong Answer

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

a = [0]
tmp = 1
for i in range(n-1):
    if s[i] == s[i+1]:
        tmp += 1
    else:
        a.append(tmp)
        tmp += 1
a.append(tmp)
m = len(a)
ans = 1
if s[0] == "1":
    for i in range(1,m-2*k):
        if i % 2 != 0:        
            ans = max(ans,a[2*k+i]-a[i-1])
        else:
            ans = max(ans,a[2*k+i-1]-a[i-1])
else:
    for i in range(1,m-2*k):
        if i % 2 == 0:        
            ans = max(ans,a[2*k+i]-a[i-1])
        else:
            ans = max(ans,a[2*k+i-1]-a[i-1])

print(ans)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
l = 2*k+1

s = input()
head = s[0]
s = s.replace('10', '1,0').replace('01', '0,1').split(',')
s = list(map(len, s))
if head == '0':
    s = [0] + s
ans, i = 0, 0

while i < len(s) - l:
    ans = max(ans, sum(s[i*2:l+i*2]))
    i += 1

if l >= n:
    ans = n
print(ans)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
s = input()
head = s[0]
s = s.replace('10', '1,0').replace('01', '0,1').split(',')
s = list(map(len, s))
if head == '0':
    s = [0] + s
l = 2*k+1
ans = -1
for i in range(n):
    if len(s[i:l+i]) < l:
        break
    i *= 2
    ans = max(ans, sum(s[i:l+i]))

if n <= l:
    ans = n
print(ans)

Submission status: Wrong Answer

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

T = []
s0, s1 = 0, 0
for i in range(N):
    if S[i] == '1':
        if s0 != 0:
            T.append(s0)
        s1 += 1
        s0 = 0
    else:
        if s1 != 0:
            T.append(s1)
        s0 += 1
        s1 = 0
if s0 != 0:
    T.append(s0)
if s1 != 0:
    T.append(s1)


E = [0]
s = 0
for t in T:
    s += t
    E.append(s)
L = len(E)
ans = 0
if 2*K >= L:
    ans = N
elif L == 2:
    ans = N
else:
    if S[0] == '0':
        ans = E[2*K]
        for l in range(0, L-2*K-1):
            if l%2==1:
                ans = max(E[l+2*K+1] - E[l], ans)
            else:
                ans = max(E[l+2*K-1] - E[l], ans)
    else:
        ans = E[2*K]
        for l in range(0, L-2*K-1):
            if l%2==0:
                ans = max(E[l+2*K+1] - E[l], ans)
            else:
                ans = max(E[l+2*K-1] - E[l], ans)
print(ans)

Submission status: Wrong Answer

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

T = []
s0, s1 = 0, 0
for i in range(N):
    if S[i] == '1':
        if s0 != 0:
            T.append(s0)
        s1 += 1
        s0 = 0
    else:
        if s1 != 0:
            T.append(s1)
        s0 += 1
        s1 = 0
if s0 != 0:
    T.append(s0)
if s1 != 0:
    T.append(s1)


E = [0]
s = 0
for t in T:
    s += t
    E.append(s)
L = len(E)
ans = 0
if 2*K >= L:
    ans = N
elif L == 2:
    ans = N
else:
    if S[0] == '0':
        ans = E[2*K]
        for l in range(0, L-2*K-1):
            if l%2==1:
                ans = max(E[l+2*K+1] - E[l], ans)
            else:
                ans = max(E[l+2*K-1] - E[l], ans)
    else:
        ans = E[2*K-1]
        for l in range(0, L-2*K-1):
            if l%2==0:
                ans = max(E[l+2*K+1] - E[l], ans)
            else:
                ans = max(E[l+2*K-1] - E[l], ans)
print(ans)

Submission status: Wrong Answer

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

a = []
b = []
tmp = 1
for i in range(n-1):
    if s[i] == s[i+1]:
        tmp += 1
    else:
        if s[i] == "1":
            a.append(tmp)
        else:
            b.append(tmp)
        tmp = 1
if s[n-1] == "1":
    a.append(tmp)
else:
    b.append(tmp)

ans = 0
m = max(len(a),len(b))
for i in range(m-k):
    c = 0
    for j in range(k):
        c +=  (a[i+j] + b[i+j])
    if s[0] == "1":
        c += a[i+j+1]
    ans = max(ans,c)
if m == 1 and s[0] == "1":
            ans = 1
print(ans)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
s = list(input())
l = 0
r = 0
cnt = 0
msum = 0
if n == 1:
  if k != 0:
    msum = 1
  else:
    if s[0] == 1:
      msum = 1
while r + 1 < n:
  if s[r] == '1' and s[r+1] == '0':
    k -=1
    while k < 0:
      l += 1
      if s[l] == '0' and s[l+1] == '1':
        l += 1
        k += 1
  if s[r+1] == '1':
    cnt = r + 1 - l + 1
  else:
    cnt = r - l + 1
  r += 1
  if msum < cnt:
    msum = cnt
print(msum)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
S = list(input()) + [-1]
bw = [0] * N
ind = 0
for i in range(N):
  bw[ind] += S[i] == S[i + 1]
  if S[i] != S[i + 1]:
    bw[ind] += 1
    ind += 1
ind = 0
res = 0
cumsumbw = [0] + [bw[0]] + [0] * (N - 1)
for i in range(1, N):
  cumsumbw[i + 1] = cumsumbw[i] + bw[i]
if N - 2 * K <= 0:
  print(N)
  exit(0)
for i in range(N - 2 * K):
  if S[i] == "1":
    res = max(res, cumsumbw[i + 2 * K + 1] - cumsumbw[i])
  else:
    res = max(res, cumsumbw[i + 2 * K] - cumsumbw[i])
print(res)

Submission status: Wrong Answer

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

streaks = []
c = s[0]
i = 1
count = 1
while True:
    if i == n:
        streaks.append(count)
        break
    if s[i] == c:
        i += 1
        count += 1
    else:
        streaks.append(count)
        c = s[i]
        i += 1
        count = 1

accs = [0]
for num in streaks:
    accs.append(accs[-1] + num)
if accs[-1] != n:
    accs.append(n)

flag = s[0] == '0'
res = []
for i in range(len(accs)):
    if flag:
        index = i + 2 * k
    else:
        index = i + 2 * k + 1
    index = min(index, len(accs) - 1)
    res.append(accs[index] - accs[i])
    flag = ~flag

print(max(res))

Submission status: Wrong Answer

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

streaks = []
c = s[0]
i = 1
count = 1
while True:
    if i == n:
        streaks.append(count)
        count = 1
        break
    if s[i] == c:
        i += 1
        count += 1
    else:
        c = s[i]
        i += 1
        streaks.append(count)
        count = 1

accs = [0]
for num in streaks:
    accs.append(accs[-1] + num)
accs.append(n)

flag = s[0] == '0'
res = []
for i in range(len(accs)):
    if flag:
        index = i + 2 * k
    else:
        index = i + 2 * k + 1
    index = min(index, len(accs) - 1)
    res.append(accs[index] - accs[i])
    flag = ~flag

print(max(res))

Submission status: Wrong Answer

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

streaks = []
c = s[0]
i = 1
count = 1
while True:
    if i == n:
        streaks.append(count)
        count = 1
        break
    if s[i] == c:
        i += 1
        count += 1
    else:
        c = s[i]
        i += 1
        streaks.append(count)
        count = 1

accs = [0]
for num in streaks:
    accs.append(accs[-1] + num)
accs.append(n + 1)

flag = s[0] == '0'
res = []
for i in range(len(accs)):
    if flag:
        index = i + 2 * k
    else:
        index = i + 2 * k + 1
    index = min(index, len(accs) - 2)
    res.append(accs[index] - accs[i])
    flag = ~flag

print(max(res))

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
S = list(input())
l = len(S)
X = []
cnt0 = 0
cnt1 = 0
for i in S:
    if i == '0':
        if cnt1 != 0:
            X.append(cnt1)
            cnt1 = 0
            cnt0 += 1
        else:
            cnt0 += 1
    else:
        if cnt0 != 0:
            X.append(cnt0)
            cnt0 = 0
            cnt1 += 1
        else:
            cnt1 += 1
if cnt1 != 0:
    X.append(cnt1)
elif cnt0 != 0:
    X.append(cnt0)
ans = []
if S[0] == '1': #Xの奇数は1
    sum0 = sum(X[0:2*K+1])
    ans.append(sum0)
    i = 0
    while 2*K+i+1 < len(X):
        ans.append(ans[i] + X[2*K+i+1] - X[i])
        i += 1
else:
    sum0 = sum(X[0:2*K])
    ans.append(sum0)
    i = 0
    while 2*K+i < len(X):
        ans.append(ans[i] + X[2*K+i] - X[i])
        i += 1
print(max(ans))

Submission status: Wrong Answer

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

list10 = {'0':[],'1':[]}
pre_c='1'
c_c = 0
cur_sum = 0
cur_max = 0
for c in s:
    if c==pre_c:
        c_c += 1
    else:
        list10[pre_c].append(c_c)
        cur_sum += c_c
        c_c = 1
        pre_c = c
    if len(list10['1'])== k+1:
        cur_max = max(cur_max,cur_sum)
        cur_sum -= (list10['0'].pop() + list10['1'].pop())
print(max(cur_max,cur_sum+c_c))

Submission status: Wrong Answer

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

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

key = int(s[0])
for i in range(n):
    if int(s[i]) == key :
        A[-1] += 1
    else:
        A.append(1)
        key = (key+1) % 2

if len(A) % 2 == 0:
    A.append(0) 

#print(A)

la = len(A)

le = -1
ri = k*2
ans = sum(A[:ri])
mans = ans
while ri < la:
    minus = A[le] if le >= 0 else 0
    plus = A[ri+1] if ri+1 <= la-1 else 0
    ans = ans - minus - A[le+1] + A[ri] + plus 
    mans = max(mans, ans)
    #print(A[le+2:ri+2],mans)
    le += 2
    ri += 2

A = [A[-i] for i in range(la)]

le = -1
ri = k*2
ans = sum(A[:ri])
mans = max(mans, ans)
while ri < la:
    minus = A[le] if le >= 0 else 0
    plus = A[ri+1] if ri+1 <= la-1 else 0
    ans = ans - minus - A[le+1] + A[ri] + plus 
    mans = max(mans, ans)
    #print(A[le+2:ri+2],mans)
    le += 2
    ri += 2

print(mans)

Submission status: Wrong Answer

Solution code:
import sys
from collections import deque
stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()  # ignore trailing spaces

n, k = na()
s = input()
s = s + "2"
li_0 = []
li_1 = []
ct = 1
for i in range(n):
    if s[i] != s[i+1]:
        if s[i] == '0':
            li_0.append(ct)
            ct = 1
        else:
            li_1.append(ct)
            ct = 1
    else:
        ct += 1
if len(li_0) <= k:
    print(len(s)-1)
else:
    li_0 = deque(li_0)
    li_1 = deque(li_1)
    if s[0] == "0":
        li_1.appendleft(0)
    Sum_0 = sum(li_0[i] for i in range(k))
    Sum_1 = sum(li_1[i] for i in range(k+1))
    Max = Sum_0 + Sum_1
    for i in range(len(li_1)-k-1):
        Sum_0 = Sum_0 - li_0[i] + li_0[i+k]
        Sum_1 = Sum_1 - li_1[i] + li_1[i+k+1]
        Max = max(Max, Sum_0 + Sum_1)
    print(Max)

Submission status: Wrong Answer

Solution code:
import sys
from collections import deque
stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()  # ignore trailing spaces

n, k = na()
s = input()
s = s + "2"
li_0 = []
li_1 = []
ct = 1
for i in range(n):
    if s[i] != s[i+1]:
        if s[i] == '0':
            li_0.append(ct)
            ct = 1
        else:
            li_1.append(ct)
            ct = 1
    else:
        ct += 1
if len(li_0) <= k:
    print(len(s)-1)
else:
    li_0 = deque(li_0)
    li_1 = deque(li_1)
    if s[0] == "0":
        li_1.appendleft(0)
    Sum_0 = sum(li_0[i] for i in range(k))
    Sum_1 = sum(li_1[i] for i in range(k+1))
    Max = Sum_0 + Sum_1
    for i in range(len(li_1)-k-1):
        Sum_0 = Sum_0 - li_0[i] + li_1[i+k]
        Sum_1 = Sum_1 - li_1[i] + li_1[i+k+1]
        Max = max(Max, Sum_0 + Sum_1)
    print(Max)

Submission status: Wrong Answer

Solution code:
import sys
from collections import deque
stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()  # ignore trailing spaces

n, k = na()
s = input()
s = s + "2"
li_0 = []
li_1 = []
ct = 1
for i in range(n):
    if s[i] != s[i+1]:
        if s[i] == '0':
            li_0.append(ct)
            ct = 1
        else:
            li_1.append(ct)
            ct = 1
    else:
        ct += 1
if len(li_0) <= k:
    print(len(s))
else:
    li_0 = deque(li_0)
    li_1 = deque(li_1)
    if s[0] == "0":
        li_1.appendleft(0)
    Sum_0 = sum(li_0[i] for i in range(k))
    Sum_1 = sum(li_1[i] for i in range(k+1))
    Max = Sum_0 + Sum_1
    for i in range(len(li_1)-k-1):
        Sum_0 = Sum_0 - li_0[i] + li_1[i+k]
        Sum_1 = Sum_1 - li_1[i] + li_1[i+k+1]
        Max = max(Max, Sum_0 + Sum_1)
    print(Max)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
s = list(input())
count = 0
l = []
count1 = 0
for i in range(n):
    if s[i] == "0":
        if count > 0:
            l.append(count)
            count = -1
        else:
            count -= 1
    else:
        if count < 0:
            l.append(count)
            count = 1
            count1 += 1
        else:
            count += 1
l.append(count)
if count < 0:
    count1 += 1
m = len(l)
count2 = 0
ans = []
if l[0] < 0:
    p = 0
else:
    p = 1
if count1 > k:
    x = 0
    ans1 = 0
    while count2 < k:
        if l[x] > 0:
            ans1 += l[x]
            x += 1
        else:
            ans1 += abs(l[x])
            x += 1
            count2 += 1
    ans1 += abs(l[x])
    ans.append(ans1)
    while x < m-2:
        if p > 0:
            ans1 = ans1 - abs(l[p]) - abs(l[p-1]) + abs(l[x+1]) + abs(l[x+2])
            p += 2
            x += 2
        else:
            ans1 = ans1 - abs(l[p]) + abs(l[x+1]) + abs(l[x+2])
            p += 2
            x += 2
        ans.append(ans1)
    if x == m-2:
        if p > 0:
            ans1 = ans1 - abs(l[p]) - abs(l[p-1]) + abs(l[x+1])
            p += 2
            x += 2
        else:
            ans1 = ans1 - abs(l[p]) + abs(l[x+1])
            p += 2
            x += 2
    print(max(ans))
else:
    print(n)

Submission status: Wrong Answer

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

pres = []
i = 0
while i < n:
    k = 0
    while s[i] == s[i+k]:
        k += 1
        if k+i >= n:
            break
    pres.append([s[i],k])
    i += k
i = 0
result = []
while i < len(pres):
    h = 0
    if pres[i][0] == 0:
        if i + 2*k <= len(pres):
            for p in range(2*k):
                h += pres[p+i][1]
            result.append(h)
        else:
            for p in range(len(pres)-i):
                h += pres[p+i][1]
            result.append(h)
    else:
        if i + 2*k+1 <= len(pres):
            for p in range(2*k+1):
                h += pres[i+p][1]
            result.append(h)
        else:
            for p in range(len(pres)-i):
                h += pres[i+p][1]
            result.append(h)
    i += 1
print(max(result))

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
s = list(input())
count = 0
l = []
count1 = 0
for i in range(n):
    if s[i] == "0":
        if count > 0:
            l.append(count)
            count = -1
        else:
            count -= 1
    else:
        if count < 0:
            l.append(count)
            count = 1
            count1 += 1
        else:
            count += 1
l.append(count)
if count < 0:
    count1 += 1
m = len(l)
count2 = 0
ans = []
if l[0] < 0:
    p = 0
else:
    p = 1
if count1 > k:
    x = 0
    ans1 = 0
    while count2 < k:
        if l[x] > 0:
            ans1 += l[x]
            x += 1
        else:
            ans1 += abs(l[x])
            x += 1
            count2 += 1
    ans1 += abs(l[x])
    ans.append(ans1)
    while x < m-2:
        if p > 0:
            ans1 = ans1 - abs(l[p]) - abs(l[p-1]) + abs(l[x+1]) + abs(l[x+2])
            p += 2
            x += 2
        else:
            ans1 = ans1 - abs(l[p]) + abs(l[x+1]) + abs(l[x+2])
            p += 2
            x += 2
        ans.append(ans1)
    print(max(ans))
else:
    print(n)

Submission status: Wrong Answer

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

compress = []
now = '1'
count = 0
for s in S:
    if s != now:
        compress.append(count)
        now = s
        count = 1
    else:
        count += 1
compress.append(count)

length = len(compress)
candidate = sum(compress[:2*k+1])
ans = candidate
if length <= 2*k+1:
    print(ans)
    exit()

for i in range(0, length-2*k-2, 2):
    if 2+k+2+i < length:
        candidate += compress[2*k+1+i] + compress[2*k+2+i] - compress[i] - compress[i+1]
    else:
        candidate += compress[2*k+1+i] - compress[i] - compress[i+1]
    ans = max(ans, candidate)

print(ans)