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()
counts = []

now=S[0]
count = 0

for i in range(N):
    if now == S[i]:
        count += 1
    else:
        counts.append(count)
        now = S[i]
        count=1

counts.append(count)

ans = 0

if S[0] == "1":
    ans = sum(counts[0:2*K+1])
    kk = ans
    for i in range(2*K+2, len(counts), 2):
        kk = ans-counts[i-2*K-2]-counts[i-2*K-1] + counts[i]+counts[i-1]
        ans = max(kk,ans)

else:
    counts = [1] + counts
    ans = sum(counts[0:2*K+1])-1
    kk = ans+1
    for i in range(2*K+2, len(counts), 2):
        kk = ans-counts[i-2*K-2]-counts[i-2*K-1] + counts[i]+counts[i-1]
        ans = max(kk,ans)

a = len(counts)
ans = max(ans, sum(counts[a-2*K-1:a]))

print(ans)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
S = input()
left = 0
rc = 0
rf = False
lf = False
ans = 0
if set(S) == set("0") or set(S) == set("1"):
    print(len(S))
    exit()
for right in range(N):
    if rf and S[right] == "0" and rc <= K:
        continue
    if S[right] == "1":
        rf = False
    if S[right] == "0":
        rc += 1
        rf = True
    if rc > K:
        while True:
            if S[left] == "1":
                left += 1
            if S[left] == "0":
                while S[left] == "0":
                    left += 1
                break
        rc -= 1
    ans = max(ans, right - left)
print(ans+1)

Submission status: Wrong Answer

Solution code:
import numpy as np


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

# search where 0
s_arr = np.fromiter(map(int, S), int)
arr = np.array([1] + s_arr.tolist() + [1])
diff = np.diff(arr)
heads = np.where(diff == -1)[0]
tails = np.where(diff == 1)[0]

assert heads.size == tails.size

# large -> small
indices = np.argsort(tails - heads)[::-1]
k = min(K, len(indices))
for i in range(k):
    idx = indices[i]
    head, tail = heads[idx], tails[idx]
    s_arr[head:tail] = 1

# search where 1
arr = np.array([0] + s_arr.tolist() + [0])
diff = np.diff(arr)
heads = np.where(diff == 1)[0]
tails = np.where(diff == -1)[0]

assert heads.size == tails.size

print((tails - heads).max())

Submission status: Wrong Answer

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

one = []
cnt = 0
for i, s in enumerate(S):
    if s == '1':
        cnt += 1
        if i == N - 1:
            one.append(cnt)
    elif s == '0':
        if cnt > 0:
            one.append(cnt)
            cnt = 0
            
zero = []
cnt = 0
for i, s in enumerate(S):
    if s == '0':
        cnt += 1
        if i == N - 1:
            zero.append(cnt)
    elif s == '1':
        if cnt > 0:
            zero.append(cnt)
            cnt = 0
            
if len(one) < len(zero):
    one = [0] + one + [0]
    
ans = -1
for i in range(len(one) - (K + 1) + 1):
    tmp = sum(one[i : i + K + 1]) + sum(zero[i : i + K])
    ans = max(ans, tmp)
    
if S.count('0') == 0:
    print(len(S))
else:
    print(ans)

Submission status: Wrong Answer

Solution code:
import numpy as np

N, K = tuple([int(x) for x in input().split(' ')])
# S = np.array([int(x) for x in input()])

y = '2'
tmp = []
for x in input():
    if x == y:
        tmp[-1] += 1
    else:
        tmp.append(1)
        y = x

out = [0]
for i in range(len(tmp)-2*K):
    tmp2 = tmp[(i+1):(i+2*K)]
    if sum(tmp2)==len(tmp2):
        out.append(sum(tmp[i:(i+2*K)]))
    else:
        out.append(sum(tmp[i:(i+2*K+1)]))

print(max(out))

Submission status: Wrong Answer

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

tans = 0
ans = 0
r = 0
l = 0
t = k

while r<n:
  if (s[r-1]=="1" and s[r]=="0" and t>0):
    t -= 1
  if s[r-1]=="1" and s[r]=="0" and t==0:
    tans -= 1
    while s[l]=="1":
      l += 1
    r += 1
  else:
    tans += 1
    r += 1
  ans = max(ans, tans)

print(ans)

Submission status: Wrong Answer

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

now=S[0]
count = 0

for i in range(N):
    if now == S[i]:
        count += 1
    else:
        counts.append(count)
        now = S[i]
        count=1

counts.append(count)

ans = 0

if S[0] == "1":
    ans = sum(counts[0:2*K+1])
    kk = ans
    for i in range(2*K+2, len(counts), 2):
        kk = ans-counts[i-2*K-2]-counts[i-2*K-1] + counts[i]+counts[i-1]
        ans = max(kk,ans)

else:
    counts = [1] + counts
    ans = sum(counts[0:2*K+1])-1
    kk = ans+1
    for i in range(2*K+2, len(counts), 2):
        kk = ans-counts[i-2*K-2]-counts[i-2*K-1] + counts[i]+counts[i-1]
        ans = max(kk,ans)

a = len(counts)
ans = max(ans, sum(counts[a-2*K:a]))

print(ans)

Submission status: Wrong Answer

Solution code:

def main():
    n, k = [int(i) for i in input().split()]
    s = input() + "3"

    start = 1
    s0 = []
    s1 = []
    cnt = 0
    for i in range(n+1):
        if start == 1 and s[i] != "1":
            s1.append(cnt)
            cnt = 1
            start = 0
        elif start == 0 and s[i] != "0":
            s0.append(cnt)
            cnt = 1
            start = 1
        else:
            cnt += 1

    ans = max(s1)
    for i in range(len(s0) - k):
        ans = max(ans, sum(s1[i:i+k+1]) + sum(s0[i:i+k]))
        if len(s0) < len(s1) and i+k == len(s0) -1:
            ans = max(ans, sum(s1[i:i + k + 1]) + sum(s0[i:i + k]) + s1[i+k+1])

    print(ans)

    #print(s1, s0)

main()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
import sys
import itertools
INF = float("inf")


def solve(N: int, K: int, S: str):
    s = []
    buf = 0
    mode = S[0]
    for c in S:
        if c == mode:
            buf += 1
        else:
            s.append(buf)
            buf = 1
            mode = c
    s.append(buf)
    # print(s)

    tot = [0] + list(itertools.accumulate(s))
    # print(tot)
    w = 2*K+1 if S[0] == "1" else 2*K
    # print(w)

    hint = [tot[i+w]-tot[i] for i in range(len(tot)-w)]
    # print(hint)
    if len(hint) == 0:
        print(len(S))
    else:
        print(max(hint))
    return


def main():

    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    K = int(next(tokens))  # type: int
    S = str(next(tokens))  # type: int
    solve(N, K, S)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
s = input()
next = 0
l = []
ans = 0
if s[0]=='0':
    cnt=1
else:
    cnt = 0
for i in range(n):
    if s[i]=='0' and s[i-1]=='1':
        cnt += 1
    if i<n-1 and s[i]=='0' and s[i+1]=='1':
        l.insert(0,i+1)
    if i<n-1 and s[i]=='1' and s[i+1]=='0' and cnt==k:
        ans = max(ans,i+1-next)
        next = l.pop()
        cnt -= 1
    if i==n-1 and s[i]=='1':
        ans = max(ans,i+1-next)
    if i==n-1 and s[i]=='0':
        ans = max(ans,i+1-next)

print(ans)

Submission status: Wrong Answer

Solution code:
from sys import exit
N,K = [int(n) for n in input().split()]
# N = int(input())
# a = [int(input()) for _ in range(N)]
S = str(input())
L = len(S)
if(S[0] == "0"):#偶数Indexがブリッジ
    st0 = True
else:
    st0 = False
# T = str(input())
# exit()
tmp = S[0]
lis = []
maxlen = 0
for i in range(1,L):
    if(S[i]!=S[i-1]):
        lis.append(len(tmp))
        maxlen = max(maxlen,len(tmp))
        tmp =S[i]
    else:
        tmp+=S[i]
lis.append(len(tmp))
# print(lis)
# print(maxlen)
# print(st0)
ans = 0
# tmpans = maxlen
L = len(lis)
if(st0):#奇数Indexをうめる時！000101010
    r = 0
    tmpans = 0
    tmpk = 0#うめたかず
    for i in range(0,L,2):
        if(tmpk < K and i+1 < L):#うめられるだけうめてない
            tmpk+=1
            tmpans+=lis[i]+lis[i+1]
        elif i+1 < L:#うめきった
            ans = max(ans,tmpans)
            tmpans-=lis[r]+lis[r+1]
            tmpans+=lis[i]+lis[i+1]
            r+=2
    print(max(ans,tmpans))
else:#偶数Indexをうめる時！11101010110011
    r = 0
    tmpans = lis[0]
    tmpk = 0#うめたかず
    for i in range(1,L,2):
        if(tmpk < K and i+1 < L):#うめられるだけうめてない
            tmpk+=1
            tmpans+=lis[i]+lis[i+1]
        elif i+1 < L:#うめきった
            ans = max(ans,tmpans)
            # print(ans,i)
            tmpans-=lis[r]+lis[r+1]
            tmpans+=lis[i]+lis[i+1]
            r+=2
    print(max(ans,tmpans))
# 14 2
# 11101010110011
# 10 1
# 0000000001

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
S = list(input())
left = 0
right = 0
onecnt = 0
zerocnt = 0
zeroflag = False
ans = 0

while right < N:
    if S[right] == '0' and not zeroflag:
        zerocnt += 1
        zeroflag = True
    elif S[right] == '1' and zeroflag:
        zeroflag = False
        
    if zerocnt > K and zeroflag:
        tmp = right - left
        if tmp > ans:
            ans = tmp
        while True:
            left += 1
            if S[left] == '0':
                left += 1
                break
        zerocnt -= 1
    right += 1

if right - left > ans:
    ans = right - left

print(ans)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
S = list(input())
flag = False
zero = 0
for s in S:
    if flag is False:
        if s == '0':
            zero += 1
            flag = True
        else:
            flag = False
    else:
        if s == '0':
            continue
        else:
            flag = False
if K >= zero:
    print(N)
    exit()
ans1 = 0
k = K
flag = False
for i in range(N):
    if flag is False:
        if S[i] == '1':
            ans1 += 1
        else:
            flag = True
            if k >= 1:
                k -= 1
                ans1 += 1
            else:
                break
    else:
        if S[i] == '1':
            flag = False
            ans1 += 1
        else:
            ans1 += 1

S.reverse()
ans2 = 0
k = K
flag = False
for i in range(N):
    if flag is False:
        if S[i] == '1':
            ans2 += 1
        else:
            flag = True
            if k >= 1:
                k -= 1
                ans2 += 1
            else:
                break
    else:
        if S[i] == '1':
            flag = False
            ans2 += 1
        else:
            ans2 += 1
print(max(ans1, ans2))

Submission status: Wrong Answer

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

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

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

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

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

    return

#E
def E():
    return

#F
def F():
    return

#G
def G():
    return

#H
def H():
    return

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

Submission status: Wrong Answer

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


l=[]
p="1"
q=0
for i in s:
    if i==p:
        q+=1
    else:
        l.append(q)
        q=1
        if p=="1":
            p="0"
        else:
            p="1"
l.append(q)

r=[0]
def ruiseki(a):
    for i in range(len(a)):
        r.append(r[i]+a[i])
ruiseki(l)
ans=0
    
for i in range(max(len(r)-(2*k+2)+1,1)):
    if min(len(r)-1,i+2*k+1)%2==1:
        ans=max(ans,r[min(len(r)-1,i+2*k+1)]-r[i])

print(ans)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import bisect
import heapq
import math
import random
import sys
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations
from operator import add, mul, sub

sys.setrecursionlimit(100000)


def read_int():
    return int(input())


def read_int_n():
    return list(map(int, input().split()))


def read_float():
    return float(input())


def read_float_n():
    return list(map(float, input().split()))


def read_str():
    return input().strip()


def read_str_n():
    return list(map(str, input().split()))


def error_print(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.time()
        ret = f(*args, **kwargs)
        e = time.time()

        error_print(e - s, 'sec')
        return ret

    return wrap


@mt
def slv(N, K, S):
    s1 = list(map(lambda x: len(x), filter(lambda x: x != '', S.split('0'))))
    s0 = list(map(lambda x: len(x), filter(lambda x: x != '', S.split('1'))))

  
    ans = 0
    
    k = 0
    l1o = 1 if S[0] == '1' else 0
    # l0o = 1 if S[0] == '0' else 0
    l0o = 0
    l1 = sum(s1[:K+l1o])
    l0 = sum(s0[:K+l0o])
    while True:
        # print(l1+l0, l1, l0)
        ans = max(ans, l1 + l0)
        if k+K < len(s1) and k+K < len(s0):
            l1 -= s1[k]
            if k+K+l1o < len(s1):
                l1 += s1[k+K+l1o]
            l0 -= s0[k]
            if k+K+l0o < len(s0):
                l0 += s0[k+K+l0o]
        else:
            break
        k += 1

    return ans


def main():
    N, K = read_int_n()
    S = read_str()
    rS = ''.join(['0' if c == '1' else '1' for c in S])
    print(max(slv(N, K, S), slv(N, K-1, rS)))

    # N = 20
    # K = random.randint(1, 10)
    # S = ''.join(random.choices('01', k=N))
    # rS = ''.join(['0' if c == '1' else '1' for c in S])
    # print(max(slv(N, K, S), slv(N, K-1, rS)))

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
N, K = [int(_) for _ in input().split()]
S = input()
O = [[0, 0]]
is1 = False
for i in range(N):
    if S[i] == '1':
        if not is1:
            O += [[i, N]]
        is1 = True
    elif S[i] == '0':
        if is1:
            O[-1][-1] = i
        is1 = False
O += [[N, N]]
K = min([len(O) + 1, K])
ans = 0
for i in range(len(O) - K):
    ans = max([ans, O[i + K][1] - O[i][0]])
print(ans)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import bisect
import heapq
import math
import random
import sys
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations
from operator import add, mul, sub

sys.setrecursionlimit(100000)


def read_int():
    return int(input())


def read_int_n():
    return list(map(int, input().split()))


def read_float():
    return float(input())


def read_float_n():
    return list(map(float, input().split()))


def read_str():
    return input().strip()


def read_str_n():
    return list(map(str, input().split()))


def error_print(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.time()
        ret = f(*args, **kwargs)
        e = time.time()

        error_print(e - s, 'sec')
        return ret

    return wrap


@mt
def slv(N, K, S):
    s1 = list(map(lambda x: len(x), filter(lambda x: x != '', S.split('0'))))
    s0 = list(map(lambda x: len(x), filter(lambda x: x != '', S.split('1'))))

    error_print(N, K)
    error_print(S)
    error_print(s1)
    error_print(s0)
    ans = 0
    
    k = 0
    l1o = 1 if S[0] == '1' else 0
    # l0o = 1 if S[0] == '0' else 0
    l0o = 0
    l1 = sum(s1[:K+l1o])
    l0 = sum(s0[:K+l0o])
    while True:
        print(l1+l0, l1, l0)
        ans = max(ans, l1 + l0)
        if k+K < len(s1) and k+K < len(s0):
            l1 -= s1[k]
            if k+K+l1o < len(s1):
                l1 += s1[k+K+l1o]
            l0 -= s0[k]
            if k+K+l0o < len(s0):
                l0 += s0[k+K+l0o]
        else:
            break
        k += 1

    return ans


def main():
    N, K = read_int_n()
    S = read_str()
    rS = ''.join(['0' if c == '1' else '1' for c in S])
    print(max(slv(N, K, S), slv(N, K-1, rS)))

    # N = 20
    # K = random.randint(1, 10)
    # S = ''.join(random.choices('01', k=N))
    # rS = ''.join(['0' if c == '1' else '1' for c in S])
    # print(max(slv(N, K, S), slv(N, K-1, rS)))

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
N,K = list(map(int,input().split()))
S = input()
n = []
p = S[0]
c = 1
for s in S[1:]:
    if s != p:
        n.append(c)
        p = s
        c = 0
    c+=1
n.append(c)
ans = max(n)
sum_n = [0]
for i in n:
    sum_n.append(i+sum_n[-1])
if S[0]=="0":
    for i in range(1,len(n)-2*K+1,2):
        ans = max(ans,sum_n[min(i+2*K+1,len(sum_n)-1)]-sum_n[i])
    sum_n.pop(1)
    n.pop(0)
    K -=1
for i in range(0,len(n)-2*K+1,2):
    ans = max(ans,sum_n[min(i+2*K+1,len(sum_n)-1)]-sum_n[i])
print(ans)

Submission status: Wrong Answer

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

before = s[0]
count = 1
lst = []
head = True if before == '1' else False
tail = True if s[-1] == '1' else False

for i in s[1:]:
    if i == before:
        count += 1
    else:
        lst.append(count)
        count = 1
        before = i
lst.append(count)

S = 0

lst1 = lst
if not head:
    S = sum(lst1[:2*k])
    lst1 = lst1[1:]
if not tail:
    S = max(S, sum(lst1[-2*k:]))
    lst1 = lst1[:-1]

N = len(lst1)
if N <= 2*k + 1:
    S = max(S, sum(lst1))
else:
    K = 2*k+1
    for i in range(0, N-K+1):
        S = max(S, sum(lst1[i:i+K]))
        #print(i, lst1[i:i+K])

print(S)

Submission status: Wrong Answer

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


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

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
S = input()
left = 0
rc = 0
rf = False
lf = False
ans = 0
for right in range(N):
    if rf and S[right] == "0" and rc <= K:
        continue
    if S[right] == "1":
        rf = False
    if S[right] == "0":
        rc += 1
        rf = True
    if rc > K:
        while True:
            if S[left] == "1":
                left += 1
            if S[left] == "0":
                while S[left] == "0":
                    left += 1
                break
        rc -= 1
    ans = max(ans, right - left)
print(ans+1)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
S = input()
L = [0]
prev = S[0]
num = 1
for s in S[1:]:
    if s == prev:
        num += 1
    else:
        L.append(num)
        num = 1
        prev = s
L.append(num)
for i in range(len(L)):
    L[i] += L[i - 1]
left = 0
if S[0] == "1":
    right = min(2 * K + 1, len(L) - 1)
else:
    right = min(2 * K, len(L) - 1)
num = 0
while right < len(L):
    num = max(num, L[right] - L[left])
    left += 2
    right += 2
print(num)

Submission status: Wrong Answer

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

if(len(s) == 1):
    print(1)
    exit()

num = [1]
flag = True

ans = 0
tmp = s[0]
if(tmp == "1"): flag = False

for i in s[1:]:
    if(tmp == i): num[-1] += 1
    else:
        num.append(1)
        if(len(num)%2 == 0):
            if(k < len(num)//2):
                tmp_ans = sum(num[:-1])
                if(ans < tmp_ans): ans = tmp_ans
                num = num[1:]
                flag = not flag
        else:
            if(k < len(num)//2 + flag):
                tmp_ans = sum(num[:-1])
                if(ans < tmp_ans): ans = tmp_ans
                num = num[1:]
                flag = not flag
        tmp = i

if(ans < sum(num)): ans = sum(num)

print(ans)

Submission status: Wrong Answer

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

c_1 = [0]
c_0 = [0]
count = 0
before = '1'
answer = 0



for i in S:
    if i == before:
        count += 1
    else:
        if before == '1':
            c_1.append(c_1[-1] + count)
        else:
            c_0.append(c_0[-1] + count)
        if i == '0':
            if K+1 < len(c_1):
                answer = max(answer, c_1[-1] - c_1[-(K+2)] + c_0[-1] - c_0[-(K+1)])
            else:
                answer = max(answer, sum(c_1) + sum(c_0))
        count = 1
        before = i

if i == '1':
    c_1.append(c_1[-1] + count)
else:
    c_0.append(c_0[-1] + count)

if K + 1 < len(c_1):
    answer = max(answer, c_1[-1] - c_1[-(K + 2)] + c_0[-1] - c_0[-(K + 1)])
else:
    answer = max(answer, sum(c_1) + sum(c_0))






print(answer)

Submission status: Wrong Answer

Solution code:
from sys import exit
N,K = [int(n) for n in input().split()]
# N = int(input())
# a = [int(input()) for _ in range(N)]
S = str(input())
L = len(S)
if(S[0] == "0"):#偶数Indexがブリッジ
    st0 = True
else:
    st0 = False
# T = str(input())
# exit()
tmp = S[0]
lis = []
maxlen = 0
for i in range(1,L):
    if(S[i]!=S[i-1]):
        lis.append(len(tmp))
        maxlen = max(maxlen,len(tmp))
        tmp =S[i]
    else:
        tmp+=S[i]
lis.append(len(tmp))
# print(lis)
# print(maxlen)
# print(st0)
ans = 0
# tmpans = maxlen
L = len(lis)
if(st0):#奇数Indexをうめる時！000101010
    r = 0
    tmpans = 0
    tmpk = 0#うめたかず
    for i in range(0,L,2):
        if(tmpk < K and i+1 < L):#うめられるだけうめてない
            tmpk+=1
            tmpans+=lis[i]+lis[i+1]
        elif i+1 < L:#うめきった
            ans = max(ans,tmpans)
            tmpans-=lis[r]+lis[r+1]
            tmpans+=lis[i]+lis[i+1]
            r+=2
    print(max(ans,tmpans))
else:#偶数Indexをうめる時！11101010110011
    r = 0
    tmpans = lis[0]
    tmpk = 0#うめたかず
    for i in range(1,L,2):
        if(tmpk < K and i+1 < L):#うめられるだけうめてない
            tmpk+=1
            tmpans+=lis[i]+lis[i+1]
        elif i+1 < L:#うめきった
            ans = max(ans,tmpans)
            print(ans,i)
            tmpans-=lis[r]+lis[r+1]
            tmpans+=lis[i]+lis[i+1]
            r+=2
    print(max(ans,tmpans))
# 14 2
# 11101010110011
# 10 1
# 0000000001

Submission status: Wrong Answer

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

    if(index==N-1):
        if(x!=0):
            li.append(0)
        li.append(0)

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

Submission status: Wrong Answer

Solution code:
N,K = list(map(int,input().split()))
S = input()
n = []
p = S[0]
c = 1
for s in S[1:]:
    if s != p:
        n.append(c)
        p = s
        c = 0
    c+=1
n.append(c)
ans = n[0]
sum_n = [0]
for i in n:
    sum_n.append(i+sum_n[-1])
if S[0]=="0":
    for i in range(1,len(n)-2*K+1,2):
        ans = max(ans,sum_n[min(i+2*K+1,len(sum_n)-1)]-sum_n[i])
    sum_n.pop(1)
    n.pop(0)
    K -=1
for i in range(0,len(n)-2*K+1,2):
    ans = max(ans,sum_n[min(i+2*K+1,len(sum_n)-1)]-sum_n[i])
print(ans)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
s = input()
c = [1]

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

if len(c) < 3:
    print(sum(c))
    exit(0)

cs = c
ans = 0

for i in range(k):
    m = 0

    for j in range(len(cs) - 2):
        s3 = cs[j] + cs[j + 1] + cs[j + 2]

        if s3 > m:
            mj = j
    cs[j : j + 3] = [s3]

    if i == k - 1:
        print(s3)

Submission status: Wrong Answer

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

now=S[0]
count = 0

for i in range(N):
    if now == S[i]:
        count += 1
    else:
        counts.append(count)
        now = S[i]
        count=1

counts.append(count)

ans = 0

if S[0] == "1":
    ans = sum(counts[0:2*K+1])
    kk = ans
    for i in range(2*K+2, len(counts), 2):
        kk = ans-counts[i-2*K-2]-counts[i-2*K-1] + counts[i]+counts[i-1]
        ans = max(kk,ans)

else:
    counts = [1] + counts
    ans = sum(counts[0:2*K+1])-1
    kk = ans+1
    for i in range(2*K+2, len(counts), 2):
        kk = ans-counts[i-2*K-2]-counts[i-2*K-1] + counts[i]+counts[i-1]
        ans = max(kk,ans)

print(ans)

Submission status: Wrong Answer

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

if S.count("1") == N or S.count("0") == N:
  print(N)
else:
  ans = 0
  for i in range(N):
    cnt = 0
    change = 0
    for j in range(i, N):
      if j != 0 and S[j] == "0" and S[j - 1] == "1":
        change += 1
      if change <= K:
        cnt += 1
      else:
        break
    ans = max(ans, cnt)
  print(ans)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-

NK = input().split(' ')
N = int(NK[0])
K = int(NK[1])
S = input()

prev = '1'
count = 0
nums = []
for i in S:
    if i == prev:
        count += 1
    else:
        nums.append(count)
        count = 1
        prev = i
if count > 0:
    nums.append(count)
# print(nums)

C = min(K * 2 + 1, len(nums))
s1 = 0
for i in range(0, C):
    s1 += nums[i]
sums = [s1]
prev = s1
for i in range(0, len(nums) - C):
    s = prev - nums[i] + nums[i + C]
    if i % 2 == 1:
        sums.append(s)
    prev = s
# print(sums)

print(max(sums))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
from bisect import bisect_left
N, K = map(int, input().split())
S = input()


S += 'X'
Zero_begin = []

begin_checked = False
for i, s in enumerate(S):
    if not begin_checked:
        if s == '0':
            Zero_begin.append(i)
            begin_checked = True

    else:
        if s != '0':
            begin_checked = False


Zero_begin.append(N)
ans = 0
for i in range(N):
    left_i = bisect_left(Zero_begin, i)
    right_i = min(left_i + K, len(Zero_begin) - 1)
    ans = max(ans, Zero_begin[right_i] - i)

print(ans)

Submission status: Wrong Answer

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

L = [[0, 0]]
re = S[0]
for i in range(1, len(S)):
  if S[i] != re:
    re = S[i]
    L[-1][1] = i
    L.append([i, 0])
l = len(L)
L[-1][1] = N

if S[0] == "0":
  z = (l // 2) + 1
else:
  z = (l + 1) // 2
  
if z <= K:
  print(N)
  exit()

ma = 0
if S[0] == "0":
  ma = max(ma, L[K * 2 - 1][1])
  s = 1
else:
  s = 0
if S[-1] == "0":
  ma = max(ma, N - L[-K * 2 + 1][0])

t = K * 2
for i in range(s, l - t, 2):
  ma = max(ma, L[t + i][1] - L[i][0])
print(ma)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
s = input()
next = 0
l = []
ans = 0
if s[0]=='0':
    cnt=1
else:
    cnt = 0
for i in range(n):
    if s[i]=='0' and s[i-1]=='1':
        cnt += 1
    if i<n-1 and s[i]=='0' and s[i+1]=='1':
        l.insert(0,i+1)
    if i<n-1 and s[i]=='1' and s[i+1]=='0' and cnt==k:
        ans = max(ans,i+1-next)
        next = l.pop()
        cnt -= 1
    if i==n-1 and s[i]=='1':
        ans = max(ans,i+1-next)

print(ans)

Submission status: Wrong Answer

Solution code:

N,K = [int(i) for i in input().split()]
S = list(input())

ans = 0
flag = 0
mae = S[0]
tmp = 1
i = 1
while i < N:
    tmp += 1
    if mae != S[i]:
        if mae == '0':
            flag += 1
        else:
            if flag == K:
                ans = max(ans, tmp-1)
                tmp = 1
                i -= 1
                flag = 0
    mae = S[i]
    i += 1
ans = max(ans, tmp)
print(ans)