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 numpy as np
N, K = map(int, input().split())
S = input()
len0 = [len(i) for i in S.split('1') if (len(i)) != 0]
len1 = [len(i) for i in S.split('0') if (len(i)) != 0]

lens = np.zeros(len(len0)+len(len1), dtype=np.int)
if(int(S[0])==0):
    lens[::2] = len0
    lens[1::2] = len1
else:
    lens[::2] = len1
    lens[1::2] = len0
lens = np.cumsum(lens)
    
even_odd = (int(S[0])+1)%2

if(K >= len(len0)):
    print(len(S))
else:
    ans = []
    max_num = 0
    for first, i in enumerate(range(even_odd, len(len1)-K+2, 2)):
        if(first == 0):
            ans = lens[i]
        else:
            ans = lens[i+K*2] - lens[i] + 1
        if(ans > max_num):
            max_num = ans

Submission status: Wrong Answer

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

b = '0'
l = []
c = 0
for s in S:
	if b == s:
		c = c + 1
	else:
		l.append(c)
		c = 1
		b = '1' if b == '0' else '0'

l.append(c)
if S[-1] == '1':
	l.append(0)

n = len(l)//2 + 1 - (l[0]==0) - (l[-1]==0)
if n <= K:
	print(N)
	exit()

l.insert(0,0)
l.append(0)

size = len(l)
cand = []
k = 2*K+1
for i in range(0,size-k,2):
	cand.append(sum(l[i:i+k]))
print(max(cand))

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
S = input()
l = 0
r = 1
ans = 0
k = 0
if S[0] == '0':
    k = 1
while r < N:
    #print(l,r)
    if S[r] == '1':
        r += 1
        ans = max(ans,r-l)
        continue
    if S[r-1] == '1' and S[r] == '0':
        if k == K:
            if l == 0:
                if S[l] == '0':
                    while True:
                        l += 1
                        if S[l] == '1':
                            break
                else:
                    while True:
                        l += 1
                        if S[l] == '0':
                            l += 1
                            break
            else:
                while True:
                    l += 1
                    if S[l] == '0':
                        l += 1
                        break
            r += 1
        else:
            k += 1
            r += 1
    else:
        r += 1
print(ans)

Submission status: Wrong Answer

Solution code:
# coding: utf-8

import sys

n, k = [int(x) for x in input().split()]
ss = [int(x) for x in input()]

zero_array = []
one_array = []
curr_num = ss[0]
flag = 0

if curr_num == 0:
    zero_array.append(0)
else:
    one_array.append(0)
    flag = 1

for i in ss:
    if curr_num == i:
        if i == 0:
            zero_array[-1] += 1
        else:
            one_array[-1] += 1
    else:
        curr_num = i
        if i == 0:
            zero_array.append(1)
        else:
            one_array.append(1)

if k >= len(zero_array):
    print(sum(zero_array) + sum(one_array))
    sys.exit()
else:
    t = 0
    tmp = sum(zero_array[t: t+k]) + sum(one_array[t: t+k+flag])
    max_count = tmp
    while t + k + flag <= len(one_array) and t + k < len(zero_array):
        if t+k+flag < len(one_array):
            tmp += zero_array[t+k] - zero_array[t] + one_array[t+k+flag] - one_array[t+flag-1]
        else:
            tmp += zero_array[t + k] - zero_array[t] - one_array[t+flag-1]
        if tmp > max_count:
            max_count = tmp
            # print(t, max_count)
        t += 1

    print(max_count)
# print(zero_array)
# print(one_array)

Submission status: Wrong Answer

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

nums = [1] + [0] * (len(S) - 1)
index = 0
first = S[0]
last = S[-1]
char = S[0]

for s in S[1:]:
    if s == char:
        nums[index] = nums[index] + 1
    else:
        nums[index + 1] = nums[index + 1] + 1
        index = index + 1
        if char == "1":
            char = "0"
        else:
            char = "1"

zero = 0
length = len(nums)
if first == "0":
    length = length - 1
    zero = zero + 1
if last == "0":
    length = length - 1
    zero = zero + 1

zero = zero + length // 2

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

result = 1

start = 0
if first == "1":
    start = 1

for i in range(zero - K):
    start_index = i * 2
    calc_num = nums[start_index: start + start_index + K * 2]
    sum = 0
    for n in calc_num:
        sum += n
    if result < sum:
        result = sum

print(result)

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
S=input()
l=[]
count=[0,0] #count[0]が0,count[1]が1
if S[0]=="0":
    now=0
    a=0
else:
    now=1
    a=1
for i in range(len(S)):
    if int(S[i])==now:
        count[now]+=1
    else:
        if now==0:
            l+=[count[now]]
        else:
            l+=[count[now]]
        count[now]=0
        now=int(S[i])
        count[now]+=1
if now==0:
    l+=[(-1)*count[now]]
else:
    l+=[count[now]]

mx=0
if a==1:
    for i in range(len(l)-2*K):
        if mx<sum(l[i:i+2*K+1]) and i%2==0:
            mx=sum(l[i:i+2*K+1])
else:
    for i in range(len(l)-2*K):
        if i==0:
            mx=sum(l[0:2*K])
        else:
            if mx<sum(l[i:i+2*K+1]) and i%2==1:
                mx=sum(l[i:i+2*K+1])    

if len(l)==1 and a==1:
    print(l[0])
else:
    print(mx)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict

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

left = defaultdict(int)
right = defaultdict(int)
index = 0
for i in range(1, N):
    if S[i-1] == 0 and S[i] == 1:
        right[index] = i
        index += 1
    elif S[i-1] == 1 and S[i] == 0:
        left[index] = i-1

    if i == N-1:
        if S[i] == 0:
            right[index] = i
            left[index+1] = i
        if S[i] == 1:
            left[index] = i

ans = 1
for ind in range(index+1):
    a = left[ind+1] - right[ind-K] + 1
    if ans < a:
        ans = a

print(ans)

Submission status: Wrong Answer

Solution code:
def calcH(S,K):
    Kcount = 0
    Hcount = 0
    isFlag = False
    for s in S:
        #0の文字列かつ最大支持
        if s =='0' and Kcount==K:
            break
        elif s =='0':
            isFlag = True
        
        #最初の1を除く1が出た時
        elif s =='1' and isFlag == True:
            isFlag = False
            Kcount += 1
    
        Hcount += 1
    return Hcount

N, K = (int(i) for i in input().split())
S = list(input())
lH = calcH(S,K)
rH = calcH(S[::-1],K)
print(max(lH,rH))

Submission status: Wrong Answer

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

z_list = []
o_list = []
zero = 0
one = 0
most_l = s[0]
most_r = s[N - 1]
prev = s[0]
if prev == '0':
    zero += 1
else:
    one += 1
s.pop(0)
for c in s:
    if c == '0':
        zero += 1
    else:
        one += 1
    if prev != c:
        if prev == '0':
            z_list.append(zero)
            zero = 0
        else:
            o_list.append(one)
            one = 0
    prev = c
z_list.append(zero)
o_list.append(one)

# print(z_list)
# print(o_list)

z_len = len(z_list)
o_len = len(o_list)
if z_len < 3:
    real_k = K
else:
    real_k = K + 1
max = 0
for i in range(N):
    sum = 0
    for k in range(real_k):
        if i + k < z_len:
            sum += z_list[i + k]
        if i + k < o_len:
            sum += o_list[i + k]
    if max < sum:
        max = sum

print(max)

# sum = 0
# g_len = len(g)
# for i in range(K):
#     if g_len <= i:
#         break
#     sum += g[i]

# print(sum)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-

import sys
from itertools import accumulate

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7

N,K=MAP()
S=input()

if N==1:
    print(1)
    exit()

L=[]
if S[0]=='0':
    L.append(0)
    cur=start=0
else:
    cur=start=1
cnt=0
for i in range(N):
    if cur==0:
        if S[i]=='0':
            cnt+=1
        else:
            L.append(cnt)
            cnt=1
            cur=1
    else:
        if S[i]=='1':
            cnt+=1
        else:
            L.append(cnt)
            cnt=1
            cur=0
else:
    if cnt!=0:
        if cur==0:
            L.append(cnt)
        else:
            L.append(cnt)
if S[-1]=='0':
    L.append(0)

acc=[0]+list(accumulate(L))
mx=0
for i in range(0, len(acc), 2):
    if i+(K*2+1)<len(acc):
        mx=max(mx, acc[i+(K*2+1)]-acc[i])
print(mx)

Submission status: Wrong Answer

Solution code:
def mark_zero1(s):
    index = [i for i, s_ in enumerate(s) if s_ == "0" and safe_get(s, i - 1) != "0"]
    return index


def mark_zero2(s):
    index = [i for i, s_ in enumerate(s) if s_ == "0" and safe_get(s, i + 1) != "0"]
    return index


def safe_get(lis, index):
    if index < 0:
        return -1
    elif index >= len(lis):
        return len(s)
    else:
        return lis[index]


def get_score(a, b, i, j):
    return safe_get(a, j + 1) - safe_get(b, i - 1) - 1


n, k = list(map(int, input().split(" ")))
s = input()
a = mark_zero1(s)
b = mark_zero2(s)

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

try_num = min(len(a), k)
ij_list = [(idx, idx + try_num - 1) for idx in range(len(a) - try_num + 1)]

print(max([get_score(a, b, i, j) for i, j in ij_list]))

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
s = input()
if s[0] == "0":
    zero = []
    one = []
    c = 1
    now = s[0]
    for i in range(1, n):
        if s[i] == now:
            c += 1
            if i == n - 1:
                if now == "0":
                    zero.append(c)
                else:
                    one.append(c)
        else:
            if now == "0":
                zero.append(c)
                if i == n - 1:
                    one.append(1)
            else:
                one.append(c)
                if i == n - 1:
                    zero.append(1)
            c = 1
            now = s[i]

    pos_zero = len(zero)
    #print(zero, one)
    if pos_zero <= k:
        print(n)
        exit()
    q = sum(zero[:k])
    sum_zero = [q]
    for i in range(pos_zero - k):
        q = q - zero[i] + zero[k + i]
        sum_zero.append(q)
    pos_one = len(one)
    q = sum(one[:k])
    sum_one = [q]
    for i in range(pos_one - k):
        q = q - one[i] + one[k + i]
        sum_one.append(q)
    ans = sum_zero[0] + sum_one[0]
    #print(sum_zero)
    #print(sum_one)
    for i in range(1, pos_zero - k + 1):
        if i - 1 < pos_one and i < len(sum_one):
            ansq = sum_zero[i] + sum_one[i] + one[i - 1]
        elif i < len(sum_one):
            ansq = sum_zero[i] + sum_one[i]
        elif i-1 < pos_one:
            ansq = sum_zero[i] + one[i-1]
        else:
            ansq = sum_zero[i]
        ans = max(ans, ansq)

    print(ans)
else:
    s = "0" + s
    n += 1
    zero = []
    one = []
    c = 1
    now = s[0]
    for i in range(1, n):
        if s[i] == now:
            c += 1
            if i == n - 1:
                if now == "0":
                    zero.append(c)
                else:
                    one.append(c)
        else:
            if now == "0":
                zero.append(c)
                if i == n - 1:
                    one.append(1)
            else:
                one.append(c)
                if i == n - 1:
                    zero.append(1)
            c = 1
            now = s[i]

    pos_zero = len(zero)
    #print(zero, one)
    if pos_zero <= k:
        print(n-1)
        exit()
    q = sum(zero[:k])
    sum_zero = [q]
    for i in range(pos_zero - k):
        q = q - zero[i] + zero[k + i]
        sum_zero.append(q)
    pos_one = len(one)
    q = sum(one[:k])
    sum_one = [q]
    for i in range(pos_one - k):
        q = q - one[i] + one[k + i]
        sum_one.append(q)
    if len(sum_zero) == 0:
        ans = sum_zero[0] + sum_one[0] - 1
    else:
        ans = sum_zero[1] + sum_one[0] - 1
    #print(sum_zero)
    #print(sum_one)
    for i in range(1, pos_zero - k):
        if i - 1 < pos_one and i < len(sum_one):
            ansq = sum_zero[i+1] + sum_one[i] + one[i - 1]
        elif i < len(sum_one):
            ansq = sum_zero[i+1] + sum_one[i]
        elif i-1 < pos_one:
            ansq = sum_zero[i+1] + one[i-1]
        else:
            ansq = sum_zero[i+1]
        ans = max(ans, ansq)

    print(ans)

Submission status: Wrong Answer

Solution code:
def init(a):
    cf=a[0]
    cc=0
    z=[]
    for it in a:
        if(it==cf):
            cc+=1
        else:
            if(cf=='0'):
                z.append(-cc)
            else:
                z.append(cc)
            cf=it
            cc=1
    z.append(cc)
    return z

def solve(a,K):
    return 0

N,K=list(map(int,input().split(" ")))
a=str(input())
z=init(a)
L=len(a)
Q=0
if(z[0]<0):
    Q=N//2+1
else:
    Q=N//2
if(K>=Q):
    print(N)
else:
    print(solve(z,K))

Submission status: Wrong Answer

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

A = []

startWithZero = (S[0] == "0")
cnt = 0

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

A.append(cnt)

if N == 1:
    A = [1]

maxLen = 0

for i in range(len(A)):

    # i番目のAがゼロを示しているとき
    if ( i % 2 == 0 and startWithZero == True ) or ( i % 2 == 1 and startWithZero == False ) :
        maxLen = max(maxLen,sum(A[i:i + K * 2 - 1]))

    else:
        maxLen = max(maxLen, sum(A[i:i+K*2+1]))

print(maxLen)

Submission status: Wrong Answer

Solution code:
def hanntenn(n):
    if n=="0":
        return "1"
    else:
        return "0"
from itertools import combinations

def numcheck(s):
    maxlen = 0
    j = 0
    for i in range(len(s)):
        if i >= len(s):
            return maxlen
        
        if s[i] == "1":
            j = i
            while(j+1 < len(s) and s[j+1] == "1"):
                j+=1
            _len = j-i+1
            if _len >= maxlen:
                maxlen = _len
            j+=1
        else:
            j+=1
            
    return maxlen
def zerocheck(s):
    pare = []
    j = 0
    for i in range(len(s)):
        i = j
        if i >= len(s):
            return pare
        
        if s[i] == "0":
            j = i
            while(j+1 < len(s) and s[j+1] == "0"):
                j+=1
            pare.append((i,j))
            j+=1
        else:
            j+=1
    return list(set(pare))
import copy
n,k = map(int, input().split())

s = input()
lists = list(s)

zerolists = zerocheck(s)

x = combinations(zerolists,k)
list_x = list(x)

max_len = 0
for i in range(len(list_x)):
    _lists = copy.copy(lists)
    pare = list_x[i]
    for j in range(k):
        for a in range(pare[j][0],pare[j][1]+1):
            _lists[a] = "1"
    _len = numcheck(_lists)
    if _len >= max_len:
        max_len = _len
if numcheck(lists) >= max_len:
    max_len = numcheck(lists)
print(max_len)

Submission status: Wrong Answer

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

run_length_S = []
for i in range(len(S)):
    if i == 0:
        is_in_zero = (S[i] == '0')
        count = 1
    else:
        if S[i] == '0' and is_in_zero:
            count += 1
        elif S[i] == '1' and not is_in_zero:
            count += 1
        elif S[i] == '0' and not is_in_zero:
            run_length_S.append('o{}'.format(count))
            count = 1
            is_in_zero = True
        elif S[i] == '1' and is_in_zero:
            run_length_S.append('z{}'.format(count))
            count = 1
            is_in_zero = False
    if i == len(S) - 1:
        if S[i] == '0':
            run_length_S.append('z{}'.format(count))
        else:
            run_length_S.append('o{}'.format(count))

for k in range(K):

    possible_ones = []

    for i in range(len(run_length_S)):
        if run_length_S[i][0] == 'z':
            if len(run_length_S) == 1:
                possible_ones.append(int(run_length_S[i][1]))
            else:
                if i == 0:
                    possible_ones.append(int(run_length_S[i][1]) + int(run_length_S[i+1][1]))
                elif i == len(run_length_S) - 1:
                    possible_ones.append(int(run_length_S[i-1][1]) + int(run_length_S[i][1]))
                else:
                    possible_ones.append(int(run_length_S[i-1][1]) + int(run_length_S[i][1]) + int(run_length_S[i+1][1]))
        else:
            possible_ones.append(-1)

    max_possible_ones = max(possible_ones)
    max_possible_idx = possible_ones.index(max_possible_ones)

    if max_possible_ones == -1:
        break
    else:
        if max_possible_idx == 0:
            run_length_S = ['o{}'.format(max_possible_ones)] + run_length_S[max_possible_idx+2:]
        elif max_possible_idx == len(possible_ones) - 1:
            run_length_S = run_length_S[:max_possible_idx-1] + ['o{}'.format(max_possible_ones)]
        else:
            run_length_S = run_length_S[:max_possible_idx-1] +['o{}'.format(max_possible_ones)] + run_length_S[max_possible_idx+2:]

ones = [int(s[1]) for s in run_length_S if s[0] == 'o']
print(max(ones))

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
S = input()
S
split_list = [0]
section_list = [0]
for i in range(1, N):
    if S[i-1] != S[i]:
        section_list.append(i - split_list[-1])
        split_list.append(i)
split_list.append(N)
section_list.append(split_list[-1] - split_list[-2])
del split_list[0]
del section_list[0]
answer = 0
if len(section_list) <= 2 * K + 1:
    answer= N
else:
    total = sum(section_list[:2 * K + 1])
    if S[0] == '0':
        for i in range(len(section_list) - 2 * K - 1):
            total = total - section_list[i] + section_list[i+2*K+1]
            if i % 2 == 0:
                if total >= answer:
                    answer = total
    if S[0] == '1':
        answer = total
        for i in range(len(section_list) - 2 * K - 1):
            total = total - section_list[i] + section_list[i+2*K+1]
            if i % 2 == 1:
                if total >= answer:
                    answer = total
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
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]
maxlen = max(maxlen,len(tmp))
lis.append(len(tmp))
# print(maxlen)
# print(lis)
# print(maxlen)
# print(st0)
ans = maxlen
# tmpans = maxlen
L = len(lis)
if(st0):#奇数Indexをうめる時！000101010
    l = 0
    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)
            if(l == 0):
                tmpans-=lis[r]
                l=9
            else:
                tmpans-=lis[r]+lis[r+1]
            tmpans+=lis[i]+lis[i+1]
            # print(tmpans,i)
            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(tmpans,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 = input()

b = '0'
l = []
c = 0
for s in S:
	if b == s:
		c = c + 1
	else:
		l.append(c)
		c = 1
		b = '1' if b == '0' else '0'

l.append(c)
if S[-1] == '1':
	l.append(0)

n = len(l)//2 + 1 - (l[0]==0) - (l[-1]==0)
if n <= K:
	print(N)
	exit()

l.insert(0,0)
l.append(0)

size = len(l)
cand = []
k = 2*K+1
for i in range(size-k):
	cand.append(sum(l[i:i+k]))
cand = cand[::2]
print(max(cand))

Submission status: Wrong Answer

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

A = [0]
B = [0]
for s in S:
  if s == '1':
    A[-1] += 1
  elif A[-1] != 0:
    A.append(0)
  if s == '0':
    B[-1] += 1
  elif B[-1] != 0:
    B.append(0)

ans = 0
c,d,e,f = 0,0,0,0

for i in range(min(len(A),len(B))):
  a = 0
  if i == 0:
    if S[0] == '1':
      c,d = sum(A[i:min(i + K + 1,len(A))]),sum(B[i:min(i + K,len(B))])
      a = c + d
    else:
      c,d = sum(A[i:min(i + K,len(A))]) , sum(B[i:min(i + K,len(B))])
      a = c + d
  else:
    if S[0] == '1':
      if i + K <= len(A):
        c += A[min(i + K,len(A) - 1)] - A[i - 1]
        d += B[min(i + K - 1,len(B) - 1)] - B[i - 1]
        a = c + d
    else:
      if i + K <= len(A):
        c += A[min(i + K - 1,len(A) - 1)] - (0 if i == 1 else A[i - 2])
        d += B[min(i + K - 1,len(B) - 1)] - B[i - 1]
        a = c + d
  ans = max(ans,a)
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
s = input()
def parse(s):
    arr = []
    prev = -1
    ans = 0
    for i in range(n):
        if s[i] == '0':
            if  prev == 0:
                ans += 1
            else:
                arr.append([prev,ans])
                ans = 1
                prev = 0
        elif s[i] == '1':
            if prev == 1:
                ans += 1
            else:
                arr.append([prev,ans])
                ans = 1
                prev = 1
    arr.append([prev,ans ])
    return arr
get = parse(s)[1:]


ll = len(get)

# print(get)

wL = wR = 0
bestL = bestWindow = 0
zeroCount = 0
ans = 0
while wR < ll:
    if zeroCount <= k :
        if get[wR][0] == 0 :
            zeroCount += 1
            ans += get[wR][1]
        else:
            ans += get[wR][1]
        wR += 1
    if zeroCount > k :
        if get[wL][0] == 0 :
            zeroCount -= 1
            ans -= get[wL][1]
        else:
            ans -= get[wL][1]

        wL += 1
    if (ans > bestWindow) and (zeroCount<=k) :
        bestWindow = ans
        bestL = wL

print(bestWindow)

Submission status: Wrong Answer

Solution code:
n,c = map(int,input().split())
s = list(str(input()))
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,k=map(int,input().split())
s=input()

if s[0]=='0':
    flag=0
else:
    flag=1

l=[]
i=0
x=0
while (i<n):
    if flag==0:
        if s[i]=='0':
            x+=1
        else:
            l.append(x)
            x=1
            flag=1
    else:
        if s[i]=='1':
            x+=1
        else:
            l.append(x)
            x=1
            flag=0
    i+=1
l.append(x)

m=len(l)
t=2*k+1
if s[0]=='1':
    if m<=t:
        ans=n
    else:
        ans=0
        for i in range(t):
            ans+=l[i]
        if m%2==1:
            for i in range(0,m-t+1,2):
                x=0
                for j in range(i,i+t):
                    x+=l[j]
                if ans<x:
                    ans=x
        else:
            for i in range(0,m-t,2):
                x=0
                for j in range(i,i+t):
                    x+=l[j]
                if ans<x:
                    ans=x
            y=0
            for i in range(m-2*k,m):
                y+=l[i]
            if ans<y:
                ans=y
else:
    if m<=2*k:
        ans=n
    else:
        ans=0
        for i in range(2*k):
            ans+=l[i]
        if m%2==0:
            for i in range(0,m-t+1,2):
                x=0
                for j in range(i,i+t):
                    x+=l[j]
                if ans<x:
                    ans=x
        else:
            for i in range(0,m-t,2):
                x=0
                for j in range(i,i+t):
                    x+=l[j]
                if ans<x:
                    ans=x
            y=0
            for i in range(m-2*k,m):
                y+=l[i]
            if ans<y:
                ans=y
print(ans)

Submission status: Wrong Answer

Solution code:
# coding:utf-8

import sys
# from collections import Counter, defaultdict

INF = float('inf')
MOD = 10 ** 9 + 7

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x) - 1 for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def II(): return int(sys.stdin.readline())
def SI(): return input()


def main():
    n, k = LI()
    s = '0' + SI() + '0'

    L = []
    R = []
    if s[1] == '0':
        L.append(1)
        R.append(1)
    for i in range(n + 1):
        if s[i] == '1' and s[i + 1] == '0':
            R.append(i)
        if s[i] == '0' and s[i + 1] == '1':
            L.append(i + 1)

    if s[-2] == '0':
        L.append(n)
        R.append(n)

    res = R[0] - L[0] + 1
    for i in range(len(R) - k):
        res = max(res, R[i + k] - L[i] + 1)

    return res


print(main())

Submission status: Wrong Answer

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

S = str(input())

L = [0]
for i in range(len(S)-1):
    if S[i] != S[i+1]:
        L.append(i+1)
L.append(len(S))
# print(L)

ans = 1
if len(L)-K*3-1 >= 0:
    for i in range(len(L)-K*3-1):
        ans = max(ans, L[i+K*3-1]-L[i])
    print(ans)
else:
    print(len(S))

Submission status: Wrong Answer

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

# いくつか入力
n, k = (int(i) for i in input().split())  

# リストに入れる
s = [int(i) for i in input()] 

j=0
t = [ [s[0],1]]

for i in range(1,n):
    if s[i] == t[j][0]:
        t[j][1] += 1
    else:
        j += 1
        t.append([s[i],1])
# print(t)
if t[0][0] == 0:
    z = (len(t) + 1) // 2 
else:
    z = len(t) // 2

if z <= k:
    print(n)
    exit()
m = 0
if t[0][0] == 0:
    for i in range(k*2):
        m += t[i][1]
else:
    for i in range(k*2+1):
        m += t[i][1]
ma = m
t.append([1,0])
if t[0][0] == 0:
    for i in range(0,z-k):
        m += t[k*2+i*2][1] + t[k*2+i*2+1][1]
        m -= t[i*2-2][1] + t[i*2-1][1]
        ma = max(ma,m)
else:
    for i in range(0,z-k):
        m += t[k*2+i*2+1][1] + t[k*2+i*2+2][1]
        m -= t[i*2-2][1] + t[i*2-1][1]
        ma = max(ma,m)



print(ma)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
s=str(input())
s=s+'e'
arr=[[] for _ in range(3)]
count=1
for i in range(n):
  if s[i]==s[i+1]:
    count+=1
  else:
    if s[i]=='1':
      arr[0].append(count)
      count=1
    else:
      arr[1].append(count)
      count=1
print(arr[0])
print(arr[1])
if len(arr[1])<=k:
  print(n)
else:
  if len(arr[0])==len(arr[1]):
    arr[0].append(0)
  elif len(arr[0])<len(arr[1]):
    arr[0].append(0)
    arr[0].append(0)
  tmp=0
  sum=0
  count=0
  max=0
  flag=False
  for i in range(len(arr[1])):
    sum+=arr[0][i]
    sum+=arr[1][i]
    count+=1
    if flag:      
      sum-=arr[0][i-k]
      sum-=arr[1][i-k]
    if count==k:
      tmp=sum
      tmp+=arr[0][i+1]
      if tmp>max:
        max=tmp
      count-=1
      flag=True
print(max)

Submission status: Wrong Answer

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

counter = 0
ruiseki_count=0
for i in range (len(S)):
    if (counter =='1' and S[i]== '0'):
        ruiseki_count +=1
        ruiseki.append(ruiseki_count)
        counter =S[i]

    else:
        ruiseki.append(ruiseki_count)
        counter =S[i]
        
result = 0
for j in range (max(ruiseki)-K+1):
    temp_result = ruiseki.index(j+K)+ ruiseki.count(j+K)-1-ruiseki.index(j)
    if temp_result> result:
        result = temp_result

if len(set(ruiseki))==1:
    print(len(ruiseki))
else:
    print(result)