Problem:


Score : 400 points

Problem Statement

N people are arranged in a row from left to right.

You are given a string S of length N consisting of 0 and 1, and a positive integer K.

The i-th person from the left is standing on feet if the i-th character of S is 0, and standing on hands if that character is 1.

You will give the following direction at most K times (possibly zero):

Direction: Choose integers l and r satisfying 1 \leq l \leq r \leq N, and flip the l-th, (l+1)-th, ..., and r-th persons. That is, for each i = l, l+1, ..., r, the i-th person from the left now stands on hands if he/she was standing on feet, and stands on feet if he/she was standing on hands.

Find the maximum possible number of consecutive people standing on hands after at most K directions.

Constraints

N is an integer satisfying 1 \leq N \leq 10^5.

K is an integer satisfying 1 \leq K \leq 10^5.

The length of the string S is N.

Each character of the string S is 0 or 1.

Input

Input is given from Standard Input in the following format:

N K
S

Output

Print the maximum possible number of consecutive people standing on hands after at most K directions.

Sample Input 1

5 1
00010

Sample Output 1

4

We can have four consecutive people standing on hands, which is the maximum result, by giving the following direction:

Give the direction with l = 1, r = 3, which flips the first, second and third persons from the left.

Sample Input 2

14 2
11101010110011

Sample Output 2

8

Sample Input 3

1 1
1

Sample Output 3

1

No directions are necessary.



Sample input:
5 1
00010


Sample output:
4

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
S = list(input())
S.append('X')
list_1 = []
list_0 = []
List = []
start = 0
ans = 0
now,count = 1,0
for i in range(len(S)):
    if S[i] == str(now):
        count += 1
    else:
        List.append(count)
        now = 1-now
        count = 1
if count != 0:
    List.append(count)

if len(List)%2 == 0:
    List.append(0)

Ruisekiwa = [0]*(len(List)+1)
for i in range(len(List)):
    Ruisekiwa[i+1] = Ruisekiwa[i]+List[i]

add = 2*K+1
for i in range(0,len(List),2):
    left = i
    right = min(i+add,len(List))
    tmp = Ruisekiwa[right] - Ruisekiwa[left]

    ans = max(tmp,ans)


print(ans)

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)]
        if(ans > max_num):
            max_num = ans
    
    print(max_num)

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
S = list(input())
S.append('X')
list_1 = []
list_0 = []
List = []
start = 0
ans = 0
now,count = 1,0
for i in range(len(S)):
    if S[i] == str(now):
        count += 1
    else:
        List.append(count)
        now = 1-now
        count = 1
if count != 0:
    List.append(count)

Ruisekiwa = [0]
for i in range(len(List)):
    Ruisekiwa.append(sum(List[:i+1]))

add = 2*K+1
for i in range(0,len(List),2):
    left = i
    right = min(i+add,len(List))
    tmp = Ruisekiwa[right] - Ruisekiwa[left]

    ans = max(tmp,ans)


print(ans)

Submission status: Wrong Answer

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

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

if S[0]=="0":
    L = [0]+L
if S[-2]=="0":
    L.append(L[-1])
#print(L)
max_r = 0
if len(L)<=2*K:
    print(sum(L))
    exit()
for i in range(0,len(L)-2*K,2):
    if(i>0):
        R = L[i+2*K]-L[i-1]
    else:
        R = L[i+2*K]
    #print(i, R, L[i+2*K])
    if max_r < R:
        max_r = R
print(max_r)

Submission status: Wrong Answer

Solution code:
n, k = list(map(int, input().split()))
s = input()
l = [0]
cur = 0
if (s[0] == '0'):
    l.append(0)
    cur = 1
begin = s[0]
for i in range(len(s)):
    if s[i] == begin:
        l[cur]+=1
    else:
        if begin == '0':
            begin ='1'
        else: begin = '0'
        l.append(1)
        cur+=1
sumup = [0]
cur = 0
for i in range(len(l)-1):
    sumup.append(l[i] + sumup[cur])
    cur+=1
sumup.append(sumup[len(l)-1] + l[len(l)-1])
tmp = []
if (s[0] == "0"):
    tmp = [sumup[i+k*2]-sumup[i] for i in range(len(sumup)-2*k)]
    if len(tmp) != 0:
        print (max(tmp))
    else:
        print(1)
else:
    tmp = [sumup[i+k*2]-sumup[i-1] for i in range(len(sumup)-2*k)]
    if len(tmp) != 0:
        print (max(tmp))
    else:
        print(1)

Submission status: Wrong Answer

Solution code:
from queue import Queue as q

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

S = input()

C = q()

one = True if S[0]=="1" else False
subK = 0 if S[0]=="1" else 1
c = 0
ans = 0
Mans = 0
for s in S:
    if s=="1":
        if one:
            c += 1
        else:
            one = True
            C.put(c)
            c = 1
    else:
        if one:
            one = False
            subK += 1
            C.put(c)
            c = 1
        else:
            c += 1
    ans += 1
    if subK>K:
        subK -= 1
        ans -= (C.get()+C.get())
    if ans > Mans:
        Mans = ans
    
print(Mans)

Submission status: Wrong Answer

Solution code:
#input
N, K = map(int, input().split())#0~K-1回目と考える。
s = input() #0~N-1人目と考える。


#output

first_pos = int(s[0:1]) #0人目が○なら1, ×なら0

nowpos = first_pos #今チェックしている立ち方
nowposcount = 0 #1にすると0人目を考えるときにバグる
Accum = [0] * (N+1) #累積和 取り出し方は[まとまり個数目] 要素数はまとまりの個数+1
nowRuisekiCount = 1 #何番目のまとまりを見ているか 0~まとまり個数-1
zeroRenzoku = 1

for i in range(len(s)): #0~N-1人目につき、全てをチェックして累積和のリストを作る
    if int(s[i:i+1]) == nowpos: #今チェックしている立ち方と同じ(左の人と同じ)なら
        nowposcount += 1 #左のまとまりの個数を1増やす
        Accum[nowRuisekiCount] = Accum[nowRuisekiCount-1] + nowposcount #まとまりの数字数を入れる
    else:
        nowpos = abs(nowpos - 1)
        nowposcount = 1 #nowposcountは1に戻る
        nowRuisekiCount += 1 #次のまとまりに移ったことになる
        Accum[nowRuisekiCount] = Accum[nowRuisekiCount-1] + nowposcount #まとまりの文字数を入れる(1を入れる)

matomariCount = nowRuisekiCount #まとまりの数

#累積和を出す
if (first_pos == 1) & (matomariCount <= (K*2+1)): #まとまり数がK*2+1以下なら、全部逆立ちさせられる
    print(len(s))
elif (first_pos == 0) & (matomariCount <= (K*2)): #最初が0だと同様にK*2以下なら
    print(len(s))
else:
    #一番最初の合計文字数
    if first_pos == 1:
        maxaccum = Accum[(K*2+1)]
    else: #最初のまとまりが0なら
        maxaccum = Accum[(K*2)]

    #2個目のまとまりからも同様に見ていく
    for i in range(2, matomariCount+1, 1):
        if i+(K*2) > matomariCount:
            break
        if abs((first_pos - ((i-1)%2))) == 0: #これが0なら左端が0なので右端を落とす
            maxaccum = max(maxaccum, Accum[i+(K*2)-1]-Accum[i-1])
        else:
            maxaccum = max(maxaccum, Accum[i+(K*2)]-Accum[i-1])
    print(maxaccum)

Submission status: Wrong Answer

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

num101 = []
if("0" == S[1]):
    num101 = [0]
for i in range(1, N + 1):
    if(S[i - 1] != S[i]):
        st = i
    if(S[i] != S[i + 1]):
        num101.append(i - st + 1)
if("0" == S[-1]):
    num101.append(0)

left = 0
right = min(2 * K + 1, len(num101))
sum101 = sum(num101[left:right])
maxsum = sum101
while(right + 2 <= len(num101)):
    left += 2
    right += 2
    sum101 -= sum(num101[left - 2:left])
    sum101 += sum(num101[right - 2:right])
    if maxsum < sum101:
        maxsum = sum101

print(maxsum)

Submission status: Wrong Answer

Solution code:

def 眠い():
	N,K=[int(_) for _ in input().split()]
	aS = [0]*N
	iS = 0
	iC = 0
	for s in input():
		if s == '0' :
			if iS % 2:
				aS[iS] = iC
				iS += 1
				iC = 1
			else:
				iC += 1
		else:
			if iS % 2:
				iC += 1
			else:
				aS[iS] = iC
				iS += 1
				iC = 1

	if iC:
		if s == '0' :
			if iS % 2:
				aS[iS]=0
				iS+=1
				aS[iS]=iC
			else:
				if len(aS) < iS +1 :
					aS.append(0)
					aS[iS]=iC
				else:
					aS[iS]=iC
		else:
			if iS % 2:
				if len(aS) < iS +1:
					aS.append(0)	
					aS[iS]=iC
				else:
					aS[iS]=iC
			else:
				aS[iS]=0
				iS +=1
				aS[iS]=iC
	aS=aS[:iS+1]

	iL = len(aS)
	iLP = iL // 2
	def parseHead(i):
		return 0 if i == -1 else i
	def parseTail(i):
		return iL if iL == i else i

	if iL % 2:
		iLP += 1
		if iLP <= K:
			print(N)
		else:
			print(max(sum(aS[parseHead(2*i-1):parseTail(2*i+2*K)]) for i in range(iLP - K )))

	else:
		if iLP <= K:
			print(N)
		else:
			print(max(sum(aS[parseHead(2*i-1):parseTail(2*i+2*K+1)]) for i in range(iLP - K)))
眠い()

Submission status: Wrong Answer

Solution code:
import re

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

store = []
start = -1
end = -1

if 1 == len(set(S)):
    print(len(S))
else:
    for i in range(N):
        if '0' == S[i] and -1 == start:
            start = i
        if '1' == S[i] and -1 != start:
            end = i
        if N - 1 == i:
            end = N
        if -1 != start and -1 != end:
            store.append([start, end, len(S[start:end])])
            start = end = -1
    store.sort(key=lambda x: x[2])
    store.reverse()

    for i in range(K):
        if len(store) == i:
            break
        start = store[i][0]
        end = store[i][1]
        count = store[i][2]
        li[start:end] = '1' * count

    count = 0
    temp = 0
    for i in range(N):
        if '1' == li[i]:
            temp += 1
        if '0' == li[i] or N-1 == i:
            count = max(temp, count)
            temp = 0
    print(count)

Submission status: Wrong Answer

Solution code:
import re

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

store = []
start = -1
end = -1

print(S[0:0])

# if 1 == len(set(S)):
#     print(len(S))
# else:
for i in range(N):
    if '0' == S[i] and -1 == start:
        start = i
    if '1' == S[i] and -1 != start:
        end = i
    if N - 1 == i:
        end = N
    if -1 != start and -1 != end:
        store.append([start, end, len(S[start:end])])
        start = end = -1
store.sort(key=lambda x: x[2])
store.reverse()

for i in range(K):
    if len(store) == i:
        break
    start = store[i][0]
    end = store[i][1]
    count = store[i][2]
    li[start:end] = '1' * count

count = 0
temp = 0
for i in range(N):
    if '1' == li[i]:
        temp += 1
    if '0' == li[i] or N-1 == i:
        count = max(temp, count)
        temp = 0
print(count)

Submission status: Wrong Answer

Solution code:
import sys
ns = lambda: sys.stdin.readline().rstrip()
ni = lambda: int(ns())
nm = lambda: map(int, sys.stdin.readline().split())
nl = lambda: list(nm())
nsl = lambda: map(str, sys.stdin.readline().split())

n, k = nm()
s = ns()
l = []
m = 1
count = 0
for i in range(n):
    if s[i] == str(m):
        count += 1
    else:
        m = 1 - m
        count = 1
    if count != 0:
        l.append(count)
if len(l) % 2 != 0:
    l.append(0)
summ = [0 for i in range(len(l) + 1)]
for i in range(len(l)):
    summ[i + 1] += summ[i] + l[i]
diff = 2 * k + 1
ans = 0
for i in range(0, len(l), 2):
    ans = max(ans, summ[min(i + diff, len(summ) - 1)] - summ[i])
print(ans)

Submission status: Wrong Answer

Solution code:
from functools import reduce
import math

def main():
    # N! を求める
    # f = math.factorial(N)
    
    # 切り捨て
    # 4 // 3
    # 切り上げ
    #-(-4 // 3)
    
    # 初期値用:十分大きい数(100億)
    # 1e10

    # 初期値用:十分小さい数(-100億)
    # -1e10
    
    # 1文字のみを読み込み
    # 入力:2
    # a = input().rstrip()
    # 変数:a='2'
    
    # スペース区切りで標準入力を配列として読み込み
    # 入力:2 4 5 7
    # a, b, c, d = (int(_) for _ in input().split())  
    # 変数:a=2 b=4 c=5 d =7
    
    # 1文字ずつ標準入力を配列として読み込み
    # 入力:2 4 5 7
    # a = list(int(_) for _ in input().split())
    # 変数:a = [2, 4, 5, 7]    

    # 1文字ずつ標準入力を配列として読み込み
    # 入力:2457
    # a = list(int(_) for _ in input())
    # 変数:a = [2, 4, 5, 7]    
    N,K = (int(_) for _ in input().split())
    S = list(int(_) for _ in input())
    now = 1
    cnt = 0
    Nums = []
    ans = 0
    for i in range(N):
        if(S[i]==now):
            cnt+=1
        else:
            Nums.append(cnt)
            now = 1 - now
            cnt=1
    if(cnt!=0):
        Nums.append(cnt)
    if(len(Nums)%2 == 0):
        Nums.append(0)
    Add = 2 * K +1
    for i in range(len(Nums)):
        tmp = 0
        left = i
        right = min(i+Add,len(Nums))
        for j in range(left,right):
            tmp += Nums[j]
        ans = max(tmp,ans)
    print(ans)
    
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
N, K = map(int,input().split())
S = input()
L = [0]
nowL = 0
if S[0] == '0':
    L = [0,0]
for i in range(N-1):
    nowL += 1
    if S[i] != S[i+1]:
        L.append(nowL)
# 終端処理
nowL += 1
L.append(nowL)
L.append(0)

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

print(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_arr.append(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)
        sum_arr.append(sum_i)

    #print(-heapq.heappop(sum_arr))
    print(max(sum_arr))

Submission status: Wrong Answer

Solution code:
#input
N, K = map(int, input().split())#0~K-1回目と考える。
s = input() #0~N-1人目と考える。


#output

first_pos = int(s[0:1]) #0人目が○なら1, ×なら0

nowpos = first_pos #今チェックしている立ち方
nowposcount = 0 #1にすると0人目を考えるときにバグる
Accum = [0] * (N+1) #累積和 取り出し方は[まとまり個数目] 要素数はまとまりの個数+1
nowRuisekiCount = 1 #何番目のまとまりを見ているか 0~まとまり個数-1
zeroRenzoku = 1

for i in range(len(s)): #0~N-1人目につき、全てをチェックして累積和のリストを作る
    if int(s[i:i+1]) == nowpos: #今チェックしている立ち方と同じ(左の人と同じ)なら
        nowposcount += 1 #左のまとまりの個数を1増やす
        Accum[nowRuisekiCount] = Accum[nowRuisekiCount-1] + nowposcount #まとまりの数字数を入れる
    else:
        nowpos = abs(nowpos - 1)
        nowposcount = 1 #nowposcountは1に戻る
        nowRuisekiCount += 1 #次のまとまりに移ったことになる
        Accum[nowRuisekiCount] = Accum[nowRuisekiCount-1] + nowposcount #まとまりの文字数を入れる(1を入れる)

matomariCount = nowRuisekiCount #まとまりの数

#累積和を出す
if (first_pos == 1) & (matomariCount <= (K*2+1)): #まとまり数がK*2+1以下なら、全部逆立ちさせられる
    print(len(s))
elif (first_pos == 0) & (matomariCount <= (K*2)): #最初が0だと同様にK*2以下なら
    print(len(s))
else:
    #一番最初の合計文字数
    if first_pos == 1:
        maxaccum = Accum[(K*2+1)]
    else: #最初のまとまりが0なら
        maxaccum = Accum[(K*2)]

    #2個目のまとまりからも同様に見ていく
    for i in range(2, matomariCount+1, 1):
        if i+(K*2) > matomariCount:
            break
        if maxaccum < Accum[i+(K*2)]-Accum[i-1]:
            if abs((first_pos - ((i+(K*2+1)-1)%2))) == 0: #これが0なら右端が0なので右端を落とす
                maxaccum = max(maxaccum, Accum[i+(K*2)-1]-Accum[i-1])
            else:
                maxaccum = max(maxaccum, Accum[i+(K*2)]-Accum[i-1])
    print(maxaccum)

Submission status: Wrong Answer

Solution code:
import re

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

store = []
start = -1
end = -1

print(S[0:0])

if 1 == len(set(S)):
    print(len(S))
else:
    for i in range(N):
        if '0' == S[i] and -1 == start:
            start = i
        if '1' == S[i] and -1 != start:
            end = i
        if len(S) - 1 == i:
            end = len(S)
        if -1 != start and -1 != end:
            store.append([start, end, len(S[start:end])])
            start = end = -1
    store.sort(key=lambda x: x[2])
    store.reverse()

    for i in range(K):
        if len(store) == i:
            break
        start = store[i][0]
        end = store[i][1]
        count = store[i][2]
        li[start:end] = '1' * count

    count = 0
    temp = 0
    for i in range(N):
        if '1' == li[i]:
            temp += 1
        if '0' == li[i] or N-1 == i:
            count = max(temp, count)
            temp = 0
    print(count)

Submission status: Wrong Answer

Solution code:
from functools import reduce
import math

def main():
    # N! を求める
    # f = math.factorial(N)
    
    # 切り捨て
    # 4 // 3
    # 切り上げ
    #-(-4 // 3)
    
    # 初期値用:十分大きい数(100億)
    # 1e10

    # 初期値用:十分小さい数(-100億)
    # -1e10
    
    # 1文字のみを読み込み
    # 入力:2
    # a = input().rstrip()
    # 変数:a='2'
    
    # スペース区切りで標準入力を配列として読み込み
    # 入力:2 4 5 7
    # a, b, c, d = (int(_) for _ in input().split())  
    # 変数:a=2 b=4 c=5 d =7
    
    # 1文字ずつ標準入力を配列として読み込み
    # 入力:2 4 5 7
    # a = list(int(_) for _ in input().split())
    # 変数:a = [2, 4, 5, 7]    

    # 1文字ずつ標準入力を配列として読み込み
    # 入力:2457
    # a = list(int(_) for _ in input())
    # 変数:a = [2, 4, 5, 7]    
    N,K = (int(_) for _ in input().split())
    S = list(int(_) for _ in input())
    now = 1
    cnt = 0
    Nums = []
    ans = 0
    for i in range(N):
        if(S[i]==now):
           cnt+=1
        else:
            Nums.append(cnt)
            now = 1 - now
            cnt=1
    if(cnt!=1):
        Nums.append(cnt)
    if(len(Nums)%2 == 0):
        Nums.append(0)

    Add = 2 * K +1
    for i in range(len(Nums)):
        tmp = 0
        left = i
        right = min(i+Add,len(Nums))
        for j in range(left,right):
            tmp += Nums[j]
        ans = max(tmp,ans)
    print(ans)
    
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

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

if S[0]=="0":
    L = [0]+L

max_r = 0
for i in range(0,len(L)-2*K,2):
    if(i>0):
        R = L[i+2*K]-L[i-1]
    else:
        R = L[i+2*K]
    if max_r < R:
        max_r = R
print(max_r)

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
S = list(input())
S.append('X')
list_1 = []
list_0 = []
List = []
start = 0
ans = 0
for i in range(1,N+1):
    if (S[i-1] != S[i]):
        if S[i-1] == '0':
            list_0.append(i-start)
        else:
            list_1.append(i-start)
        start = i


for i in range(min(len(list_0),len(list_1))):
    List.append(list_1[i])
    List.append(list_0[i])
    

            
if len(list_1)>min(len(list_0),len(list_1)):
    List.append(list_1[-1])
else:
    List.append(list_0[-1])

Ruisekiwa = [0]
for i in range(len(List)):
    Ruisekiwa.append(sum(List[:i+1]))

add = 2*K+1
for i in range(0,len(List),2):
    left = i
    right = min(i+add,len(List))
    tmp = Ruisekiwa[right] - Ruisekiwa[left]

    ans = max(tmp,ans)

print(List)
print(ans)

Submission status: Wrong Answer

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

L=[]
for i,x in enumerate(S):
	if i==0:
		cnt=1
	else:
		if prev==x:
			cnt+=1
		else:
			L.append(cnt)
			cnt=1
	prev=x
else:
	L.append(cnt)	


LSUM=[0]
for x in L:
	LSUM.append(LSUM[-1]+x)

l=len(LSUM)


if S[0]=="0":
	h=2*K		
else:
	h=2*K+1


ans=0
for i in range(l):
	j=min(l-1,i+h)
	ans=max(ans, LSUM[j]-LSUM[i])

print ans

Submission status: Wrong Answer

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

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

ruisekiwa = [0]
sums = []
for i in range(len(sl)):
    ruisekiwa.append(ruisekiwa[i] + sl[i])
# print(sl)
for i in range(len(sl)):
    if(k % 2 == i % 2):
        left = i-k-1
        if (left < 0):
            left = 0
        right = i + k
        if (right >= len(ruisekiwa)):
            right = len(ruisekiwa)-1
        # print(ruisekiwa[right], "-", ruisekiwa[left],
        #      "=", ruisekiwa[right]-ruisekiwa[left])
        sums.append(ruisekiwa[right]-ruisekiwa[left])
# print(ruisekiwa)
print(max(sums))

Submission status: Wrong Answer

Solution code:
import re

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

store = []
start = -1
end = -1

if 1 == len(set(S)):
    print(len(S))
else:
    for i in range(N):
        if '0' == S[i] and -1 == start:
            start = i
        if '1' == S[i] and -1 != start or len(S)-1 == i:
            end = i
        if -1 != start and -1 != end:
            if start == end:
                end += 1
            store.append([start, end, len(S[start:end])])
            start = end = -1

    store.sort(key=lambda x: x[2])
    store.reverse()

    for i in range(K):
        if len(store) == i:
            break
        start = store[i][0]
        end = store[i][1]
        count = store[i][2]
        li[start:end] = '1' * count

    count = 0
    temp = 0
    for i in range(N):
        if '1' == li[i]:
            temp += 1
        if '0' == li[i] or N-1 == i:
            count = max(temp, count)
            temp = 0
    print(count)

Submission status: Wrong Answer

Solution code:
#d=["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"]
#import numpy as np
#import math
#n=int(input())
#li=input().split()
#n=int(input())
#l=len(S)

lis=list(map(int,input().split()))
S=input()

ans=0
a=[] #0のリスト
b=[] #1のリスト
i,flag=0,0
while i<lis[0]:
    j=1
    if S[i]=="0":
        while flag==0 and i+j<lis[0]:
            if S[i+j]=="0":
                j+=1
            elif S[i+j]=="1":
                flag=1
        a.append(j)
        i+=j
    else:
        while flag==0 and i+j<lis[0]:
            if S[i+j]=="1":
                j+=1
            elif S[i+j]=="0":
                flag=1
        b.append(j)
        i+=j
    flag=0

A=len(a)
B=len(b)

if A<=lis[1]:
    ans=lis[0]
else:
    if A>B:
        for i in range(A-lis[1]+1):
            if i==0:
                tmp=sum(a[i:lis[1]+i])+sum(b[i:lis[1]+i])
                if ans<tmp:
                    ans=tmp
            elif i>0 and i<A-lis[1]:
                tmp=sum(a[i:lis[1]+i])+sum(b[i-1:lis[1]+i])
                if ans<tmp:
                    ans=tmp
            else:
                tmp=sum(a[i:lis[1]+i])+sum(b[i:lis[1]+i-1])
                if ans<tmp:
                    ans=tmp
    elif A<B:
        for i in range(A-lis[1]+1):
            tmp=sum(a[i:lis[1]+i])+sum(b[i:lis[1]+i+1])
            if ans<tmp:
                ans=tmp
    else:
        if S[0]=="0":
            for i in range(A-lis[1]+1):
                if i==0:
                    tmp=sum(a[i:lis[1]+i])+sum(b[i:lis[1]+i])
                    if ans<tmp:
                        ans=tmp
                else:
                    tmp=sum(a[i:lis[1]+i])+sum(b[i-1:lis[1]+i])
                    if ans<tmp:
                        ans=tmp
        else:
            for i in range(A-lis[1]+1):
                if i!=A-lis[1]:
                    tmp=sum(a[i:lis[1]+i])+sum(b[i:lis[1]+i+1])
                    if ans<tmp:
                        ans=tmp
                else:
                    tmp=sum(a[i:lis[1]+i])+sum(b[i:lis[1]+i])
                    if ans<tmp:
                        ans=tmp

print(ans)

Submission status: Wrong Answer

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

L=[]
for i,x in enumerate(S):
	if i==0:
		cnt=1
	else:
		if prev==x:
			cnt+=1
		else:
			L.append(cnt)
			cnt=1
	prev=x
else:
	L.append(cnt)	


LSUM=[0]
for x in L:
	LSUM.append(LSUM[-1]+x)

l=len(LSUM)


if S[0]=="0":
	h=2*K		
else:
	h=2*K+1


ans=float("-inf")
for i in range(l):
	j=min(l-1,i+h)
	ans=max(ans, LSUM[j]-LSUM[i])

print ans

Submission status: Wrong Answer

Solution code:
a = []

n,k = map(int, input().split())
for x in range(k+1):
  a.append(0)
q = input()

p = 0
c = 0
mx = 0

for i in range(n-1):
  p = p+1
  c = c+1  
  if q[i] == q[i+1]:
    lalala = 1

  elif (int(q[i]) == 1) & (int(q[i+1]) == 0) :
    p = p-a[0]
    if p > mx:
      mx = p
    
  elif (int(q[i]) == 0) & (int(q[i+1]) == 1):
    a.pop(0)
    a.append(c)
    c = 0

if int(q[n-1]) == 1:
  if p+1- a[0]> mx:
    mx = p +1 - a[0]
else:
  if p+1 - a[0] -a[1] > mx:
    mx = p - -a[1] +1
  
  


print(mx)

Submission status: Wrong Answer

Solution code:
a = []

n,k = map(int, input().split())
for x in range(k+1):
  a.append(0)
q = input()

p = 0
c = 0
mx = 0

for i in range(n-1):
  p = p+1
  c = c+1  
  if q[i] == q[i+1]:
    lalala = 1

  elif (int(q[i]) == 1) & (int(q[i+1]) == 0) :
    p = p-a[0]
    if p > mx:
      mx = p
    
  elif (int(q[i]) == 0) & (int(q[i+1]) == 1):
    a.pop(0)
    a.append(c)
    c = 0
  print(a,c,p)

if int(q[n-1]) == 1:
  if p+1- a[0]> mx:
    mx = p +1 - a[0]
else:
  if p+1 - a[0] -a[1] > mx:
    mx = p - -a[1] +1
  
  


print(mx)

Submission status: Wrong Answer

Solution code:
import re

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

store = []
start = -1
end = -1

if 1 == len(set(S)):
    print(len(S))
else:
    for i in range(N):
        if '0' == S[i] and -1 == start:
            start = i
        if '1' == S[i] and -1 != start or len(S)-1 == i:
            end = i
        if -1 != start and -1 != end:
            store.append([start, end, len(S[start:end])])
            start = end = -1

    store.sort(key=lambda x: x[2])
    store.reverse()

    for i in range(K):
        if len(store) == i:
            break
        start = store[i][0]
        end = store[i][1]
        count = store[i][2]
        li[start:end] = '1' * count

    count = 0
    temp = 0
    for i in range(N):
        if '1' == li[i]:
            temp += 1
        if '0' == li[i] or N-1 == i:
            count = max(temp, count)
            temp = 0
    print(count)

Submission status: Wrong Answer

Solution code:
import heapq
import sys

import numpy as np

reatInt = lambda: int(sys.stdin.readline())
reatIntN = lambda: [int(v) for v in sys.stdin.readline().split(" ")]


n, k = reatIntN()
ss = sys.stdin.readline().strip()


class Node:
    def __init__(self, p, n, v):
        self.prev = p
        self.next = n
        self.value = v


r = Node(None, None, None)
r.next = r
r.prev = r


def append(n, value):
    nn = Node(n, n.next, value)
    n.next.prev = nn
    n.next = nn


def remove(n):
    pn = n.prev
    nn = n.next
    nn.prev = pn
    pn.next = nn
    n.next = None
    n.prev = None
    n.value = None



i = 0
while i < len(ss):
    b = i
    c = ss[i]
    while (i < len(ss)) and (c == ss[i]):
        i += 1
    append(r.prev, (int(c), i - b, i))


def _calc(p):
    v = 0
    if (p is not r) and (p.value[0] == 0):
        v = p.value[1]
        if p.next is not r:
            v += p.next.value[1]
        if p.prev is not r:
            v += p.prev.value[1]
    return v


p = r.next
pq = []
while p is not r:
    d = _calc(p)
    if 0 < d:
        heapq.heappush(pq, (-d, p.value[2], p))
    p = p.next


for i in range(k):
    if len(pq) == 0:
        break

    d, _, p = heapq.heappop(pq)
    if (p.value is not None) and (p.value[0] == 0):
        if p.next is not r:
            remove(p.next)
        if p.prev is not r:
            remove(p.prev)
        p.value = (1, -d)

        dp = _calc(p.prev)
        if 0 < dp:
            heapq.heappush(pq, (-dp, p.prev.value[2], p.prev))
        dn = _calc(p.next)
        if 0 < dn:
            heapq.heappush(pq, (-dn, p.next.value[2], p.next))

ret = 0
p = r.next
while p is not r:
    if p.value[0] == 1:
        ret = max(ret, p.value[1])
    p = p.next
print(ret)

Submission status: Wrong Answer

Solution code:
#d=["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"]
#import numpy as np
#import math
#n=int(input())
#li=input().split()
#n=int(input())
#l=len(S)

lis=list(map(int,input().split()))
S=input()

ans=0
a=[] #0のリスト
b=[] #1のリスト
i,flag=0,0
while i<lis[0]:
    j=1
    if S[i]=="0":
        while flag==0 and i+j<lis[0]:
            if S[i+j]=="0":
                j+=1
            elif S[i+j]=="1":
                flag=1
        a.append(j)
        i+=j
    else:
        while flag==0 and i+j<lis[0]:
            if S[i+j]=="1":
                j+=1
            elif S[i+j]=="0":
                flag=1
        b.append(j)
        i+=j
    flag=0

A=len(a)
B=len(b)

if A<=lis[1]:
    ans=lis[0]
else:
    if A>B:
        for i in range(A-lis[1]+1):
            if i!=A-lis[1]:
                tmp=sum(a[i:lis[1]+i])+sum(b[i:lis[1]+i])
                if ans<tmp:
                    ans=tmp
            else:
                tmp=sum(a[i:lis[1]+i])+sum(b[i:lis[1]+i-1])
                if ans<tmp:
                    ans=tmp
    elif A<B:
        for i in range(A-lis[1]+1):
            tmp=sum(a[i:lis[1]+i])+sum(b[i:lis[1]+i+1])
            if ans<tmp:
                ans=tmp
    else:
        if S[0]=="0":
            for i in range(A-lis[1]+1):
                if i==0:
                    tmp=sum(a[i:lis[1]+i])+sum(b[i:lis[1]+i])
                    if ans<tmp:
                        ans=tmp
                else:
                    tmp=sum(a[i:lis[1]+i])+sum(b[i-1:lis[1]+i])
                    if ans<tmp:
                        ans=tmp
        else:
            for i in range(A-lis[1]+1):
                if i!=A-lis[1]:
                    tmp=sum(a[i:lis[1]+i])+sum(b[i:lis[1]+i+1])
                    if ans<tmp:
                        ans=tmp
                else:
                    tmp=sum(a[i:lis[1]+i])+sum(b[i:lis[1]+i])
                    if ans<tmp:
                        ans=tmp

print(ans)