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()


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

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

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

if len(values) == 1:
    if values[0] < 0:
        print(abs(values[0]))
    elif K >= 1:
        print(abs(values[0]))
    exit()

ans = 0

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

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

    if i + renzoku > len(values):
        renzoku = len(values) - i

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

    ans = max(ans, temp)

print(ans)

Submission status: Time Limit Exceeded

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

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: 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):
    top = min(len(num_block),i+2*k+1)
    ans = max(ans,sum(num_block[i:top]))
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)

if k > len(zeros):
    k = len(zeros)

scores = []
for i in range(len(zeros) - k + 1):
    scores.append(sum(ones[i:i+k+1]) + sum(zeros[i:i+k]))

print(max(scores))

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 = 0
for i in range(len(a)):
     ans = max(count(a,k,i,p[0]), ans)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import itertools

n, k = map(int, input().split())
S = input()
blocks = [(k, len(list(v))) for k, v in itertools.groupby(S)]
res = 0
for i in range(len(blocks)):
  if blocks[i][0] == '0':
    res = max(res, sum([b[1] for b in blocks[i:i+2*k]]))
  else:
    res = max(res, sum([b[1] for b in blocks[i:i+2*k+1]]))
print(res)

Submission status: Time Limit Exceeded

Solution code:
n,k=map(int,input().split())
s=input()
s_dimlist=[s[i+1]==s[i] for i in range(n-1)]
s_seqcnt=[]
if s[0]=='0':
    s_seqcnt.append(0)
pre=-1
for i in range(n-1):
    if not s_dimlist[i]:
        s_seqcnt.append(i-pre)
        pre=i
s_seqcnt.append(n-1-pre)
if s[n-1]=='0':
    s_seqcnt.append(0)
# print(s_dimlist)
# print(s_seqcnt)
width=2*k+1
length=len(s_seqcnt)
s_sum=[0 for i in range(length//2+length%2)]
for i in range(length//2+length%2):
    num=0
    # for j in range(width):
    #     if 2*i+j>=length:
    #         break
    #     num+=s_seqcnt[2*i+j]
    # s_sum[i]=num
    if 2*i+width-1>=length:
        s_sum[i]=sum(s_seqcnt[2*i:])
        break
    else:
        s_sum[i]=sum(s_seqcnt[2*i:2*i+width])
# print(s_sum)
print(max(s_sum))

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]))
    ans = max(ans,sum(h[i+1: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:
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)

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

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

Submission status: Time Limit Exceeded

Solution code:
N,K = (int(x) for x in input().split())
S = list(input())
size = [1]
memo = [S[0],0]
for i in range(1,N):
    if S[i] == memo[0]:
        size[memo[1]] += 1
    else:
        memo = [S[i],memo[1]+1]
        size += [1]

if S[0] == '0':
    size = [0] + size
if S[-1] == '0':
    size += [0]

if int((len(size)-1)/2) <= K:
    print(N)
else:
    ans = 0
    for i in range(len(size)-(K*2))[::2]:
        a = sum(size[i:i+K*2+1])
        if ans < a:
            ans = a
    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
for i in range(len(lst)):
	if lst[i] < 0:
		if i != 0:
			temp = list(map(abs, lst[i-1:i+2*K]))
		else:
			temp = list(map(abs, lst[i:i+2*K]))
		maximum = max(maximum, sum(temp))
	else:
		temp = list(map(abs, lst[i:2*K+1]))
		maximum = max(maximum, sum(temp))
print(maximum)

Submission status: Time Limit Exceeded

Solution code:
def main():
    n, k = map(int, input().split())
    s = input()
    m = count(s)
    l = [s]
    for i in range(k):
        for a in l:
            nl = []
            for l in range(n):
                for r in range(l+1, n):
                    t = a[:l] + invert(a[l:r]) + a[r:]
                    c = count(t)
                    # print('%d: %s' % (c, t))
                    m = m if m > c else c
                    nl.append(t)
        l = nl
    print(m)

def count(s):
    return len(sorted(s.split('0'))[-1])

def invert(s):
    return ''.join([str(abs(p-1)) for p in map(int, list(s))])

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
n,k = map(int,input().split())	
s=list(input())	
s = np.array(s, dtype = int)
li = np.array([])
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=np.append(li,cnt1)
            cnt1 = 0
        if s[i] == 1 and s[i-1] == 0: 
            li=np.append(li,cnt0)
            cnt0 = 0

if (cnt1 > 0):
    li=np.append(li,cnt1)
if (cnt0 > 0):
    li=np.append(li,cnt0)
pivot = 2 * k 

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

print(int(np.max(anslist)))

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:
# coding: utf-8
# Your code here!

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

s = input()

now = s[0]
tmp = 0

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

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

ans = 0

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

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

print(ans)

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

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

if len(zeros) <= K:
    print(len(S))
else:
    ans = 0
    for i in range(len(zeros) - K+1):
        aaa = sum(zeros[i:i + K]) + sum(ones[i:i + K])+ones[i+K]
        # if (S[-1] != 0 and i == saigo)or (S[0] != 0 and i == 0):
        #aaa += ones[i+K]
        ans = max(aaa, ans)
    print(ans)

Submission status: Time Limit Exceeded

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

first_one = S[0] == '1'
cnts = []
cnt = 1
for i, e in enumerate(S):
    if i == 0:
        pass
    else:
        if e != S[i-1]:
            cnts.append(cnt)
            cnt = 1
        else:
            cnt += 1
cnts.append(cnt)


zero_obj = []
for i, c in enumerate(cnts):
    if first_one:
        if i % 2 == 1:
            left = 0
            if i - 1 >= 0:
                left = cnts[i-1]
            right = 0
            if i + 1 < len(cnts):
                right = cnts[i+1]
            zero_obj.append((left, c, right))
    else:
        if i % 2 == 0:
            left = 0
            if i - 1 >= 0:
                left = cnts[i-1]
            right = 0
            if i + 1 < len(cnts):
                right = cnts[i+1]
            zero_obj.append((left, c, right))

if len(zero_obj) <= K:
    print(len(S))
else:
    m = -1
    for i in range(len(zero_obj) - K + 1):
        tmp = zero_obj[i][0]
        for j in range(K):
            tmp += zero_obj[i+j][1] + zero_obj[i+j][2]
        if tmp > m:
            m = tmp
    print(m)

Submission status: Time Limit Exceeded

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

li = np.array([])
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=np.append(li,cnt1)
            cnt1 = 0
        if s[i] == "1" and s[i-1] == "0": 
            li=np.append(li,cnt0)
            cnt0 = 0

if (cnt1 > 0):
    li=np.append(li,cnt1)
if (cnt0 > 0):
    li=np.append(li,cnt0)
pivot = 2 * k 

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

print(int(np.max(anslist)))

Submission status: Time Limit Exceeded

Solution code:
n,k=map(int,input().split())
s=input()
s_dimlist=[s[i+1]==s[i] for i in range(n-1)]
s_seqcnt=[]
if s[0]=='0':
    s_seqcnt.append(0)
pre=-1
for i in range(n-1):
    if not s_dimlist[i]:
        s_seqcnt.append(i-pre)
        pre=i
s_seqcnt.append(n-1-pre)
if s[n-1]=='0':
    s_seqcnt.append(0)
# print(s_dimlist)
# print(s_seqcnt)
width=2*k+1
length=len(s_seqcnt)
s_sum=[0 for _ in range(length//2+length%2)]
cnt=0
for i in range(length//2+length%2):
    num=0
    for j in range(width):
        if 2*i+j>=length:
            break
        num+=s_seqcnt[2*i+j]
    s_sum[i]=num
    if 2*i+width-1>=length:
        break
# print(s_sum)
print(max(s_sum))

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':

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

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

Submission status: Time Limit Exceeded

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

arr = []
now = S[0]
if now == "1":
    count = 1
else:
    count = -1
for c in S[1:]:
    next = c
    if c == now:
        if now == "1":
            count += 1
        else:
            count -= 1
    else:
        arr.append(count)
        if now == "1":
            count = -1
            now = "0"
        else:
            count = 1
            now = "1"
arr.append(count)

anss = []
Sum = 0
if arr[0] >= 0:
    head = 0
else:
    head = 0
    arr = [0] + arr
while head+2*K <= len(arr):
    tail = head+2*K
    Sum += sum(map(abs, arr[head:tail+1]))
    anss.append(Sum)
    head += 2
    Sum = 0
if anss == []:
    ans = sum(map(abs,arr))
else:
    ans = max(anss)
print(ans)

Submission status: Time Limit Exceeded

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

one_zero_count = []
former = -1

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

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

Submission status: Time Limit Exceeded

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

s = input()

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

    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)
            flag = True
            count = 0

a.append(count+1)

m = 0

m = max(m, sum(a[:k*2]))
m = max(m, sum(a[-1:-k*2-1:-1]))

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

print(m)

Submission status: Time Limit Exceeded

Solution code:
n, k = [int(x) for x in input().split()]
s = input()
l = []
o = []
y = 0
for i, x in enumerate(s):
    l.append(int(x))
    if i == 0:
        if l[i] == 0:
            o.append(-1)
        else:
            o.append(1)
    else:
        if l[i] != l[i - 1]:
            o.append(-1 if l[i] == 0 else 1)
            y += 1
        else:
            o[y] += 1 if l[i] == 1 else -1

m = 0
r = len(o)
for i in range(r):
    s = 0
    p = 0
    for j in range(i, r):
        if o[j] < 0:
            p += 1
            if p > k:
                break
        s += abs(o[j])
    if s > m:
        m = s
print(m)

Submission status: Time Limit Exceeded

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

one_zero_count = []
former = -1

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

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

Submission status: Time Limit Exceeded

Solution code:

def kakaka():
    a = list(map(int, input().split()))
    b = map(int, input().split())
    c = int(input())

def start_process():
    return
def main():
    all_num, inst_num = map(int, input().split())
    data = list(input())

    data_la = []
    ans_0, ans_1 = 0, 0
    for i in range(all_num):
        if data[i] == '1':
            ans_1 += 1
            if ans_0 > 0:
                data_la.append(ans_0)
                ans_0 = 0
        else:
            ans_0 += 1
            if ans_1 > 0:
                data_la.append(ans_1)
                ans_1 = 0
    if ans_1 > 0:
        data_la.append(ans_1)
    elif ans_0 > 0:
        data_la.append(ans_0)

    ans_data = 0
    if data[0] == '1':
        for i in range(len(data_la))[::2]:
            fin = min(len(data_la), i + inst_num * 2 + 1)
            now_count = sum(data_la[i : fin])
            if ans_data < now_count:
                ans_data = now_count
            if fin >= len(data_la):
                break

    if data[0] == '0':
        fin = min(len(data_la), inst_num * 2)
        ans_data = sum(data_la[0: fin])
        for i in range(1, len(data_la))[::2]:
            fin = min(len(data_la), i + inst_num * 2 + 1)
            now_count = sum(data_la[i: fin])
            if ans_data < now_count:
                ans_data = now_count
            if fin >= len(data_la):
                break

    print(ans_data)






if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

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 = []
    for i in range(int((len(Slist)-2*K+1)/2)):
        sumlist.append(sum(Slist[2*i:2*K+1+2*i]))
    print(max(sumlist))

Submission status: Time Limit Exceeded

Solution code:
N, K = map(int,input().split())
S = input()
nowL = 0
if S[0] == '0':
    nowL = 1
L = [0] * N
for i in range(N-1):
    if S[i] == S[i+1]:
        L[nowL] = L[nowL] + 1
    else:
        L[nowL] = L[nowL] + 1
        nowL = nowL + 1
# 終端処理
L[nowL] = L[nowL] + 1

ans = 0
for j in range(N):
    ans = max(ans,sum(L[2*j:2*j+2*K+1]))
    if sum(L[2*j:2*j+2*K+1]) == 0:
        break
        
print(ans)

Submission status: Time Limit Exceeded

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

continuous_list = [0] * N

for i in range(N):
    tmp_K = K
    length = 0
    zero_mode = False  # 連続した0

    # print("i" + str(i))

    for j in range(i, N):
        # print("j" + str(j))

        if S[j] == '1':
            zero_mode = False
            length += 1
        else:
            if zero_mode:
                length += 1
            else:
                if tmp_K > 0:
                    tmp_K -= 1
                    length += 1
                    zero_mode = True
                else:
                    break

    # print("length:" + str(length))
    continuous_list[i] = length


# print(continuous_list)

print(max(continuous_list))

Submission status: Time Limit Exceeded

Solution code:
n,k = map(int,input().split())
s=input()
li=[]
li_z=[]
li_o=[]


    
check="1"
seq=0
for i in s:
    if check!=i:
        check=i
        li.append(seq)
        seq=1
    else:
        seq+=1
        
li.append(seq)

for  i in range(len(li)):
    if i%2==0:
        li_o.append(li[i])
    else:
        li_z.append(li[i])
    
if s[n-1]=="0":
    li_o.append(0)


if len(li_o)<=k:
    print(n)
else:
    m=0
    for i in range(len(li_o)-k):
        tmp=sum(li_o[i:i+k+1])+sum(li_z[i:i+k])
        m=max(tmp,m)
        
    print(m)

Submission status: Time Limit Exceeded

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

kukan = []
cnt0 = 0
cnt1 = 0
for n in range(N):
        if S[n] == '0':
                cnt0 +=1
                if cnt1 > 0:
                        kukan.append(cnt1)
                cnt1 = 0
        if S[n] == '1':
                cnt1 +=1
                if cnt0 > 0:
                        kukan.append(cnt0)
                cnt0 = 0
kukan.append(max(cnt1,cnt0))

#print(kukan)
ans = []

if S[0] == '0':
        start = 1
else:
        start = 0
l = len(kukan)
if l == 1:
        print(N)
        exit()

for i in range(l-K):
        if (i+start)%2 == 0:
                #print(i+2*K)
                ans.append(sum(kukan[i:min(l+1,i+2*K+1)]))
        else:
                #print(i+2*K-1)
                ans.append(sum(kukan[i:min(l+1,i+2*K)]))

print(max(ans))

Submission status: Time Limit Exceeded

Solution code:
n,k=map(int,input().split())
s=input()
s_dimlist=[s[i+1]==s[i] for i in range(n-1)]
s_seqcnt=[]
if s[0]=='0':
    s_seqcnt.append(0)
pre=-1
for i in range(n-1):
    if not s_dimlist[i]:
        s_seqcnt.append(i-pre)
        pre=i
s_seqcnt.append(n-1-pre)
if s[n-1]=='0':
    s_seqcnt.append(0)
# print(s_dimlist)
# print(s_seqcnt)
width=2*k+1
length=len(s_seqcnt)
s_sum=[0 for _ in range(length//2+1)]
cnt=0
for i in range(length):
    if i%2==0:
        num=0
        for j in range(width):
            if i+j>=length:
                break
            num+=s_seqcnt[i+j]
        s_sum[cnt]=num
        cnt+=1
# print(s_sum)
print(max(s_sum))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
n,k=map(int,input().split())
s=input()
s_dimlist=[s[i+1]==s[i] for i in range(n-1)]
s_seqcnt=[]
if s[0]=='0':
    s_seqcnt.append(0)
pre=-1
for i in range(n-1):
    if not s_dimlist[i]:
        s_seqcnt.append(i-pre)
        pre=i
s_seqcnt.append(n-1-pre)
if s[n-1]=='0':
    s_seqcnt.append(0)
# print(s_dimlist)
# print(s_seqcnt)
width=2*k+1
length=len(s_seqcnt)
s_sum=[]
for i in range(length):
    if i%2==0:
        num=0
        for j in range(width):
            if i+j>=length:
                break
            num+=s_seqcnt[i+j]
        s_sum.append(num)
# print(s_sum)
print(max(s_sum))

Submission status: Time Limit Exceeded

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
    
#print(inversion_command(0, 3, S))

s_list = set([S])

for _ in range(K):
    tmp_s_list = set()
    for l in range(N + 1):
        for r in range(l, N + 1):
            for s in s_list:
                tmp_s_list.add(inversion_command(l, r, s))
    s_list = tmp_s_list

ans = 0
for s in s_list:
    s_split = s.split('0')
    s_len = [len(s) for s in s_split]
    s_max = max(s_len)
    #print(s_len)
    #print(s_max)
    ans = max(ans, s_max)
print(ans)

Submission status: Time Limit Exceeded

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

change_points = [i for i in range(n) if i == 0 or s[i-1] != s[i]]
max_ = 0
for i in range(len(change_points)):
    count = 0
    cur_k = 0
    for j in range(i, len(change_points)):
        p = change_points[j]
        if s[p] == '1':
            if j < len(change_points) - 1:
                count += change_points[j+1] - p
            else:
                count += len(s) - p
        else:
            if cur_k < k:
                cur_k += 1
                if j < len(change_points) - 1:
                    count += change_points[j+1] - p
                else:
                    count += len(s) - p
            else:
                break

    if count > max_:
        max_ = count
print(max_)

Submission status: Time Limit Exceeded

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

kukan = []
cnt0 = 0
cnt1 = 0
for n in range(N):
        if S[n] == '0':
                cnt0 +=1
                if cnt1 > 0:
                        kukan.append(cnt1)
                cnt1 = 0
        if S[n] == '1':
                cnt1 +=1
                if cnt0 > 0:
                        kukan.append(cnt0)
                cnt0 = 0
kukan.append(max(cnt1,cnt0))

#print(kukan)
ans = []

if S[0] == '0':
        start = 1
else:
        start = 0
l = len(kukan)
if l == 1:
        print(N)
        exit()

for i in range(l-K):
        if (i+start)%2 == 0:
                #print(i+2*K)
                ans.append(sum(kukan[i:min(l+1,i+2*K+1)]))
        if (i+start)%2 == 1:
                #print(i+2*K-1)
                ans.append(sum(kukan[i:min(l+1,i+2*K)]))

print(max(ans))

Submission status: Time Limit Exceeded

Solution code:
nk = input()
nk = nk.split()
n = int(nk[0])
k = int(nk[1])

s = input()

count = 0
now = None
clist = []

for ss in s:
    if now is None:
        if ss == '0':
            clist.append(0)

    if now is None or now == ss:
        count += 1
    else:
        clist.append(count)
        count = 1
    now = ss

clist.append(count)

max_len = k * 2 + 1
max_sum = 0


for i in range(0, len(clist)):
    if i % 2 == 1:
        continue
    j = i + max_len
    try:
        max_sum = max(max_sum, sum(clist[i:j]))
    except:
        pass

print(max_sum)