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:
def judge(c, d, h, g):
    m = 0
    if g == 0:
        if len(c) == 0 or len(d) == 0 or max(c) == 1 and max(d) == 1:
            return h * 2
        else:
            for i in range(len(c) - h + 1):
                z = 0
                for x in range(h):
                    if i + x >= len(c) or i + x >= len(d):
                        break
                    z += c[i + x] + d[i + x]
                if m < z:
                    m = z
    else:
        if len(c) == 0 or len(d) == 0 or max(c) == 1 and max(d) == 1:
            return h * 2 - 1
        else:
            for i in range(len(d) - h + 1):
                z = 0
                for x in range(h):
                    if x != 0:
                        z += d[i] + c[i - 1]
                    else:
                        z += d[i]
                if m < z:
                    m = z
    return m


a, b = map(int, input().split())
l = input()
one = list(filter(lambda a: a != "", l.split("0")))
for i in range(len(one)):
    one[i] = len(one[i])
zero = list(filter(lambda a: a != "", l.split("1")))
for i in range(len(zero)):
    zero[i] = len(zero[i])
f = 0 if l[0] == "0" else 1
m = judge(zero, one, b, f)
print(m)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
S = input() + '2'
a = []
if S[0] == '0':
    a.append(0)
cnt = 1
for i in range(1, N + 1):
    if S[i] != S[i - 1]:
        a.append(cnt)
        cnt = 1
    else:
        cnt += 1

ans = s = sum(a[:2*K + 1])
for i in range(2*K + 2, len(a), 2):
    s += a[i] + a[i - 1] - a[i - 2*K - 1] - a[i - 2*K - 2]
    ans = max(ans, s)
print(ans)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3.4
# -*- coding: utf-8 -*-

# abc124_d

# input
n, k = [int(s) for s in input().split()]
S = input()
if n<100: exit(0)
# get ans
node = [len(s) for s in S.split('0')]
node = [s for i, s in enumerate(node) if not (s==0 and i!=0 and i!=len(node)-1)]
arch = [len(s) for s in S.split('1')]
arch = list(filter(int, arch))
def window(iterable, size):
    #TODO size = min(size, len(iterable))
    from itertools import tee
    iters = tee(iterable, size)
    for i in range(1, size):
        for each in iters[i:]:
            next(each, None)
    return zip(*iters)
k = min(k, len(arch))
sls = [(i,i+k) for i in range(len(arch)-k+1)]
m = (sum(arch[i:j]+node[i:j+1]) for i, j in sls)
ans = max(m)
# output
# print(node)
# print(arch)
print(ans)

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
S= [i for i in input()]
list_1 = []
max = 0
if S[0] == '0':
	list_1.append(0)
for i in range(N-1):
	if S[i] == '1' and S[i+1] == '0':
		list_1.append(i)
if S[-1] == '0':
	list_1.append(N-1)
if K>=len(list_1) or '0' not in S:
	print(len(S))
else:
	for i in range(len(list_1)-K):
		if list_1[i+K]-list_1[i]+1 > max:
			max = list_1[i+K]-list_1[i]+1
	print(max)

Submission status: Wrong Answer

Solution code:
times = input().split()
N = int(times[0])
K = int(times[1])
S = input()


def one_counter(S):
    S = S.split("0")
    high = 0
    tmp = 0
    for i in S:
        tmp = i.count("1")
        if tmp > high:
            high = tmp
    return high


last_score = 0
for i in range(K):
    if S.find("0") == -1:
        last_score = len(S)
        break
    zero_counter = []
    start_index = -1
    end_index = -1
    diff = 0
    for j, value in enumerate(S):
        if value == "0" and start_index == -1:
            start_index = j
        elif value == "1" and start_index != -1:
            end_index = j
            diff = end_index - start_index
            zero_counter.append([start_index, end_index, diff])
            start_index = -1
            end_index = -1
            diff = 0
    high_score = one_counter(S)
    high_S = S
    for k in zero_counter:
        S_f = S[:k[0]]
        S_b = S[k[1]:]
        ones = "1"*k[2]
        tmp_S = S_f+ones+S_b
        score = one_counter(tmp_S)
        if score > high_score:
            high_S = tmp_S
            last_score = score
    S = high_S


print(last_score)

Submission status: Wrong Answer

Solution code:
n,k = list(map(int,input().split()))
s = input()
c = [0]
ans = 0
for i in range(1,n):
    if s[i-1] == '0' and s[i] == '1':
        c.append(c[i-1] + 1)
    else:
        c.append(c[i-1])
for i in range(n-1, -1, -1):
    if s[i] == '0':
        c[i] += 1
    else:
        break
right = 0
for left in range(0,n):
    while right < n and c[right] - c[left] <= k:
        right += 1
    ans = max(right - left, ans)
    if right == left: right += 1
    else: pass
print(ans)

Submission status: Wrong Answer

Solution code:
N,K = (int(x) for x in(input().split()))
n=list(map(int,list(input())))
if n.count(0)<=K:
    print(N)
else:
    p=0
    q=0
    last=0
    for i in range(N-1):
        if p==K:
            break
        elif n[i]==0 and n[i+1]==1:
            p=p+1
            q=i
        else:
            pass
    for j in range(q+1):
        if n[j]==0:
            n[j]=1
        else:
            pass
    for f in range(N-1):
        if n[f]==1 and n[f+1]==0:
            last=f
            break
        else:
            pass

    print(n[:last].count(1)+1)

Submission status: Wrong Answer

Solution code:
import itertools
import copy

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

sp_S = []
zero = []
cur = -1

for i in range(N):
	if i == 0 or ( i>0 and S[i]!=S[i-1]) :
		sp_S.append([S[i]])
		cur += 1
		if S[i] == '0':
			zero.append(cur)
	else:
		sp_S[cur].append(S[i])

# print('sp_S =', sp_S)
# print('zero =', zero)

K_comb = list(itertools.combinations(zero, K))

# print('K_comb =', K_comb)
max = 1

for comb in K_comb:
	sp_cp = copy.deepcopy(sp_S)
	for i in comb:
		# print('comb = ', i)
		sp_cp[i] = ['1' for x in range(len(sp_cp[i]))]
	
	# print(''.join(map(''.join, sp_cp)))
	S_mod = ''.join(map(''.join, sp_cp))
		
	now = 0
	for j in range(len(S_mod)):
		if S_mod[j] == '1':
			now += 1
			if now>max:
				max=now
		else :
			now = 0
	
print(max)

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
S= [i for i in input()]
list_1 = []
max = 0
if S[0] == '0':
	list_1.append(0)
for i in range(N-1):
	if S[i] == '1' and S[i+1] == '0':
		list_1.append(i)
if S[-1] == '0':
	list_1.append(N)
if K>=len(list_1) or '0' not in S:
	print(len(S))
else:
	for i in range(len(list_1)-K):
		if list_1[i+K]-list_1[i]+1 > max:
			max = list_1[i+K]-list_1[i]+1
	print(max)

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 m<=k+1:
  print(int(n))
else:
  c=0
  for i in range(m-k):
    d=y[i:i+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()
l=[]
count=0
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+=1
    else:
        l+=[count]
        count=1
        now=int(S[i])
l+=[count]

mx=0
if a==1:
    if len(l)-2*K>0:
        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:
        mx=sum(l)
    
else:
    if len(l)-2*K>0:
        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]) 
    else:
        mx=sum(l) 
 

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

Submission status: Wrong Answer

Solution code:
N, K = map(int,input().split())
state_list = [int(_) for _ in input()]
state_num_list = []

counter = 1
prev_num = state_list[0]
# リストの改変
for n in range(1, N):

    if state_list[n] == prev_num:
        counter += 1
        continue
    else:
        state_num_list.append(counter)
        counter = 1
        prev_num = state_list[n]
state_num_list.append(counter)

state_num_list.append(0)
state_num_list.insert(0,0)

max_sum = 0
# メイン
for s in range(0, len(state_num_list), 2):
    count_num = K * 2 + 1
    
    check_sum = sum(state_num_list[s:s + count_num])

    max_sum = check_sum if max_sum < check_sum else max_sum

print(max_sum)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3.4
# -*- coding: utf-8 -*-

# abc124_d

# input
n, k = [int(s) for s in input().split()]
S = input()
# get ans
node = [len(s) for s in S.split('0')]
node = [s for i, s in enumerate(node) if not (s==0 and i!=0 and i!=len(node)-1)]
arch = [len(s) for s in S.split('1')]
arch = list(filter(int, arch))
def window(iterable, size):
    #TODO size = min(size, len(iterable))
    from itertools import tee
    iters = tee(iterable, size)
    for i in range(1, size):
        for each in iters[i:]:
            next(each, None)
    return zip(*iters)
k = min(k, len(arch))
sls = [(i,i+k) for i in range(len(arch)-k+1)]
m = (sum(arch[i:j]+node[i:j+1]) for i, j in sls)
ans = max(m)
# output
print(node)
print(arch)
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
 
N, K= map(int, input().split())
S = input()
 
ones_list = S.split('0')
zeros_list = S.split('1')
 
ones = []
if S[0] == '0':
    ones.append(0)
for k in range(len(ones_list)):
    if ones_list[k] != '':
        ones.append(len(ones_list[k]))
if S[-1] == '0':
    ones.append(0)
 
zeros = []
for k in range(len(zeros_list)):
    if zeros_list[k] != '':
        zeros.append(len(zeros_list[k]))
 
if K >= len(zeros):
    print(len(S))
    sys.exit()
 
result = []
maximum = 0
for k in range(K):
    maximum += ones[k]
    maximum += zeros[k]
maximum += ones[K]
result.append(maximum)
 
 
for j in range(len(ones) - K - 1):
    maximum += zeros[K+j] + ones[K+1+j] - ones[j] - zeros[j]
    result.append(maximum)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
S = list(str(input()))
list = [int(s) for s in S]
L = []
if list[0] == 0:
    L.append(0)
cnt = 1
for i in range(len(list) - 1):
    if list[i] == list[i + 1]:
        cnt += 1
    else:
        L.append(cnt)
        cnt = 1
L.append(cnt)

if list[N-1] == 0:
    L.append(0)
ans = 0
for i in range((len(L) + 1)//2 - K):
    if sum(L[(2 * i):(2 * i) + (K * 2) + 1]) > ans:
        ans = sum(L[(2 * i):(2 * i) + (K * 2) + 1])
if len(L) == 1:
    ans = max(ans, L[0])
print(ans)

Submission status: Wrong Answer

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

ones = []
zeros = []

zero_count, one_count = 0, 0
prev = '1'
for c in list(s):
    if c == '0':
        zero_count += 1
        if prev == '1':
            ones.append(one_count)
            one_count = 0
        prev = '0'
    else:
        one_count += 1
        if prev == '0':
            zeros.append(zero_count)
            zero_count = 0
        prev = '1'
if prev == '0':
    zeros.append(zero_count)
    ones.append(0)
else:
    ones.append(one_count)

if len(zeros) == 0:
    print(ones[0])
    exit()

mxp, mxi = 0, 0
for i in range(len(zeros)):
    p = ones[i] + zeros[i] + ones[i+1]
    if mxp < p:
        mxp = p
        mxi = i

ans_l = 0
for j in range(k):
    if j == 0:
        ans_l += ones[mxi+1]
    idx = mxi - j
    if idx < 0:
        break
    ans_l += ones[idx]
    ans_l += zeros[idx]

ans_r = 0
for j in range(k):
    if j == 0:
        ans_r += ones[mxi]
    idx = mxi + j
    if idx >= len(zeros):
        break
    ans_r += ones[idx + 1]
    ans_r += zeros[idx]

print(ans_l if ans_l > ans_r else ans_r)

Submission status: Wrong Answer

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

one_list = []
zero_list = []

cur_char = S[0]
char_num = 0
for i in range(N):
    if S[i] == cur_char:
        char_num += 1
    else:
        if cur_char == '0':
            zero_list.append(char_num)
        else:
            one_list.append(char_num)
        cur_char = S[i]
        char_num = 1
if cur_char == '0':
    zero_list.append(char_num)
else:
    one_list.append(char_num)

len_zero_list = len(zero_list)
len_one_list = len(one_list)
# print(len_one_list)
# print(len_zero_list)

cont_length_list = []

if K >= len(zero_list):
    ans = N
else:
    if S[0] == '1':
        tmp_length = sum(one_list[:K+1])+sum(zero_list[:K])
        cont_length_list.append(tmp_length)
        for i in range(min(len_zero_list,len_one_list)-K):
            if K+1+i<len_one_list:
                tmp_length += one_list[K+1+i]+zero_list[K+i]
            else:
                tmp_length += zero_list[K+i]
            tmp_length -= one_list[i]+zero_list[i]
            cont_length_list.append(tmp_length)
        # print(cont_length_list)
    else:
        tmp_length = sum(one_list[:K])+sum(zero_list[:K])
        cont_length_list.append(tmp_length)
        if K < len_zero_list-1:
            tmp_length += zero_list[K]+one_list[K]
            tmp_length -= zero_list[0]
            cont_length_list.append(tmp_length)
            for i in range(1, min(len_zero_list,len_one_list)-K):
                if K+i<len_one_list and K+i < len_zero_list:
                    tmp_length += one_list[K+i]+zero_list[K+i]
                else:
                    tmp_length += zero_list[K+i]
                tmp_length -= one_list[i-1]+zero_list[i]
                cont_length_list.append(tmp_length)
        # print(cont_length_list)
    # print(cont_length_list)
    ans = max(cont_length_list)

print(ans)

Submission status: Wrong Answer

Solution code:
n, k = map(int,input().split())
s = input()
ha = []
owa = []
ju = 0
for i in range(len(s)) :
    if s[i] == "1" :
        if ju == 0 :
            ha.append(i)
            ju = 1
    else :
        if ju == 1 :
            owa.append(i - 1)
            ju = 0
if not ha :
    print(1)
else :
    if ha[0] != 0 :
        ha.insert(0,0)
        owa.insert(0,0)
    owa.append(len(s)-1)
    if len(ha) != len(owa) :
        ha.append(len(s) - 1)
    #print(ha,owa)
    dai = 0
    for i in range(len(owa) - k) :
        dai = max(dai,owa[i+k] - ha[i] + 1)
    if len(ha) > k :
        print(dai)
    else :
        print(len(s))

Submission status: Wrong Answer

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

one_list = []
zero_list = []

cur_char = S[0]
char_num = 0
for i in range(N):
    if S[i] == cur_char:
        char_num += 1
    else:
        if cur_char == '0':
            zero_list.append(char_num)
        else:
            one_list.append(char_num)
        cur_char = S[i]
        char_num = 1
if cur_char == '0':
    zero_list.append(char_num)
else:
    one_list.append(char_num)

len_zero_list = len(zero_list)
len_one_list = len(one_list)
# print(len_one_list)
# print(len_zero_list)

cont_length_list = []

if K >= len(zero_list):
    ans = N
else:
    if S[0] == '1':
        tmp_length = sum(one_list[:K+1])+sum(zero_list[:K])
        cont_length_list.append(tmp_length)
        for i in range(min(len_zero_list,len_one_list)-K):
            if K+1+i<len_one_list:
                tmp_length += one_list[K+1+i]+zero_list[K+i]
            else:
                tmp_length += zero_list[K+i]
            tmp_length -= one_list[i]+zero_list[i]
            cont_length_list.append(tmp_length)
        # print(cont_length_list)
    else:
        tmp_length = sum(one_list[:K])+sum(zero_list[:K])
        cont_length_list.append(tmp_length)
        if K < len_zero_list-1:
            tmp_length += zero_list[K]+one_list[K]
            tmp_length -= zero_list[0]
            cont_length_list.append(tmp_length)
            for i in range(1, min(len_zero_list,len_one_list)-K):
                if K+i<len_one_list and K+i < len_zero_list:
                    tmp_length += one_list[K+i]+zero_list[K+i]
                elif K+i<len_one_list:
                    tmp_length += one_list[K+i]
                else:
                    tmp_length += zero_list[K+i]
                tmp_length -= one_list[i-1]+zero_list[i]
                cont_length_list.append(tmp_length)
        # print(cont_length_list)
    # print(cont_length_list)
    ans = max(cont_length_list)

print(ans)

Submission status: Wrong Answer

Solution code:
import math                                                                                            

n,k = raw_input().split()
s = list(raw_input())


cnt = 0

prev = ''
tmp = ''
ar = []
for i in s:
    if i == '0' and prev !=  1:
        tmp += '0'
        prev = 0
    elif i == '0' and prev == 1:
        prev = 0
        ar.append(tmp)
        tmp = '0'
    elif i == '1' and prev != 0:
        tmp += '1'
        prev = 1
    elif i == '1' and prev == 0:
        prev = 1
        ar.append(tmp)
        tmp = '1'

ar.append(tmp)
k = int(k)


j = 0
cnt = 0
total = 0
m = 0

for i in ar:
    total += len(i)
    if '0' in i:
        cnt += 1
    if '0' in i and cnt == k + 1:
        total = total - (len(ar[j-2*k]) + len(ar[j-2*k-1]))                                            
        cnt = k                                    
    m = max(m, total)
    j += 1

print m

Submission status: Wrong Answer

Solution code:
N,K = (int(x) for x in(input().split()))
n=list(map(int,list(input())))
if n.count(0)<=K:
    print(N)
else:
    p=0
    q=0
    last=0
    for i in range(N-1):
        if p==K:
            break
        elif n[i]==0 and n[i+1]==1:
            p=p+1
            q=i
        else:
            pass
    for j in range(q+1):
        if n[j]==0:
            n[j]=1
        else:
            pass
    for f in range(N-1):
        if n[f]==1 and n[f+1]==0:
            last=f
            break
        else:
            pass

    print(n[:last].count(1))

Submission status: Wrong Answer

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

if s[0] == "1":
    retu = "whiteblack"
else:
    retu = "blackwhite"


count = 0
pre_person = s[0]
groups = []
for i in range(n):
    person = s[i]

    if person != pre_person:
        groups.append(count)
        count = 0
        pre_person = person
    count += 1
groups.append(count)


maxscore1 = 0
if retu == "whiteblack":
    score1 = 0
    blackgroups = len(groups)//2
    if k < blackgroups:
        start_index = 1
        score1 = groups[start_index-1]
        for j in range(k):
            score1 += groups[start_index+2*j]
            if start_index + 2*j+1 < len(groups):
                score1 += groups[start_index+2*j+1]
        for i in range(blackgroups - k + 1):
            score1 -= groups[start_index + 2*i - 1]
            if start_index+2*k+1+2*i < len(groups):
                score1 += groups[start_index + 2 * k + 1 + 2 * i]
        maxscore1 = max(score1, maxscore1)
    else:
        maxscore1 = n

else:
    score1 = 0
    blackgroups = len(groups) - len(groups)//2
    if k < blackgroups:
        start_index = 0
        score1 = 0
        for j in range(k):
            score1 += groups[start_index+2*j]
            if start_index + 2*j+1 < len(groups):
                score1 += groups[start_index+2*j+1]
        for i in range(blackgroups - k + 1):
            if i >1:
                score1 -= groups[start_index + 2*i - 1]
            if start_index+2*k+1+2*i < len(groups):
                score1 += groups[start_index + 2 * k + 1 + 2 * i]
        maxscore1 = max(score1, maxscore1)
    else:
        maxscore1 = n

print(maxscore1)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
from string import ascii_uppercase
import sys, bisect, math
stdin = sys.stdin
read_int = lambda : list(map(int,stdin.readline().split()))
read_str = lambda : stdin.readline().rstrip()

N, K = read_int()
S = read_str()

# 1110           4
#     10         2
#       10       2
        # 110011 6
chunks = []
zero = []
def solve():
    ans = 0

    dif = False
    ren = 1
    prev = ''
    for i in range(N):
        # print(i)
        # print(chunks)
        if i == N-1:
            chunks.append(ren)
        elif dif and S[i] != prev:
            chunks.append(ren)
            dif = False
            ren = 1

        elif S[i] != prev:
            dif = True
            ren += 1
        else:
            ren += 1
        prev = S[i]
    if len(chunks) > 2:
        p = chunks.pop()
        chunks[len(chunks)-1] += p

    index = 0
    while index <= len(chunks):
        if len(chunks) <= K:
            ans = sum(chunks)
            break
        ans = max(sum(chunks[index:index+K]), ans)
        index += 1

    return ans

if __name__ == "__main__":
    print(solve())
    # print(chunks)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
S = input() + '2'
a = []
if S[0] == '0':
    a.append(0)
cnt = 1
for i in range(1, N + 1):
    if S[i] != S[i - 1]:
        a.append(cnt)
        cnt = 1
    else:
        cnt += 1

ans = s = sum(a[:2*K + 1])
for i in range(2*K, len(a), 2):
    s += a[i] - a[i - 2*K - 1]
    ans = max(ans, s)
print(ans)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
S = list(map(int, input()))
 
Z = [j for j, s in enumerate(S) if (j > 0 and s == 0 and S[j] != S[j-1]) or (j == 0 and s == 0)] + [N]
Y = [j for j, s in enumerate(S) if (j > 0 and s == 1 and S[j] != S[j-1]) or (j == 0 and s == 1)] + [N]
result = 0
if len(Z) <= K:
    print(N)
else:
    for i in range(len(Z)-K):
        if i == 0:
            result = Z[i+K]
        else:
            result = max(result, Z[i+K] - (Y[i] if len(Y) > len(Z) else Y[i-1]))
    print(result)

Submission status: Wrong Answer

Solution code:
x,y = map(int,input().split())	
s=input()
l = s.split('1')
r = s.split('0')
l = [x for x in l if x]
r = [x for x in r if x]
j = len(l)
k = len(r)
p = 0
q = 0
for i in range(k-y):
    if len(l) < len(r):
        if i == k - y - 1:
            for m in range(y):
               p = p + len(l[i+m]) + len(r[i+m])
            p = p + len(r[i+y])
            if p > q:
               q = p
    else:
        for m in range(y):
               p = p + len(l[i+m]) + len(r[i+m])
        if p > q:
            q = p
    p = 0

print(q)

Submission status: Wrong Answer

Solution code:
"""
https://atcoder.jp/contests/abc124/tasks/abc124_d
未完成 メモ用
"""

def flip(S, X):
	for x in X:
		S[x] = -S[x]
	if X[0]==0:
		return sum(S[X[0]:X[len(X)]+2])
	else:
		return sun(S[X[0]-1:X[len(X)]+2])

def initialize(s):
	S = []
	c = 0
	for i in range(N):
		if s[i:i+1]=='0' and c<=0:
			c -= 1
		elif s[i:i+1]=='1' and c>=0:
			c += 1
		elif s[i:i+1]=='0' and c>0:
			S.append(c)
			c = -1
		elif s[i:i+1]=='1' and c<0:
			S.append(c)
			c = 1
	S.append(c)
	return S

def solve(S, K):
	A = []

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

Submission status: Wrong Answer

Solution code:
N, K = list(map(int, input().split()))
S = str(input())
list = []
cnt = 1
for i in range(len(S)):
  if(i + 2) > len(S): 
    list.append(cnt)
    break
  if (S[i+1] == S[i]):
    cnt += 1
  else:
    list.append(cnt)
    cnt = 1

# 配列ができた
ans = 0
  #配列の奇数のところに0が連続して入っている
if (S[0] == '1'):
  # 0の塊の数がK以下の時は全て変化させられる
  if(len(list) // 2 <= K):
    ans = len(S)
  else:
    # 0の塊を1にした時の最大値を出してみる
    for i in range((len(list) // 2) + 1):
      if(i + 2 * K + 1) > len(list): break
      tmp = 0
      for j in range(2 * K + 1):
        tmp += int(list[i + j])
      if(tmp > ans):
        ans = tmp
#配列の奇数のところに1が連続して入っている
else:
  # 0の塊の数がK以下の時は全て変化させられる
  if((len(list) // 2) + 1 <= K):
    ans = len(S)
  else:
    # 0の塊を1にした時の最大値を出してみる
    for i in range(len(list) // 2):
      if(i + 2 * K) > len(list): break
      tmp = 0
      for j in range(2 * K):
        tmp += int(list[i + j])
      if(tmp > ans):
        ans = tmp
print(ans)

Submission status: Wrong Answer

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

flag = False
li = [0]
for i in range(n):
    if bool(int(t[i])) == flag:
        li[-1] += 1
    else:
        flag = not flag
        li.append(1)

zeros = li[0::2]
ones = li[1::2]

ans = []
for i in range(len(zeros)):
    ans.append(sum(zeros[i:i+k]) + sum(ones[i-1:i+k]))
print(max(ans))

Submission status: Wrong Answer

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

cnt_arr = [0]
idx = 0
cnt_seq_0 = 1
cnt_seq_1 = 0
cnt_seq_0_arr = []
cnt_seq_1_arr = []
idx_seq_0 = []

prev = 1
for i, s_i in enumerate(S):
    if s_i == 0:
        if prev == 0:
            cnt_seq_0 += 1
        elif prev == 1:
            cnt_seq_1_arr.append(cnt_seq_1)
            cnt_seq_1 = 1
        if prev != 0:
            idx_seq_0.append(i)
        if i == len(S) - 1:
            cnt_seq_0_arr.append(cnt_seq_0)
            cnt_seq_1_arr.append(0)
    elif s_i == 1:
        if prev == 1:
            cnt_seq_1 += 1
        elif prev == 0:
            cnt_seq_0_arr.append(cnt_seq_0)
            cnt_seq_0 = 1
        if i == len(S) - 1:
            cnt_seq_1_arr.append(cnt_seq_1)
    prev = s_i

r_cnt = {}

import heapq
sum_arr = []

if len(cnt_seq_0_arr) == 0:
    print(len(cnt_seq_1_arr))
else:
    K = min(K, len(cnt_seq_0_arr))
    sum_0 = cnt_seq_1_arr[0]
    for i in range(K):
        sum_0 += cnt_seq_0_arr[i] + cnt_seq_1_arr[i + 1]

    heapq.heappush(sum_arr, -sum_0)
    sum_i = sum_0

    for i in range(0, len(cnt_seq_0_arr) - K):
        sum_i += -cnt_seq_0_arr[i] - cnt_seq_1_arr[i] + cnt_seq_0_arr[i + K] + cnt_seq_1_arr[i + K + 1]
        heapq.heappush(sum_arr, -sum_i)

    print(-heapq.heappop(sum_arr))

Submission status: Wrong Answer

Solution code:
N,K = [int(x) for x in input().split()]
S = input()

DP = [0]
ans = 0
l = 0

for index,x in enumerate(S):
    if(index==0):
        if(x=='0'):
            DP.append(0)
            DP.append(1)
        else:
            DP.append(1)
    else:
        if(S[index-1]!=x):
            DP.append(DP[-1]+1)
        else:
            DP[-1]+=1
    if(index==N-1):
        if(x=='1'):
            DP.append(DP[-1])
        DP.append(DP[-1])

while(1+2*K+l<len(DP)):
    ans = max(DP[2*K+1+l]-DP[l],ans)
    l+=2
if(not '0' in S):
    ans = len(S)

print(ans)

Submission status: Wrong Answer

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

first_index=0
last_index=0

res = 0
for f in range (first_index, n):
  print("f: " + str(f))
  for l in range (last_index, n+1):
    print("  l: " + str(l))
    if f > l:
      continue
    check = 0
    for i in range(f, l):
      if s[i] == "0":
        check += 1
    if check <= k:
      if l-f > res:
        res = l-f

print(res)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
S = list(str(input()))
list = [int(s) for s in S]
L = []
if list[0] == 0:
    L.append(0)
cnt = 1
for i in range(len(list) - 1):
    if list[i] == list[i + 1]:
        cnt += 1
    else:
        L.append(cnt)
        cnt = 1
L.append(cnt)

if list[N-1] == 0:
    L.append(0)
ans = 0
for i in range((len(L) + 1)//2 - K):
    print(sum(L[(2 * i):(2 * i) + (K * 2) + 1]))
    if sum(L[(2 * i):(2 * i) + (K * 2) + 1]) > ans:
        ans = sum(L[(2 * i):(2 * i) + (K * 2) + 1])
if len(L) == 1:
    ans = max(ans, L[0])
print(ans)

Submission status: Wrong Answer

Solution code:
import sys

def parse(inp):
    n,k=map(int,next(inp).split())
    S=list(next(inp))[:-1]
    S=list(map(int,S))
    return n,k,S
  
def main(inp):
    N,K,S=inp
    
    if(len(S)==1): return 1
    
    numlist=[1]
    for i in range(1,len(S)):
        if(S[i]!=S[i-1]): numlist.append(0)
        numlist[-1]+=1
    
    first=S[0]
    last=S[-1]
    ans=0
    
    if(first==0): start=-1
    else: start=0
    for i in range(start,len(numlist)-2*(K-1),2):
        if(i==-1): 
            ans = max(ans,sum(numlist[0:i+2*K+1]))
        else:
            ans = max(ans,sum(numlist[i:i+2*K+1]))
    print(ans)

main(parse(sys.stdin))

Submission status: Wrong Answer

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

C = []
c = 1
s = S[0]

for i in range(len(S)):
    if i == 0:
        continue
    if S[i] == S[i-1]:
        c += 1
    else:
        C.append((c, s))
        c = 1
        s = '1' if s == '0' else '0'

C.append((c, s))

ans = 0
start = time()
st = 0
for i in range(len(C)):
    flg = False
    count = 0
    if len(C) - i < (K*2-1):
        ans = max(ans, sum(map(lambda x: x[0], C[i:])))
        break

    for j in range(len(C) - i):
        if C[i+j][1] == '0':
            count += 1
            if count > K:
                ans = max(ans, sum(map(lambda x: x[0], C[i: i+j])))
                flg = True
                break

    if not flg:
        ans = max(ans, sum(map(lambda x: x[0], C[i:])))
print(ans)
print(time() - start, st)

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

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