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:
N, K = map(int, input().split())
S = input()

l = []
for i in range(N):
  if i == 0 or S[i - 1] != S[i]:
    l.append(1)
  else:
    l[-1] += 1

if S.count("1") == N or S.count("0") == N:
  print(N)
else:
  ans = 0
  for i in range(len(l)):
    cnt = 0
    if S[0] == "0":
      if i % 2 == 0:
        if i != 0:
          continue
        for j in range(K * 2):
          cnt += l[j]
          if j == len(l) - 1:
            break
      else:
        for j in range(i, i + K * 2 + 1):
          cnt += l[j]
          if j == len(l) - 1:
            break  
    else:
      if i % 2 == 0:
        for j in range(i, i + K * 2 + 1):
          cnt += l[j]
          if j == len(l) - 1:
            break
      else:
        continue

    ans = max(ans, cnt)
  print(ans)

Submission status: Time Limit Exceeded

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 solve(n, k, s):
    s0, s1 = norm(s)

    if len(s1) == 1:
        return(s1[0])

    if len(s0) <= k:
        return(sum(s0) + sum(s1))
    
    al = []
    mv = 0
    for s0_idx in range(0, len(s0) - k + 1):
        v = sum(s0[s0_idx:s0_idx+k]) + sum(s1[s0_idx:s0_idx+k+1])
        if mv < v:
            mv = v

    return mv


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

Submission status: Time Limit Exceeded

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
        past_i = S[i]
        
    else:
        blobs.append(cnt)
        cnt = 1
        past_i = S[i]
    if i == len(S)-1:
        blobs.append(cnt)

if S[-1] == 0:
    blobs.append(0)
sums = []
len_blobs = len(blobs)

# 
for i in range(len(blobs)//2-min(K,len_blobs//2)+1):
    temp_sum = sum(blobs[2*i:2*(i+K)+1])
    sums.append(temp_sum)
    if temp_sum == N:
        break
print(max(sums))

Submission status: Time Limit Exceeded

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

# 奇数番目は1のカタマリの人数　偶数番目は0のカタマリの人数 両端は必ず1のカタマリの人数
now_state, now_count = '1', 0
L = []
for i in range(len(S)):
  if S[i] == now_state:
    now_count += 1
  else:
    L.append(now_count)
    # reset
    now_count = 1
    now_state = '1' if now_state == '0' else '0'
L.append(now_count)
if now_state == '0':
  L.append(0)

#print(L)
# 0の塊をj個ひっくり返す
# j==1のとき101の3個の和の最大
# j==2のとき10101の5個の和の最大
# j==jのとき101...1のj*2+1個の和の最大

def ngram(n, _list):
  return [_list[i:i+n] for i in range(len(_list)-n+1)]

now_max = 0
j = min(int((len(L)-1)/2), K)
tmp = ngram(j*2+1, L)
# 偶数番目だけ
tmp = [sum(tmp[i]) for i in range(len(tmp)) if i%2 == 0]
tmp = max(tmp) if len(tmp) > 0 else 0
now_max = max(tmp, now_max)
print(now_max)

Submission status: Time Limit Exceeded

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

s = input()

a = []
b = []
flag = True
count = 0
m = 0

for i in range(len(s) - 1):
    if i == 0 and s[i] == '1':
        a.append(0)
        flag = False

    if flag:
        if s[i] == s[i+1]:
            count += 1
        else:
            a.append(count+1)

            flag = False
            count = 0
    else:
        if s[i] == s[i+1]:
            count += 1
        else:
            a.append(count+1)
            m = max(m, sum(a[-1:-k*2-2:-1]))

            flag = True
            count = 0

a.append(count+1)

if s[-1] == '0':
    m = max(m, sum(a[-1:-k*2-1:-1]))
else:
    m = max(m, sum(a[-1:-k*2-2:-1]))

print(m)

Submission status: Time Limit Exceeded

Solution code:
n, k = map(int, input().split())
S = input()
c = []
ans = [0] * 10**5
seq = 1
for i in range(1, n):
  if (S[i - 1] == S[i]):
    seq += 1
  else:
    c.append(seq)
    seq = 1
c.append(seq)

if (1 + len(c)//2 <= k):
  print(n)
  exit()

if (S[0] == '0'):
  ans.append(sum(c[:2 * k]))
  c.remove(c[0])

for i in range((n - 1 -2 * k)//2):
  ans[i] = sum(c[2*i:2*i + 2 * k +1])
print(max(ans))

Submission status: Time Limit Exceeded

Solution code:
import math

N,K=map(int,input().split())
S=input()
L=[]
seq=0
prev='2'
for i in S:
  if i == prev:
    seq += 1
  else:
    L.append(seq)
    prev = i
    seq = 1
L.append(seq)
L.append(0)

L_cum = [0]*(len(L)+1)
for i in range(len(L)):
  L_cum[i]=sum(L[0:i+1])

if S[0] == '0':
  num_0 = math.ceil((len(L)-2)/2)
  if num_0<=K:
    ans=N
  else:
    rev=[]
    for i in range(num_0-K+1):
      rev.append(L_cum[2*(K+i)]-L_cum[i*2-1])
    ans=max(rev)
else:
  num_0 = math.floor((len(L)-2)/2)
  if num_0<=K:
    ans=N
  else:
    rev=[]
    for i in range(num_0-K+1):
      rev.append(L_cum[2*(K+i)+1]-L_cum[2*i])
    ans=max(rev)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n, k = map(int, input().split())
s = list(map(int, input()))
a = []
c = 1
m = 0
for i in range(n):
    if s[i] == c:
        m += 1
    else:
        a.append(m)
        c = 1 - c
        m = 1
a.append(m)
if len(a) % 2 == 0:
    a.append(0)
r = len(a) // 2
if k >= r:
    print(n)
else:
    print(max(sum(a[2*i:2*(i+k)+1]) for i in range(r-k+1)))

Submission status: Time Limit Exceeded

Solution code:
n, k=map(int, input().split())
s=list(map(int, list(input())))
renzoku_list=[0]
sum=1
before=s[0]
for i in range(1, n):
    if s[i]==before:
        sum=sum+1
        before=s[i]
    else:
        renzoku_list.append(sum)
        sum=1
        before=s[i]
renzoku_list.append(sum)
renzoku_list.append(0)
result_list=[]
length=len(renzoku_list)

max=0
if s[0]==0 and -(-(length-2)//2)<=k:
    print(n)
elif s[0]==1 and (length-2)//2<=k:
    print(n)
elif s[0]==0:
    for i in range(length-(2*k+1)+1):
        if i%2==0:
            result=0
            for j in range(i, i+2*k+1):
                result=result+renzoku_list[j]
            if max<=result:
                max=result
    print(max)
elif s[0]==1:
    for i in range(length-(2*k+1)+1):
        if i%2==1:
            result=0
            for j in range(i, i+2*k+1):
                result=result+renzoku_list[j]
            if max<=result:
                max=result
    print(max)

Submission status: Time Limit Exceeded

Solution code:
N, K = map(int, input().split())
S = input()
l = []
a = 0
if S[0] == '1':
	a += 1
else:
	l.append(a)
	a += 1
for i in range(1, N, 1):
	if S[i] == S[i - 1]:
		a += 1
	else:
		l.append(a)
		a = 1
l.append(a)
if S[N - 1] == '0':
	l.append(0)
if (len(l) - 1) // 2 + 1 - K < 1:
	print(N)
else:
	m = []
	for k in range((len(l) - 1) // 2 + 1 - K):
		x = l[2 * k]
		for j in range(2 * K):
			x += l[2 * k + j + 1]
		m.append(x)
	print(max(m))

Submission status: Time Limit Exceeded

Solution code:
import copy

data = input().split()
N = int(data[0])
K = int(data[1])

TL = input()
TL = list(TL)
TLlist = []
now = TL[0]
nowlist = now
inx = []

for lop in range(1,len(TL)):
    if not (now == TL[lop]):
        if nowlist[:1] == "0":
            inx.append(len(TLlist))
        TLlist.append(nowlist)
        nowlist = TL[lop]
        now = TL[lop]
        
    else:
        nowlist += now
if nowlist[:1] == "0":
    inx.append(len(TLlist))
TLlist.append(nowlist)

maxS = 0
cnt = 0
fg = 0
tfg = 0
fg2 = False
loplist = copy.deepcopy(TLlist)

for lop3 in range(len(loplist)):
    for lop2 in inx:
        if fg <= tfg:
            loplist[lop2] = loplist[lop2].replace('0', '1')
            cnt += 1
            if cnt == K:
                fg2 = True
                break
        else:
            tfg += 1
    loplist = "".join(loplist).split("0")
    for lop2 in loplist:
        if maxS < len(lop2):
            maxS = len(lop2)

    loplist = copy.deepcopy(TLlist)
    fg += 1
    cnt = 0
    tfg = 0
    if fg2 == False:
        break
    else:
        fg2 = False
print(maxS)

Submission status: Time Limit Exceeded

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

import time

t = time.time()
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)

ans = 0

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

Submission status: Time Limit Exceeded

Solution code:
import sys
import re

def longest(s):
	match = re.finditer(r'1+',s)
	maxlen=0
	pos=-1
	for m in match:
		if maxlen < len(m.group(0)):
			maxlen=len(m.group(0))
			pos=m.start(0)
	return maxlen

def do_flip(s,l,r):
	lst=list(s)
	for i in range(l,r):
		if lst[i]=='1':
			lst[i]='0'
		else:
			lst[i]='1'
	return ''.join(lst)

def do_cycle(niter,s):
	match = re.finditer(r'(0+|1+)',s)
	lengths=[]
	lengths.append(longest(s))
	for m in match:
		x0=m.start(0)
		x1=m.end(0)
		stmp=do_flip(s,x0,x1)
		lengths.append(longest(stmp))
		if niter > 1:
			lengths.append(do_cycle(niter-1,stmp))
	return max(lengths)

tmp=input().split(' ')
n=int(tmp[0])
k=int(tmp[1])
s=input()
maxlen=do_cycle(k,s)
print(maxlen)

Submission status: Time Limit Exceeded

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


def re_cum_list(li):
  count_0 = 0
  count_1 = 0
  re_li = []
  for i in li:
    if i == 0:
      count_0 -= 1
      if count_1 > 0:
        re_li.append(count_1)
        count_1 = 0
    else:
      count_1 += 1
      if count_0 < 0:
        re_li.append(count_0)
        count_0 = 0
  if count_1 > 0:
    re_li.append(count_1)
  if count_0 < 0:
    re_li.append(count_0)
  return re_li


cum_1_s = re_cum_list(s)

sum_list = []
su = 0
count = 0
j = 0
while j <= len(cum_1_s):
  for i in range(j, len(cum_1_s)):
    if cum_1_s[i] > 0:
      pass
    else:
      if count >= k:
        break
      count += 1
    su += abs(cum_1_s[i])
  sum_list.append(su)
  j += 1
  count = 0
  su = 0


print(max(sum_list))

Submission status: Time Limit Exceeded

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
k = 2*K+1
if(a[0] == 1):
	for l in range(0,len(b), 2):
		e  = sum(b[l:l+k])
		if(ans < e):
			ans = e
else:
	for l in range(1,len(b), 2):
		e = sum(b[l:l+k])
		if(ans < e):
			ans = e
	if(sum(b[0:2*K]) > ans):
		ans = sum(b[0:2*K])
if(2*K > len(b)):
	ans = sum(b)
print(ans)

Submission status: Time Limit Exceeded

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

li = []
is1s = False
if s[0] == "1":
    is1s = True
cnt0 = 0
cnt1 = 0
for i in range(len(s)):
    if s[i] == "0" :
        cnt0 += 1
    if s[i] == "1":
        cnt1 += 1
    if i >= 1:
        if s[i] == "0" and s[i-1] == "1":
            li.append(cnt1)
            cnt1 = 0
        if s[i] == "1" and s[i-1] == "0": 
            li.append(cnt0)
            cnt0 = 0

if (cnt1 > 0):
    li.append(cnt1)
if (cnt0 > 0):
    li.append(cnt0)
pivot = 2 * k 
li = np.array(li)
anslist=[]
if(is1s):
    for i in range(len(li)):
        if(i % 2 == 0):
            tmp = np.sum(li[i:min(len(li), i+pivot+1)])
            anslist.append(tmp)
else:
    for i in range(len(li)):
        if(i % 2 == 1):
            tmp = np.sum(li[i:min(len(li), i+pivot+1)])
            anslist.append(tmp)
    anslist.append(sum(li[0:pivot]))

print(max(anslist))

Submission status: Time Limit Exceeded

Solution code:
import copy

data = input().split()
N = int(data[0])
K = int(data[1])

TL = input()
TL = list(TL)
TLlist = []
now = TL[0]
nowlist = now

for lop in range(1,len(TL)):
    if not (now == TL[lop]):
        TLlist.append(nowlist)
        nowlist = TL[lop]
        now = TL[lop]
    else:
        nowlist += now
TLlist.append(nowlist)

maxS = 0
cnt = 0
fg = 0
tfg = 0
fg2 = False
loplist = copy.deepcopy(TLlist)

for lop3 in range(len(loplist)):
    for lop2 in range(len(loplist)):
        if loplist[lop2][:1] == "0":
            if fg <= tfg:
                loplist[lop2] = loplist[lop2].replace('0', '1')
                cnt += 1
                if cnt == K:
                    fg2 = True
                    break
            else:
                tfg += 1
    loplist = "".join(loplist).split("0")
    for lop2 in loplist:
        if maxS < len(lop2):
            maxS = len(lop2)

    loplist = copy.deepcopy(TLlist)
    fg += 1
    cnt = 0
    tfg = 0
    if fg2 == False:
        break
    else:
        fg2 = False

print(maxS)

Submission status: Time Limit Exceeded

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
        past_i = S[i]
        
    else:
        blobs.append(cnt)
        cnt = 1
        past_i = S[i]
    if i == len(S)-1:
        blobs.append(cnt)

if S[-1] == 0:
    blobs.append(0)
sums = []
for i in range(len(blobs)//2-min(K,len(blobs)//2)+1):
    temp_sum = sum(blobs[2*i:min(2*(i+K)+1,len(blobs))])
    sums.append(temp_sum)
    if temp_sum == N:
        break
print(max(sums))

Submission status: Time Limit Exceeded

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

start = 0
rl = []
for i in range(N):
  t = 0
  c = 0
  k = K
  for s in S[i::]:
    if k == 0 and s == 0:
      break
    c+=1
    if(s == 0 and t == 0):
      t = 1
    elif(s == 1 and t == 1):
      k -= 1
      t = 0
  rl.append(c)

print(max(rl))

Submission status: Time Limit Exceeded

Solution code:
import math

N,K=map(int,input().split())
S=input()
L=[]
seq=0
prev='2'
for i in S:
  if i == prev:
    seq += 1
  else:
    L.append(seq)
    prev = i
    seq = 1
L.append(seq)
L.append(0)

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

if S[0] == '0':
  num_0 = math.ceil((len(L)-2)/2)
  if num_0<=K:
    ans=N
  else:
    rev=[]
    for i in range(num_0-K+1):
      rev.append(L_cum[2*(K+i)]-L_cum[i*2-1])
    ans=max(rev)
else:
  num_0 = math.floor((len(L)-2)/2)
  if num_0<=K:
    ans=N
  else:
    rev=[]
    for i in range(num_0-K+1):
      rev.append(L_cum[2*(K+i)+1]-L_cum[2*i])
    ans=max(rev)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N, K = map(int, input().split())
people = list(input())
lst = []
count = 0
flag = people[0]
for i in range(len(people)):
	if i == len(people) - 1:
		lst.append(count + 1) if flag == '1' else lst.append(count * -1 - 1)
	elif people[i] == flag and people[i+1] != flag:
		lst.append(count + 1) if flag == '1' else lst.append(count * -1 - 1)
		count = 0
		flag = people[i + 1]
	else:
		count += 1

maximum = 0
l = len(lst)
for i in range(l):
	if lst[i] < 0:
		end = i+2*K
		if i != 0:
			temp = list(map(abs, lst[i-1:end]))
		else:
			temp = list(map(abs, lst[i:end]))
		maximum = max(maximum, sum(temp))
		if end > l:
			break
	else:
		end = 2*K+1
		temp = list(map(abs, lst[i:end]))
		maximum = max(maximum, sum(temp))
		if end > l:
			break
print(maximum)

Submission status: Time Limit Exceeded

Solution code:
n, k = map(int, input().split())
S = input()
c = []
ans = []
seq = 1
for i in range(1, n):
  if (S[i - 1] == S[i]):
    seq += 1
  else:
    c.append(seq)
    seq = 1
c.append(seq)
if (1 + len(c)//2 <= k):
  print(n)
  exit()
if (S[0] == '0'):
  ans.append(sum(c[:2 * k]))
  c.remove(c[0])

for i in range((n - 1 -2 * k)//2):
  ans.append(sum(c[2*i:2*i + 2 * k +1]))
print(max(ans))

Submission status: Time Limit Exceeded

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

a0 = list(x.split("1"))
a1 = list(x.split("0"))

l0 = [len(a0[i]) for i in range(len(a0)) if len(a0[i]) > 0]
l1 = [len(a1[i]) for i in range(len(a1)) if len(a1[i]) > 0]

if x[0] == "0":
    l1.insert(0,0)
if x[len(x)-1] == "0":
    l1.append(0)

ll = [sum(l1[i:i+N[1]+1]) + sum(l0[i:i+N[1]])  for i in range(len(l1))]

print(max(ll))

Submission status: Time Limit Exceeded

Solution code:
n,k = list(map(int,input().split()))
s = str(input())
num_block = list()
serial_num = 1
ans = 0
for i in range(1,n):
    if s[i] != s[i-1]:
        num_block.append(serial_num)
        serial_num = 0
    serial_num += 1
num_block.append(serial_num)

if s[0] == "0":
    ans = sum(num_block[0:2*k])
    del num_block[0]

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

Submission status: Time Limit Exceeded

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

S=list(input())

d=0

for i in range(N):
    for j in range(i,N):
        s=0
        c=0
        for k in range(i,j+1):
            if S[k]=='0' and s==0:
                c+=1
                s=1
            elif S[k]=='1':
                s=0

            if c>K:
                break
        if d<j+1-i and c<=K:
            d=j+1-i
            
    else:
        continue
    break

    
print(d)

Submission status: Time Limit Exceeded

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

# 奇数番目は1のカタマリの人数　偶数番目は0のカタマリの人数 両端は必ず1のカタマリの人数
now_state, now_count = '1', 0
L = []
for i in range(len(S)):
  if S[i] == now_state:
    now_count += 1
  else:
    L.append(now_count)
    # reset
    now_count = 1
    now_state = '1' if now_state == '0' else '0'
L.append(now_count)
if now_state == '0':
  L.append(0)

#print(L)
# 0の塊をj個ひっくり返す
# j==1のとき101の3個の和の最大
# j==2のとき10101の5個の和の最大
# j==jのとき101...1のj*2+1個の和の最大

def ngram(n, _list):
  return [_list[i:i+n] for i in range(len(_list)-n+1)]

now_max = 0
for j in range(0,K+1):
  tmp = ngram(j*2+1, L)
  # 偶数番目だけ
  tmp = [sum(tmp[i]) for i in range(len(tmp)) if i%2 == 0]
  tmp = max(tmp) if len(tmp) > 0 else 0
  #print(tmp)
  #tmp = [sum(t) for t in tmp]
  #print(tmp)
  #tmp = max(tmp)
  #print(j,tmp)
  now_max = max(tmp, now_max)
print(now_max)

Submission status: Time Limit Exceeded

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)

ans = 0

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

Submission status: Time Limit Exceeded

Solution code:
import copy

data = input().split()
N = int(data[0])
K = int(data[1])

TL = input()
TL = list(TL)
TLlist = []
now = TL[0]
nowlist = now

for lop in range(1,len(TL)):
    if not (now == TL[lop]):
        TLlist.append(nowlist)
        nowlist = TL[lop]
        now = TL[lop]
    else:
        nowlist += now
TLlist.append(nowlist)

maxS = 0
cnt = 0
fg = 0
tfg = 0
fg2 = False
loplist = copy.deepcopy(TLlist)

for lop3 in range(len(loplist)):
    for lop2 in range(len(loplist)):
        if loplist[lop2][:1] == "0":
            if fg <= tfg:
                num = len(loplist[lop2])
                loplist[lop2] = ""
                for lop3 in range( num ):
                    loplist[lop2] += "1"
                cnt += 1
                if cnt == K:
                    fg2 = True
                    break
            else:
                tfg += 1
    loplist = "".join(loplist).split("0")
    for lop2 in loplist:
        if maxS < len(lop2):
            maxS = len(lop2)

    loplist = copy.deepcopy(TLlist)
    fg += 1
    cnt = 0
    tfg = 0
    if fg2 == False:
        break
    else:
        fg2 = False

print(maxS)

Submission status: Time Limit Exceeded

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

longest=0

for bgn_flip in range(len(S)):

    S_copy=S[:]
    zero=False
    icluster=-1
    for i,s in enumerate(S):
        if s==0 and zero==False:
            zero=True
            icluster+=1
        if s==1:
            zero=False

        if s==0 and icluster>=bgn_flip and icluster < bgn_flip+K:
            S_copy[i]=1

    seq=0
    for sc in S_copy:
        if sc==1: seq+=1
        if sc==0: seq=0
        if seq>longest:longest=seq

print(longest)

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin
# import math
# import numpy as np
# N = int(stdin.readline().rstrip().split()[0])
# A = [int(x) for x in stdin.readline().rstrip().split()]
# N, X = [int(x) for x in stdin.readline().rstrip().split()]
# S = stdin.readline().rstrip().split()[0]
N, K = [int(x) for x in stdin.readline().rstrip().split()]
S = stdin.readline().rstrip().split()[0]

B = []
count = 0
oz = 1
for s in S:
  if int(s) == oz:
    count+=1
  else:
    B.append(count)
    count=1
    oz=int(s)
B.append(count)
if oz==0:
  B.append(0)
# print(B)
L = len(B)
if 2*K+1>L:
  K = int(L/2)
# print("K",K)
C = []
for i in range(L):
  start = 2*i
  last = 2*i+2*K+1
  if last>L:
    # print("break",last)
    break
  C.append(sum(B[start:last]))
# print(C)
ans = max(C)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n, k = map(int, input().split())
seq = [bool(int(c)) for c in input()]

ones = []
if seq[0] is False:
    ones.append(0)
zeros = []
state = True
cnt = 0
for i, c in enumerate(seq):
    if i == 0:
        state = c
        cnt = 1
        continue

    if c == state:
        cnt += 1
    else:
        if state:
            ones.append(cnt)
        else:
            zeros.append(cnt)
        cnt = 1
        state = c

if state is True:
    ones.append(cnt)
else:
    ones.append(0)
    zeros.append(cnt)

# print(ones)
# print(zeros)

n_zeros = len(zeros)

k = min(k, n_zeros)

max_score = 0
for i in range(n_zeros - k + 1):
    score = sum(ones[i:i+k+1]) + sum(zeros[i:i+k])
    if max_score < score:
        max_score = score

print(max_score)

Submission status: Time Limit Exceeded

Solution code:
read = input
n,k = map(int, read().split())
a = read()
a = '1' + a + '1'
# -1 if use 0, -1 if use len(a) -1
pstart = 0
pcnt = 0
ptype = '1'
b = []
# (start, end, cnt)
for tp in enumerate(a):
    i, c = tp
    if ptype == c:
        pcnt = pcnt + 1
    else :
        b.append((pstart, i-1, pcnt))
        pstart = i
        ptype = c
        pcnt = 1
b.append((pstart, i, pcnt))
# print(b)
if k >= len(b)//2:
    print(sum(map(lambda tp:tp[2], b)) - 2)
else :
    start = 0
    end = k * 2
    ans = 0
    while(end <= len(b) - 1):
        tmp = sum(map(lambda tp:tp[2], b[start:(end+1)]))
        if b[start][0] == 0:
            tmp = tmp - 1
        if b[end][1] == len(a)-1:
            tmp = tmp -1
#         print(start, end ,tmp)
        ans = max(ans, tmp)
        start = start + 2
        end = end + 2
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
n, k = map(int,input().split())
p = list(input())
a = []
ape = 1
for i in range(1,n):
     if p[i-1] != p[i]:
          a.append(ape)
          ape = 1
     else:
          ape += 1
a.append(ape)

def count(a,k,i,start):#start = p[0],a[i]からスタート
     ans = 0
     if start == '1':
          if i%2 == 0:
               for j in range(2*k+1):
                    if i+j>=len(a):
                         break
                    ans += a[i+j]
          else:
               for j in range(2*k):
                    if i+j>=len(a):
                         break
                    ans += a[i+j]
     else:
          if i%2 == 1:
               for j in range(2*k+1):
                    if i+j>=len(a):
                         break
                    ans += a[i+j]
          else:
               for j in range(2*k):
                    if i+j>=len(a):
                         break
                    ans += a[i+j]
     return ans

ans = count(a,k,0,p[0])
if (p[0] == '1') or (len(a) == 1):
     kaisi = 0
else:
     kaisi = 1
for i in range(kaisi,len(a),2):
     ans = max(count(a,k,i,p[0]), ans)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N,K = map(int,input().split())
s = list(input())
if s[0] == '0':
  s0 = -1
  s1 = 0
else:
  s0 = 0
  s1 = 1
s_ = []
for i in range(1,N):
  if s0 < 0 and s[i]=='1':
    s_.append(s0)
    s0 = 0
    s1 = 1
  elif s1 > 0 and s[i] == '0':
    s_.append(s1)
    s0 = -1
    s1 = 0
  elif s0 <= 0 and s1 == 0 and s[i] == '0':
    s0 -= 1
  elif s1 >= 0 and s0 == 0 and s[i] == '1':
    s1 += 1
if s0 < 0:
  s_.append(s0)
elif s1 > 0:
  s_.append(s1)
ans = 0
for j in range(len(s_)):
  count = 0
  ans_tmp = 0
  for k in range(j,len(s_)):
    if s_[k] < 0:
      count+=1
    ans_tmp+=abs(s_[k])
    if count == K+1:
      ans_tmp-=abs(s_[k])
      break
  if ans < ans_tmp:
    ans = ans_tmp
print(ans)

Submission status: Time Limit Exceeded

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

data = []
cnt = 0
curr = '1'
for i in range(len(s)):
    if s[i] != curr:
        data.append(cnt)
        curr = s[i]
        cnt = 1
    else:
        cnt += 1
data.append(cnt)

ret = sum(data[0:0 + 2*k+1])
for start in range(0, len(data)-k, 2):
    val = sum(data[start:start + 2*k+1])
#     print(start, val)
    if val > ret:
        ret = val
print(ret)

Submission status: Time Limit Exceeded

Solution code:
def main():
    input = sys.stdin.readline
    try:
        n,k = map(int,input().split())
        s = input()
        patternB = r"(?=(" + "1*0*" * k + "1*))"
        import re
        mb = re.findall(patternB,s)
        print(len(max(mb,key=len)))
        
    finally:
        input = None

import sys; sys.setrecursionlimit(50000)
if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

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

cnt = 0
c0 = '1'
for c in s:
    if c == c0:
        cnt += 1
    else:
        x.append(cnt)
        c0 = c
        cnt = 1
else:
    if cnt != 0:
        x.append(cnt)

if s[-1] == '0':
        x.append(0)

if len(x) <= k*2+1:
    print(sum(x))
else:
    y = [sum(x[i:i+k*2+1]) for i in range(0,len(x)-k*2,2)]
    print(max(y))

Submission status: Time Limit Exceeded

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+2*K+1])
		if(ans < e):
			ans = e
else:
	for l in range(1,len(b), 2):
		e = sum(b[l:l+2*K+1])
		if(ans < e):
			ans = e
	if(sum(b[0:2*K]) > ans):
		ans = sum(b[0:2*K])
if(2*K > len(b)):
	ans = sum(b)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n,k=map(int,input().split())
S = input()
binary = []
one = []
zero = []
tmp_z = 0
tmp_o = 0
zeroflag = (S[0]=="0")
saigozero = (S[-1]=="0")
for i,s in enumerate(S):
    if s=="0":
        tmp_z += 1
        if not zeroflag:
            one.append(tmp_o)
            tmp_o=0
            zeroflag=True
    if s =="1":
        tmp_o+=1
        if zeroflag:
            zero.append(tmp_z)
            tmp_z=0
            zeroflag=False
if tmp_z!=0:
    zero.append(tmp_z)
if tmp_o!=0:
    one.append(tmp_o)

zeroflag = (S[0]=="0")
saigozero = (S[-1]=="0")

if zeroflag:
    one = [1] + one
if saigozero:
    one.append(1)

if len(zero)<=k:
    print(len(S))
else:
    ans = 0
    
    for i in range(len(zero)-k + 1):
        if zeroflag and i==0:
            tmp = sum(zero[i:i+k])+sum(one[i:i+k+1]) -1
        elif saigozero and i==len(zero)-k:
            tmp = sum(zero[i:i+k])+sum(one[i:i+k+1]) -1
        else:
            tmp = sum(zero[i:i+k])+sum(one[i:i+k+1])
        ans = max(ans,tmp)
    print(ans)

Submission status: Time Limit Exceeded

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

S = input()
a= S[0]
b = int(S[0])
table = []
c = 0
for s in S:
    if int(s) == b:
        c += 1
    else:
        b = not(b)
        table.append(c)
        c = 1

table.append(c)

if a =='0':
    N = 2*K+1
    ans = sum(table[0:2*K])
    for i in range(1,len(table),2):
        ans = max(ans,sum(table[i:i+N]))

else:
    ans =0
    N = 2*K +1
    for i in range(0,len(table),2):
        ans = max(ans,sum(table[i:i+N]))
#print(table)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
N,K=[int(i) for i in input().split()]
S=input()
linelist=[1]
for i in range(1,len(S)):
	if S[i-1]!=S[i]:
		linelist.append(1)
	else:
		linelist[-1]+=1
c=0
line=np.array(linelist)
if S[0]=="0":
	for i,num in enumerate(line):
		if i%2==0:
			if i+2*K<=len(line):
				c=max(c,np.sum(line[i:i+2*K]))
			else:
				c=max(c,np.sum(line[i:]))
				break
		else:
			if i+2*K+1<=len(line):
				c=max(c,np.sum(line[i:i+2*K+1]))
			else:
				c=max(c,np.sum(line[i:]))
				break
else:
	for i,num in enumerate(line):
		if i%2==1:
			if i+2*K<=len(line):
				c=max(c,np.sum(line[i:i+2*K]))
			else:
				c=max(c,np.sum(line[i:]))
				break
		else:
			if i+2*K+1<=len(line):
				c=max(c,np.sum(line[i:i+2*K+1]))
			else:
				c=max(c,np.sum(line[i:]))
				break
print(c)

Submission status: Time Limit Exceeded

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


def main():
    n, k, s = load_input()

    max_len = 0
    init_pos = 0
    while True:
        next_init = False
        order = k
        current = 1
        prev = s[init_pos]
        if s[init_pos] == '0':
            order -= 1
        for i in range(init_pos + 1, len(s)):
            if prev != s[i]:
                if s[i] == '0':
                    if 0 < order:
                        order -= 1
                    else:
                        if max_len < current:
                            max_len = current
                        break
                else:
                    if next_init is False:
                        next_init = True
                        init_pos = i
            prev = s[i]
            current += 1
        if max_len < current:
            max_len = current
        if next_init is False:
            break
    print(max_len)


if __name__ == '__main__':
    main()