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:
import sys
# import bisect
# import math
# import itertools
# import numpy as np
# import collections

"""Template"""


class IP:
    """
    入力を取得するクラス
    """

    def __init__(self):
        self.input = sys.stdin.readline

    def I(self):
        """
        1文字の取得に使います
        :return: int
        """
        return int(self.input())

    def S(self):
        """
        1文字の取得(str
        :return: str
        """
        return self.input()

    def IL(self):
        """
        1行を空白で区切りリストにします(int
        :return: リスト
        """
        return list(map(int, self.input().split()))

    def SL(self):
        """
        1行の文字列を空白区切りでリストにします
        :return: リスト
        """
        return list(map(str, self.input().split()))

    def ILS(self, n):
        """
        1列丸々取得します(int
        :param n: 行数
        :return: リスト
        """
        return [int(self.input()) for _ in range(n)]

    def SLS(self, n):
        """
        1列丸々取得します（str
        :param n: 行数
        :return: リスト
        """
        return [self.input() for _ in range(n)]

    def SILS(self, n):
        """
        Some Int LineS
        横に複数、縦にも複数
        :param n: 行数
        :return: list
        """
        return [self.IL() for _ in range(n)]

    def SSLS(self, n):
        """
        Some String LineS
        :param n: 行数
        :return: list
        """
        return [self.SL() for _ in range(n)]


class Idea:
    def __init__(self):
        pass

    def HF(self, p):
        """
        Half enumeration
        半分全列挙です
        pの要素の和の組み合わせを作ります。
        ソート、重複削除行います
        :param p: list : 元となるリスト
        :return: list : 組み合わせられた和のリスト
        """
        return sorted(set(p[i] + p[j] for i in range(len(p)) for j in range(i, len(p))))

    def Bfs2(self, a):
        """
        bit_full_search2
        bit全探索の改良版
        全探索させたら2進数のリストと10進数のリストを返す
        :return: list２つ : １個目 2進数(16桁)  2個目 10進数
        """
        # 参考
        # https://blog.rossywhite.com/2018/08/06/bit-search/
        # https://atcoder.jp/contests/abc105/submissions/4088632
        value = []
        for i in range(1 << len(a)):
            output = []

            for j in range(len(a)):
                if self.bit_o(i, j):
                    """右からj+1番目のiが1かどうか判定"""
                    # output.append(a[j])
                    output.append(a[j])
            value.append([format(i, 'b').zfill(16), sum(output)])

        value.sort(key=lambda x: x[1])
        bin = [value[k][0] for k in range(len(value))]
        val = [value[k][1] for k in range(len(value))]
        return bin, val

    def S(self, s, r=0, m=-1):
        """
        ソート関係行います。色々な設定あります。
        :param s: 元となるリスト
        :param r: reversするかどうか 0=False 1=True
        :param m: （2次元配列）何番目のインデックスのソートなのか
        :return: None
        """
        r = bool(r)
        if m == -1:
            s.sort(reverse=r)
        else:
            s.sort(reverse=r, key=lambda x: x[m])

    def bit_n(self, a, b):
        """
        bit探索で使います。0以上のときにTrue出します
        自然数だからn
        :param a: int
        :param b: int
        :return: bool
        """
        return bool((a >> b & 1) > 0)

    def bit_o(self, a, b):
        """
        bit探索で使います。1のときにTrue出すよ
        oneで1
        :param a: int
        :param b: int
        :return: bool
        """
        return bool(((a >> b) & 1) == 1)

    def ceil(self, x, y):
        """
        Round up
        小数点切り上げ割り算
        :param x: int
        :param y: int
        :return: int
        """
        return -(-x // y)

    def ave(self, a):
        """
        平均を求めます
        :param a: list
        :return: int
        """
        return sum(a) / len(a)

    def gcd(self, x, y):
        if y == 0:
            return x
        else:
            return self.gcd(y, x % y)


"""ここからメインコード"""


def main():
    # 1文字に省略
    r, e, p = range, enumerate, print
    ip = IP()
    id = Idea()
    mod = 10 ** 9 + 7

    """この下から書いてね"""
    n, k = ip.IL()
    s = input() + "2"

    zeros = []
    ones = [0]
    cnt = 0
    start = 1
    for x in s:
        if start == 1 and x != "1":
            ones += [cnt]
            cnt = 1
            start = 0
        elif start == 0 and x != "0":
            zeros += [cnt]
            cnt = 1
            start = 1
        else:
            cnt += 1

    if len(zeros) == len(ones):
        ones += [0]

    ones += [0]

    ans = sum(ones[0:k+1]) + sum(zeros[0:k])
    n_ans = ans

    for i in r(len(zeros) - k):
        n_ans = n_ans + ones[k+1+i] + zeros[k+i] - ones[i] - zeros[i]
        ans = max(ans, n_ans)

    p(ans)


main()

Submission status: Wrong Answer

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

"""Template"""


class IP:
    """
    入力を取得するクラス
    """

    def __init__(self):
        self.input = sys.stdin.readline

    def I(self):
        """
        1文字の取得に使います
        :return: int
        """
        return int(self.input())

    def S(self):
        """
        1文字の取得(str
        :return: str
        """
        return self.input()

    def IL(self):
        """
        1行を空白で区切りリストにします(int
        :return: リスト
        """
        return list(map(int, self.input().split()))

    def SL(self):
        """
        1行の文字列を空白区切りでリストにします
        :return: リスト
        """
        return list(map(str, self.input().split()))

    def ILS(self, n):
        """
        1列丸々取得します(int
        :param n: 行数
        :return: リスト
        """
        return [int(self.input()) for _ in range(n)]

    def SLS(self, n):
        """
        1列丸々取得します（str
        :param n: 行数
        :return: リスト
        """
        return [self.input() for _ in range(n)]

    def SILS(self, n):
        """
        Some Int LineS
        横に複数、縦にも複数
        :param n: 行数
        :return: list
        """
        return [self.IL() for _ in range(n)]

    def SSLS(self, n):
        """
        Some String LineS
        :param n: 行数
        :return: list
        """
        return [self.SL() for _ in range(n)]


class Idea:
    def __init__(self):
        pass

    def HF(self, p):
        """
        Half enumeration
        半分全列挙です
        pの要素の和の組み合わせを作ります。
        ソート、重複削除行います
        :param p: list : 元となるリスト
        :return: list : 組み合わせられた和のリスト
        """
        return sorted(set(p[i] + p[j] for i in range(len(p)) for j in range(i, len(p))))

    def Bfs2(self, a):
        """
        bit_full_search2
        bit全探索の改良版
        全探索させたら2進数のリストと10進数のリストを返す
        :return: list２つ : １個目 2進数(16桁)  2個目 10進数
        """
        # 参考
        # https://blog.rossywhite.com/2018/08/06/bit-search/
        # https://atcoder.jp/contests/abc105/submissions/4088632
        value = []
        for i in range(1 << len(a)):
            output = []

            for j in range(len(a)):
                if self.bit_o(i, j):
                    """右からj+1番目のiが1かどうか判定"""
                    # output.append(a[j])
                    output.append(a[j])
            value.append([format(i, 'b').zfill(16), sum(output)])

        value.sort(key=lambda x: x[1])
        bin = [value[k][0] for k in range(len(value))]
        val = [value[k][1] for k in range(len(value))]
        return bin, val

    def S(self, s, r=0, m=-1):
        """
        ソート関係行います。色々な設定あります。
        :param s: 元となるリスト
        :param r: reversするかどうか 0=False 1=True
        :param m: （2次元配列）何番目のインデックスのソートなのか
        :return: None
        """
        r = bool(r)
        if m == -1:
            s.sort(reverse=r)
        else:
            s.sort(reverse=r, key=lambda x: x[m])

    def bit_n(self, a, b):
        """
        bit探索で使います。0以上のときにTrue出します
        自然数だからn
        :param a: int
        :param b: int
        :return: bool
        """
        return bool((a >> b & 1) > 0)

    def bit_o(self, a, b):
        """
        bit探索で使います。1のときにTrue出すよ
        oneで1
        :param a: int
        :param b: int
        :return: bool
        """
        return bool(((a >> b) & 1) == 1)

    def ceil(self, x, y):
        """
        Round up
        小数点切り上げ割り算
        :param x: int
        :param y: int
        :return: int
        """
        return -(-x // y)

    def ave(self, a):
        """
        平均を求めます
        :param a: list
        :return: int
        """
        return sum(a) / len(a)

    def gcd(self, x, y):
        if y == 0:
            return x
        else:
            return self.gcd(y, x % y)


"""ここからメインコード"""


def main():
    # 1文字に省略
    r, e, p = range, enumerate, print
    ip = IP()
    id = Idea()
    mod = 10 ** 9 + 7

    """この下から書いてね"""
    n, k = ip.IL()
    s = input() + "2"

    zeros = []
    ones = [0]
    cnt = 0
    start = 1
    for x in s:
        if start == 1 and x != "1":
            ones += [cnt]
            cnt = 1
            start = 0
        elif start == 0 and x != "0":
            zeros += [cnt]
            cnt = 1
            start = 1
        else:
            cnt += 1
    
    if len(zeros) == len(ones):
        ones += [0]
    
    ones += [0]

    ans = sum(ones[:k+1]) + sum(zeros[:k])
    n_ans = ans
    
    for i in r(len(zeros) - k):
        n_ans = n_ans + ones[k+1+i] + zeros[k+i] - ones[i] - zeros[i]
        ans = max(ans, n_ans)

    p(ans)


main()

Submission status: Wrong Answer

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

dic = {'0': '1', '1':'0'}


L = []
prev = dic[S[0]]
for i in range(N):
    if S[i] != prev:
        L.append(1)
        prev = S[i]
    else:
        L[-1] += 1

cum_sum = [0] * (len(L) + 1)

for j in range(len(L)):
    cum_sum[j + 1] = cum_sum[j] + L[j]

if S[0] == '0':
    ans = 0
    for k in range(len(L) + 1):
        if k + (2 * K) <= len(L):
            saksadati = cum_sum[k + (2 * K)] - cum_sum[k]
            ans = max(ans, saksadati)

else:
    ans = 1
    for k in range(len(L) + 1):
        if k + (2 * K) + 1 <= len(L):
            saksadati = cum_sum[k + (2 * K) + 1] - cum_sum[k]
            ans = max(ans, saksadati)
print(ans)

Submission status: Wrong Answer

Solution code:
import numpy as np

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

#N = 20
#K = 4
#S = np.random.randint(0, 2, N)

output_S = S.copy()

answer = 0

for k in range(K):
    seq_length = []
    sequent = 1
    index_list = []
    count = 1
    present_number = output_S[0].copy()
    for i in output_S[1:]:
        if present_number == i:
            sequent += 1
        else:
            index_list.append(count-sequent)
            seq_length.append(sequent)
            sequent = 1
        present_number = i
        count += 1
        
    seq_length.append(sequent)
    index_list.append(count-sequent)
    
    longest_sequence = max(seq_length)
    longest_start_index = index_list[seq_length.index(longest_sequence)]
    longest_end_index = longest_start_index + longest_sequence
    
    if answer < np.sum(output_S[longest_start_index:longest_end_index+1]):
        answer = np.sum(output_S[longest_start_index:longest_end_index+1])
    
    if output_S[longest_start_index] == 1:
        output_S[longest_start_index:longest_end_index] = 0
    elif output_S[longest_start_index] == 0:
        output_S[longest_start_index:longest_end_index] = 1
        
    if answer < np.sum(output_S[longest_start_index:longest_end_index+1]):
        answer = np.sum(output_S[longest_start_index:longest_end_index+1])
    #print(output_S)

    
print(answer)

Submission status: Wrong Answer

Solution code:
import numpy as np

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

output_S = S.copy()

answer = 0

for k in range(K):
    seq_length = []
    sequent = 1
    index_list = []
    count = 1
    present_number = output_S[0].copy()
    for i in output_S[1:]:
        if present_number == i:
            sequent += 1
        else:
            index_list.append(count-sequent)
            seq_length.append(sequent)
            sequent = 1
        present_number = i
        count += 1
        
    seq_length.append(sequent)
    index_list.append(count-sequent)
    
    longest_sequence = max(seq_length)
    longest_start_index = index_list[seq_length.index(longest_sequence)]
    longest_end_index = longest_start_index + longest_sequence
    if answer < np.sum(output_S[longest_start_index:longest_end_index+1]):
        answer = np.sum(output_S[longest_start_index:longest_end_index+1])
    if output_S[longest_start_index] == 1:
        output_S[longest_start_index:longest_end_index] = 0
    elif output_S[longest_start_index] == 0:
        output_S[longest_start_index:longest_end_index] = 1

    
print(answer)

Submission status: Wrong Answer

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

ones = [0]
zeros = []
flag = 1
for s in S:
    if s == '1':
        if flag == 1:
            ones[-1] += 1
        else:
            ones.append(1)
            flag = 1
    if s == '0':
        if flag == 1:
            zeros.append(1)
            flag = 0
        else:
            zeros[-1] += 1
ones = ones + [0]

tmp = sum(ones[0 : K + 1]) + sum(zeros[0 : K])
ans = tmp
for i in range(len(zeros) - (K + 1) - 1):
    tmp = tmp + ones[i + K + 1] - ones[i] + zeros[i + K] - zeros[i]
    ans = max(ans, tmp)
    
print(ans)

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]
elif len(one) == len(zero):
    one = [0] + one
    
for i in range(len(one) - 1):
    one[i + 1] = one[i] + one[i + 1]
one = [0] + one
    
for i in range(len(zero) - 1):
    zero[i + 1] = zero[i] + zero[i + 1]
zero = [0] + zero
    
ans = -1
for i in range(len(zero) - (K + 1) + 1):
    tmp = one[i + K] - one[i - 1] + zero[i + K - 1] - zero[i - 1]
    ans = max(ans, tmp)
    # print(tmp)
    
if len(zero) - 1 <= K:
    print(len(S))
else:
    print(ans)

Submission status: Wrong Answer

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

ones = [0]
zeros = []
flag = 1
for s in S:
    if s == '1':
        if flag == 1:
            ones[-1] += 1
        else:
            ones.append(1)
            flag = 1
    if s == '0':
        if flag == 1:
            zeros.append(1)
            flag = 0
        else:
            zeros[-1] += 1
ones = ones + [0]

tmp = sum(ones[0 : K + 1]) + sum(zeros[0 : K])
ans = tmp
for i in range(len(ones) - (K + 1) - 1):
    tmp = tmp + ones[i + K + 1] - ones[i] + zeros[i + K] - zeros[i]
    ans = max(ans, tmp)
    
print(ans)

Submission status: Wrong Answer

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

def count_consecutive(s):
    return max(list(map(lambda x: len(x), s.split('0'))))

def f(s, k):
    global mx
    for j in range(len(pos)-k+1):
        tmp = s
        for n in range(k):
            i, count = pos[j+n]
            tmp = tmp[:i] + '1'*count + tmp[i+count:]
        mx = max(count_consecutive(tmp), mx)
    return mx

l, mx, pos = len(s), count_consecutive(s), []
i = 0 
while i < l:
    if s[i] == '0':
        count = 0 
        while i+count < l and s[i+count] == '0':
            count += 1
        pos.append([i, count])
        i += count
    else:
        i += 1
print(f(s, k))

Submission status: Wrong Answer

Solution code:
import sys
stdin = sys.stdin 
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline()

def Solve():
    N,K=na();S=ns()
    l=[]
    N=len(S)
    if(S[0]=="0"):
        l.append(0)
    b=S[0]
    ct=1
    for i in S[1:]:
        if(b!=i):
            l.append(ct)
            ct=1
            b=i
        else:
            ct+=1
    l.append(ct)
    len_l=len(l)
    if(len_l//2 <= K):
        return sum(l)
    ans = res = sum(l[:2*K+1])
    
    if(len_l%2==0):
        l.append(0)
        len_l+=1
    g = K * 2 + 1
    for i in range(K*2+1,len_l-1,2):
        ans -= l[i-g] + l[i-g+1]
        ans += l[i] + l[1+i]
        if(res<ans):
            res=ans
    #print(S)
    #print(l)
    #print(len_l)
    return res

print(Solve())

Submission status: Wrong Answer

Solution code:
import sys
stdin = sys.stdin 
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline()

def Solve():
    N,K=na();S=ns()
    l=[]
    N=len(S)
    if(S[0]=="0"):
        l.append(0)
    b=S[0]
    ct=1
    for i in S[1:]:
        if(b!=i):
            l.append(ct)
            ct=1
            b=i
        else:
            ct+=1
    l.append(ct)
    len_l=len(l)
    if(len_l//2 <= K):
        return sum(l)
    ans = res = sum(l[:2*K+1])
    
    if(len_l%2==0):
        l.append(0)
        len_l+=1
    g = K * 2 + 1
    for i in range(K*2+1,len_l-1,2):
        ans -= l[i-g] + l[i-g+1]
        ans += l[i] + l[1+i]
        if(res<ans):
            res=ans
    print(S)
    print(l)
    print(len_l)
    return res

print(Solve())

Submission status: Wrong Answer

Solution code:
import sys
stdin = sys.stdin 
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline()

def Solve():
    N,K=na();S=ns()
    l=[]
    N=len(S)
    if(S[0]=="0"):
        l.append(0)
    b=S[0]
    ct=1
    for i in S[1:]:
        if(b!=i):
            l.append(ct)
            ct=1
            b=i
        else:
            ct+=1
    l.append(ct)

    len_l=len(l)
    if(len_l//2 <= K):
        return sum(l)
    ans = res = sum(l[:2*K+1])
    
    if(len_l%2==0):
        l.append(0)
        len_l+=1
    g = K * 2 + 1
    for i in range(K*2+1,len_l-1,2):
        ans -= l[i-g] + l[i+1-g]
        ans += l[i] + l[1+i]
        res = max(ans,res)
    #print(l)
    #print(l)
    return res

print(Solve())

Submission status: Wrong Answer

Solution code:
import numpy as np

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

ones = [len(x) for x in s.replace('0', ' ').split()]
zeros = [len(x) for x in s.replace('1', ' ').split()]

if s[0]=='0':
  ones = [0] + ones
if s[-1]=='0':
  ones = ones + [0]

if k >= len(zeros):
  result = n
else: 
  result = [
    np.sum(ones[i:i+k+1]) + np.sum(zeros[i:i+k])
    for i in range(len(zeros)-k)]
  result = max(result)

print(result)

Submission status: Wrong Answer

Solution code:
n,c = map(int,input().split())
s = list(str(input()))
if s.count("1") == n or s.count("0") == n:
    print(n)
    exit()
for _ in range(c):
    z = []
    o = []
    k = []
    i = 0
    tmp = 1
    while i < n:
        if i == n - 1:
            if s[i] == "0":
                z.append([tmp,i - tmp + 1])
                k.append(["0" for _ in range(tmp)])
            else:
                o.append([tmp,i - tmp + 1])
                k.append(["1" for _ in range(tmp)])
            break
        if s[i] == s[i+1]:
            tmp += 1
            i += 1 
        else:
            if s[i] == "0":
                z.append([tmp,i - tmp + 1])
                k.append(["0" for _ in range(tmp)])
            else:
                o.append([tmp,i - tmp + 1])
                k.append(["1" for _ in range(tmp)])
            tmp = 1
            i += 1
    index = 0
    tmptl = 0
    for i in range(len(k)):
        if k[i][0] == "0":
            try:
                bl = len(k[i-1])
            except:
                bl = 0
            try:
                al = len(k[i+1])
            except:
                al = 0
            tl = len(k[i]) + bl + al
            if tl >= tmptl:
                tmptl = tl
                index = i
    #print(index)
    for j in range(len(k[index])):
        k[index][j] = "1"
    #print(k)
    tmps = ""
    for j in k:
        tmps += "".join(j)
    #print(tmps)
    s = tmps
#print(s)
ans = 0
i = 0
tmp = 1
while i < n:
    if i == n - 1:
        ans = max(ans,tmp)
        break
    if s[i] == "1":
        if s[i] == s[i+1]:
            tmp += 1
        else:
            ans = max(ans,tmp)
            tmp = 1
    i += 1
print(ans)

Submission status: Wrong Answer

Solution code:
n,c = map(int,input().split())
s = list(str(input()))
if s.count("1") == n or s.count("0") == n:
    print(n)
    exit()
z = []
o = []
k = []
i = 0
tmp = 1
while i < n:
    if i == n - 1:
        if s[i] == "0":
            z.append([tmp,i - tmp + 1])
            k.append(["0" for _ in range(tmp)])
        else:
            o.append([tmp,i - tmp + 1])
            k.append(["1" for _ in range(tmp)])
        break
    if s[i] == s[i+1]:
        tmp += 1
        i += 1 
    else:
        if s[i] == "0":
            z.append([tmp,i - tmp + 1])
            k.append(["0" for _ in range(tmp)])
        else:
            o.append([tmp,i - tmp + 1])
            k.append(["1" for _ in range(tmp)])
        tmp = 1
        i += 1
for _ in range(c):
    index = 0
    tmptl = 0
    for i in range(len(k)):
        if k[i][0] == "0":
            try:
                bl = len(k[i-1])
            except:
                bl = 0
            try:
                al = len(k[i+1])
            except:
                al = 0
            tl = len(k[i]) + bl + al
            if tl >= tmptl:
                tmptl = tl
                index = i
    #print(index)
    for j in range(len(k[index])):
        k[index][j] = "1"
    #print(k)
    tmps = ""
    for j in k:
        tmps += "".join(j)
    print(tmps)
    s = tmps
#print(s)
ans = 0
i = 0
tmp = 1
while i < n:
    if i == n - 1:
        ans = max(ans,tmp)
        break
    if s[i] == "1":
        if s[i] == s[i+1]:
            tmp += 1
        else:
            ans = max(ans,tmp)
            tmp = 1
    i += 1
print(ans)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
a=input()
l=len(a)
src=[int(a[i:i+1]) for i in range(l)]
from itertools import groupby
y =[sum(1 for e in it) for _, it in groupby(src, key=lambda x: x==1)]
m=len(y)
if src[0]==1 and src[l-1]==1:
  if (m-1)/2<=k:
    print(int(n))
  else:
    c=0
    for i in range(m-2*k+1):
      if i%2==0:
        d=y[i:i+2*k+1]
        f=sum(d)
        if f>=c:
          c=f
    print(c)
if src[0]==1 and src[l-1]==0:
  if m/2<=k:
    print(int(n))
  else:
    c=0
    for i in range(m-2*k+1):
      if i%2==0:
        d=y[i:i+2*k+1]
        f=sum(d)
        if f>=c:
          c=f
    print(c)
if src[0]==0 and src[l-1]==1:
  if m/2<=k:
    print(int(n))
  else:
    c=0
    for i in range(m-2*k+1):
      if i%2==1:
        d=y[i:i+2*k+1]
        f=sum(d)
      elif i==0:
        d=y[0:2*k]
        f=sum(d)
        if f>=c:
          c=f
    print(c)
if src[0]==0 and src[l-1]==0:
  if (m+1)/2<=k:
    print(int(n))
  else:
    c=0
    for i in range(m-2*k+1):
      if i%2==1:
        d=y[i:i+2*k+1]
        f=sum(d)
      elif i==0:
        d=y[0:2*k]
        f=sum(d)
        if f>=c:
          c=f
    print(c)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
a=input()
l=len(a)
src=[int(a[i:i+1]) for i in range(l)]
from itertools import groupby
y =[sum(1 for e in it) for _, it in groupby(src, key=lambda x: x==1)]
m=len(y)
if src[0]==1 and src[l-1]==1:
  if (m-1)/2<=k:
    print(int(n))
  else:
    c=0
    for i in range(m-2*k+1):
      if i%2==0:
        d=y[i:i+2*k+1]
        f=sum(d)
        if f>=c:
          c=f
    print(c)
if src[0]==1 and src[l-1]==0:
  if m/2<=k:
    print(int(n))
  else:
    c=0
    for i in range(m-2*k+1):
      if i%2==0:
        d=y[i:i+2*k+1]
        f=sum(d)
        if f>=c:
          c=f
    print(c)
if src[0]==0 and src[l-1]==1:
  if m/2<=k:
    print(int(n))
  else:
    c=0
    for i in range(m-2*k+1):
      if i%2==1:
        d=y[i:i+2*k+1]
        f=sum(d)
      elif i==0:
        d=y[0:2*k]
        f=sum(d)
        if f>=c:
          c=f
    print(c)
if src[0]==0 and src[l-1]==0:
  if (m+1)/2<=k:
    print(int(n))
  else:
    c=0
    for i in range(m-2*k+1):
      if i%2==1:
        d=y[i:i+2*k+1]
        f=sum(d)
        if f>=c:
          c=f
    print(c)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
S = input()
binary = []
one = []
zero = []
tmp_z = 0
tmp_o = 0
zeroflag = (S[0]=="0")
saigozero = (S[-1]=="0")
for i,s in enumerate(S):
    if s=="0":
        tmp_z += 1
        if not zeroflag:
            one.append(tmp_o)
            tmp_o=0
            zeroflag=True
    if s =="1":
        tmp_o+=1
        if zeroflag:
            zero.append(tmp_z)
            tmp_z=0
            zeroflag=False
if tmp_z!=0:
    zero.append(tmp_z)
if tmp_o!=0:
    one.append(tmp_o)

zeroflag = (S[0]=="0")
saigozero = (S[-1]=="0")

if zeroflag:
    one = [1] + one
if saigozero:
    one.append(1)
    
one_sum = [0]*(len(one)+1)
zero_sum = [0]*(len(zero)+1)
for i in range(len(one)):
    one_sum[i+1] = one_sum[i]+one[i]
for i in range(len(zero)):
    zero_sum[i+1]=zero_sum[i]+zero[i]
    


if len(zero)<=k:
    print(len(S))
else:
    ans = 0
    
    for i in range(len(zero)-k + 1):
        if zeroflag and i==0:
#             tmp = sum(zero[i:i+k])+sum(one[i:i+k+1]) -1
            tmp = zero_sum[i+k]-zero_sum[i]+one_sum[i+k+1]-one_sum[i]-1
        elif saigozero and i==len(zero)-k:
            tmp = zero_sum[i+k]-zero_sum[i]+one_sum[i+k+1]-1
        else:
            tmp = zero_sum[i+k]-zero_sum[i]+one_sum[i+k+1]-one_sum[i]
        ans = max(ans,tmp)
    print(ans)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
S = input()
binary = []
one = []
zero = []
tmp_z = 0
tmp_o = 0
zeroflag = (S[0]=="0")
saigozero = (S[-1]=="0")
for i,s in enumerate(S):
    if s=="0":
        tmp_z += 1
        if not zeroflag:
            one.append(tmp_o)
            tmp_o=0
            zeroflag=True
    if s =="1":
        tmp_o+=1
        if zeroflag:
            zero.append(tmp_z)
            tmp_z=0
            zeroflag=False
if tmp_z!=0:
    zero.append(tmp_z)
if tmp_o!=0:
    one.append(tmp_o)

zeroflag = (S[0]=="0")
saigozero = (S[-1]=="0")

if zeroflag:
    one = [1] + one
if saigozero:
    one.append(1)
    
one_sum = [0]*(len(one)+1)
zero_sum = [0]*(len(zero)+1)
for i in range(len(one)):
    one_sum[i+1] = one_sum[i]+one[i]
for i in range(len(zero)):
    zero_sum[i+1]=zero_sum[i]+zero[i]
    


if len(zero)<=k:
    print(len(S))
else:
    ans = 0
    
    for i in range(len(zero)-k + 1):
        if zeroflag and i==0:
#             tmp = sum(zero[i:i+k])+sum(one[i:i+k+1]) -1
            tmp = zero_sum[i+k]-zero_sum[i]+one_sum[i+k+1]-one_sum[i]-1
            print(tmp)
        elif saigozero and i==len(zero)-k:
            tmp = zero_sum[i+k]-zero_sum[i]+one_sum[i+k+1]-1
        else:
            tmp = zero_sum[i+k]-zero_sum[i]+one_sum[i+k+1]-one_sum[i]
        ans = max(ans,tmp)
    print(ans)

Submission status: Wrong Answer

Solution code:
N, K = [int(x) for x in input().split()]
S = [int(x) for x in list(str(input()))]
 
S_list = [0]
 
s_b = S[0]
for idx in range(1,N):
  s_n = S[idx]
  if s_n != s_b:
    S_list.append(idx)
  s_b = s_n

S_list.append(N)

cand_list = []
idx = 0

if 2*K+1 >= len(S_list):
  print(N)
  exit()

while idx+2*K+1 < len(S_list):
	cand_list.append(S_list[idx+2*K+1]-S_list[idx])
	idx += 2
    
print(max(cand_list))

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
a=input()
l=len(a)
src=[int(a[i:i+1]) for i in range(l)]
from itertools import groupby
y =[sum(1 for e in it) for _, it in groupby(src, key=lambda x: x==1)]
m=len(y)
if src[0]==1 and src[l-1]==1:
  if (m-1)/2<=k:
    print(int(n))
  else:
    c=0
    for i in range(m-2*k+1):
      if i%2==0:
        d=y[i:i+2*k+1]
        f=sum(d)
        if f>=c:
          c=f
    print(c)
if src[0]==1 and src[l-1]==0:
  if m/2<=k:
    print(int(n))
  else:
    c=0
    for i in range(m-2*k+1):
      if i%2==0:
        d=y[i:i+2*k+1]
        f=sum(d)
      else:
        d=y[m-2*k+1:m]
        f=sum(d)
        if f>=c:
          c=f
    print(c)
if src[0]==0 and src[l-1]==1:
  if m/2<=k:
    print(int(n))
  else:
    c=0
    for i in range(m-2*k+1):
      if i%2==1:
        d=y[i:i+2*k+1]
        f=sum(d)
      else:
        d=y[0:2*k]
        f=sum(d)
        if f>=c:
          c=f
    print(c)
if src[0]==0 and src[l-1]==0:
  if (m+1)/2<=k:
    print(int(n))
  else:
    c=0
    for i in range(m-2*k+1):
      if i%2==1:
        d=y[i:i+2*k+1]
        f=sum(d)
      elif i==l-1:
        d=y[m-2*k+1:m]
        f=sum(d)
      else:
        d=y[0:2*k]
        f=sum(d)      
        if f>=c:
          c=f
    print(c)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
a=input()
l=len(a)
src=[int(a[i:i+1]) for i in range(l)]
from itertools import groupby
y =[sum(1 for e in it) for _, it in groupby(src, key=lambda x: x==1)]
m=len(y)
if src[0]==1 and src[l-1]==1:
  if (m-1)/2<=k:
    print(int(n))
  else:
    c=0
    for i in range(m-2*k+1):
      if i%2==0:
        d=y[i:i+2*k+1]
        f=sum(d)
        if f>=c:
          c=f
    print(c)
if src[0]==1 and src[l-1]==0:
  if m/2<=k:
    print(int(n))
  else:
    c=0
    for i in range(m-2*k+1):
      if i%2==0:
        d=y[i:i+2*k+1]
        f=sum(d)
      else:
        d=y[l-2*k-2:l-1]
        f=sum(d)
        if f>=c:
          c=f
    print(c)
if src[0]==0 and src[l-1]==1:
  if m/2<=k:
    print(int(n))
  else:
    c=0
    for i in range(m-2*k+1):
      if i%2==1:
        d=y[i:i+2*k+1]
        f=sum(d)
      else:
        d=y[0:2*k]
        f=sum(d)
        if f>=c:
          c=f
    print(c)
if src[0]==0 and src[l-1]==0:
  if (m+1)/2<=k:
    print(int(n))
  else:
    c=0
    for i in range(m-2*k+1):
      if i%2==1:
        d=y[i:i+2*k+1]
        f=sum(d)
      elif i==l-1:
        d=y[l-2*k-2:l-1]
        f=sum(d)
      else:
        d=y[0:2*k]
        f=sum(d)      
        if f>=c:
          c=f
    print(c)