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

nums=[]

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

num=s[0]
numi=0
for i in range(n):
    if(s[i]==num):
        numi+=1
    else:
        nums.append(numi)
        numi=1
        num=s[i]
else:
    nums.append(numi)

if(s[-1]=='1'):
    nums.append(0)

#print(nums)

ans=0
zero_num=len(nums)//2
if(zero_num<=k):
    print(sum(nums))
else:
    l=2*k
    sumi=sum(nums[:l+1])
    ans=sumi
    i=0
    while(l+2<len(nums)):
        sumi+=(nums[l+1]+nums[l+2])
        l+=2
        sumi-=(nums[i]+nums[i+1])
        i+=2
        ans=max(ans,sumi)
    print(ans)

Submission status: Wrong Answer

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

L = []
from itertools import groupby
for key ,value in groupby(s):
    L.append((key,len(list(value))))
#print(L)
c = []
for i in range(len(L)):
    c.append(L[i][1])
c = [0] + c

for i in range(1,len(c)):
    c[i] +=c[i-1]
#print(c)
l = 0 
r = 2*k

ans = 0
while True:
    if L[l][0] == "1":
        if l + r +1 < len(L):
            ans = max(ans,c[r+1] - c[l])#add 5
        else:
            ans = max(ans,c[len(L)] - c[l])
    elif L[l][0] == "0":
        if l + r < len(L):
            ans = max(ans,c[r] - c[l])#add 4
        else:
            ans = max(ans,c[len(L)] - c[l])
    if l == len(L)-1:
        break
    l +=1
print(ans)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
s = input()
if "0" not in s:
    print(len(s))
    exit()
    
L = []
from itertools import groupby
for key ,value in groupby(s):
    L.append((key,len(list(value))))
#print(L)
c = []
for i in range(len(L)):
    c.append(L[i][1])
c = [0] + c

for i in range(1,len(c)):
    c[i] +=c[i-1]
#print(c)
l = 2*k
b = 0
ans = 0

if l >=len(s):
    print(len(s))
    exit()
else:
    while b + l < len(L):
        if L[b][0] == "1":
            ans = max(ans,c[l+b+1]-c[b])
            b+=1
            #l+=1
            #print(ans)
        elif L[b][0] == "0":
            ans = max(ans,c[l+b]-c[b])
            b+=1
            l+=1
            #print(ans)
        #print(b,l)
print(ans)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
s = input()
L = []
from itertools import groupby
for key ,value in groupby(s):
    L.append(list(value))
#print(L)
one_cnt = 0
zero_cnt = 0
zero_idx = 0
l,r = 0,0
ans = 0
while r <=len(L)-1:
    if L[r][0] == "1":
        one_cnt +=len(L[r])
        r +=1
    elif L[r][0] == "0":
        if zero_idx <k:
            zero_cnt +=len(L[r])
            r +=1
            zero_idx +=1
            #print(zero_cnt,one_cnt)
        elif zero_idx ==k:
            zero_cnt +=len(L[r][0])
            ans = max(ans,zero_cnt + one_cnt)
            while zero_idx == k:
                #print(zero_cnt,one_cnt)
                if l < len(L)-2:
                    l +=1
                elif l == len(L)-1:
                    break
                if L[l][0] == "1":
                    one_cnt -= len(L[l])
                    l += 1
                elif L[l][0] == "0":
                    zero_cnt -=len(L[l])
                    zero_idx -=1
    elif l == r:
        r +=1
print(ans)

Submission status: Wrong Answer

Solution code:
import sys

def LI2(): return list(map(int,sys.stdin.readline().rstrip()))  #空白なし
def S(): return sys.stdin.readline().rstrip()

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

A = []
count = 0
for i in range(N):
    if i == 0:
        count = 1
    else:
        if S[i] == S[i-1]:
            count += 1
        else:
            A.append(count)
            count = 1
A.append(count)

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

if S[0] == 1:
    A = [1] + A
if S[-1] == 1:
    A = A + [1]
# 両端は0だと考えてよい

from itertools import accumulate

M = (len(A)-1)//2
A = list(accumulate([0]+A))

ans = 0
for i in range(M-K+2):
    if i == 0:
        ans = max(ans,A[2*(K+i)]-A[2*i])
    elif i == M-K+1:
        ans = max(ans,A[2*(K+i)-1]-A[2*i-1])
    else:
        ans = max(ans,A[2*(K+i)]-A[2*i-1])

print(ans)

Submission status: Wrong Answer

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

nums = []
now = 1 # 今見ている値
cnt = 0 # 今見ている値の連続する個数
for i in range(n):
    if s[i] == str(now): # 今見ているもの同じ場合
        cnt += 1
    else:
        nums.append(cnt)
        now ^= 1
        cnt = 1
# if cnt != 0: # この条件分岐は不要
nums.append(cnt)
print(nums)
# 配列の大きさが奇数でない（入力が1で終わっていない）なら，0を追加
if len(nums) % 2 == 0:
    nums.append(0)

res = 0
for i in range(0, len(nums), 2): # 1のところを見ていく
    # この書き方はよくない
    # if 2 * k + 1 + i > len(nums):
    #     break
    # tmp = sum(nums[i : 2 * k + 1 + i])
    left, right = i, min(i + 2 * k + 1, len(nums))
    tmp = sum(nums[left:right])
    res = max(res, tmp)
print(res)

Submission status: Wrong Answer

Solution code:
from itertools import accumulate

def runlength(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 = list(input())
    T = runlength(S)
    if S[-1] == '0':
        T.append(0)
    
    w = 2 * K + 1
    if len(T) <= w:
        print(N)
        exit()

    acc = [0] + list(accumulate(T))
    ans = 0
    for i in range(0, len(T) - w + 1, 2):
        ans = max(ans, acc[i+w] -acc[i])

    print(ans)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:


def runlength(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 = list(input())
    T = runlength(S)    
    L = [0] * (len(T) + 1)
    for i in range(len(T)):
        L[i+1] = L[i] + int(T[i])
    ans = 0
    if S[0] == '0':
        for i in range(0,len(T),2):
            tmp = 0
            for k in range(K+1):
                j = min(i+2*k,len(T))
                tmp = L[j] - L[i]
                ans = max(ans, tmp)
    else:
        for i in range(0,len(T),2):
            tmp = 0
            for k in range(K+1):
                j = min(i+2*k+1,len(T))
                tmp = L[j] - L[i]
                ans = max(ans, tmp)

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

Submission status: Wrong Answer

Solution code:
def main():
    N, K = map(int, input().split())
    S = input()
    T = ""
    cnt = 1
    for i in range(N-1):
        if S[i] == S[i+1]:
            cnt += 1
        else:
            T += str(cnt)
            cnt = 1
    else:
        T += str(cnt)
    
    L = [0] * (len(T) + 1)
    for i in range(len(T)):
        L[i+1] = L[i] + int(T[i])
    ans = 0
    if S[0] == '0':
        for i in range(len(T)):
            tmp = 0
            for k in range(K+1):
                if i % 2 == 0:
                    j = min(i+2*k,len(T))
                else:
                    j = min(i+2*k+1,len(T))
                tmp = L[j] - L[i]
                ans = max(ans, tmp)
    else:
        for i in range(len(T)):
            tmp = 0
            for k in range(K+1):
                if i % 2 == 0:
                    j = min(i+2*k+1,len(T))
                else:
                    j = min(i+2*k,len(T))
                tmp = L[j] - L[i]
                ans = max(ans, tmp)

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

Submission status: Wrong Answer

Solution code:
def main():
    N, K = map(int, input().split())
    S = input()
    T = ""
    cnt = 1
    for i in range(N-1):
        if S[i] == S[i+1]:
            cnt += 1
        else:
            T += str(cnt)
            cnt = 1
    else:
        T += str(cnt)
    
    L = [0] * (len(T) + 1)
    for i in range(len(T)):
        L[i+1] = L[i] + int(T[i])
    ans = 0
    if S[i] == '0':
        for i in range(len(T)):
            tmp = 0
            for k in range(K+1):
                if i % 2 == 0:
                    j = min(i+2*k,len(T))
                else:
                    j = min(i+2*k+1,len(T))
                tmp = L[j] - L[i]
                ans = max(ans, tmp)
    else:
        for i in range(len(T)):
            tmp = 0
            for k in range(K+1):
                if i % 2 == 0:
                    j = min(i+2*k+1,len(T))
                else:
                    j = min(i+2*k,len(T))
                tmp = L[j] - L[i]
                ans = max(ans, tmp)

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

Submission status: Wrong Answer

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

S += '1' if S[N-1] == '0' else '0'
sumz = [0]*(N+2)
right = 1
cnt = 0
maxlen = 0
left = 0
while True:
    if left >= N: break
    if right <= left: right = left+1
    while (right <N) and ((cnt < K) or ((cnt == K) and ((S[right] == S[right-1]) or S[right] == '1'))):
        right += 1
        if (S[right] != S[right - 1]) and (S[right-1] == '0'):
            cnt += 1
    if not( S[right-1] == '0' and (K == 0)):
        if maxlen < right - left:
            maxlen = right - left
    #next
    while left < N and S[left] == S[left+1]:
        left += 1
    if S[left] == '0':
        cnt -= 1

    left += 1

print(maxlen)

Submission status: Wrong Answer

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

S += '1' if S[N-1] == '0' else '0'
sumz = [0]*(N+2)
right = 1
cnt = 0
maxlen = 0
left = 0
while True:
    if left >= N: break
    if right <= left: right = left+1
    while (right <N) and ((cnt < K) or ((cnt == K) and ((S[right] == S[right-1]) or S[right] == '1'))):
        right += 1
        if (S[right] != S[right - 1]) and (S[right-1] == '0'):
            cnt += 1

    if maxlen < right - left:
        maxlen = right - left

    #next
    while left < N and S[left] == S[left+1]:
        left += 1
    if S[left] == '0':
        cnt -= 1

    left += 1

print(maxlen)

Submission status: Wrong Answer

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

S += '1' if S[N-1] == '0' else '0'
sumz = [0]*(N+2)
right = 1
cnt = 0
maxlen = 0
for left in range(N+1):
    if right <= left: right = left+1
    while (right <N) and ((cnt < K) or ((cnt == K) and ((S[right] == S[right-1]) or S[right] == '1'))):
        right += 1
        if (S[right] != S[right - 1]) and (S[right-1] == '0'):
            cnt += 1

    if maxlen < right - left:
        maxlen = right - left

    #next
    while left < N and S[left] == S[left+1]:
        left += 1
    if S[left] == '0':
        cnt -= 1

print(maxlen)

Submission status: Wrong Answer

Solution code:
def main():
    N, K = map(int, input().split())
    S = input()
    T = ""
    cnt = 1
    for i in range(N-1):
        if S[i] == S[i+1]:
            cnt += 1
        else:
            T += str(cnt)
            cnt = 1
    else:
        T += str(cnt)
    
    L = [0] * (len(T) + 1)
    for i in range(len(T)):
        L[i+1] = L[i] + int(T[i])
    ans = 0
    for i in range(len(T)):
        tmp = 0
        for k in range(K+1):
            if S[i] == '0':    
                j = min(i+2*k,len(T))
            else:
                j = min(i+2*k+1,len(T))
            tmp = L[j] - L[i]
            ans = max(ans, tmp)

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

Submission status: Wrong Answer

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

i = 0
a = []
while i < n:
    l = 1
    while i+1 < n and s[i]==s[i+1]:
        l+=1
        i+=1
    a.append(l)
    i+=1

n = len(a)
if s[0] == 0:
    st = 0
else:
    st = 1

acum = [0]*(n+1)
for i in range(n):
    acum[i+1] = acum[i]+a[i]

ans = 0
if s[0] == 1:
    st = 0
else:
    st = 1
for i in range(st, n+1):
    end = min(i+2*k+1, n)
    ans = max(ans, acum[end] - acum[i])
print(ans)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
S = list(input())
A = {}
if S[0] == "1":
    A[1] = 0
else:
    A[0] = 0
c = 1
for i in range(N-1):
    if S[i] == "0" and S[i+1] == "1":
        c += 1
        A[c] = i+1
B = {}
c = 1
for i in range(N-1):
    if S[i] == "1" and S[i+1] == "0":
        B[c] = i
        c += 1

ans = 0
for p, v in A.items():
    t = B.get(p+K, N-1)
    ans = max(t-v+1, ans)
print(ans)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
s=input()
ban=[]
if s[0]=="0": ban.append(0)
cnt=1
for i,val in enumerate(s[:-1]):
    if val!=s[i+1]:
        ban.append(cnt)
        cnt=0
    cnt+=1
if cnt>0: ban.append(cnt)
ans=sum(ban[0:2*k+1])
su=sum(ban[0:2*k+1])
now=0
while now+2*k+2<len(ban):
    su-=ban[now]+ban[now+1]
    su+=ban[now+2*k+1]+ban[now+2*k+2]
    if su>ans: ans=su
    now+=2
print(ans)

Submission status: Wrong Answer

Solution code:
def main():
    n, k = map(int, input().split())
    s = list(input())
    answer = 0
    l = [[s[0], 1]]
    for i in range(1, n):
        if s[i] == l[-1][0]:
            l[-1][1] += 1
        else:
            l.append([s[i], 1])
    l.append(["0", 0])
    for _ in range(2):
        now_ans = 0
        now_k = k
        for standing, count in l:
            now_ans += count
            if now_k == 0:
                break
            elif standing == "0":
                now_k -= 1
        l = list(reversed(l))
        answer = max(answer, now_ans)
        k += 1
    print(answer)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n, k = map(int,input().split())
s = input()
sakidata = [1]
lastdata = [0]
#data... わける.

mode = False
for cnt, i in enumerate(s):
	if mode:
		if i == "0":
			lastdata.append(cnt)
			mode = False
	else:
		if i == "1":
			sakidata.append(cnt+1)
			mode = True

if mode:
	lastdata.append(cnt+1)

sakidata.append(n)
lastdata.append(n)

ans = 0

#print(sakidata)
#print(lastdata)

for i in range(len(lastdata)-k):
	targ = lastdata[i+k]-sakidata[i]+1
	ans = max(targ, ans)
print(ans)

Submission status: Wrong Answer

Solution code:
n, k = map(int,input().split())
s = input()
sakidata = [1]
lastdata = [0]
#data... わける.

mode = False
for cnt, i in enumerate(s):
	if mode:
		if i == "0":
			lastdata.append(cnt)
			mode = False
	else:
		if i == "1":
			sakidata.append(cnt+1)
			mode = True

if mode:
	lastdata.append(cnt+1)

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

Submission status: Wrong Answer

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

l_01 = []
l_10 = []
if s[0] == '1':
    l_01.append(0)
for i in range(n-1):
    if s[i] == '0' and s[i+1] == '1':
        l_01.append(i+1)
    elif s[i] == '1' and s[i+1] == '0':
        l_10.append(i+1)
if s[-1] == '1':
    l_10.append(n)

cnt_0 = len(l_01) - 1
if s[0] == '0':
    cnt_0 += 1
if s[-1] == '0':
    cnt_0 += 1
cnt_1 = len(l_01)

if k >= cnt_0:
    print(n)
else:
    res = 0
    i = 0
    while i + k < cnt_1:
        res = max(res, l_10[i+k] - l_01[i])
        i += 1
    if s[0] == '0':
        res = max(res, l_10[k-1])
    if s[-1] == '0':
        res = max(res, n - l_01[-k] + 1)
    print(res)

Submission status: Wrong Answer

Solution code:
def m():
    N, K = map(int, input().split())
    S = input()
    comp = []
    prev = S[0]
    cnt = 1
    for i in range(1, N):
        if prev == S[i]:
            cnt += 1
            prev = S[i]
        else:
            comp.append(cnt)
            prev = S[i]
            cnt = 1
    comp.append(cnt)
    comp_n = len(comp)
    acum = [0] * (comp_n + 1)
    acum[0] = 0
    for i in range(comp_n):
        acum[i + 1] = comp[i] + acum[i]
    left = 0
    right = 2 * K + 1 if S[0] == '1' else K * 2
    if (right-1) >= N: return N
    ans = 0
    while right <= comp_n:
        a = acum[right] - acum[left]
        ans = max(ans, a)
        left +=1
        right +=1
    return ans


print(m())

Submission status: Wrong Answer

Solution code:
# input()
# int(input())
# map(int, input().split())
# list(map(int, input().split()))
# list(map(int, list(input()))) # スペースがない数字リストを読み込み
import math
import fractions
import sys
import bisect
import heapq  # 優先度付きキュー(最小値取り出し)
import collections
from collections import Counter
from collections import deque
import pprint

sr = lambda: input()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

"""nを素因数分解"""
"""2以上の整数n => [[素因数, 指数], ...]の2次元リスト"""
def factorization(n):
    arr = []
    temp = n
    if n == 1:
        return arr

    for i in range(2, int(-(-n**0.5//1))+1):
        if temp%i==0:
            cnt=0
            while temp%i==0:
                cnt+=1
                temp //= i
            arr.append([i, cnt])

    if temp!=1:
        arr.append([temp, 1])

    if arr==[]:
        arr.append([n, 1])

    return arr

# a^n
def power(a,n,mod):
    x = 1
    while n:
        if n & 1:
            x *= a % mod
        n >>= 1
        a *= a % mod
    return x % mod

# n*(n-1)*...*(l+1)*l
def kaijo(n, l, mod):
    if n == 0:
        return 1
    a = n
    tmp = n-1
    while(tmp >= l):
        a = a*tmp%mod
        tmp -= 1
    return a

inf = 10**18
mod = 10**9+7

n,k = lr()
s = sr()
length1 = 0
tmp1 = 0
one = []
for c in s:
    if c == '1':
        tmp1+=1
    else:
        if tmp1 != 0:
            length1 += 1
            one.append(tmp1)
            tmp1 = 0
if tmp1 != 0:
    length1 += 1
    one.append(tmp1)

if length1 == 0:
    print(len(s))
    sys.exit()

zero = [0 for i in range(length1+1)]
ind = 0
while ind != n and s[ind] == '0':
    zero[0]+=1
    ind+=1
tmp0 = 0
ind0 = 1
while ind != n:
    while ind != n and s[ind] == '1':
        if tmp0 != 0:
            zero[ind0] = tmp0
            tmp0 = 0
            ind0+=1
        ind+=1
    while ind != n and s[ind] == '0':
        tmp0+=1
        ind+=1
if tmp0 != 0:
    zero[-1] = tmp0

a2 = [0 for i in range(len(zero)+1)]
a2[1] = zero[0]+one[0]
for i in range(2,len(zero)):
    a2[i] = a2[i-1]+zero[i-1]+one[i-1]
a2[len(zero)] = a2[len(zero)-1]+zero[len(zero)-1]

def sumArea(l,r):
    if l == 0:
        return a2[r]-a2[l]
    else:
        return a2[r]-a2[l]+one[l-1]

ans = 0
for i in range(len(zero)-k+1):
    ans = max(ans, sumArea(i,i+k))
print(ans)

Submission status: Wrong Answer

Solution code:
import itertools

def countstrs(s):
    return [(k, len(list(g))) for k, g in itertools.groupby(s)]

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

total = [0] * (n + 1)
cur = 0
for i in range(len(c)):
    cur += c[i][1]
    total[i+1] = cur



#print(dat)
#print(total)
res = 0
l = 0
r = (2*k) + 0
while l < len(c):
    if c[l][0] == "0":
        rr = r
    else:
        rr = r+1
    if rr > n:
        rr = n
    res = max(res, total[rr] - total[l])
    l += 1
    r += 1

print(res)

Submission status: Wrong Answer

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

#圧縮（正の数が直立，負の数が逆立ち）
people = []
tmp = 0
for s in S:
    if s == "0":#直立
        if tmp >= 0:
            tmp += 1
        else:
            people.append(tmp)
            tmp = 1
    else:#逆立ち
        if tmp <= 0:
            tmp -= 1
        else:
            people.append(tmp)
            tmp = -1
people.append(tmp)

#print(people)

rev = 0#反転回数
ans = 0
#初めの範囲で
for i,num in enumerate(people):
    if num < 0:#元から逆立ちの時
        ans += -num
    else:
        rev += 1
        ans += num
    if rev == K:#それ以上反転できないので，右隣の逆立ち(居れば)の分だけ足して終わる
        try:
            ans += -people[i+1]
            break
        except IndexError:#最後に足した逆立ち群がそもそも右端だったら
            break

#print("initial ans",ans)

#SlidingWindow 右端と左端
right = min(len(people)-1,i+1)
left = 0

tmp = ans
#SlidingWindow
while True:
    #左端の更新
    if people[left] < 0:
        tmp -= abs(people[left])
        try:
            tmp -= abs(people[left+1])
            left += 2
        except IndexError:
            break
    else:
        tmp -= abs(people[left])
        left += 1
    
    #右端の更新
    try:
        tmp += abs(people[right])
        right += 1
    except IndexError:
        break
    try:
        tmp += abs(people[right+1])
        right += 1
    except IndexError:
        break
    ans = max(tmp, ans)

ans = max(tmp, ans)
print(ans)

Submission status: Wrong Answer

Solution code:
import itertools

def countstrs(s):
    return [(k, len(list(g))) for k, g in itertools.groupby(s)]

n, k = map(int, input().split())
s = input()
c = countstrs(s)
dat = []
for char, num in c:
    dat.append(num)

total = [0] * (n + 1)
cur = 0
for i in range(len(dat)):
    cur += dat[i]
    total[i+1] = cur

#print(dat)
#print(total)
res = 0
l = 0
r = (2*k) + 0
while l < len(c):

    if c[l][0] == "0":
        rr = r
    else:
        rr = r+1
    if rr > n:
        rr = n

    res = max(res, total[rr] - total[l])

    l += 1
    r += 1

print(res)

Submission status: Wrong Answer

Solution code:
import sys
mina=10**10*(-1)
t=0
N,K=map(int,input().split())
S=input()
L=list()
c=S[0]
for i in range(N):
  if S[i]==c:
    t+=1
    if i==N-1:
      L.append(t)
  else:
    L.append(t)
    t=1
    if c=="0":
      c="1"
    else:
      c="0"
    if i==N-1:
      L.append(t)
if len(L)==2:
  print(sum(L))
  sys.exit()
if len(L)<=2*K:
  print(sum(L))
  sys.exit()
if len(L)>2*K and len(L)<=(2*K)+2:
  if S[0]==1:
    print(sum(L))
    sys.exit()
  else:
    print(max(sum(L[1:]),sum(L[:-1])))
    sys.exit()
for i in range(len(L)-(2*K)):
  if i%2==0:
    mina=max(mina,sum(L[i:i+2*K+1]))
print(mina)

Submission status: Wrong Answer

Solution code:
n,k=list(map(int,input().split()))
s=input()
cnt=[[],[]]
st=1
c=0
for i in s:
  if st==int(i):
    c+=1
  else:
    cnt[st].append(c)
    c=1
    st=1-st
cnt[st].append(c)
cnt[0].append(0)
cnt[1].append(0)
sm=[[],[]]
s=0
for i in cnt[0]:
  s+=i
  sm[0].append(s)
s=0
for i in cnt[1]:
  s+=i
  sm[1].append(s)
sm[0][-1]=0
sm[1][-1]=0
if len(cnt[0])<=k+1:
  print(n)
else:
  m=0
  for i in range(len(cnt[0])-k):
    s=sm[0][i+k-1]-sm[0][i-1]+sm[1][min(i+k,len(cnt[0])-2)]-sm[1][i-1]
    m=max(s,m)
  print(m)

Submission status: Wrong Answer

Solution code:
n,k=list(map(int,input().split()))
s=input()
cnt=[[],[]]
st=1
c=0
for i in s:
  if st==int(i):
    c+=1
  else:
    cnt[st].append(c)
    c=1
    st=1-st
cnt[st].append(c)
cnt[0].append(0)
cnt[1].append(0)
sm=[[],[]]
s=0
for i in cnt[0]:
  s+=i
  sm[0].append(s)
s=0
for i in cnt[1]:
  s+=i
  sm[1].append(s)
sm[0][-1]=0
sm[1][-1]=0

if len(cnt[0])<=k+1:
  print(n)
else:
  m=0
  for i in range(len(cnt[0])-k):
    s=sm[0][i+k-1]-sm[0][i-1]+sm[1][i+k]-sm[1][i-1]
    m=max(s,m)
  print(m)

Submission status: Wrong Answer

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

X=[]
x0=S[0]
if x0=="0":
    X.append(0)
cnt=1
for i in range(1,N):
    if x0==S[i]:
        cnt+=1
    else:
        X.append(cnt)
        cnt=1
        x0=S[i]
X.append(cnt)

L=len(X)
temp=sum(X[0:2*K+1])
ans=temp
idx=2
while idx<L:
    if idx+2*K>=L:
        temp=temp-(X[idx-2]+X[idx-1])
    else:
        temp=temp-(X[idx-2]+X[idx-1])+X[idx+2*K-1]+X[idx+2*K]
    ans=max(ans,temp)
    idx+=2
print(ans)

Submission status: Wrong Answer

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

X=[]
x0=S[0]
if x0=="0":
    X.append(0)
cnt=1
for i in range(1,N):
    if x0==S[i]:
        cnt+=1
    else:
        X.append(cnt)
        cnt=1
        x0=S[i]
X.append(cnt)

L=len(X)
ans=sum(X[0:2*K+1])
temp=ans
idx=2
while idx<L:
    if idx+2*K>=L:
        temp=temp-(X[idx-2]+X[idx-1])
    else:
        temp=temp-(X[idx-2]+X[idx-1])+(X[idx+2*K-1]+X[idx+2*K])
    ans=max(ans,temp)
    idx+=2
print(ans)

Submission status: Wrong Answer

Solution code:
N,K = map(int, input().split())
S = input()
M = []
count = 1
K2 = 0
if S[0]=="1":
  K2=K*2+1
else:
  K2=K*2
for n in range(0,N-1):
  if S[n]==S[n+1]:
    count+=1
  else:
    M.append(count)
    count = 1
M.append(count)
if len(M)<=K2:
  print(sum(M))
else:
  Max = sum(M[:K2])
  total = Max
  for n in range(len(M)-K2):
    total = total - M[n] + M[n+K2]
    if total >Max:
      Max = total
  print(Max)

Submission status: Wrong Answer

Solution code:
N,K = map(int, input().split())
S = input()
M = []
count = 1
for n in range(0,N-1):
  if S[n]==S[n+1]:
    count+=1
  else:
    M.append(count)
    count = 1
M.append(count)
if len(M)<=K*2+1:
  print(sum(M))
else:
  Max = sum(M[:K*2+1])
  total = Max
  for n in range(len(M)-K*2-1):
    total = total - M[n] + M[n+K*2+1]
    if total >Max:
      Max = total
  print(Max)

Submission status: Wrong Answer

Solution code:
import sys
import itertools
# import numpy as np
import time
import math
 
sys.setrecursionlimit(10 ** 7)
 
from collections import defaultdict
 
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

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

prev = S[0]
i = 0
count = 0
run_length = []
if S[0] == '0':
    run_length.append(0)
while i < len(S):
    c = S[i]
    if c == prev:
        count += 1
    else:
        run_length.append(count) 
        count = 1
    prev = c
    i += 1
run_length.append(count) 

acc = [0] * (len(run_length) + 1)
acc[0] = 0

for i in range(len(acc) - 1):
    acc[i + 1] = acc[i] + run_length[i]

ans = 0
for i in range(0, len(acc), 2):
    j = 2 * K + 1
    if j + i >= len(acc):
        break
    x = acc[j + i] - acc[i]
    ans = max(ans, x)

if ans == 0:
    for i in range(0, len(run_length), 2):
        ans = max(ans, run_length[i])
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
import itertools
# import numpy as np
import time
import math
 
sys.setrecursionlimit(10 ** 7)
 
from collections import defaultdict
 
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

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

prev = S[0]
i = 0
count = 0
run_length = []
if S[0] == '0':
    run_length.append(0)
while i < len(S):
    c = S[i]
    if c == prev:
        count += 1
    else:
        run_length.append(count) 
        count = 1
    prev = c
    i += 1
run_length.append(count) 

acc = [0] * (len(run_length) + 1)
acc[0] = 0

for i in range(len(acc) - 1):
    acc[i + 1] = acc[i] + run_length[i]

ans = 0
for i in range(0, len(acc), 2):
    j = 2 * K + 1
    if j + i >= len(acc):
        break
    x = acc[j + i] - acc[i]
    ans = max(ans, x)

for i in range(0, len(run_length), 2):
    ans = max(ans, run_length[i])
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
import itertools
# import numpy as np
import time
import math
 
sys.setrecursionlimit(10 ** 7)
 
from collections import defaultdict
 
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

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

prev = S[0]
i = 0
count = 0
run_length = []
if S[0] == '0':
    run_length.append(0)
while i < len(S):
    c = S[i]
    if c == prev:
        count += 1
    else:
        run_length.append(count) 
        count = 1
    prev = c
    i += 1
run_length.append(count) 

acc = [0] * (len(run_length) + 1)
acc[0] = 0

for i in range(len(acc) - 1):
    acc[i + 1] = acc[i] + run_length[i]

ans = 0
for i in range(0, len(acc), 2):
    j = 2 * K + 1
    if j + i >= len(acc):
        break
    x = acc[j + i] - acc[i]
    ans = max(ans, x)

if ans == 0:
    ans = max(acc)
print(ans)

Submission status: Wrong Answer

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

compressed = []

first = s[0]
prev = s[0]
cnt = 1
for x in s[1:]:
    if prev == x:
        cnt += 1
    else:
        if x == "1":
            compressed.append(cnt)
            cnt = 1
        else:
            compressed.append(cnt)
            cnt = 1
    prev = x
compressed.append(cnt)

if first == "1":
    zeros = len(compressed) // 2
else:
    zeros = (len(compressed) + 1) // 2

if zeros <= k:
    print(len(s))
    sys.exit()

ans = []
if first == "1":
    for i in range(0, (len(compressed) + 1) // 2, 2):
        ans.append(sum(compressed[i::2][:k + 1]) + sum(compressed[i + 1::2][:k]))
    for i in range(1, (len(compressed) + 1) // 2, 2):
        ans.append(sum(compressed[i::2][:k]) + sum(compressed[i + 1::2][:k]))
else:
    for i in range(0, (len(compressed) + 1) // 2, 2):
        ans.append(sum(compressed[i::2][:k]) + sum(compressed[i + 1::2][:k]))
    for i in range(1, (len(compressed) + 1) // 2, 2):
        ans.append(sum(compressed[i::2][:k + 1]) + sum(compressed[i + 1::2][:k]))

print(max(ans))

Submission status: Wrong Answer

Solution code:
import sys
import time
#from collections import deque, Counter, defaultdict
#from fractions import gcd
#import bisect
#import heapq
import math
start=time.time()
input = sys.stdin.readline
sys.setrecursionlimit(10**8)
inf = 10**18
MOD = 1000000007
ri = lambda : int(input())
rs = lambda : input().strip()
rl = lambda : list(map(int, input().split()))
n,k = rl()

s=rs()
now=s[0]
cnt=0
s+=str(1-int(s[-1]))

nums=[]
for i in s:
    if i==now:
        cnt+=1
    else:
        now=str(1-int(now))
        nums.append(cnt)
        cnt=1

SUM=[0]
for i in range(len(nums)):
    SUM.append(SUM[i]+nums[i])

skip=2*k+1
ans=0
for i in range(len(SUM)):
    right=min(i+skip, len(SUM)-1)
    left =i
    ans=max(ans,-SUM[left]+SUM[right])
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
import time
#from collections import deque, Counter, defaultdict
#from fractions import gcd
#import bisect
#import heapq
import math
start=time.time()
input = sys.stdin.readline
sys.setrecursionlimit(10**8)
inf = 10**18
MOD = 1000000007
ri = lambda : int(input())
rs = lambda : input().strip()
rl = lambda : list(map(int, input().split()))
n,k = rl()

s=rs()
now=s[0]
cnt=0
s+=str(1-int(s[-1]))

nums=[]
for i in s:
    if i==now:
        cnt+=1
    else:
        now=str(1-int(now))
        nums.append(cnt)
        cnt=1

SUM=[nums[0]]
for i in range(len(nums)-1):
    SUM.append(SUM[i]+nums[i+1])

skip=2*k+1
ans=0
for i in range(0,max(1,len(SUM)-skip)):
    right=min(i+skip, len(SUM)-1)
    left =i
    ans=max(ans,-SUM[left]+SUM[right])
print(ans)