Problem:


Score : 400 points

Problem Statement

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

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

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

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

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

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

Constraints

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

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

The length of the string S is N.

Each character of the string S is 0 or 1.

Input

Input is given from Standard Input in the following format:

N K
S

Output

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

Sample Input 1

5 1
00010

Sample Output 1

4

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

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

Sample Input 2

14 2
11101010110011

Sample Output 2

8

Sample Input 3

1 1
1

Sample Output 3

1

No directions are necessary.



Sample input:
5 1
00010


Sample output:
4

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
s = input()
r = 1
ans = 0
cnt = 0

for l in range(0, n):
    if l == 0:
        if s[l] == '0':
            cnt += 1

    if s[l] == '1' and s[l-1] == '0':
        cnt -= 1

    while True:
        # print('\n', l, r)
        # print('count:', cnt)
        if r > n - 1:
            length = r - l
            ans = max(ans, length)
            break

        if s[r] == '0' and s[r-1] == '1':
            if cnt < k:
                cnt += 1
            else:
                # print('over!')
                length = r - l
                ans = max(ans, length)
                break
        r += 1

print(ans)

Submission status: Wrong Answer

Solution code:
from collections import deque

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

hoge = deque()

now = int(S[0])
Sum = 1
for i in range(1, N):
    if now == int(S[i]):
        Sum += 1
    else:
        hoge.append(Sum)

        Sum = 1
        now = (now + 1) % 2

    if i == N - 1:
        hoge.append(Sum)

rui = deque()
rui.append(0)
Sum = 0
for i in range(len(hoge)):
    Sum += hoge[i]
    rui.append(Sum)

first = (int(S[0]) +1)%2
max = 0
for i in range(len(rui)):

    if (2*(K+1)+2*i+first) <= len(rui):
        X = rui[2*K+1+2*i+first]- rui[2*i+first]
        if max < X:
            max = X

    else:
        break

print(max)

Submission status: Wrong Answer

Solution code:
from itertools import groupby
N, K = map(int,input().split())
S = list(input())
Sg = groupby(S)
L = []
for k, g in Sg:
  L.append(len(list(g)))
#print(L)

ans = 0
X = 2 * K + 1
for i in range(len(L)):
  if S[0] == "0" and i == 0:
    end = min(2 * K, len(L))
  else:
    end = min(i + X,len(L))
  ans = max(ans,sum(L[i:end]))
print(ans)

Submission status: Wrong Answer

Solution code:
def convert(arr):
    return [int(n) for n in arr]


def first(arr):
    appended = False
    if arr[0] == 0:
        arr.insert(0, 1)
        appended = True
    return arr,appended


def tank(arr):
    tank = []

    if len(arr) == 1:
        tank.append(1)
        return tank

    tankItemCount = 0
    for i in range(0, len(arr)):
        if i == 0:
            tankItemCount = tankItemCount + 1
            continue

        if arr[i] == arr[i - 1]:
            tankItemCount = tankItemCount + 1
        else:
            tank.append(tankItemCount)
            tankItemCount = 1

        if i == len(arr) - 1:
            tank.append(tankItemCount)

    return tank


def sumArr(x):
    sum = 0
    for i in range(len(x)):
        sum = sum + x[i]
    return sum


def calculateSum(arr, indexes):
    sum = 0
    for index in indexes:
        sum = sum + arr[index]

    return sum


def calculateIndex(arr, k):
    bigest = 0
    finalIndexes = []
    sss = [n for n in range(1, len(arr), 2)]
    for s in range(len(sss)):
        tmpIndexes = sss[s:s + k]
        tmp = calculateSum(arr, tmpIndexes)
        if tmp > bigest:
            bigest = tmp
            finalIndexes = tmpIndexes

    return finalIndexes


def sumTotal(arr,indexs):
    sum = 0
    for i in range(len(indexs)):

        sum = sum + arr[indexes[i]-1]
        sum = sum + arr[indexes[i]]
        if i == len(indexs) - 1:
            if(len(arr)-1) > indexes[i]:
                sum = sum + arr[indexes[i]+1]

    return sum


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

S = list(input())

arr = convert(S)

arr,appended = first(arr)
a = tank(arr)

if(len(a) == 1):
    print(len(a))
else:
    indexes = (calculateIndex(a, K))
    result = sumTotal(a, indexes)
    if indexes[0] == 1 and appended:
        result = result - 1

    print(result)

Submission status: Wrong Answer

Solution code:
from itertools import accumulate


def main():
    n, k = map(int, input().split())
    s = input()

    c = []
    cnt = 1
    prev = s[0]
    for e in s[1:]:
        if e == prev:
            cnt += 1

        else:
            c.append(cnt)
            cnt = 1
            prev = e

    c.append(cnt)

    ans = max(c)

    acm = list(accumulate(c))
    acm.insert(0, 0)

    if s[0] == "1":
        start = 2 * k + 1
    else:
        start = 2 * k

    end = len(acm)
    prev = 0
    for i in range(start, end, k):
        ans = max(ans, acm[i] - acm[prev])
        prev += k

    if start >= end:
        ans = len(s)

    print(ans)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

    s = "1" + s + "0"

    idx = [-1] + [i for i in range(len(s) - 1) if s[i:i+2] == "10"]

    if len(idx) < k + 2:
        print(n)
        exit()

    ans = 0
    for i in range(len(idx) - k - 1):
        ans = max(ans, idx[i+k+1] - idx[i] - 1)

    print(ans)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

    s = "1" + s + "0"

    idx = [-1] + [i for i in range(len(s) - 1) if s[i:i+2] == "10"]

    ans = max([idx[i+1] - idx[i] - 1 for i in range(len(idx) - 1)])
    for i in range(len(idx) - k - 1):
        ans = max(ans, idx[i+k+1] - idx[i] - 1)

    print(ans)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
s=list(input())
cnt=0
ans=0
j=0
for i in range(n):
    if s[i]=="0":
        if i==0 or s[i-1]=="1":
            cnt+=1
    if cnt>k:
        while s[j]=="0":
            j+=1
        while s[j]=="1":
            j+=1
        cnt-=1
    ans=max(ans,i-j+1)

print(ans)

Submission status: Wrong Answer

Solution code:
from itertools import groupby, accumulate
N, K = map(int, input().split())
S = groupby(list(input()))
state = []
number = []
for i, j in S:
    state.append(i)
    number.append(len(list(j)))

answer = 0

if len(state) == 1:
    print(number[0])
    exit()

number = list(accumulate([0] + number))
l = len(number) - 1

if state[0] == '0':
    answer = number[min(l, 2 * K)] - number[0]

start = state.index('1') + 1
for i in range(start, max(2, l - 2 * K + 1), 2):
    answer = max(answer, number[2 * K + i] - number[i - 1])

print(answer)

Submission status: Wrong Answer

Solution code:
from itertools import groupby, accumulate
N, K = map(int, input().split())
S = groupby(list(input()))
state = []
number = []
for i, j in S:
    state.append(i)
    number.append(len(list(j)))

answer = 0

if len(state) == 1:
    print(number[0])
    exit()

number = list(accumulate([0] + number))
l = len(number) - 1

if state[0] == '0':
    answer = number[min(l, 2 * K)] - number[0]

start = state.index('1') + 1
for i in range(start, l - 2 * K + 1, 2):
    answer = max(answer, number[2 * K + i] - number[i - 1])

print(answer)

Submission status: Wrong Answer

Solution code:
from itertools import groupby, accumulate
N, K = map(int, input().split())
S = groupby(list(input()))
state = []
number = []
for i, j in S:
    state.append(i)
    number.append(len(list(j)))

answer = 0

if len(state) == 1:
    print(number[0])
    exit()

number = list(accumulate([0] + number))
l = len(number) - 1

if state[0] == '0':
    answer = number[min(l, 2 * K)] - number[0]

start = state.index('1') + 1
for i in range(start, l, 2):
    answer = max(answer, number[min(2 * K + i + 1, l)] - number[i - 1])

print(answer)

Submission status: Wrong Answer

Solution code:
from itertools import groupby, accumulate
N, K = map(int, input().split())
S = groupby(list(input()))
state = []
number = []
for i, j in S:
    state.append(i)
    number.append(len(list(j)))

answer = 0

if len(state) == 1:
    print(number[0])
    exit()

number = list(accumulate([0] + number))
l = len(number) - 1

if state[0] == '0':
    answer = number[min(l, 2 * K)] - number[0]

start = state.index('1') + 1

for i in range(start, l, 2):
    answer = max(answer, number[min(2 * K + i + 1, l)] - number[i - 1])

print(answer)

Submission status: Wrong Answer

Solution code:
from itertools import groupby, accumulate
N, K = map(int, input().split())
S = groupby(list(input()))
state = []
number = []
for i, j in S:
    state.append(i)
    number.append(len(list(j)))

answer = 0

if len(state) == 1:
    print(number[0])
    exit()

number = list(accumulate([0] + number))
l = len(number) - 1

if state[0] == '0':
    answer = number[min(l, 2 * K)] - number[0]

start = state.index('1')

for i in range(start, l, 2):
    answer = max(answer, number[min(2 * K + i + 2, l)] - number[i])

print(answer)

Submission status: Wrong Answer

Solution code:
# D - Handstand

# 逆立ちしている人が連続する区間のインデックスを保存する
# 指示1回あたり2つの区間を繋ぐことができる
# 連続したK+1個の区間を順に選んで繋ぎ、最も逆立ち区間が長くなる場合を探す。端から繋ぐ場合は例外処理

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

if len(set(S)) == 1 or K >= N//2 + 1:
    print(N)
    exit()

handstand_range_idx = []
range_start_idx = 0
for idx in range(1,N):
    if (S[idx-1] == "1" and S[idx] == "0"):
        range_end_idx = idx - 1
        handstand_range_idx.append([range_start_idx, range_end_idx])
    elif idx == N-1 and S[idx] == "1":
        range_end_idx = idx
        handstand_range_idx.append([range_start_idx, range_end_idx])
    elif S[idx-1] == "0" and S[idx] == "1":
        range_start_idx = idx

ans = 0
try:
    ans = max(handstand_range_idx[K-1][1]+1, (N-1) - handstand_range_idx[-K][0] + 1)
except IndexError:
    ans = max(handstand_range_idx[0][1]+1, (N-1) - handstand_range_idx[0][0] + 1)

for first_link_range_idx in range(len(handstand_range_idx)):
    try:
        ans = max(ans, handstand_range_idx[first_link_range_idx + K][1] - handstand_range_idx[first_link_range_idx][0] + 1)
    except IndexError:
        pass

print(ans)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
list1 = list(map(int,input()))
list2 = []
count = 1
for i in range(1,n):
	if list1[i] != list1[i-1]:
		list2.append(count)
		count = 1
	else:
		count += 1
list2.append(count) #最後の処理
first,last = 0,0
ans = 0
length = 0
#以下のループはn=1のときのみうまく動作しないので、例外処理
if n ==1:
	print(1)
	exit()

if list1[0] == 0: #長さはk*2
	length1 = k*2 #初期値
	length2 = k*2 + 1
	for i in range(len(list2)):
		if length != length1:
			length = length1
		else:
			length = length2
		last = i
		first = last + length
		if first >= n:
			break
		ans = max(ans,sum(list2[last:first]))

else: #長さはk*2+1
	length1 = k*2 +1 #初期値
	length2 = k*2
	for i in range(len(list2)):
		if length != length1:
			length = length1
		else:
			length = length2
		last = i
		first = last + length
		if first >= n:
			break
		ans = max(ans,sum(list2[last:first]))

print(ans)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
list1 = list(map(int,input()))
list2 = []
count = 1
for i in range(1,n):
	if list1[i] != list1[i-1]:
		list2.append(count)
		count = 1
	else:
		count += 1
list2.append(count) #最後の処理
first,last = 0,0
i = 0
ans = 0

#以下のループはn=1のときのみうまく動作しないので、例外処理
if n ==1:
	print(1)
	exit()

if list1[0] == 0: #長さはk*2
	while True:
		if i % 2 == 0:
			length = k*2
		else:
			length = k*2+1
		last = i
		first = last + length
		if first >= n:
			break
		ans = max(ans,sum(list2[last:first]))
		i += 1

else: #長さはk*2+1
		while True:
			if i % 2 == 0:
				length = k*2 +1
			else:
				length = k*2
			last = i
			first = last + length
			if first >= n:
				break
			ans = max(ans,sum(list2[last:first]))
			i += 1

print(ans)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
s=list(input())
a=[]
f=1
s.append(2)
for i in range(n):
    if s[i]==s[i+1]:
        f+=1
    else:
        a.append(f)
        f=1
g=len(a)
r=[0]*(g+1)
for i in range(g):
    r[i+1]=r[i]+a[i]
if s[0]=='1':
    ans=sum(a[0:2*k+1])
    if s[-2]=='0':
        ans=max(ans,sum(a[max(0,g-2*k-1):g]))
    for i in range(g):
        if 2*i+2*k+1<g+1:
            ans=max(ans,r[2*i+2*k+1]-r[2*i])
        else:
            break
    print(ans)
else:
    ans=sum(a[0:2*k])
    if s[-2]=='0':
        ans=max(ans,sum(a[max(0,g-2*k):g]))
    for i in range(g):
        if 2*i+2*k+2<g+1:
            ans=max(ans,r[2*i+2*k+2]-r[2*i+1])
        else:
            break
    print(ans)

Submission status: Wrong Answer

Solution code:
import sys
import bisect
import collections
import fractions
import heapq
import math
from operator import mul
from functools import reduce
from functools import lru_cache


def slove():
    input = sys.stdin.readline
    mod = 10 ** 9 + 7
    n, k = list(map(int, input().rstrip('\n').split()))
    s = str(input().rstrip('\n'))
    ls = []
    cnt = 1
    for i in range(1, n):
        if s[i] != s[i-1]:
            ls.append(cnt)
            cnt = 0
        cnt += 1
    ls.append(cnt)

    wc = 0
    b = True if s[0] == "1" else False
    cnt = 0
    pos = 0
    m_cnt = 0
    for i in range(len(ls)):
        if not b:
            wc += 1
        if wc > k:
            cnt -= ls[pos]
            if s[0] == "1":
                cnt -= ls[pos+1]
                pos += 1
            pos += 1
            wc -= 1
        b = not b
        cnt += ls[i]
        m_cnt = max(m_cnt, cnt)
    print(m_cnt)


if __name__ == '__main__':
    slove()

Submission status: Wrong Answer

Solution code:
import sys
import bisect
import collections
import fractions
import heapq
import math
from operator import mul
from functools import reduce
from functools import lru_cache


def slove():
    input = sys.stdin.readline
    mod = 10 ** 9 + 7
    n, k = list(map(int, input().rstrip('\n').split()))
    s = str(input().rstrip('\n'))
    ls = []
    cnt = 1
    for i in range(1, n):
        if s[i] != s[i-1]:
            ls.append(cnt)
            cnt = 0
        cnt += 1
    ls.append(cnt)

    wc = 0
    b = True if s[0] == "1" else False
    cnt = 0
    pos = 0
    m_cnt = 0
    for i in range(len(ls)):
        if b:
            b = not b
        else:
            wc += 1
            b = not b
        if wc > k:
            cnt -= ls[pos]
            pos += 1
            m_cnt = max(m_cnt, cnt)
            cnt -= ls[pos]
            pos += 1
            wc -= 1
        cnt += ls[i]
        m_cnt = max(m_cnt, cnt)
    print(m_cnt)


if __name__ == '__main__':
    slove()

Submission status: Wrong Answer

Solution code:
import sys
import bisect
import collections
import fractions
import heapq
import math
from operator import mul
from functools import reduce
from functools import lru_cache


def slove():
    input = sys.stdin.readline
    mod = 10 ** 9 + 7
    n, k = list(map(int, input().rstrip('\n').split()))
    s = str(input().rstrip('\n'))
    ls = []
    cnt = 1
    for i in range(1, n):
        if s[i] != s[i-1]:
            ls.append(cnt)
            cnt = 0
        cnt += 1
    ls.append(cnt)

    bc = 0
    wc = 0
    b = True if s[0] == "1" else False
    cnt = 0
    pos = 0
    m_cnt = 0
    for i in range(len(ls)):
        if b:
            bc += 1
            b = not b
        else:
            wc += 1
            b = not b
        if wc > k:
            cnt -= ls[pos]
            pos += 1
            cnt -= ls[pos]
            pos += 1
            wc -= 1
        cnt += ls[i]
        m_cnt = max(m_cnt, cnt)
    print(m_cnt)


if __name__ == '__main__':
    slove()

Submission status: Wrong Answer

Solution code:
import sys
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
mod = 10**9 + 7
inf = float('inf')
ans = int(0)

N, K = LI()
S = str(input())
S = S + "2"
d1 = []
d0 = []
k = S[0]
if k=="0":
    d1.append(0)
cou = int(1)
for i in range(N):
    if S[i]!=S[i+1]:
        if S[i]=="0":
            d0.append(cou)
        if S[i]=="1":
            d1.append(cou)
        cou = int(1)
    else:
        cou +=1

#print(d1)
#print(d0)

ansC = []
t = 2*K+1
newN = len(d1)
if len(d1)==1 or d0==[] or K>=N//2+1:
    ansC.append(N)

if newN!=len(d0):
    d0.append(0)
    d0.append(0)
sumd1 = [0]*(newN+t)
sumd0 = [0]*(newN+t)
for i in range(newN):
    sumd1[i+1] = d1[i]
    sumd0[i+1] = d0[i]
for i in range(newN+1):
    sumd1[i+1] += sumd1[i]
    sumd0[i+1] += sumd0[i]

#for i in range(max(newN-K+1,K)):
for i in range(newN+K):
    ans = (sumd1[i+K+1]-sumd1[i]) + (sumd0[i+K]-sumd0[i])
    ansC.append(ans)


print(max(ansC))
#print(sumd1)
#print(sumd0)

Submission status: Wrong Answer

Solution code:
import sys
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
mod = 10**9 + 7
inf = float('inf')
ans = int(0)

N, K = LI()
S = str(input())
S = S + "2"
d1 = []
d0 = []
k = S[0]
if k=="0":
    d1.append(0)
cou = int(1)
for i in range(N):
    if S[i]!=S[i+1]:
        if S[i]=="0":
            d0.append(cou)
        if S[i]=="1":
            d1.append(cou)
        cou = int(1)
    else:
        cou +=1

#print(d1)
#print(d0)

ansC = []
t = 2*K+1
newN = len(d1)
if len(d1)==1 or d0==[] or K>=N//2+1:
    ansC.append(N)

if newN!=len(d0):
    d0.append(0)
    d0.append(0)
sumd1 = [0]*(newN+t)
sumd0 = [0]*(newN+t)
for i in range(newN):
    sumd1[i+1] = d1[i]
    sumd0[i+1] = d0[i]
for i in range(newN+1):
    sumd1[i+1] += sumd1[i]
    sumd0[i+1] += sumd0[i]

for i in range(max(newN-K+1,K)):
    ans = (sumd1[i+K+1]-sumd1[i]) + (sumd0[i+K]-sumd0[i])
    ansC.append(ans)


print(max(ansC))
#print(sumd1)
#print(sumd0)

Submission status: Wrong Answer

Solution code:
import sys
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
mod = 10**9 + 7
inf = float('inf')
ans = int(0)

N, K = LI()
S = str(input())
S = S + "2"
d1 = []
d0 = []
k = S[0]
if k=="0":
    d1.append(0)
cou = int(1)
for i in range(N):
    if S[i]!=S[i+1]:
        if S[i]=="0":
            d0.append(cou)
        if S[i]=="1":
            d1.append(cou)
        cou = int(1)
    else:
        cou +=1

#print(d1)
#print(d0)

ansC = []
t = 2*K+1
newN = len(d1)
if len(d1)==1 or d0==[] or K>=N//2+1:
    ansC.append(N)

if newN!=len(d0):
    d0.append(0)
    d0.append(0)
sumd1 = [0]*(newN+t)
sumd0 = [0]*(newN+t)
for i in range(newN):
    sumd1[i+1] = d1[i]
    sumd0[i+1] = d0[i]
for i in range(newN+1):
    sumd1[i+1] += sumd1[i]
    sumd0[i+1] += sumd0[i]

for i in range(max(newN-K+1,1)):
    ans = (sumd1[i+K+1]-sumd1[i]) + (sumd0[i+K]-sumd0[i])
    ansC.append(ans)


print(max(ansC))
#print(sumd1)
#print(sumd0)

Submission status: Wrong Answer

Solution code:
import sys
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
mod = 10**9 + 7
inf = float('inf')
ans = int(0)

N, K = LI()
S = str(input())
S = S + "2"
d1 = []
d0 = []
k = S[0]
if k=="0":
    d1.append(0)
cou = int(1)
for i in range(N):
    if S[i]!=S[i+1]:
        if S[i]=="0":
            d0.append(cou)
        if S[i]=="1":
            d1.append(cou)
        cou = int(1)
    else:
        cou +=1

#print(d1)
#print(d0)

ansC = []
t = 2*K+1
newN = len(d1)
if len(d1)==1 or d0==[]:
    ansC.append(N)

if newN!=len(d0):
    d0.append(0)
    d0.append(0)
sumd1 = [0]*(newN+t)
sumd0 = [0]*(newN+t)
for i in range(newN):
    sumd1[i+1] = d1[i]
    sumd0[i+1] = d0[i]
for i in range(newN+1):
    sumd1[i+1] += sumd1[i]
    sumd0[i+1] += sumd0[i]

for i in range(max(newN-K+1,1)):
    ans = (sumd1[i+K+1]-sumd1[i]) + (sumd0[i+K]-sumd0[i])
    ansC.append(ans)


print(max(ansC))
#print(sumd1)
#print(sumd0)

Submission status: Wrong Answer

Solution code:
N, K = [int(x) for x in input().split()]
S = input()
lst = []
if (S[0] == '0'): lst.append(0)
cnt = 1
for i in range(N-1):
    if (S[i] == S[i+1]):
        cnt += 1
    else:
        lst.append(cnt)
        cnt = 1 
lst.append(cnt)
if (S[-1] == '0'): lst.append(0)
tmp = [0] + lst[:]
ans = 0 if (len(lst) != 1) else lst[0] 
for i in range(len(lst)):
    tmp[i+1] += tmp[i]
for i in range(0, (len(lst)+1)//2-K):
    l = 2*i
    r = l + 1 + 2*K
    s = tmp[r] - tmp[l]
    if (ans < s):
        ans = s
print(ans)

Submission status: Wrong Answer

Solution code:
N, K = [int(x) for x in input().split()]
S = input()
lst = []
if (S[0] == '0'): lst.append(0)
cnt = 1
for i in range(N-1):
    if (S[i] == S[i+1]):
        cnt += 1
    else:
        lst.append(cnt)
        cnt = 1 
lst.append(cnt)
if (S[-1] == '0'): lst.append(0)
tmp = []
ans = 0 if (len(lst) != 1) else lst[0] 
# print(lst)
for i in range(0, (len(lst)+1)//2-K):
    l = 2*i
    r = l+1 + 2*K
    s = sum(lst[l:r])
    # print(lst[l:r])
    if (ans < s):
        ans = s
print(ans)

Submission status: Wrong Answer

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

def ranlength(A):
    num = list(A)
    count = 1
    table = []
    for i in range(len(num)):
        try:
            if num[i] == num[i+1]:
               count = count + 1
            else:
                table.append(count)
                count = 1
        except:
            table.append(count)
    return(table)

status=ranlength(S)
if S[0]==0:
    status.insert(0,0)
if S[-1]==0:
    status.append(0)

ans=0
for i in range(len(status)//(2*K-1)+1):
    L=status[i*2:i*2+2*K+1]
    ans=max(ans,sum(L))

print(ans)

Submission status: Wrong Answer

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

def ranlength(A):
    num = list(A)
    count = 1
    table = []
    for i in range(len(num)):
        try:
            if num[i] == num[i+1]:
               count = count + 1
            else:
                table.append(count)
                count = 1
        except:
            table.append(count)
    return(table)

start=S[0]
end=S[-1]
status=ranlength(S)
if start==0:
    status.insert(0,0)
if end==0:
    status.append(0)

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

print(ans)

Submission status: Wrong Answer

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

def ranlength(A):
    num = list(A)
    count = 1
    table = []
    for i in range(len(num)):
        try:
            if num[i] == num[i+1]:
               count = count + 1
            else:
                table.append(count)
                count = 1
        except:
            table.append(count)
    return(table)

start=S[0]
end=S[-1]
status=ranlength(S)
if start==0:
    status.insert(0,0)
if end==0:
    status.append(0)

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

print(ans)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
s=input()
s=s+"w"
lists0=[]
lists1=[] 
count=0
for i in range(n):
    if s[i]!=s[i+1] and s[i]=="0":
        lists0.append(i-count)
        count=i
        
    elif s[i]!=s[i+1] and s[i]=="1":
        lists1.append(i-count)
        count=i

if s[0]=="1":
    lists1[0]+=1
elif s[0]=="0":
    lists0[0]+=1     
#下準備の完成
if s[0]=="1" and s[-2]=="1":
    #do nothing special

    
    if k>=len(lists0):
        print(n)
    elif k<len(lists0):
        big=0
        ans=sum(lists1[:k+1])+sum(lists0[:k])
        for i in range(len(lists0)-k):
            ans1=ans+lists1[k+1+i]+lists0[k+i]-lists1[i]-lists0[i]
            big=max(ans,ans1)
            ans=ans1
        print(big)
       
        
elif s[0]=="1" and s[-2]=="0":
    # 最後にlists1の方に0を挿入する
    lists1.append(0)

    if k>=len(lists0):
        print(n)
    elif k<len(lists0):
        big=0
        ans=sum(lists1[:k+1])+sum(lists0[:k])
        for i in range(len(lists0)-k):
            ans1=ans+lists1[k+1+i]+lists0[k+i]-lists1[i]-lists0[i]
            big=max(ans,ans1)
            ans=ans1
        print(big)
    
elif s[0]=="0" and s[-2]=="1":
    #さいしょのlist1の方に0を挿入する
    lists1.insert(0,0)

    if k>=len(lists0):
        print(n)
    elif k<len(lists0):
        big=0
        ans=sum(lists1[:k+1])+sum(lists0[:k])
        for i in range(len(lists0)-k):
            ans1=ans+lists1[k+1+i]+lists0[k+i]-lists1[i]-lists0[i]
            big=max(ans,ans1)
            ans=ans1
        print(big)    
elif s[0]=="0" and s[-2]=="0":
    #最初と最後にlist1の方に0を挿入する
    lists1.insert(0,0)
    lists1.append(0)

    if k>=len(lists0):
        print(n)
    elif k<len(lists0):            
        big=0
        ans=sum(lists1[:k+1])+sum(lists0[:k])
        for i in range(len(lists0)-k):
            ans1=ans+lists1[k+1+i]+lists0[k+i]-lists1[i]-lists0[i]
            big=max(ans,ans1)
            ans=ans1
        print(big)

Submission status: Wrong Answer

Solution code:
import numpy as np

N, K = map(int, input().split())
S = [int(x) for x in input()] + [2]

blocks = [] # 同じ数の塊
seq = 1
for i in range(N):
    if S[i+1] == S[i]:
        seq += 1
    else:
        blocks.append(seq)
        seq = 1
answer = 0
l = 2 * K + 1
b_length = len(blocks)
#l = min(b_length-1, l)

start = 0
if S[0] == 0:
    answer = sum(blocks[:2*K])
    start = 1
for i in range(start, max(1, b_length-l+1), 2):
    use = blocks[i:i+l]
    s = sum(use)
    answer = max(answer, s)
print(answer)

Submission status: Wrong Answer

Solution code:
import numpy as np

N, K = map(int, input().split())
S = [int(x) for x in input()] + [2]

blocks = [] # 同じ数の塊
seq = 1
for i in range(N):
    if S[i+1] == S[i]:
        seq += 1
    else:
        blocks.append(seq)
        seq = 1
answer = 0
l = 2 * K + 1
b_length = len(blocks)
l = min(b_length-1, l)

start = 0
if S[0] == 0:
    answer = sum(blocks[:2*K])
    start = 1
for i in range(start, b_length-l+1, 2):
    use = blocks[i:i+l]
    s = sum(use)
    answer = max(answer, s)
print(answer)

Submission status: Wrong Answer

Solution code:
import numpy as np

N, K = map(int, input().split())
S = [int(x) for x in input()] + [2]

blocks = [] # 同じ数の塊
seq = 1
for i in range(N):
    if S[i+1] == S[i]:
        seq += 1
    else:
        blocks.append(seq)
        seq = 1
answer = 0
l = 2 * K + 1
b_length = len(blocks)
l = min(b_length-1, l)

start = 0
if S[0] == 0:
    answer = sum(blocks[:2*K])
    start = 1
for i in range(start, b_length-l, 2):
    use = blocks[i:i+l+1]
    s = sum(use)
    answer = max(answer, s)

print(answer)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
S = list(map(int, input()))
lst = []
cnt = 0
now = 1

for i in range(N):
	if S[i] == now:
		cnt += 1
	else:
		lst.append(cnt)
		cnt = 1
		now = 1 - now
		
lst.append(cnt)

#1 0 1 0 1の形にしたい
if len(lst) % 2 == 0:
	lst.append(0)

ret = 0
for i in range(0, len(lst), 2):
	if i - 2 + 2 * K + 1 > len(lst) - 1: break
	if i == 0:
		total = sum(lst[0 : 2 * K + 1])
	else:
		total -= sum(lst[i - 2 : i])
		total += sum(lst[(i - 2) + 2 * K + 1 : min((i - 2) + 2 * K + 3, len(lst))])
	ret = max(ret, total)
	
print(ret)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
S = list(map(int, input()))
lst = []
cnt = 0
now = 1

for i in range(N):
	if S[i] == now:
		cnt += 1
	else:
		lst.append(cnt)
		cnt = 1
		now = 1 - now
		
lst.append(cnt)
print(lst)

#1 0 1 0 1の形にしたい
if len(lst) % 2 == 0:
	lst.append(0)

ret = 0
for i in range(0, len(lst), 2):
	if i - 2 + 2 * K + 1 > len(lst) - 1: break
	if i == 0:
		total = sum(lst[0 : 2 * K + 1])
	else:
		total -= sum(lst[i - 2 : i])
		total += sum(lst[(i - 2) + 2 * K + 1 : min((i - 2) + 2 * K + 3, len(lst))])
	ret = max(ret, total)
	
print(ret)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
S = list(map(int, input()))
lst = []
cnt = 0
now = 1

for i in range(N):
	if S[i] == now:
		cnt += 1
	else:
		lst.append(cnt)
		cnt = 1
		now = 1 - now
		
lst.append(cnt)

#1 0 1 0 1の形にしたい
if len(lst) % 2 == 0:
	lst.append(0)

ret = 0
for i in range(0, len(lst), 2):
	if i + 2 * K + 3 > len(lst): break
	if i == 0:
		total = sum(lst[0 : 2 * K + 1])
	else:
		total -= sum(lst[i - 2 : i])
		total += sum(lst[i + 2 * K + 1 : i + 2 * K + 3])
	ret = max(ret, total)
	
print(ret)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
S = list(map(int, input()))
lst = []
cnt = 0
now = 1

for i in range(N):
	if S[i] == now:
		cnt += 1
	else:
		lst.append(cnt)
		cnt = 1
		now = 1 - now
		
lst.append(cnt)

#1 0 1 0 1の形にしたい
if len(lst) % 2 == 0:
	lst.append(0)

ret = 0
for i in range(0, len(lst), 2):
	if i == 0:
		total = sum(lst[0 : 2 * K + 1])
	else:
		total -= sum(lst[i - 2 : i])
		total += sum(lst[2 * K + 1 : 2 * K + 3])
	ret = max(ret, total)
	
print(ret)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
S = list(map(int, input()))
lst = []
cnt = 0
now = 1

for i in range(N):
	if S[i] == now:
		cnt += 1
	else:
		lst.append(cnt)
		cnt = 1
		now = 1 - now
		
if not cnt == 0:
	lst.append(cnt)

#1 0 1 0 1の形にしたい
if len(lst) % 2 == 0:
	lst.append(0)

ret = 0
for i in range(0, len(lst), 2):
	if i == 0:
		total = sum(lst[0 : 2 * K + 1])
	else:
		total -= sum(lst[i - 2 : i])
		total += sum(lst[2 * K + 1 : 2 * K + 3])
	ret = max(ret, total)
	
print(ret)

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
s  = input()
l_s =[]
k = 1
for i in range(1,len(s)):
    if s[i] == s[i-1]:
        k += 1
    else:
        l_s.append(k)
        k = 1
l_s.append(k)
if s[-1] == "0":
    l_s.append(0)
if s[0] == "0":
    l_s.insert(0,0)
m = min(K,len(l_s)//2)
ans = sum(l_s[:2*m+1])
if 2*m+2 <= len(l_s):
    for i in range(0,len(l_s)-2*m-2,2):
        if (l_s[i]+l_s[i+1]) < (l_s[2*m+i+1]+l_s[2*m+i+2]):
            ans = sum(l_s[i+2:2*m+i+3])
print(ans)

Submission status: Wrong Answer

Solution code:
from itertools import groupby, accumulate
N, K = map(int, input().split())
S = input()
g_cnt = [len(list(v)) for k, v in groupby(S)]
ans = 1

if S[0]=="0":
    g_cnt = [0] + g_cnt
if S[-1]=="0":
    g_cnt = g_cnt + [0]
    
accum = [0] + list(accumulate(g_cnt))
ans = 1
for i in range(0, len(accum)-2*K, 2):
    ans = max(ans , accum[i+2*K+1]-accum[i])
print(ans)

Submission status: Wrong Answer

Solution code:
from itertools import groupby
N, K = map(int, input().split())
S = input()
g_cnt = [len(list(v)) for k, v in groupby(S)]
ans = 1

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

Submission status: Wrong Answer

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

L = S.replace('01', '0,1')
L = L.replace('10', '1,0')
L = L.split(',')
L = [len(l) for l in L]

# TLE examin
print(0)

Submission status: Wrong Answer

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

L = []
t = S[0]
l = 1

for s in S[1:]:
    if t == s:
        l += 1
    else:
        L.append(l)
        t = s
        l = 1

L.append(l)

# For TLE erxamin
print(0)