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: Time Limit Exceeded

Solution code:
def main():
    N, K = map(int, input().split())
    S = input()
    nums = []
    now = 1
    cnt = 0
    for s in S:
        if int(s) == now:
            cnt += 1
        else:
            nums.append(cnt)
            now ^= 1
            cnt = 1
    if cnt > 0:
        nums.append(cnt)
    if len(nums) % 2 == 0:
        nums.append(0)
    add = 2 * K + 1
    ans = 0
    for cnt in range(0, len(nums), 2):
        tmp = 0
        left = cnt
        right = min(cnt + add, len(nums))
        for j in range(left, right):
            tmp += nums[j]
        ans = max(tmp, ans)
    print(ans)


main()

Submission status: Time Limit Exceeded

Solution code:
N, K = map(int, input().split())
S = input()
list = [1,]
list_head = S[0]
prev = S[0]
count = 0
for i in S[1:]:
    if prev == i:
        list[count] += 1
    else:
        count += 1
        list.append(1)
        prev = i
ans = 0
if list_head == "0":
    for i in range(len(list)):
        END = i+2*K if i+2*K<len(list) else len(list)
        if i%2==0:
            score = sum(list[i:END])
        else:
            score = sum(list[i:END+1])
        if score>ans: ans=score
else:
    for i in range(len(list)):
        END = i+2*K if i+2*K<len(list) else len(list)
        if i%2==1:
            score = sum(list[i:END])
        else:
            score = sum(list[i:END+1])
        if score>ans: ans=score
print(ans)

Submission status: Time Limit Exceeded

Solution code:
#D
import math

N,K = (int(x) for x in input().split())
S = input()

list1 = []
temp = S[0]
count = 1

for i in range(len(S)-1):
    if temp == S[i+1]:
        count += 1
    else:
        list1.append(count)
        temp = S[i+1]
        count = 1
else:
    list1.append(count)

#print(list1) 

maxc = 0
if S[0] == "0":
    for i in range(math.ceil(N)):
        minl = 2*i-1
        maxl = 2*(i+K-1)+2
        
        if minl<0:
            minl = 0
        if maxl > N:
            maxl = N
        
        count = sum(list1[minl : maxl])
        if maxc < count:
            maxc = count
            
    #print(maxc)
else:
    for i in range(math.floor(N)):
        minl = 2*i
        maxl = 2*(i+K)+1
        
        if minl<0:
            minl = 0
        if maxl > N:
            maxl = N
            
        count = sum(list1[minl : maxl])
        if maxc < count:
            maxc = count
         
    
print(maxc)

Submission status: Time Limit Exceeded

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

Nums = []
now = 1#今見てる数
cnt = 0#nowがいくつ並んでるか

for i in range(0,n):
    if s[i] == str(now):
        cnt += 1
    else:
        Nums.append(cnt)
        now = 1 - now#０と１の切り替え
        cnt = 1#新しいのをカウントし始める
if cnt != 0:Nums.append(cnt)

#長さが２の倍数であるならば任意の数を最後に加える．ながさを奇数倍にしたい
if len(Nums) % 2 == 0:Nums.append(0)

add = 2 * k + 1 #ひっくり返せる範囲を指定
ans = 0
for i in range(0,len(Nums),2):
    tmp = 0
    left = i#(左端)
    right = min(i + add,len(Nums))#みる長さの最大値を調節（右端）

    for j in range(left,right):
        tmp += Nums[j]

    ans = max(tmp,ans)

print(ans)

Submission status: Time Limit Exceeded

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

if S[N-1] == '1':
    S += '0'
else:
    S += '1'

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

m = 0
l = []
if S[0] == '1':
    for i in range(len(memo)):
        if i%2 == 0:
            s = sum(memo[i:i+2*K+1])
            l.append(s)
        else:
            s = sum(memo[i:i+2*K])
            l.append(s)



        # if s > m:
        #     m = s
else:
    for i in range(len(memo)):
        if i%2 == 1:
            s = sum(memo[i:i+2*K+1])
            l.append(s)
        else:
            s = sum(memo[i:i+2*K])
            l.append(s)


        # if s > m:
        #     m = s

print(max(l))

Submission status: Time Limit Exceeded

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

if S[N-1] == '1':
    S += '0'
else:
    S += '1'

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

max = 0

if S[0] == '1':
    for i in range(len(memo)):
        if i%2 == 0:
            s = sum(memo[i:i+2*K+1])
        else:
            s = sum(memo[i:i+2*K])


        if s > max:
            max = s
else:
    for i in range(len(memo)):
        if i%2 == 1:
            s = sum(memo[i:i+2*K+1])
        else:
            s = sum(memo[i:i+2*K])


        if s > max:
            max = s

print(max)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
N,K = list(map(int,input().split()))
A = input()
A = list(A)
A = list(map(int,A))
l = [0]
s = []
for i in range(N-1):
    if A[i] != A[i+1]:
        l.append(i+1)
        s.append(l[len(l)-1]-l[len(l)-2])
l.append(N)
s.append(l[len(l)-1]-l[len(l)-2])
a = 0
s = np.array(s)
if A[0]==0:
    for i in range(len(s)):
        if i%2==0:
            a = max(a,sum(s[i:i+2*K]))
        else :
            a = max(a,sum(s[i:i+2*K+1]))
else:
    for i in range(len(s)):
        if i%2==0:
            a = max(a,sum(s[i:i+2*K+1]))
        else:
            a = max(a,sum(s[i:i+2*K]))
print(a)

Submission status: Time Limit Exceeded

Solution code:
N,K = list(map(int,input().split()))
A = input()
A = list(A)
A = list(map(int,A))
l = [0]
s = []
for i in range(N-1):
    if A[i] != A[i+1]:
        l.append(i+1)
        s.append(l[len(l)-1]-l[len(l)-2])
l.append(N)
s.append(l[len(l)-1]-l[len(l)-2])
a = 0
if A[0]==0:
    for i in range(len(s)):
        if i%2==0:
            a = max(a,sum(s[i:i+2*K]))
        else :
            a = max(a,sum(s[i:i+2*K+1]))
else:
    for i in range(len(s)):
        if i%2==0:
            a = max(a,sum(s[i:i+2*K+1]))
        else:
            a = max(a,sum(s[i:i+2*K]))
print(a)

Submission status: Time Limit Exceeded

Solution code:
n, k = map(int, input().split())
s = list(input())
s = list((map(int, s)))
tipS = []
cnt = 1
ans = 0

for i in range(n-1):
    if s[i] == s[i+1]:
        cnt += 1
    else:
        tipS.append([s[i], cnt])
        cnt = 1
tipS.append([s[-1], cnt])

for i in range(len(tipS)):
    tmp = tipS[i][1]
    if tipS[i][0] == 0:
        cnt_tmp = 1
    else:
        cnt_tmp = 0

    for j in range(i + 1,len(tipS)):

        if tipS[j][0] == 1:
            tmp += tipS[j][1]

        else:
            if cnt_tmp < k:
                cnt_tmp += 1
                tmp += tipS[j][1]
            else:
                break
    ans = max(ans, tmp)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
N,K = list(map(int,input().split()))
A = input()
A = list(A)
A = list(map(int,A))
l = [0]
s = []
for i in range(N-1):
    if A[i] != A[i+1]:
        l.append(i+1)
        s.append(l[len(l)-1]-l[len(l)-2])
l.append(N)
s.append(l[len(l)-1]-l[len(l)-2])
a = 0
for i in range(len(s)):
    if A[0]==0:
        if i%2==0:
            a = max(a,sum(s[i:i+2*K]))
        else:
            a = max(a,sum(s[i:i+2*K+1]))
    else:
        if i%2==0:
            a = max(a,sum(s[i:i+2*K+1]))
        else:
            a = max(a,sum(s[i:i+2*K]))
print(a)

Submission status: Time Limit Exceeded

Solution code:
n,k=map(int,input().split())
s=input()
a=[]
tmp=1
for i in range(len(s)-1):
	if s[i] == s[i+1]:
		tmp+=1
	else:
		a.append(tmp)
		tmp=1
a.append(tmp)
if s[0]=='0':
	m=2*k
else:
	m=2*k+1
t=4*k+1
_max=a[0]
for i in range(len(a)):
	tmp=sum(a[i:i+m])
	if tmp>_max:
		_max=tmp
	m=t-m
print(_max)

Submission status: Time Limit Exceeded

Solution code:
n,k=map(int,input().split())
s=input()
a=[]
tmp=1
for i in range(len(s)-1):
	if s[i] == s[i+1]:
		tmp+=1
	else:
		a.append(tmp)
		tmp=1
a.append(tmp)

b=[]
b.append(a[0])
if s[0]=='0':
	m=2*k
else:
	m=2*k+1
t=4*k+1
for i in range(len(a)):
	b.append(sum(a[i:i+m]))
	m=t-m
print(max(b))

Submission status: Time Limit Exceeded

Solution code:
from itertools import groupby

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

indexes = []
counts = []
for key, group in groupby(S):
    indexes.append(int(key))
    counts.append(len(list(group)))

candidates = []
for i in range(max(1, len(indexes) - 2 * K + 1)):
    if indexes[i] == 0:
        if len(indexes) > i + 2 * K:
            candidates.append(sum(counts[i: (i + 2 * K)]))
        else:
            candidates.append(sum(counts[i:]))
    elif indexes[i] == 1:
        if len(indexes) > i + 2 * K + 1:
            candidates.append(sum(counts[i: (i + 2 * K + 1)]))
        else:
            candidates.append(sum(counts[i:]))
print(max(candidates))

Submission status: Time Limit Exceeded

Solution code:
input_array = [int(i) for i in input().split()]
S = str(input())

N = int(input_array[0])
K = int(input_array[1])

S_array = list(S)

S_count_array = [0]

count = 0
for i in range(len(S_array)):
  if i == 0:
    S_count_array[count] = 1
  else:
    if S_array[i] == S_array[i-1]:
      S_count_array[count] += 1
    else:
      S_count_array.append(1)
      count += 1

max_sum_number = 0
if S_array[0] == "0":
  for i in range(len(S_count_array)):
    if i % 2 == 0:
      sum_number = sum(S_count_array[i:i+(K*2)])
      if max_sum_number < sum_number:
        max_sum_number = sum_number
    else:
      sum_number = sum(S_count_array[i:i+(K*2)+1])
      if max_sum_number < sum_number:
        max_sum_number = sum_number

else:
  for i in range(len(S_count_array)):
    if (i+1) % 2 == 0:
      sum_number = sum(S_count_array[i:i+(K*2)])
      if max_sum_number < sum_number:
        max_sum_number = sum_number
    else:
      sum_number = sum(S_count_array[i:i+(K*2)+1])
      if max_sum_number < sum_number:
        max_sum_number = sum_number

print(max_sum_number)

Submission status: Time Limit Exceeded

Solution code:
input_array = [int(i) for i in input().split()]
S = str(input())

N = int(input_array[0])
K = int(input_array[1])

S_array = list(S)

S_count_array = [0]

count = 0
for i in range(len(S_array)):
  if i == 0:
    S_count_array[count] = 1
  else:
    if S_array[i] == S_array[i-1]:
      S_count_array[count] += 1
    else:
      S_count_array.append(1)
      count += 1

sum_s_number = []
if S_array[0] == "0":
  for i in range(len(S_count_array)):
    if (i+1) % 2 == 0:
      sum_s_number.append(sum(S_count_array[i:i+(K*2)+1]))
    else:
      sum_s_number.append(sum(S_count_array[i:i+(K*2)]))

else:
  for i in range(len(S_count_array)):
    if (i+1) % 2 == 0:
      sum_s_number.append(sum(S_count_array[i:i+(K*2)]))
    else:
      sum_s_number.append(sum(S_count_array[i:i+(K*2)+1]))

print(max(sum_s_number))

Submission status: Time Limit Exceeded

Solution code:
n,k = map(int, input().split())
s = input()
cont_list = []
counter = 0
ans = 0
if s[0] == "0":
    cont_list.append(0)
    counter = 1
else:
    counter += 1
for i in range(1,n+1):
    if i == n:
        cont_list.append(counter)
    elif s[i] != s[i-1]:
        cont_list.append(counter)
        counter = 1
    else:
        counter += 1
if s[n-1] == "0":
    cont_list.append(0)
if n == 1:
    ans = 1
else:
    ans = 0
for i in range(n):
    if (i - k)% 2 == 0 & (i - k) >= 0:
        if ans < sum(cont_list[i-k:i+k+1]):
            ans = sum(cont_list[i-k:i+k+1])
print(ans)

Submission status: Time Limit Exceeded

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

one = list(map(len, s.replace('0', ' ').split()))
zero = list(map(len, s.replace('1', ' ').split()))
if s[0] == '0':
    one.insert(0, 0)
if s[-1] == '0':
    one.append(0)
    
ans = 0
for i in range(len(zero)):
    length = sum(one[i:i+k+1]) + sum(zero[i:i+k])
    ans = max(ans, length)
    
    if i+k > len(zero):
        break
    
if s.count('1') == len(s):
    print(len(s))
else:
    print(ans)

Submission status: Time Limit Exceeded

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

one = list(map(len, s.replace('0', ' ').split()))
zero = list(map(len, s.replace('1', ' ').split()))
if s[0] == '0':
    one.insert(0, 0)
if s[-1] == '0':
    one.append(0)
    
ans = 0
for i in range(len(zero)):
    length = sum(one[i:i+k+1]) + sum(zero[i:i+k])
    ans = max(ans, length)

if s.count('1') == len(s):
    print(len(s))
else:
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
# encoding:utf-8
import copy
import numpy as np
import random
import bisect #bisect_left　これで二部探索の大小検索が行える
import math


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

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

if cnt!=0:
    Nums.append(cnt)

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

#累積和
ans = 0
Add = 2*K+1
for i in range(N):
    if i%2==0:
        left = i
        right = i+Add
        tmp = sum(Nums[left:right])
        ans = max(tmp,ans)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
_, K = map(int, input().split())
S = input() + '2'

current_length = 1
sections = []  # (start_i, end_i)
current_section = (S[0], 1)

max_length = 0

k = 0


def display_section(sections, current_section):
    a = sections[:]
    b = current_section[:]
    a += b
    s = ''
    for section in a:
        s += section[0] * section[1]
    return s


for i in range(1, len(S)):
    t = display_section(sections, [current_section, (S[i], 1)])
    # print(t)
    if S[i - 1] == S[i]:
        current_section = (S[i], current_section[1] + 1)
        current_length += 1
    else:
        if current_section[0] == '0':
            k += 1
            while k > K:
                if sections[0][0] == '0':
                    k -= 1
                current_length -= sections[0][1]
                sections.pop(0)
        sections.append(current_section)

        if current_length > max_length:
            max_length = current_length

        current_section = (S[i], 1)
        current_length += 1

print(max_length)

Submission status: Time Limit Exceeded

Solution code:
import copy

def main():
  a, reverse_cnt = map(int, input().split(" "))
  h_list = input()
  h_list_henkei = []
  tmpstate = 0
  tmpcnt = 0
  for h in h_list:
    if int(h) == tmpstate:
      tmpcnt = tmpcnt + 1
    else:
      h_list_henkei.append({"state":tmpstate, "cnt":tmpcnt})
      tmpstate = int(h)
      tmpcnt = 1
  h_list_henkei.append({"state":tmpstate, "cnt":tmpcnt})
  del h_list

  max_sum_length_first_is_one = 1 + reverse_cnt * 2
  max_sum_length_first_is_zero = reverse_cnt * 2

  max_cnt = 0

  for i in range(len(h_list_henkei)):
    tmp_cnt = 0
    if h_list_henkei[i]["state"] == 1:
      tmp_cnt = get_max(h_list_henkei, i, max_sum_length_first_is_one)
    elif i == 0:
      tmp_cnt = get_max(h_list_henkei, i, max_sum_length_first_is_zero)
    if max_cnt < tmp_cnt:
      max_cnt = tmp_cnt
  print (max_cnt)  

def get_max(lst, now, max_len):
  tmp = 0
  for i in range(max_len):
    tmp = tmp + get_cnt(lst, now + i)
  return tmp

def get_cnt(lst, index):
  try:
    _l = lst[index]
  except:
    _l = False
  if _l:
    return _l["cnt"]
  else:
    return 0

main()

Submission status: Time Limit Exceeded

Solution code:
import copy

def main():
  a, reverse_cnt = map(int, input().split(" "))
  h_list = input()
  h_list_henkei = []
  template = {"state":0, "cnt":0}
  tmp = copy.copy(template)
  for h in h_list:
    if int(h) == tmp["state"]:
      tmp["cnt"] = tmp["cnt"] + 1
    else:
      h_list_henkei.append(tmp)
      tmp = copy.copy(template)
      tmp["state"] = int(h)
      tmp["cnt"] = 1
  h_list_henkei.append(tmp)

  max_sum_length_first_is_one = 1 + reverse_cnt * 2
  max_sum_length_first_is_zero = reverse_cnt * 2

  max_cnt = 0

  for i in range(len(h_list_henkei)):
    tmp_cnt = 0
    if h_list_henkei[i]["state"] == 1:
      tmp_cnt = get_max(h_list_henkei, i, max_sum_length_first_is_one)
    elif i == 0:
      tmp_cnt = get_max(h_list_henkei, i, max_sum_length_first_is_zero)
    if max_cnt < tmp_cnt:
      max_cnt = tmp_cnt
  print (max_cnt)  

def get_max(lst, now, max_len):
  tmp = 0
  for i in range(max_len):
    tmp = tmp + get_cnt(lst, now + i)
  return tmp

def get_cnt(lst, index):
  try:
    _l = lst[index]
  except:
    _l = False
  if _l:
    return _l["cnt"]
  else:
    return 0

main()

Submission status: Time Limit Exceeded

Solution code:
import copy

def main():
  a, reverse_cnt = map(int, input().split(" "))
  h_list = input()
  h_list_henkei = []
  template = {"state":0, "cnt":0}
  tmp = copy.copy(template)
  for h in h_list:
    if int(h) == tmp["state"]:
      tmp["cnt"] = tmp["cnt"] + 1
    else:
      h_list_henkei.append(tmp)
      tmp = copy.copy(template)
      tmp["state"] = int(h)
      tmp["cnt"] = 1
  h_list_henkei.append(tmp)

  max_sum_length_first_is_one = 1 + reverse_cnt * 2
  max_sum_length_first_is_zero = reverse_cnt * 2

  max_cnt = 0

  for i in range(len(h_list_henkei)):
    tmp_cnt = 0
    if h_list_henkei[i]["state"] == 0:
      tmp_cnt = get_max(h_list_henkei, i, max_sum_length_first_is_zero)
    else:
      tmp_cnt = get_max(h_list_henkei, i, max_sum_length_first_is_one)
    if max_cnt < tmp_cnt:
      max_cnt = tmp_cnt
  print (max_cnt)  

def get_max(lst, now, max_len):
  tmp = 0
  for i in range(max_len):
    tmp = tmp + get_cnt(lst, now + i)
  return tmp

def get_cnt(lst, index):
  try:
    _l = lst[index]
  except:
    _l = False
  if _l:
    return _l["cnt"]
  else:
    return 0

main()

Submission status: Time Limit Exceeded

Solution code:
N,K=map(int,input().split())
S=[] #Sは文字列のリスト

for i in input():
  S.append(int(i))
  
S_data=[]#１と０の塊の数
cnt=0
bf=S[0]
for i in S:
  if i==bf:
    cnt+=1
  else:
    S_data.append(cnt)
    cnt=1
    bf=i
S_data.append(cnt)

fd=[]#答えのリスト

if len(S_data)%2==0:
  if S[0]==0:
    S_data.insert(0,0)
   
  else:
    S_data.append(0)
else:
  if S[0]==0:
    S_data.append(0)
    S_data.insert(0,0)
    
if (len(S_data)-1)//2<=K:
  print(sum(S_data))
  
else:
  for i in range(0,(len(S_data)-2*K+1)//2):
    SS=0
    for j in range(2*i,2*i+2*K+1):
      SS=SS+S_data[j]
    fd.append(SS)
  print(max(fd))

Submission status: Time Limit Exceeded

Solution code:
"""
map(int,input().split())

"""
N,K = map(int,input().split())
S = input()
s = []
for i in range(0,len(S)):
    s.append(S[i])


Nums = []
now = 1
cnt = 0
for i in range(0,N):
    if(s[i] == str(now)):
        cnt+=1
    else:
        Nums.append(cnt)
        now = 1-now
        cnt = 1

if cnt!=0:
    Nums.append(cnt)

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

Add = 2*K + 1
ans = 0
for i in range(0,len(Nums),2):
    tmp = 0
    left = i
    right = min(i + Add , len(Nums))

    for j in range(left,right):
        tmp += Nums[j]
    ans = max(tmp,ans)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
N,K=map(int,input().split())
S=[] #Sは文字列のリスト

for i in input():
  S.append(int(i))
  
S_data=[]#１と０の塊の数
cnt=0
bf=S[0]
for i in S:
  if i==bf:
    cnt+=1
  else:
    S_data.append(cnt)
    cnt=1
    bf=i
S_data.append(cnt)

fd=[]#答えのリスト



if len(S_data)%2==0:
  if S[0]==0:
    S_data.insert(0,0)
   
  else:
    S_data.append(0)
    
else:
  if S[0]==0:
    S_data.append(0)
    S_data.insert(0,0)
    


if (len(S_data)-1)//2<=K:
  print(sum(S_data))
  
else:
  for i in range(0,(len(S_data)-2*K+1)//2):
    SS=0
    for j in range(2*i,2*i+2*K+1):
      SS=SS+S_data[j]
    fd.append(SS)
  print(max(fd))

Submission status: Time Limit Exceeded

Solution code:
n,k=map(int,input().split())
a=input()
l=len(a)
src=[int(a[i:i+1]) for i in range(l)]
from itertools import groupby
y =[sum(1 for e in it) for _, it in groupby(src, key=lambda x: x==1)]
m=len(y)
if src[0]==1 and src[l-1]==1:
  if (m-1)/2<=k:
    print(int(n))
  else:
    c=0
    for i in range(m-2*k+1):
      if i%2==0:
        d=y[i:i+2*k+1]
        f=sum(d)
        if f>=c:
          c=f
    print(c)
if src[0]==1 and src[l-1]==0:
  if m/2<=k:
    print(int(n))
  else:
    c=0
    for i in range(m-2*k+1):
      if i%2==0:
        d=y[i:i+2*k+1]
        f=sum(d)
        if f>=c:
          c=f
    print(c)
if src[0]==0 and src[l-1]==1:
  if m/2<=k:
    print(int(n))
  else:
    c=0
    for i in range(m-2*k+1):
      if i%2==1:
        d=y[i:i+2*k+1]
        f=sum(d)
      elif i==0:
        d=y[0:2*k]
        f=sum(d)
      if f>=c:
        c=f
    print(c)
if src[0]==0 and src[l-1]==0:
  if (m+1)/2<=k:
    print(int(n))
  else:
    c=0
    for i in range(m-2*k+1):
      if i%2==1:
        d=y[i:i+2*k+1]
        f=sum(d)
      elif i==0:
        d=y[0:2*k]
        f=sum(d)
      if f>=c:
        c=f
    print(c)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

n, k = [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()]

if s[0]=='0':
  ones = [0] + ones
if s[-1]=='0':
  ones = ones + [0]

if k >= len(zeros):
  result = n
else: 
  result = [
    np.sum(ones[i:i+k+1]) + np.sum(zeros[i:i+k])
    for i in range(len(zeros)-k+1)]
  result = max(result)

print(result)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

n, k = [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()]

if s[0]=='0':
  ones = [0] + ones
if s[-1]=='0':
  ones = ones + [0]

if k >= len(zeros):
  result = n
else: 
  result = [
    np.sum(ones[i:i+k+1]) + np.sum(zeros[i:i+k])
    for i in range(len(zeros)) if i+k+1 <= len(ones)]
  result = max(result)

print(result)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
import re
import itertools

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

pattern = r"0+"
iterator = re.finditer(pattern ,S)
l = [match.span() for match in iterator]

if len(l) <= K:
    ans = N
else:
    ans = 0
    for v in itertools.permutations(l, K):
        s = S
        for i in range(K):
            add = "1"*(v[i][1]-v[i][0])
            s = s[:v[i][0]] + add + s[v[i][1]:]
            splt = re.split(pattern, s)
            a = [len(splt[i]) for i in range(len(splt))]
            if max(a) >= ans:
                ans = max(a)
print(ans)

Submission status: Time Limit Exceeded

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

n_sum = []

ct = 1
for i in range(N-1):
    if S[i+1] == S[i]:
        ct += 1
    else:
        n_sum.append(ct)
        ct += 1

n_sum.append(ct)

if len(n_sum) == 1:
    if S[0] == '0':
        for i in range(2*K+1):
            n_sum.append(ct)
            n_sum.insert(0, 0)
    else:
        for i in range(2*K):
            n_sum.append(0)
            n_sum.insert(0, 0)

else:
    if S[-1] == '0':
        n_sum.append(ct)
    if S[0] == '0':
        n_sum.insert(0, 0)

if K < len(n_sum)//2:
    Max = -1
    i = 0
    while i+2*K < len(n_sum):
        if i == 0:
            tmp = n_sum[i+2*K]
        else:
            tmp = n_sum[i+2*K] - n_sum[i-1]
        i += 2
        Max = max(Max, tmp)
else:
    Max = ct

print(Max)

Submission status: Time Limit Exceeded

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

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

if S[0] == '0':
	ans = max([sum(L[i:i+K*2]) for i in range(len(S)) if i % 2 == 0]
    	      + [sum(L[i:i+K*2+1]) for i in range(len(S)) if i % 2 == 1])
elif S[0] == '1':
	ans = max([sum(L[i:i+K*2]) for i in range(len(S)) if i % 2 == 1]
    	      + [sum(L[i:i+K*2+1]) for i in range(len(S)) if i % 2 == 0])

print(ans)

Submission status: Time Limit Exceeded

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

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

C.append((c, s))

ans = 0
for i in range(len(C)):
    if C[i][1] == '0':
        ans = max(ans, sum(map(lambda x: x[0], C[i: i+2*K])))
    else:
        ans = max(ans, sum(map(lambda x: x[0], C[i: i+2*K+1])))
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N,K=map(int,input().split())
S=list(map(int,input()))
def saka(x):
    if x==0:return 1
    else:return 0
allS=[S]
def sousa(x):
    for l in range(1,len(S)+1):
        for r in range(l,len(S)+1):
            temp=allS[x][0:l-1]+list(map(saka,allS[x][l-1:r]))+allS[x][r:]
            allS.append(temp)
for i in range(K):
    for j in range(len(allS)):
        sousa(j)
maxlength=1
for j in allS:
    tempcnt=0
    for m in range(N):
        if j[m]==1: 
            tempcnt+=1
            maxlength=max(maxlength,tempcnt)
        else: 
            maxlength=max(maxlength,tempcnt)
            tempcnt=0
print(maxlength)

Submission status: Time Limit Exceeded

Solution code:
N, K = map(int, input().split())
S = input()
Nums = []
now = 1 # 今見ている数
cnt = 0 # nowがいくつ並んでいるか
for i in range(N):
    if S[i] == str(now):
        cnt += 1
    else:
        Nums.append(cnt)
        now = 1 - now   # 0と1を切り替えるときの計算 now ^= 1
        cnt = 1
if cnt != 0:
    Nums.append(cnt)
# 1-0-1-0-1-0-1 って感じの配列が欲しい
# 1-0-1-0-1-0 みたいに0で終わっていたら適当に1つ足す
if len(Nums) % 2 == 0:
    Nums.append(0)

Add = 2 * K + 1
ans = 0
# 1-0-1... の1から始めるので、偶数番目だけ見る
for i in range(0, len(Nums), 2):
    tmp = 0
    left = i
    right = min(i + Add, len(Nums))
    for j in range(left, right):
        tmp += Nums[j]
    ans = max(tmp, ans)

print(ans)

Submission status: Time Limit Exceeded

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

DP = [0]
ans = 0

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

for i in range(K+1):
    l=0
    while(1+2*i+l<len(DP)):
        ans = max(DP[2*i+1+l]-DP[l],ans)
        l+=2

if(not '0' in S):
    ans = len(S)
elif(not '1' in S):
    ans = len(S)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8
# Your code here!
N, K = [int(_) for _ in input().split()]
S = str(input())

Nums = []
now, cnt = 1, 0
for i in range(N):
    if S[i] == str(now): cnt += 1
    else:
        Nums.append(cnt)
        now = 1 - now
        cnt = 1
if cnt != 0: Nums.append(cnt)
# print(Nums)

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

add = 2 * K + 1
ans = 0
for i in range(0, len(Nums), 2):
    tmp = 0
    left = i
    right = min(i+add, len(Nums))
    j = left
    while j < right:
        tmp += Nums[j]
        j += 1
    ans = max(tmp, ans)
print(ans)

Submission status: Time Limit Exceeded

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

S = input()
n = list(map(int, str(S)))
count = 1
mas = []
for i in range(N-1):
    if n[i] == n[i + 1]:
        count += 1
        if i == N-2:
            mas.append(count)
    else:
        mas.append(count)
        count = 1
        if i == N-2:
            mas.append(1)
        

mas2 = []
if n[0] == 0:
    mas.insert(0, 0)
    
if n[N-1] == 0:
    mas.append(0)
    
l=len(mas)
mas2=[0]
for i in range(1, 1+len(mas)):
    mas2.append(sum(mas[:i]))
    
if K >= (l-1)/2:
    print(N)
else:
    mas3 = []
    k = int(((l-1)/2)-K+1)
    for i in range(k):
        mas3.append(mas2[K*2 + 2*i+1]-mas2[0 + 2*i])
    print(max(mas3))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

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

init = s[0]
nw_letter=s[0]
lst = []
temp_lst = []
num=0

#境界部処理
for i in range (n):
    if s[i] == nw_letter:
        num += 1
    else:
        nw_letter = s[i]
        lst.append(num)
        num = 1
    if i == n-1:
        lst.append(num)

lst_num=len(lst)

if init == '1':
    if lst_num //2 <= k:
        print (n)
    elif lst_num % 2 == 1:
        for i in range (lst_num //2):
            temp_lst.append(sum(lst[2*i:2*i+2*k+1]))
        print (max(temp_lst))
    else:
        for i in range (lst_num //2 -1):
            temp_lst.append(sum(lst[2*i:2*i+2*k+1]))
        temp_lst.append(sum(lst[lst_num-2*k:lst_num]))
        print (max(temp_lst))
else:
    if np.ceil(lst_num/2) <= k:
        print (n)
    elif lst_num % 2== 1:
        temp_lst.append(sum(lst[0:2*k]))
        for i in range (1, int(np.ceil(lst_num/2))):
            temp_lst.append(sum(lst[2*i-1:2*i+2*k]))
        temp_lst.append(sum(lst[lst_num-2*k:lst_num]))
        print (max(temp_lst))
    else:
        temp_lst.append(sum(lst[0:2*k]))
        for i in range (1, int(np.ceil(lst_num/2))):
            temp_lst.append(sum(lst[2*i-1:2*i+2*k]))
        print (max(temp_lst))

Submission status: Time Limit Exceeded

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

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

add = 2*K+1
ans = 0
for i in range(len(Nums))[::2]:
    tmp = 0
    left = i
    right = min(i + add, len(Nums))
    for j in range(left, right):
        tmp += Nums[j]
    ans = max(tmp, ans)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
import re
import itertools

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

pattern = r"0+"
iterator = re.finditer(pattern ,S)
l = [match.span() for match in iterator]

if len(l) <= K:
    ans = N
else:
    ans = 0
    for v in itertools.permutations(l, K):
        s = S
        for i in range(K):
            add = "1"*(v[i][1]-v[i][0])
            s = s[:v[i][0]] + add + s[v[i][1]:]
            splt = re.split(pattern, s)
            a = [len(splt[i]) for i in range(len(splt))]
            if max(a) >= ans:
                ans = max(a)
print(ans)