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:
from sys import stdin
from collections import deque, defaultdict as ddic
from itertools import combinations, permutations, product
import bisect
import heapq

rs = lambda: stdin.readline().strip()
ri = lambda: int(rs())
rsl = lambda: stdin.readline().split()
rim = lambda: map(int, rsl())
ril = lambda: list(rim())

n, k = rim()
s = list(rs())
zeros = []
temp = []
instreak = False
for i, char in enumerate(s):
    if char == '0':
        if not instreak:
            zeros.append(i)
            instreak = True
    else:
        instreak = False

# 1111111
if not zeros:
    print(len(s))
else:
    zeros = [-1] + zeros + [len(s)]
    maximum = 0
    index = 0
    left = zeros[index]
    if k == 0:
        # just count 1
        streak, maximum = 0, 0
        for i, char in enumerate(s):
            if char == '1':
                streak += 1
            else:
                if streak > maximum:
                    maximum = streak
                streak = 0

            if i == len(s)-1 and char == '1':
                if streak > maximum:
                    maximum = streak
    else:
        for i in range(k+1, len(zeros)):
            right = zeros[i]
            streak = right - 1 - left
            if streak > maximum:
                maximum = streak
            index += 1
            left = zeros[index]

    print(maximum)

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
s = input()
A = [1]
for i in range(N-1):
    if s[i] != s[i+1]:
        A.append(i+1)
A.append(len(s))

width = 2 * K + 1
score = 0

if len(A) <= width:
    print(len(s))
else:
    for i in range(len(A)-width):
        if score <= A[i+width] - A[i]:
            score = A[i+width] - A[i]
    print(score)

Submission status: Wrong Answer

Solution code:
from collections import deque
n, k = [int(item) for item in input().split()]
s = input() + "#"
deq = deque([])

start0 = False
count = 0
num = 0
series = []
for i, item in enumerate(s):
    if i == 0:
        prev = item
        count += 1
        if item == "0":
            num += 1
            start0 = True
        continue
    if prev == item:
        count += 1
    elif prev == "0" and item == "1":
        deq.append(count)
        count = 1
        prev = item
        series.append(sum(deq))
    elif prev == "1" and item == "0":
        num += 1
        deq.append(count)
        if num > k:
            series.append(sum(deq))
            if not start0:
                deq.popleft()
                start0 = False
            deq.popleft()
        else:
            series.append(sum(deq))
        count = 1
        prev = item
    elif item == "#":
        deq.append(count)
        series.append(sum(deq))

print(max(series))

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
s = input()
ans_0 = []
ans_1 = []
cnt = 0

if s.count("1") == 1:
    print(max(n - 1 - s.index("1"), s.index("1") + 1))
elif len(set(s)) == 1:
    print(n)
else:
    s = list(s)
    for i in range(n):
        if s[i] == "1" and cnt == 0:
            ans_0.append(i)
            cnt += 1
        elif s[i] == "0":
            cnt = 0
    cnt = 0
    for i in range(n):
        if s[n-1 - i] == "1" and cnt == 0:
            ans_1.append(n-1-i)
            cnt += 1
        elif s[n-1 - i] == "0":
            cnt = 0
    ans_1.sort()
    ans_list = []
    if len(ans_0)-1 <= k:
        print(n)
    else:
        for i in range(len(ans_0) - k):
            ans_list.append(ans_1[i+k] - ans_0[i])
        print(max(ans_list) + 1)

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
S = list(map(int,input()))
blobs = []
if S[0] == 0:
    blobs.append(0)
cnt = 0
past_i = 0
for i in range(len(S)):
    if i == 0:
        past_i = S[i]
    if past_i == S[i]:
        cnt += 1
    if i == len(S)-1:
        blobs.append(cnt)
    else:
        blobs.append(cnt)
        cnt = 1
        past_i = S[i]
if S[-1] == 0:
    blobs.append(0)
sums = []
for i in range(max(len(blobs)//2-K+1,1)):
    sums.append(sum(blobs[2*i:2*(i+K+1)]))
print(max(sums))

Submission status: Wrong Answer

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


a = []
b = []
count = 1
pri = s[0]
for i in range(1, n):

    

    if pri==1 and s[i]==1:
        count += 1
    elif pri==1 and s[i]==0:
        a.append(count)
        count = 1
    elif pri==0 and s[i]==1:
        b.append(count)
        count = 1
    elif pri==0 and s[i]==0:
        count += 1

    pri = s[i]

if s[n-1] != s[n-2]:
    if s[n-1]==1:
        a.append(1)
    else:
        b.append(1)
elif s[n-1]==1:
    a.append(count)
else:
    b.append(count)

maxx = 0
for i in range(len(a)):
    temp = sum(a[i:i+k+1]) + sum(b[i:i+k])
    if temp > maxx:
        maxx = temp

print(maxx)

Submission status: Wrong Answer

Solution code:
length, change_cnt = map(int, input().split())
state = input()

reverse_len = 0
reverse_cnt = 0
zero_list = []
zero_cnt = 0
one_list = []
one_cnt = 0
ans_max = 0
for x in state:
    x = int(x)
    if x == 0:
        zero_cnt += 1
        if one_cnt > 0:
            one_list.append(one_cnt)
        one_cnt = 0
    else:
        one_cnt += 1
        if zero_cnt > 0:
            zero_list.append(zero_cnt)
        zero_cnt = 0
    if len(zero_list) <= change_cnt:
        ans_max = max(ans_max, sum(one_list) + sum(zero_list) + one_cnt)
    else:
        zero_list = zero_list[1:]
        one_list = one_list[1:]
        ans_max = max(ans_max, sum(one_list) + sum(zero_list))
print(ans_max)

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Your code here!

n,k = map(int,input().split())

s = input()

now = s[0]
tmp = 0

h = []
if s[0]=="0":
    h.append(0)

for i in s:
    if i != now:
        now = i
        h.append(tmp)
        tmp=0
        
    tmp+=1
h.append(tmp)
h.append(0)
h.append(0)

ans = 0

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

for i in range(1,len(h)-k-1,2):
    ans = max(ans,sum(h[i:i+k*2]))

print(ans)

Submission status: Wrong Answer

Solution code:
import re

def norm(s):
    s1 = list(map(len, re.split('0+', s)))
    s0 = list(map(len, re.split('1+', s)))
    if s0[0] == 0:
        s0.pop(0)
    if s0[-1] == 0:
        s0.pop(-1)
    return [s0,s1]

def next_step(s0, s1):
    nv = []
    for idx in range(len(s1)-1):
        nv.append(s1[idx] + s1[idx+1] + s0[idx])
        
    # print(nv)
    
    max_value = max(nv)
    max_idx = nv.index(max_value)
    # print(max_idx)

    s0.pop(max_idx)
    s1.pop(max_idx)
    s1[max_idx] = max_value

    return([s0,s1])


def solve(n, k, s):
    s0, s1 = norm(s)
    while(1):
        # print(f"s0 {s0}")
        # print(f"s1 {s1}")
        if len(s1) == 1:
            return(s1[0])
        if k == 0:
            return(max(s1))
        next_step(s0, s1)
        k = k - 1


n, k = map(int,input().split())
s = input()
print(solve(n, k, s))

Submission status: Wrong Answer

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

wl = []
wc = 0
prevs = None
for i, _s in enumerate(s):
  if prevs is None:
    wl.append(i)
    prevs = _s
    if _s == '0':
      wc += 1
  elif prevs != _s:
    wl.append(i)
    prevs = _s
    if _s == '0':
      wc += 1
wl.append(n)

if k >= wc:
  print(n)
elif s[0] == '0':
  maxl = max(wl[2*k] - wl[0], wl[-1] - wl[-2*k-1])
  for l, r in zip(wl[1:], wl[2*k+2:-1]):
    if r - l > maxl:
      maxl = r - l
  print(maxl)
else:
  maxl = 0
  for l, r in zip(wl, wl[2*k+1:]):
    if r - l > maxl:
      maxl = r - l
  print(maxl)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
S = [int(i) for i in list(input())]
t = 0
c = 0
for s in S:
  if K == 0:
    break
  c+=1
  if(s == 0 and t == 0):
    t = 1
  elif(s == 1 and t == 1):
    K -= 1
    t = 0
a = 0
for s in S[c::]:
  if(s == 1):
    a+=1
  else:
    break

print(c + a)

Submission status: Wrong Answer

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


l=[]
p="1"
q=0
for i in s:
    if i==p:
        q+=1
    else:
        l.append(q)
        q=1
        if p=="1":
            p="0"
        else:
            p="1"
l.append(q)

r=[0]
def ruiseki(a):
    for i in range(len(a)):
        r.append(r[i]+a[i])
ruiseki(l)
ans=0

if len(r)-(2*k+2)+1<=0:
    print(r[-1])
    import sys
    sys.exit()
    
for i in range(len(r)-(2*k+2)+1):
    if min(len(r)-1,i+2*k+1)%2==1:
        ans=max(ans,r[min(len(r)-1,i+2*k+1)]-r[i])

print(ans)

Submission status: Wrong Answer

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

b = []
c = 0
d = 0
for i in a:
	if(i == 1):
		if(d>0):
			b.append(d)
			d = 0
		c = c + 1
	else:
		if(c>0):
			b.append(c)
			c = 0
		d = d + 1
b.append(max(c,d))

ans = 0
e = 0

if(a[0] == 1):
	for l in range(0,len(b), 2):
		e  = sum(b[l:l+5])
		if(ans < e):
			ans = e
else:
	for l in range(1,len(b), 2):
		e = sum(b[l:l+5])
		if(ans < e):
			ans = e
if(2*K > len(b)):
	ans = sum(b)
print(ans)

Submission status: Wrong Answer

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


status = S[0]
temp = 0
values = []

for i in range(N):
    if S[i] == status:
        temp += 1
    else:
        if status == '1':
            temp *= -1
        values.append(temp)
        status = S[i]
        temp = 1

if status == '1':
    temp *= -1
values.append(temp)

if len(values) / 2 < K:
    ans = 0
    for i in values:
        ans += abs(i)
    print(ans)
    exit()

ans = 0

for i in range(len(values)):
    temp = 0

    renzoku = 0
    if values[i] < 0:
        renzoku = (2 * K + 1)
    else:
        renzoku = 2 * K

    if i + renzoku > len(values):
        break

    for x in range(renzoku):
        temp += abs(values[i + x])

    ans = max(ans, temp)

print(ans)

Submission status: Wrong Answer

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

if S.count("1") == N or S.count("0") == N:
  print(N)
else:
  ans = 0
  last_selected = S[0]
  for i in range(N):
    cnt = 0
    change = 0
    if i != 0 and S[i] == last_selected:
      continue
    last_selected = S[i]
    for j in range(i, N):
      if j != 0 and S[j] == "0" and S[j - 1] == "1":
        change += 1
      if S[0] == "0" and change < K or S[0] == "1" and change <= K:
        cnt += 1
      else:
        break
    ans = max(ans, cnt)
  print(ans)

Submission status: Wrong Answer

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

zero_list = [len(x) for x in s.split("1") if len(x) > 0]
one_list = [len(x) for x in s.split("0") if len(x) > 0]

if k > len(zero_list):
    print(len(s))
else:
    max_num = 0

    if len(zero_list) < len(one_list):
        for i in range(len(zero_list) - k + 1):
            tmp = sum(zero_list[i:i+k]) + sum(one_list[i:i+k+1])
            if tmp > max_num:
                max_num = tmp
    else:
        max_num = sum(zero_list[:k]) + sum(one_list[:k])
        for i in range(len(one_list) - k):
            tmp = sum(zero_list[i+1:i+k+1]) + sum(one_list[i:i+k+1])
            if tmp > max_num:
                max_num = tmp
        tmp = sum(zero_list[-k:]) + sum(one_list[-k:])
        if tmp > max_num:
            max_num = tmp

    print(max_num)

Submission status: Wrong Answer

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

patterns = []

last = "1"
cnt = 0

for i, l in enumerate(s):
  if l == last:
    cnt += 1
    if i+1 == len(s):
      patterns.append(cnt)
  else:
    last = l
    patterns.append(cnt)
    cnt = 1

max_len = 1
#print(patterns)
#偶数番目が１の人たち
for i in range(2*k+1 , len(patterns) + 1, 2):
  length = sum(patterns[i-2*k-1 : i])
  #print(i, length)
  if length >= max_len:
    max_len = length
    
print(max_len)

Submission status: Wrong Answer

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

wl = []
wc = 0
prevs = None
for i, _s in enumerate(s):
  if prevs is None:
    wl.append(i)
    prevs = _s
    if _s == '0':
      wc += 1
  elif prevs != _s:
    wl.append(i)
    prevs = _s
    if _s == '0':
      wc += 1
wl.append(n)

if k >= wc:
  print(n)
elif s[0] == '0':
  maxl = max(wl[2*k] - wl[0], wl[-2*k-1] - wl[-1])
  for l, r in zip(wl[1:], wl[2*k+2:-1]):
    if r - l > maxl:
      maxl = r - l
  print(maxl)
else:
  maxl = 0
  for l, r in zip(wl, wl[2*k+1:]):
    if r - l > maxl:
      maxl = r - l
  print(maxl)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
S = list(input())
left = 0
right = 0
onecnt = 0
zerocnt = 0
zeroflag = False
leftflag = False
ans = 0

while right < N:
    if S[right] == '0' and not zeroflag:
        zerocnt += 1
        zeroflag = True
    elif S[right] == '1' and zeroflag:
        zeroflag = False
        
    if zerocnt > K and zeroflag:
        tmp = right - left
        if tmp > ans:
            ans = tmp
        while True:
            left += 1
            if S[left] == '0' and not leftflag:
                leftflag = True
            elif S[left] == '1' and leftflag:
                leftflag = False
                break
        zerocnt -= 1
    right += 1

if right - left > ans:
    ans = right - left

print(ans)

Submission status: Wrong Answer

Solution code:
from sys import exit
N,K = [int(n) for n in input().split()]
# N = int(input())
# a = [int(input()) for _ in range(N)]
S = str(input())
L = len(S)
if(S[0] == "0"):#偶数Indexがブリッジ
    st0 = True
else:
    st0 = False
tmp = S[0]
lis = []
maxlen = 0
for i in range(1,L):
    if(S[i]!=S[i-1]):
        lis.append(len(tmp))
        maxlen = max(maxlen,len(tmp))
        tmp =S[i]
    else:
        tmp+=S[i]
maxlen = max(maxlen,len(tmp))
lis.append(len(tmp))
# print(maxlen)
# print(lis)
# print(maxlen)
# print(st0)
ans = maxlen
# tmpans = maxlen
L = len(lis)
if(st0):#奇数Indexをうめる時！000101010
    r = 0
    tmpans = 0
    tmpk = 0#うめたかず
    for i in range(0,L,2):
        if(tmpk < K and i+1 < L):#うめられるだけうめてない
            tmpk+=1
            tmpans+=lis[i]+lis[i+1]
        elif i+1 < L:#うめきった
            ans = max(ans,tmpans)
            tmpans-=lis[r]+lis[r+1]
            tmpans+=lis[i]+lis[i+1]
            r+=2
    print(max(ans,tmpans))
else:#偶数Indexをうめる時！11101010110011
    r = 0
    tmpans = lis[0]
    tmpk = 0#うめたかず
    for i in range(1,L,2):
        if(tmpk < K and i+1 < L):#うめられるだけうめてない
            tmpk+=1
            tmpans+=lis[i]+lis[i+1]
        elif i+1 < L:#うめきった
            ans = max(ans,tmpans)
            # print(ans,i)
            tmpans-=lis[r]+lis[r+1]
            tmpans+=lis[i]+lis[i+1]
            r+=2
    print(max(ans,tmpans))
# 14 2
# 11101010110011
# 10 1
# 0000000001

Submission status: Wrong Answer

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

now=S[0]
count = 0

for i in range(N):
    if now == S[i]:
        count += 1
    else:
        counts.append(count)
        now = S[i]
        count=1

counts.append(count)

ans = 0

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

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

a = len(counts)
ans = max(ans, sum(counts[a-2*K:a]))

print(ans)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
S = input()
last_s = "0"
count = 0
A = []
for i, s in enumerate(S):
	if i == 0:
		count += 1
	else:
		if s != last_s:
			A.append(count)
			count = 0
		count += 1
	last_s = s

if count != 0:
	A.append(count)


val = sum(A[:2*K+1])
currentMax = val

for i in range(0, len(A)-2*K-2, 2):
	old_sum_of_two = sum(A[i:i+2])
	sum_of_two = sum(A[i+2*K+1:i+2*K+3])
	val += sum_of_two - old_sum_of_two
	if val > currentMax:
		currentMax = val

print(currentMax)

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Your code here!

n,k = map(int,input().split())

s = input()

now = s[0]
tmp = 0

h = []
if s[0]=="0":
    h.append(0)

for i in s:
    if i != now:
        now = i
        h.append(tmp)
        tmp=0
        
    tmp+=1
h.append(tmp)
h.append(0)
h.append(0)

ans = 0

for i in range(0,len(h)-k-1,2):
    ans = max(ans,sum(h[i:i+k+3]))


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

print(ans)

Submission status: Wrong Answer

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

array = [0]
result = 0
if S[0] == '1':
    flag = False

else:
    flag = True

flag2 = False

for i in range(N):
    if S[i] == '0':
        if not flag:
            flag = True
            array.append(i)
            if len(array) == K * 2 + 2:
                flag2 = True
                result = max(array[-1] - array[0], result)
                del array[0]
                del array[0]
            

    else:
        if flag:
            flag = False
            array.append(i)

result = max(N - array[0], result)

print(result if flag2 else N)

Submission status: Wrong Answer

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

from itertools import chain
from functools import reduce
from collections import OrderedDict, Counter, deque
import operator

IS = lambda: int(input())
IA = lambda: [int(x) for x in input().split()]
IM = lambda N: [IA() for _ in range(N)]

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

l = 0
c = S[0]
con = []
for s in S:
    if c == s:
        l += 1
    else:
        con.append(l)
        l = 1
        c = s
con.append(l)
lc = len(con)
s = S[0] == '1'
if lc <= K * 2 + s:
    print(sum(con))
else:
    if not s:
        con = [0] + con
        lc += 1
    tl = K * 2 + 1
    ini = sum(con[:tl])
    t = False
    ans = ini
    cur = ini
    for l, r in zip(con[:lc - tl], con[tl:]):
        cur -= l
        cur += r
        if t:
            ans = max(ans, cur)
        t = not t
    print(ans)

Submission status: Wrong Answer

Solution code:
import sys
from collections import deque

input = sys.stdin.readline
n, k = map(int, input().split())
s_list = list(map(int, list(input()[:-1])))
new_list = []
tmp = 0
for s in s_list:
    if s == 1:
        if tmp < 0:
            new_list.append(tmp)
            tmp = 1
        else:
            tmp += 1
    else:
        if tmp > 0:
            new_list.append(tmp)
            tmp = -1
        else:
            tmp -= 1
new_list.append(tmp)
q = deque([]) 
new_len = len(new_list)
if new_list[0] < 0:
    flag = 0
else:
    flag = 1
check = 0
cnt = 0
for i in range(new_len):
    if new_list[i] > 0:
        q.append(new_list[i])
    else:
        if check < k:
            q.append(abs(new_list[i]))
            check += 1
        else:
            q.popleft()
            q.popleft()
            q.append(abs(new_list[i]))
    cnt = max(cnt, sum(q))
print(cnt)

Submission status: Wrong Answer

Solution code:
import math

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

S=input()
zerostart=0

if S[0]=='0':
  zerostart=1

continouslist=[]

nowchar=S[0]
nowcon=1
for i in range(1,len(S)):
  if nowchar!=S[i]:
    continouslist.append(nowcon)
    nowcon=1
    nowchar=S[i]
  else:
    nowcon+=1
continouslist.append(nowcon)

if zerostart:
  #print('Zero')
  if K>=math.ceil(len(continouslist)/2):
    print(N)
    exit()
  else:
    now=0
    ans=0
    for i in range(len(continouslist)):
      now+=continouslist[i]
      if K<i and i%2==0:
        now-=continouslist[i-2*K]
        if (i-2*K)!=0:
          now-=continouslist[i-2*K-1]
      #print(now)
      ans=max(now,ans)
  
else:
  #print('ONe')
  if K>=math.floor(len(continouslist)/2):
    print(N)
    exit()
  else:
    now=0
    ans=0
    for i in range(len(continouslist)):
      now+=continouslist[i]
      if K<i-1 and i%2==1:
        now-=continouslist[i-2*K]
        now-=continouslist[i-2*K-1]
      #print(now)
      ans=max(now,ans)
#print(continouslist)
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
from sys import exit
N,K = [int(n) for n in input().split()]
# N = int(input())
# a = [int(input()) for _ in range(N)]
S = str(input())
L = len(S)
if(S[0] == "0"):#偶数Indexがブリッジ
    st0 = True
else:
    st0 = False
tmp = S[0]
lis = []
maxlen = 0
for i in range(1,L):
    if(S[i]!=S[i-1]):
        lis.append(len(tmp))
        maxlen = max(maxlen,len(tmp))
        tmp =S[i]
    else:
        tmp+=S[i]
lis.append(len(tmp))
# print(maxlen)
# print(lis)
# print(maxlen)
# print(st0)
ans = maxlen
# tmpans = maxlen
L = len(lis)
if(st0):#奇数Indexをうめる時！000101010
    r = 0
    tmpans = 0
    tmpk = 0#うめたかず
    for i in range(0,L,2):
        if(tmpk < K and i+1 < L):#うめられるだけうめてない
            tmpk+=1
            tmpans+=lis[i]+lis[i+1]
        elif i+1 < L:#うめきった
            ans = max(ans,tmpans)
            tmpans-=lis[r]+lis[r+1]
            tmpans+=lis[i]+lis[i+1]
            r+=2
    print(max(ans,tmpans))
else:#偶数Indexをうめる時！11101010110011
    r = 0
    tmpans = lis[0]
    tmpk = 0#うめたかず
    for i in range(1,L,2):
        if(tmpk < K and i+1 < L):#うめられるだけうめてない
            tmpk+=1
            tmpans+=lis[i]+lis[i+1]
        elif i+1 < L:#うめきった
            ans = max(ans,tmpans)
            # print(ans,i)
            tmpans-=lis[r]+lis[r+1]
            tmpans+=lis[i]+lis[i+1]
            r+=2
    print(max(ans,tmpans))
# 14 2
# 11101010110011
# 10 1
# 0000000001

Submission status: Wrong Answer

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

prev = S[0]
if prev == "0":
  fst_is_0 = True
else:
  fst_is_0 = False

cnt = 0
cnt_0 = 0

cnt_lst = []
for idx, s in enumerate(S):
  if s == prev:
    cnt += 1
  else:
    cnt_lst.append(cnt)
    cnt = 1
    if s == "0":
      cnt_0 += 1
    prev = s
else:
  cnt_lst.append(cnt)
#print(cnt_lst)


if cnt_0 <= K:
  print(len(S))
else:
  l = [sum(cnt_lst[idx*2:2*idx+2*K+1]) for idx in range( int( (len(cnt_lst)+1)/2) -K)   ]
  #print(l)
  ss = []
  for idx,sss in enumerate(l):
    if idx == 0:
      ss.append(sss-1)
    elif idx == len(l)-1:
      
      ss.append(sss-1)
    else:
      ss.append(sss)
      
  print(max(ss))

Submission status: Wrong Answer

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

now=S[0]
count = 0

for i in range(N):
    if now == S[i]:
        count += 1
    else:
        counts.append(count)
        now = S[i]
        count=1

counts.append(count)

ans = 0

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

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

a = len(counts)
ans = max(ans, sum(counts[a-2*K-1:a]))

print(ans)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline

def main():
    n, k = [int(i) for i in input().split()]
    s = input() + "3"

    start = 1
    s0 = []
    s1 = []
    cnt = 0
    for i in range(n+1):
        if start == 1 and s[i] != "1":
            s1.append(cnt)
            cnt = 1
            start = 0
        elif start == 0 and s[i] != "0":
            s0.append(cnt)
            cnt = 1
            start = 1
        else:
            cnt += 1

    ans = max(s1)
    for i in range(len(s0) - k + 1 ):
        ans = max(ans, sum(s1[i:i+k+1]) + sum(s0[i:i+k]))
        #if len(s0) < len(s1) and i+k == len(s0) - 1:
        #    ans = max(ans, sum(s1[i:i + k + 1]) + sum(s0[i:i + k]) + s1[i+k+1])

    print(ans)

    #print(s1, s0)

main()

Submission status: Wrong Answer

Solution code:
import numpy as np
N, K = map(int, input().split())
S = input()

one_zero_count = []
former = -1

count = 1
for idx, s in enumerate(S):
    s = int(s)
    if former < 0:
        former = s
        if s == 0:
            one_zero_count.append(0)
      
    else:
        if former == s:
            count += 1
        else:
            former = s
            one_zero_count.append(count)
            count = 1
    
    if idx == len(S) - 1:
        one_zero_count.append(count)
        if s == 0:
            one_zero_count.append(0)
        
one_zero_count = np.asarray(one_zero_count)
        
seq_len = K*2 + 1
ans = 0
for idx in range(len(one_zero_count) - seq_len+1):
    if idx %2 == 0:
        idx_list = [idx+i for i in range(seq_len)]
        seq_sum = sum(one_zero_count[idx_list])
        if ans < seq_sum:
            ans = seq_sum
    else:
        continue

if all([int(s) for s in S]):
    print(len(S))
else:
    print(ans)

Submission status: Wrong Answer

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

c_1 = [0]
c_0 = [0]
count = 0
before = '1'
answer = 0



for i in S:
    if i == before:
        count += 1
    else:
        if before == '1':
            c_1.append(c_1[-1] + count)
        else:
            c_0.append(c_0[-1] + count)
        if i == '0':
            if K+1 < len(c_1):
                answer = max(answer, c_1[-1] - c_1[-(K+2)] + c_0[-1] - c_0[-(K+1)])
            else:
                answer = max(answer, c_1[-1] + c_0[-1])
        count = 1
        before = i

if i == '1':
    c_1.append(c_1[-1] + count)
else:
    c_0.append(c_0[-1] + count)

if K + 1 < len(c_1):
    answer = max(answer, c_1[-1] - c_1[-(K + 2)] + c_0[-1] - c_0[-(K + 1)])
else:
    answer = max(answer, c_1[-1] + c_0[-1])


print(answer)

Submission status: Wrong Answer

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

if S.count("1") == N or S.count("0") == N:
  print(N)
else:
  ans = 0
  for i in range(N):
    cnt = 0
    change = 0
    for j in range(i, N):
      if j != 0 and S[j] == "0" and S[j - 1] == "1":
        change += 1
      if S[0] == "0" and change < K or S[0] == "1" and change <= K:
        cnt += 1
      else:
        break
    ans = max(ans, cnt)
  print(ans)

Submission status: Wrong Answer

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

dp = [0] * (n + 1)
dp[0] = [0, 0]
for i in range(1, n + 1):
    if s[i - 1]:
        dp[i] = [dp[i - 1][0], dp[i - 1][1] + 1]
    elif i == 1 or s[i - 1 - 1]:
        dp[i] = [dp[i - 1][0] + 1, dp[i - 1][1] + 1]
    else:
        dp[i] = [dp[i - 1][0], dp[i - 1][1] + 1]

K = [0] * max((n + 1), k + 1)
for x in dp:
    K[x[0]] = max(K[x[0]], x[1])

# print(K, dp, dp[-1][0])
ans = 0

for i in range(n + 1):
    if dp[i][0] + k > dp[-1][0]:
        ans = max(ans, K[dp[-1][0]] - dp[i][1])
    else:
        ans = max(ans, K[dp[i][0] + k] - dp[i][1])

print(ans)

Submission status: Wrong Answer

Solution code:
from sys import stdin
from collections import deque, defaultdict as ddic
from itertools import combinations, permutations, product
import bisect
import heapq

rs = lambda: stdin.readline().strip()
ri = lambda: int(rs())
rsl = lambda: stdin.readline().split()
rim = lambda: map(int, rsl())
ril = lambda: list(rim())

n, k = rim()
s = list(rs())
zeros = []
temp = []
instreak = False
for i, char in enumerate(s):
    if char == '0' and not instreak:
        zeros.append(i)
        instreak = True
    else:
        instreak = False

if not zeros:
    print(len(s))
else:
    zeros = [-1] + zeros + [len(s)]
    maximum = 0
    index = 0
    left = zeros[index]
    for i in range(k+1, len(zeros)):
        right = zeros[i]
        streak = right - 1 - left
        if streak > maximum:
            maximum = streak
        index += 1
        left = zeros[index]

    print(maximum)

Submission status: Wrong Answer

Solution code:
# coding: utf-8

import sys

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

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

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

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

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

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

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
S = input()
S
split_list = [0]
section_list = [0]
for i in range(1, N):
    if S[i-1] != S[i]:
        section_list.append(i - split_list[-1])
        split_list.append(i)
split_list.append(N)
section_list.append(split_list[-1] - split_list[-2])
del section_list[0]
answer = 0
if len(section_list) <= 2 * K + 1:
    answer = N
else:
    total = sum(section_list[:2 * K + 1])
    for i in range(len(section_list) - (2 * K + 1)):
        total = total - section_list[i] + section_list[i+2*K+1]
        if total >= answer:
            answer = total
print(answer)