Problem:


Score : 400 points

Problem Statement

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

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

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

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

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

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

Constraints

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

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

The length of the string S is N.

Each character of the string S is 0 or 1.

Input

Input is given from Standard Input in the following format:

N K
S

Output

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

Sample Input 1

5 1
00010

Sample Output 1

4

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

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

Sample Input 2

14 2
11101010110011

Sample Output 2

8

Sample Input 3

1 1
1

Sample Output 3

1

No directions are necessary.



Sample input:
5 1
00010


Sample output:
4

Submission status: Wrong Answer

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

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

# リストに入れる
p = input()
num = str(p)
list_num = list(num)
s = [int(i) for i in list_num] 

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

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

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


print(ma)

Submission status: Wrong Answer

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

one = []
cnt = 0
for i, s in enumerate(S):
    if s == '1':
        cnt += 1
        if i == N - 1:
            one.append(cnt)
    elif s == '0':
        if cnt > 0:
            one.append(cnt)
            cnt = 0
            
zero = []
cnt = 0
for i, s in enumerate(S):
    if s == '0':
        cnt += 1
        if i == N - 1:
            zero.append(cnt)
    elif s == '1':
        if cnt > 0:
            zero.append(cnt)
            cnt = 0
            
if len(one) < len(zero):
    one = [0] + one + [0]
    
for i in range(len(one) - 1):
    one[i + 1] = one[i] + one[i + 1]
one = [0] + one
    
for i in range(len(zero) - 1):
    zero[i + 1] = zero[i] + zero[i + 1]
zero = [0] + zero
    
ans = -1
for i in range(len(one) - (K + 1) + 1):
    tmp = one[i + K] - one[i - 1] + zero[i + K - 1] - zero[i - 1]
    ans = max(ans, tmp)
    # print(tmp)
    
if S.count('0') == 0:
    print(len(S))
elif len(zero) - 1 <= K:
    print(len(S))
else:
    print(ans)

Submission status: Wrong Answer

Solution code:
import heapq
class PrioritySet(object):
    def __init__(self):
        self.heap = []
        self.set = set()
    def add(self, pri, d):
        heapq.heappush(self.heap, (pri, d))
        if not d in self.set:
            self.set.add(d)
    def get(self):
        pri, d = heapq.heappop(self.heap)
        self.set.remove(d)
        return (pri, d)
    def empty(self):
        return len(self.heap) == 0

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

zero_potentials = []
zero_chunks = []
one_chunks = []
zero_chunk = 0
one_chunk = 0
for s in S:
    if s == '1':
        one_chunk += 1
        if zero_chunk > 0:
            zero_chunks.append(zero_chunk)
            zero_potential = [0, zero_chunk, 0]
            if len(one_chunks) > 0:
                zero_potential[0] = one_chunks[-1]
            zero_potentials.append(zero_potential)
            zero_chunk = 0
    elif s == '0':
        zero_chunk += 1
        if one_chunk > 0:
            one_chunks.append(one_chunk)
            if len(zero_potentials) > 0:
                zero_potentials[-1][2] = one_chunk
            one_chunk = 0

if zero_chunk > 0:
    zero_chunks.append(zero_chunk)
    zero_potential = [0, zero_chunk, 0]
    if len(one_chunks) > 0:
        zero_potential[0] = one_chunks[-1]
    zero_potentials.append(zero_potential)

if one_chunk > 0:
    if len(zero_potentials) > 0:
        zero_potentials[-1][2] = one_chunk

pset = PrioritySet()
for i in range(len(zero_potentials)):
    pset.add(-sum(zero_potentials[i]), i)

result = len(S)
for k in range(K):
    if pset.empty():
        break
    pri, i = pset.get()

    if i > 0:
        zero_potentials[i-1][2] = -pri
        pset.add(-sum(zero_potentials[i-1]), i-1)
    if i < len(zero_potentials)-1:
        zero_potentials[i+1][0] = -pri
        pset.add(-sum(zero_potentials[i+1]), i+1)
    result = -pri

print(result)

Submission status: Wrong Answer

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

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

print(sl)
cl = []
for i in range(n):
    st = i - k
    ed = i + k + 1
    if (st < 0):
        st = 0
    if (ed > len(sl)-1):
        ed = len(sl)-1
    if (k % 2 == 1):  # kが奇数の時、0の箇所（すなわちiが偶数の箇所）を中心に反転
        if (i % 2 == 0):
            print(st, i, ed, sum(sl[st:ed]))
            cl.append(sum(sl[st:ed]))
    else:  # kが偶数の時、1の箇所（すなわちiが奇数の箇所）を中心に反転
        if (i % 2 == 1):
            print(st, i, ed, sum(sl[st:ed]))
            cl.append(sum(sl[st:ed]))

print(max(cl))

Submission status: Wrong Answer

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

series = []
prev = "#"
for i, c in enumerate(s):
    if prev != c:
        series.append(i) 
        prev = c

ans = 0
if s[0] == "1":
    for i in range(0, len(series) -1 -2*k, 2):
        ans = max(ans, series[i + 2*k + 1] - series[i])
    ans = max(ans, series[min(k*2+1, len(series)-1)])
else:
    for i in range(1, len(series) -1 -2*k, 2):
        ans = max(ans, series[i + 2*k + 1] - series[i])
    ans = max(ans, series[min(k*2, len(series)-1)])
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)

A.append(0)
A.append(0)

#print(A)

val = 0
if S[0] == "0":
	val = sum(A[:2*K])
	currentMax = val
	old = A[0]
	new = sum(A[1+2*K+1:1+2*K+3])
	val += new - old
	if val > currentMax:
		currentMax = val
	for i in range(3, len(A)-2*K, 2):
		old_sum_of_two = sum(A[i:i+2])
		#print("old_sum_of_two", old_sum_of_two)
		sum_of_two = sum(A[i+2*K+1:i+2*K+3])
		#print("sum_of_two", sum_of_two)
		val += sum_of_two - old_sum_of_two
		if val > currentMax:
			currentMax = val
	print(currentMax)
else:
	val = sum(A[:2*K+1])
	currentMax = val
	for i in range(0, len(A)-2*K, 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:
n,k = list(map(int,input().split()))
s = input()
c = [0]
ans = -1
for i in range(1,n):
    if s[i-1] == '0' and s[i] == '1':
        c.append(c[i-1] + 1)
    else:
        if i == n-1 and s[i-1] == '1' and s[i] == '0':
            c.append(c[i-1] + 1)
        else:
            c.append(c[i-1])
right = 0
for left in range(0,n):
    while right < n and c[right] - c[left] <= k:
        right += 1
    ans = max(right - left, ans)
    if right == left: right += 1
    else: pass
print(ans)

Submission status: Wrong Answer

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

if len(s) <= 2:
    zerodig = 1
A.append(len(s))
A.append(len(s))

if s[0] == "0":
    check = 0
else:
    check = 1

width = 2 * K + 1
score = 0

if zerodig <= K:
    print(len(s))
else:
    for i in range(len(A)-width):
        if score <= A[i+width] - A[i] and (i+check) % 2 == 0:
            score = A[i+width] - A[i]
    print(score)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
s=input()
List=[]
i=1
j=1
while i<n:
  if s[i-1]==s[i]:
    j+=1
    i+=1
  else:
    List.append(j)
    i+=1
    j=1
List.append(j)
if s[0]=="0":
  List.insert(0,0)
ans=max(List)
rg=2*k+1
#for i in range(0,len(List)-rg+1,2):
#  m=sum(List[i:i+rg])
#  ans=max(ans,m)
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':
        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
    for kk in range(1, k+1):
        index = 0
        left = zeros[index]
        for i in range(kk+1, len(zeros)):
            right = zeros[i]
            streak = right - 1 - left
            if streak > maximum:
                maximum = streak
            index += 1
            left = zeros[index]

        if maximum == len(s):
            print(maximum)
            quit()

    print(maximum)

Submission status: Wrong Answer

Solution code:
import numpy as np

n, k = np.array(
  [int(i) for i in input().split()])
s = input()

ones = [len(x) for x in 
   s.replace('0',  ' ').split()]
zeros = [len(x) for x in 
   s.replace('1',  ' ').split()]

# 1から始まり1で終わると仮定
if s[0]=='0':
  ones = [0] + ones
if s[-1]=='0':
  ones = ones + [0]
  
for _ in range(k):
  
  if len(zeros)==0:
    break
  
  # 当該0を潰すことで得られる効果
  zeros_new = [
    zeros[i]+ones[i]+ones[i+1]
    for i in range(len(zeros))]

  # 一番大きいものを消す
  idx_del = np.argmax(zeros_new)

  # 更新
  del ones[idx_del:idx_del+2]
  del zeros[idx_del]
  ones.insert(
    idx_del, zeros_new[idx_del])
  
  if len(zeros)==0:
    break
  
print(np.max(ones))

Submission status: Wrong Answer

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

now = "0"
nl = []
for i, s in enumerate(S):
    if s != now:
        now = s
        nl.append(i)

nl_even = nl[0::2]
nl_odd = nl[1::2]
if s[-1] == "1":
    nl_odd.append(len(S))
if s[0] == "0":
    nl_odd.insert(0, 0)
if len(nl_odd)>1:
    x = nl_odd[1]
else:
    x = 1
for i in range(len(nl_even)-k):
    x_ = nl_odd[i+k] - nl_even[i]
    x = max(x, x_)

print(x)

Submission status: Wrong Answer

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

def numcheck(s):
    maxlen = 0
    j = 0
    for i in range(len(s)):
        if i >= len(s):
            return maxlen
        
        if s[i] == "1":
            j = i
            while(j+1 < len(s) and s[j+1] == "1"):
                j+=1
            _len = j-i+1
            if _len >= maxlen:
                maxlen = _len
            j+=1
        else:
            j+=1
            
    return maxlen

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

s = input()
lists = list(s)

zerolists = zerocheck(s)
max_len = 0
print(zerolists)
for i in range(len(zerolists)):
    _lists = copy.copy(lists)
    for j in range(i,i+k):
        if j >= len(zerolists):
            continue
        for l in range(zerolists[j][0],zerolists[j][1]+1):
            _lists[l] = "1"
    _len = numcheck(_lists)
    if _len >= max_len:
        max_len = _len
if numcheck(lists) >= max_len:
    max_len = numcheck(lists)
print(max_len)

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
S=input()
if S[-1]=="0":
    S+="1"
else:
    S+="0"
List=[]
ini=S[0]
now=0
zeronum=0
for i in range(N+1):
    if S[i]==ini:
        now+=1
        continue
    else:
        if ini=="1":
            List.append(now)
        else:
            List.append(now*(-1))
            zeronum+=1
        ini=S[i]
        now=1
if List[0]<0:
    List.insert(0,0)
N=len(List)
sum=0
SUM=0

if zeronum<=K:
    print(N)
    quit()
for i in range(2*K+1):
    if i%2==1:
        sum+=List[i]*(-1)
    else:
        sum+=List[i]
SUM=max(sum,SUM)
for i in range(6):
    List.append(0)
ind=0
for i in range(K+1,N//2+2):
    sum+=-List[ind]+List[ind+1]-List[2*i-1]+List[2*i]
    ind+=2
    SUM=max(sum,SUM)
print(SUM)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
s = input()
 
if s[0]== "1":
  patterns = []
else:
  patterns = [0]

last = "1"
cnt = 1
 
for i, l in enumerate(s):
  if l == last:
    cnt += 1
    if i == len(s) - 1:
      patterns.append(cnt)
  else: # 変化があったとき
    last = l
    patterns.append(cnt)
    cnt = 1
    
max_len = 1


#print(patterns)
#偶数番目が１の人たち
if len(patterns) <= 2 * k + 1:
  print(len(s))
else:
  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:
# -*- coding: utf-8 -*-

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

def array_count(arr):
    arr_target = [[]]
    cnt = 1
    for i in range(1,len(arr)):
        if arr[i] == arr[i-1]:
            cnt += 1
        else:
            arr_target.append([arr[i-1],cnt])
            cnt = 1
    arr_target.append([arr[len(arr)-1],cnt])
    del arr_target[0]
    return arr_target

ss = array_count(s)

if ss[0][0] == 0:
    ss = [[1,0]] + ss

if ss[-1][0] == 1:
    ss.append([0,0])

ss_sum = []
temp = 0
for i in range(len(ss)):
    temp += ss[i][1]
    ss_sum.append(temp)

ans = -1
for i in range(len(ss_sum)):
    temp = 0
    if i % 2 == 0:
        if i + k * 2 < len(ss_sum):
            temp = ss_sum[i+k*2] - ss_sum[i]
    else:
        if i + k * 2 + 1 < len(ss_sum):
            temp = ss_sum[i+k*2+1] -  ss_sum[i]
    ans = max(temp,ans)

if k * 2 + 1 >= len(ss):
    ans = ss_sum[-1]

print(ans)

Submission status: Wrong Answer

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

def inversion_command(l, r, s):
    s_l = s[:l]
    s_c = s[l:r]
    s_r = s[r:]
    
    s_c = s_c.replace('0', 'X')
    s_c = s_c.replace('1', '0')
    s_c = s_c.replace('X', '1')
    return s_l + s_c + s_r

import re

def nchars(s, n):
    """文字列 s に、同じ文字が n 個以上連続している部分文字列を見つける
    """
    assert n > 0
    reg = re.compile("(.)\\1{%d,}" % (n - 1))  # カンマを取ると n 個ちょうどになる
    while True:
        m = reg.search(s)
        if not m:
            break
        yield m.group(0)
        s = s[m.end():]

import queue
q = queue.Queue()
s = S
q.put(list(nchars(s, 1)))

for _ in range(K):
    tmp_q = queue.Queue()
    while not q.empty():
        s = q.get()
        #print(s)
        for i in range(len(s)):
            if '0' in s[i]:
                tmp_q.put(s[:i] + [s[i].replace('0', '1')] + s[i+1:])
    
    q = tmp_q
    
#print(q)

ans = 1
while not q.empty():
    s = q.get()
    s = ''.join(s)
    #print(s)
                
    s_split = s.split('0')
    s_len = [len(s) for s in s_split]
    s_max = max(s_len)
    ans = max(ans, s_max)
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
stdin = sys.stdin
 
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
nn = lambda: list(stdin.readline().split())
ns = lambda: stdin.readline().rstrip()

n,k = na()
s = ns()
ones = []
a = 0
now = '0'

for i in s:
    if i == now:
        a += 1
    else:
        ones.append(a)
        a = 1
        now = i

ones.append(a)

if (len(ones)+1)//2 <= k:
    print(sum(ones))
    exit()
ssu = 0
d = []
for u in ones:
    ssu += u
    d.append(ssu)
d.append(0)
ans = 0
for j in range((len(ones)+1)//2+1-k):
    q = max(0,j*2-1)
    w = 0 if q == 0 else d[q-1]
    ans = max(ans, d[(j+k)*2-1]-w)

print(ans)

Submission status: Wrong Answer

Solution code:
import sys

N, K = map(int, input().strip().split(' '))
S = input().strip()
prev = ''
l = 0
lst = []
for s in S:
    if s == prev:
        l += 1
    else:
        lst += [l]
        l = 1
    prev = s
lst += [l]
lst = lst[1:]
# print(lst)

flip = S[0] == '1'
res = sum(lst[0:2*K+flip])
cur = res
# print(cur)
for i in range(1, len(lst)):
    if flip:
        cur -= lst[i-1]
    else:
        cur += sum(lst[i+2*K:i+2*K+2])
        res = max(cur, res)
        cur -= lst[i]
    # print(lst[i:i+2*K+flip])
    # res = max(res, sum(lst[i:i+2*K+flip]))
    # print(cur)
    flip ^= 1

#print(N, K)
# print(S)
print(res)

Submission status: Wrong Answer

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

center = []
side = [0]

last = '1'
for s in S:
    if s != last:
        if s == '0':
            center.append(0)
        else:
            side.append(0)
    if s == '0':
        center[-1] += 1
    else:
        side[-1] += 1
    last = s
if last == '0':
    side.append(0)

max_ = max(side)

for i in range(K):
    if max_ == N:
        break
    new = []
    for j in range(len(center)):
        new.append(side[j] + center[j] + side[j+1])
    max_ = max(new)
    max_idx = new.index(max_)
    del center[max_idx]
    del side[max_idx]
    side[max_idx] = max_

print(max_)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
s = input()
l = []
now = False
nown = 0

for i in range(n):
    if now ^ (s[i] == '1'):
        nown += 1
        continue
    else:
        now = not now
        l.append(nown)
        nown = 1
l.append(nown)
wa=[l[0]]
for i in range(1, len(l)):
    wa.append(wa[i - 1] + l[i])

# print('wa =',wa)

ans = 0
if len(wa) < 2 * k:
    ans = n
elif wa[0] == 0:
    for i in range(len(wa) - 2 * k):
        if i % 2 == 1:
            tmp=wa[i + k * 2] - wa[i]+ 1
            ans = max(ans, tmp)
        else:
            tmp = wa[i + k * 2] - wa[i]
            ans = max(ans, tmp)

        # print(i,i+k*2,tmp)

else:
    for i in range(len(wa) - 2 * k):
        if i % 2 == 1:
            tmp=wa[i + k * 2] - wa[i]
            ans = max(ans, tmp)
        else:
            tmp = wa[i + k * 2] - wa[i] + 1
            ans = max(ans, tmp)

        # print(i,i+k*2,tmp)

print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import sys
N, K = map(int, input().split())
S = sys.stdin.readline()

check = S[0]
if check == '0':
    Slist = [0]
else:
    Slist = []
count = 1
for i in range(len(S)-1):
    if S[i+1] == check:
        count += 1
    else:
        Slist.append(count)
        check = S[i+1]
        count = 1
Slist.append(count)

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

if K >= int((len(Slist)-1)/2):
    print(sum(Slist))
else:
    sumlist = [sum(Slist[2*i:2*K+1+2*i]) for i in range(int((len(Slist)-2*K+1)/2))]
    print(max(sumlist))

Submission status: Wrong Answer

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


def one_counter(S):
    tmp = 0
    high = 0
    for z in S:
        if z == "1":
            tmp += 1
        else:
            if tmp > high:
                high = tmp
                tmp = 0
            else:
                tmp = 0
    if tmp > high:
        high = tmp
    return high


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


print(one_counter(S))

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
li = [int(i) for i in list(input())]
kukanidx = []
idx = 0
for i in li:
    if i != li[idx-1]:
        kukanidx.append(idx)
    idx += 1
idx = 1
kukanidx.append(n)

kukan = kukanidx.copy()
for i in kukanidx[1:]:
    kukan[idx] = kukanidx[idx]-kukanidx[idx-1]
    idx += 1

anskouho = []
idx = 0
for i in kukan:
    anskouho.append(sum(kukan[idx:idx+(k+3)]))
    idx += 1
    if idx > len(kukan)-(k+3):
        break

print(max(anskouho))

Submission status: Wrong Answer

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

c = []
ci = 1
for i in range(n):
  if s[i] == s[i+1]:
    ci += 1
  else:
    c.append(ci)
    ci = 1

c = c + [0]
ans = 1

if s[0] == "1":
  for i in range(0,len(c)-(2*k+1)+1,2):
    ans = max(ans, sum(c[i:i+2*k+1]))
else:
  c = [0] + c
  for i in range(0,len(c)-(2*k+1)+1,2):
    ans = max(ans, sum(c[i:i+2*k+1]))

print(ans)

Submission status: Wrong Answer

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

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

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

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
S = input()
lt0, lt1 = [], []
cnt0, cnt1 = 0, 0
for i in range(N):
    if i == 0:
        if S[i] == '0':
            cnt0 += 1
        else:
            cnt1 += 1
    elif S[i] == '0':
        if S[i] == S[i-1]:
            cnt0 += 1
        else:
            lt1.append(cnt1)
            cnt0 = 1
            cnt1 = 0
    elif S[i] == '1':
        if S[i] == S[i-1]:
            cnt1 += 1
        else:
            lt0.append(cnt0)
            cnt0 = 0
            cnt1 = 1
if S[N-1] == '0':
    lt0.append(cnt0)
elif S[N-1] == '1':
    lt1.append(cnt1)

n = len(lt0)
mxcnt = 0
flg = int(S[0])
for i in range(n-K+1):
    cnt = sum(lt0[i:i+K]) + sum(lt1[i:i+K+flg])
    mxcnt = max(mxcnt, cnt)
if n == 0:
    mxcnt = N

print(mxcnt)

Submission status: Wrong Answer

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

tans = 0
ans = 0
r = 0
l = 0
t = k

while r<n:
  if (s[r-1]=="1" and s[r]=="0" and t>0):
    t -= 1
  if s[r-1]=="1" and s[r]=="0" and t==0:
    while l==0 or s[l]==s[l-1]:
      l += 1
      tans -= 1
    r += 1
  else:
    tans += 1
    r += 1
  ans = max(ans, tans)

print(ans)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
S = list(str(input()))
list = [int(s) for s in S]
L = []
cnt = 1
for i in range(len(list) - 1):
    if list[i] == list[i + 1]:
        cnt += 1
    else:
        L.append(cnt)
        cnt = 1
L.append(cnt)
ans = 0
length = len (L) - (2 - list[0] - list[N-1])
for i in range((length + 1)//2 - K):
    #print(sum(L[(2 * i):(2 * i) + (K * 2) + 1]))
    if sum(L[(2 * i):(2 * i) + (K * 2) + 1]) > ans:
        ans = sum(L[(2 * i):(2 * i) + (K * 2) + 1])
if length == 1:
    ans = max(ans,L[0])
if list[0] == 0:
    ans = max(ans, sum(L[0:(2 * K)]))
if list[N-1] == 0:
    ans = max(ans, sum(L[-(2 * K):]))

print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import numpy as np


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

# search where 1
arr = np.array([0] + s_arr.tolist() + [0])
diff = np.diff(arr)
heads = np.where(diff == 1)[0]
tails = np.where(diff == -1)[0]
assert heads.size == tails.size

ans = 0
for i, head in enumerate(heads):
    idx = min(i + K, len(tails) - 1)
    tail = tails[idx]
    ans = max(ans, tail - head)

print(ans)

Submission status: Wrong Answer

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

if S[0] == "0":
  List = []
  count = 0
  mark = "0"
  for i in range(N):
    if S[i] == mark:
      count += 1
    else:
      List = List + [count]
      count = 1
      if mark == "0":
        mark = "1"
      else:
        mark = "0"
  List = List + [count]
  l = len(List)
  if l <= 2 * K:
    print(N)
  else:
    List = [0] + List
    ans = sum(List[:2 * K + 1])
    List = List + [0, 0, 0]
    p = ans
    for i in range(2, l - 2 * K + 1, 2):
      p = p - List[i - 2] - List[i - 1] + List[i + 2 * K - 1] + List[i + 2 * K]
      if p > ans:
        ans = p
    print(ans)
    '''
    ans = sum(List[:2 * K])
    List = List + [0, 0, 0]
    p = sum(List[1:2 * K + 1])
    if p > ans:
      ans = p
    for i in range(3, l - 2 * K + 2, 2):
      p = p - List[i - 2] - List[i - 1] + List[i + 2 * K - 1] + List[i + 2 * K]
      if p > ans:
        ans = p
    print(ans)
    '''
    
if S[0] == "1":
  List = []
  count = 0
  mark = "1"
  for i in range(N):
    if S[i] == mark:
      count += 1
    else:
      List = List + [count]
      count = 1
      if mark == "0":
        mark = "1"
      else:
        mark = "0"
  List = List + [count]
  l = len(List)
  if l <= 2 * K + 1:
    print(N)
  else:
    ans = sum(List[:2 * K + 1])
    List = List + [0, 0, 0]
    p = ans
    for i in range(2, l - 2 * K + 1, 2):
      p = p - List[i - 2] - List[i - 1] + List[i + 2 * K - 1] + List[i + 2 * K]
      if p > ans:
        ans = p
    print(ans)

Submission status: Wrong Answer

Solution code:
import sys
N, K = map(int, input().split())
S = sys.stdin.readline()

check = S[0]
if check == '0':
    Slist = [0]
else:
    Slist = []
count = 1
for i in range(len(S)-1):
    if S[i+1] == check:
        count += 1
    else:
        Slist.append(count)
        check = S[i+1]
        count = 1
Slist.append(count)

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

if K >= int((len(Slist)-1)/2):
    print(1)
    print(sum(Slist))
else:
    sumlist = [sum(Slist[2*i:2*K+1+2*i]) for i in range(int((len(Slist)-2*K+1)/2))]
    print(max(sumlist))

Submission status: Wrong Answer

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

ans = 0
flag = 0
mae = S[0]
p = 0
i = 1
tmp = 1
while i < N:
    tmp += 1
    if mae != S[i]:
        if mae == '0':
            p = i
            flag += 1

        else:
            if flag == K:
                ans = max(ans,tmp-1)
                i = p
                flag = 0
                tmp = 1

    if i < N:
        mae = S[i]
    i += 1

if N != 1:
    ans = max(ans,tmp)
else:
    ans = 1

print(ans)