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 = list(map(int,input()))
tmp = 1
nums = []
cont = 0
for s in S:
    if int(s) == tmp:
        cont += 1
    else:
        nums.append(cont)
        cont = 1
        tmp = 1 - tmp
if cont != 0:
    nums.append(cont)
if len(nums) % 2 == 0:
    nums.append(0)
ans = 0
lst = []
for i in range(0,len(nums),2):
    for j in range(i,min(i+K*2+1,len(nums))):
        ans += nums[j]
    lst.append(ans)
    ans = 0
print(max(lst))

Submission status: Time Limit Exceeded

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

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

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

S3 = [S2[0][1]]
K2 = min((len(S2)-1)//2,K)
for i in range(0,len(S2)-2*K2,2):
    SUM = 0
    for j in range(2*K2 + 1):
        SUM += S2[i+j][1]
    S3.append(SUM)
print(max(S3))

Submission status: Time Limit Exceeded

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

if s[0]=='1':
    l = [1]
else:
    l = [0,1]

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

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

print(ans)

Submission status: Time Limit Exceeded

Solution code:
if __name__ == "__main__":
    n, k = map(int, input().split())
    s = input()
    res = 0
    b = []
    count = 0
    temp = 0
    isStand = False
    for ss in s:
        if ss == "0":
            if not isStand:
                isStand = True
                b.append(temp)
                temp = 1
            else:
                temp += 1
        else:
            if isStand:
                isStand = False
                b.append(temp)
                temp = 1
            else:
                temp += 1
    b.append(temp)
    if len(b) < 2*k:
        res = sum(b)
    else:
        res2 = [sum(b[i:2*k+1+i]) for i in range(0,len(b)-1,2)]
        res = max(res2)
    print(res)

Submission status: Time Limit Exceeded

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

if __name__ == "__main__":
    n, k = map(int, input().split())
    s = input()
    res = 0
    b = []
    count = 0
    temp = 0
    isStand = False
    for ss in s:
        if ss == "0":
            if isStand:
                temp += 1
            else:
                isStand = True
                b.append(temp)
                temp = 1
        else:
            if isStand:
                isStand = False
                b.append(temp)
                temp = 1
            else:
                temp += 1
                pass
    b.append(temp)
    if len(b) < 2*k:
        res = sum(b)
    else:
        res = sum(b[:2*k+1])
        for i in range(2,len(b),+2):
            res2 = sum(b[i:2*k+1+i])
            if res2 > res:
                res = res2
    print(res)

Submission status: Time Limit Exceeded

Solution code:
import re
n,k=map(int, input().split())
s=input()
i=[[m.start(),m.end()] for m in re.finditer("1(?=0)|0(?=1)",s)]
i.insert(0,[0,0]);i.append([len(s)-1,0])
ss=[i[j+1][0]-i[j][1]+1 for j in range(len(i)-1)]
if s[0]=="0": ss.insert(0,0);ss.append(0)
res=[sum(ss[2*m:2*(m+k)+1]) for m in range(max((len(ss)-1)//2+1-k,1))]
print(max(res))

Submission status: Time Limit Exceeded

Solution code:
import re
n,k=map(int, input().split())
s=input()
i=[[m.start(),m.end()] for m in re.finditer("1(?=0)|0(?=1)",s)]
i.insert(0,[0,0]);i.append([len(s)-1,0])
ss=[i[j+1][0]-i[j][1]+1 for j in range(len(i)-1)]
is1ststr1 = True if s[0]=="1" else False
if not is1ststr1: ss.insert(0,0);ss.append(0)
res=[sum(ss[2*m:2*(m+k)+1]) for m in range(len(ss)//2+1)]
print(max(res))

Submission status: Time Limit Exceeded

Solution code:
from bisect import bisect_left, bisect_right
from collections import Counter, defaultdict, deque, OrderedDict
from copy import deepcopy
from fractions import gcd
from functools import lru_cache, reduce
from math import ceil, floor
from sys import setrecursionlimit

import heapq
import itertools
import operator


# globals
inf = float('inf')
N, K = 0, 0
S = ""


def set_inputs():
    global N, K, S
    N, K = get_li()
    S = get_str()
    return


def main():
    setrecursionlimit(100000)
    set_inputs()
    # ----------MAIN----------
    cont = []
    cur = S[0]
    count = 1
    for s in S[1:]:
        if cur == s:
            count += 1
        else:
            cont.append((count, cur))
            cur = s
            count = 1
    cont.append((count, cur))
    
    if len(cont) == 1:
        print(len(S))
        return
    ans = 0
    for i in range(len(cont)):
        if cont[i][1] == "0":
            ans = max(ans, sum(map(operator.itemgetter(0), cont[i:i+2*K])))
        else:
            ans = max(ans, sum(map(operator.itemgetter(0), cont[i:i+2*K+1])))
    print(ans)
    return


def get_int():
    return int(input())


def get_float():
    return float(input())


def get_str():
    return input().strip()


def get_li():
    return [int(i) for i in input().split()]


def get_lf():
    return [float(f) for f in input().split()]


def get_lc():
    return list(input().strip())


def get_data(n, types, sep=None):
    if len(types) == 1:
        return [types[0](input()) for _ in range(n)]
    return list(zip(*(
        [t(x) for t, x in zip(types, input().split(sep=sep))]
        for _ in range(n)
    )))


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

ans = 0

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

# extend list
if blocks[0][0] == '0':
    blocks.insert(0, ('1', 0))
if blocks[-1][0] == '0':
    blocks.append(('1', 0))

counters = []
def count(blocks):
    cnt = 0
    for block in blocks:
        cnt += block[1]
    return cnt

for i in range(0, int(len(blocks)), 2):
    if i == int(len(blocks)):
        break
    else:
        counters.append(count(blocks[i:i+(2*k+1)]))

print(max(counters))

Submission status: Time Limit Exceeded

Solution code:
N,K = map(int,input().split())
s = list(map(int,input()))
s.append(2)
start = s[0]
choku = [0]
saka = [0]
number = 1
for i in range(N):
    if s[i] == s[i+1]:
        number += 1
    else:
        if s[i] == 0:
            choku.append(number)
            number = 1
        else:
            saka.append(number)
            number = 1
choku.append(0)
saka.append(0)
while(1):
    max_align = 0
    m = len(choku)
    n = len(saka)
    if m <= K:
        print(N)
        break
    if start == 0: #最初が直立の場合
        for i in range(m-K+1):
            if max_align <= sum(choku[i:i+K]) + sum(saka[i-1:i+K]):
                max_align = sum(choku[i:i+K]) + sum(saka[i-1:i+K])
    else:
        for i in range(m-K+1):
            if max_align <= sum(choku[i:i+K]) + sum(saka[i:i+K+1]):
                max_align = sum(choku[i:i+K]) + sum(saka[i:i+K+1])
    print(max_align)
    break

Submission status: Time Limit Exceeded

Solution code:
import sys
def main():
  if 'get_ipython' not in globals():  
    # For Subsession
    N,K = map(int, input().split())
    S = sys.stdin.readline()
  else:
    # For Test
    # N = 5
    # K = 1
    # S = "00010"
    N = 14
    K = 2
    S = "11101010110011"
    # N = 1
    # K = 1
    # S = "1"
    
  SC = []
  i = 0
  if S[0] == "0":SC.append(0)
  while i < N :
    if S[i] == "0":
      j = S.find("1",i)
    else:
      j = S.find("0",i)
    if j == -1: j = N 
    cnt = j - i
    i = j
    SC.append(cnt)
    
  if 'get_ipython' in globals(): print(SC)
    
  i = 0
  lenSC = len(SC) - 1
  ans = 0
  for i in range(0,lenSC+1,2):
  #while i <= lenSC:
    cnt = 0
    k = i + K * 2
    if k > lenSC:
      k = lenSC
    k += 1
    cnt = sum(SC[i:k])
    if ans < cnt:
      ans = cnt
  # i += 2

  print(ans)

main()

Submission status: Time Limit Exceeded

Solution code:
import sys
def main():
  if 'get_ipython' not in globals():  
    # For Subsession
    N,K = map(int, input().split())
    S = sys.stdin.readline()
  else:
    # For Test
    # N = 5
    # K = 1
    # S = "00010"
    N = 14
    K = 2
    S = "11101010110011"
    # N = 1
    # K = 1
    # S = "1"
    
  SC = []
  i = 0
  if S[0] == "0":SC.append(0)
  while i < N :
    if S[i] == "0":
      j = S.find("1",i)
    else:
      j = S.find("0",i)
    if j == -1: j = N 
    cnt = j - i
    i = j
    SC.append(cnt)
    
  if 'get_ipython' in globals(): print(SC)
    
  i = 0
  lenSC = len(SC) - 1
  ans = 0
  while i <= lenSC:
    cnt = 0
    k = i + K * 2
    if k > lenSC:
      k = lenSC
    k += 1
    cnt = sum(SC[i:k])
    if ans < cnt:
      ans = cnt
    i += 2

  print(ans)

main()

Submission status: Time Limit Exceeded

Solution code:

import sys
input = sys.stdin.readline
N, K = map(int, input().split())
S = input()
first = S[0]
a = []

ct = 1
idx = 0
for i in range(1,N):
    if S[i] == S[i-1]:
        ct += 1
    else:
        a = a + [ct]
        ct = 1
        idx += 1
a = a + [ct]

if first == '0':
    zero = a[0::2]
    one = a[1::2]
    ite = max(len(zero)-K+1, 1)
    ans = 0
    for i in range(ite):
        cand0 = sum(zero[i:i+K]) + sum(one[i:i+K])
        cand1 = sum(zero[i+1:i+K+1]) + sum(one[i:i+K+1])
        if cand0 > ans:
            ans = cand0
        if cand1 > ans:
            ans = cand1

else:
    one = a[0::2]
    zero = a[1::2]
    ite = max(len(one)-K+1, 1)
    ans = 0
    for i in range(ite):
        cand0 = sum(zero[i:i+K]) + sum(one[i+1:i+K+1])
        cand1 = sum(zero[i:i+K]) + sum(one[i:i+K+1])
        if cand0 > ans:
            ans = cand0
        if cand1 > ans:
            ans = cand1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
N, K = map(int, input().split())
S = input()
first = S[0]
a = []
ct = 1
idx = 0
for i in range(1,N):
    if S[i] == S[i-1]:
        ct += 1
    else:
        a = a + [ct]
        ct = 1
        idx += 1
a = a + [ct]
if first == '0':
    zero = a[0::2]
    one = a[1::2]
    ite = max(len(zero)-K+1, 1)
    ans = 0
    for i in range(ite):
        cand0 = sum(zero[i:i+K]) + sum(one[i:i+K])
        cand1 = sum(zero[i+1:i+K+1]) + sum(one[i:i+K+1])
        if cand0 > ans:
            ans = cand0
        if cand1 > ans:
            ans = cand1
else:
    one = a[0::2]
    zero = a[1::2]
    ite = max(len(one)-K+1, 1)
    ans = 0
    for i in range(ite):
        cand0 = sum(zero[i:i+K]) + sum(one[i+1:i+K+1])
        cand1 = sum(zero[i:i+K]) + sum(one[i:i+K+1])
        if cand0 > ans:
            ans = cand0
        if cand1 > ans:
            ans = cand1
print(ans)

Submission status: Time Limit Exceeded

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

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

if first == '0':
    zero = a[0::2]
    one = a[1::2]
    ite = max(len(zero)-K+1, 1)
    ans = 0
    for i in range(ite):
        cand0 = sum(zero[i:i+K]) + sum(one[i:i+K])
        cand1 = sum(zero[i+1:i+K+1]) + sum(one[i:i+K+1])
        if cand0 > ans:
            ans = cand0
        if cand1 > ans:
            ans = cand1

else:
    one = a[0::2]
    zero = a[1::2]
    ite = max(len(one)-K+1, 1)
    ans = 0
    for i in range(ite):
        cand0 = sum(zero[i:i+K]) + sum(one[i+1:i+K+1])
        cand1 = sum(zero[i:i+K]) + sum(one[i:i+K+1])
        if cand0 > ans:
            ans = cand0
        if cand1 > ans:
            ans = cand1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
# ABC124_D

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

count = 0
Max = 0
list = []

Front = int(S[0])

for i in range(N):
    if(i == 0):
        count = 1
    elif(int(S[i - 1]) == int(S[i])):
        count = count + 1
    else:
        list.append(count)
        count = 1
list.append(count)

if(Front == 0):
    Zero = (len(list) + 1) // 2
else:
    Zero = len(list) // 2

temp = 0

if(K >= Zero):
    Max = len(S)
else:
    if (Front == 0):
        for i in range(0, 2 * Zero, 2):
            if (i == 0):
                for j in range(0, 2 * K):
                    temp = temp + list[j]
            else:
                for j in range(i - 1, (2 * K) + i):
                    if(j < len(list)):
                        temp = temp + list[j]
            if temp > Max:
                Max = temp
            temp = 0
    elif (Front == 1):
        for i in range(1, 2 * Zero + 1, 2):
            for j in range(i - 1, (2 * K) + i):
                if(j < len(list)):
                    temp = temp + list[j]
            if temp > Max:
                Max = temp
            temp = 0

print(Max)

Submission status: Time Limit Exceeded

Solution code:
def main():
  if 'get_ipython' not in globals():  
    # For Subsession
    N,K = map(int, input().split())
    S = input()
  else:
    # For Test
    # N = 5
    # K = 1
    # S = "00010"
    N = 14
    K = 2
    S = "11101010110011"
    # N = 1
    # K = 1
    # S = "1"
    
  SC = []
  i = 0
  if S[0] == "0":SC.append(0)
  while i < N :
    if S[i] == "0":
      j = S.find("1",i)
    else:
      j = S.find("0",i)
    if j == -1: j = N 
    cnt = j - i
    i = j
    SC.append(cnt)
    
  if 'get_ipython' in globals(): print(SC)
    
  i = 0
  lenSC = len(SC) - 1
  ans = 0
  while i <= lenSC:
    cnt = 0
    k = i + K * 2
    if k > lenSC:
      k = lenSC
    for j in range(i,k + 1):
      cnt += SC[j]
    if ans < cnt:
      ans = cnt
    i += 2

  print(ans)

main()

Submission status: Time Limit Exceeded

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

#print(s)

status = s[0]
count = 1
info = []

for i in range(1,n):
    if s[i] == status:
        count = count + 1
    else:
        info.append([status,count])
        count = 1
        status = s[i]

info.append([status,count])

#print(info)

c = 0
for x in range(0,len(info)):
    if info[x][0] == 0:
        c = c + 1

#print(c)

if c <= k:
    print(n)

else:
    p = 0
    l = []

    if s[0] == 0:
        while 2*(p+k-1) < len(info):
            w = 0
            for q in range(2*p-1,2*(p+k)):
                if q >= 0 and q < len(info):
                    w += info[q][1]
            l.append(w)
            p += 1

    else:
        while 2*(p+k-1)+1 < len(info):
            w = 0
            for q in range(2*p,2*(p+k)+1):
                if q >= 0 and q < len(info):
                    w += info[q][1]
            l.append(w)
            p += 1
    print(max(l))

Submission status: Time Limit Exceeded

Solution code:
def main():
  if 'get_ipython' not in globals():  
    # For Subsession
    N,K = map(int, input().split())
    S = input()
  else:
    # For Test
    # N = 5
    # K = 1
    # S = "00010"
    N = 14
    K = 2
    S = "11101010110011"
    # N = 1
    # K = 1
    # S = "1"
    
  SC = []
  i = 0
  if S[0] == "0":SC.append(0)
  while i < N:
    cnt = 1
    if S[i] == "0":
      i += 1
      while i < N:
        if S[i] == "0":
          cnt += 1
          i += 1
        else:
          break
    else:
      i += 1
      while i < N:
        if S[i] == "1":
          cnt += 1
          i += 1
        else:
          break
    SC.append(cnt)
    
  if 'get_ipython' in globals(): print(SC)
    
  i = 0
  lenSC = len(SC) - 1
  ans = 0
  while i <= lenSC:
    cnt = 0
    k = i + K * 2
    if k > lenSC:
      k = lenSC
    for j in range(i,k + 1):
      cnt += SC[j]
    if ans < cnt:
      ans = cnt
    i += 2

  print(ans)

main()

Submission status: Time Limit Exceeded

Solution code:
def main():
  if 'get_ipython' not in globals():  
    # For Subsession
    N,K = map(int, input().split())
    S = input()
  else:
    # For Test
    # N = 5
    # K = 1
    # S = "00010"
    N = 14
    K = 2
    S = "11101010110011"
    # N = 1
    # K = 1
    # S = "1"
    
  SC = []
  i = 0
  if S[0] == "0":SC.append(0)
  while i < N:
    if S[i] == "0":
      cnt = -1
      i += 1
      while i < N:
        if S[i] == "0":
          cnt -= 1
          i += 1
        else:
          break
    else:
      cnt = 1
      i += 1
      while i < N:
        if S[i] == "1":
          cnt += 1
          i += 1
        else:
          break
    SC.append(cnt)
    
  if 'get_ipython' in globals(): print(SC)
    
  i = 0
  lenSC = len(SC) - 1
  ans = 0
  while i <= lenSC:
    if SC[i] >= 0:
      cnt = 0
      k = i + K * 2
      if k > lenSC:
        k = lenSC
      for j in range(i,k + 1):
        cnt += abs(SC[j])
      if ans < cnt:
        ans = cnt
    i += 2

  print(ans)

main()

Submission status: Time Limit Exceeded

Solution code:
def main():
  if 'get_ipython' not in globals():  
    # For Subsession
    N,K = map(int, input().split())
    S = input()
  else:
    # For Test
    # N = 5
    # K = 1
    # S = "00010"
    # N = 14
    # K = 2
    # S = "11101010110011"
    N = 1
    K = 1
    S = "1"
    
  SC = []
  i = 0
  if S[0] == "0":SC.append(0)
  while i < N:
    if S[i] == "0":
      cnt = -1
      i += 1
      while i < N:
        if S[i] == "0":
          cnt -= 1
          i += 1
        else:
          break
    else:
      cnt = 1
      i += 1
      while i < N:
        if S[i] == "1":
          cnt += 1
          i += 1
        else:
          break
    SC.append(cnt)
    
  if 'get_ipython' in globals(): print(SC)
    
  i = 0
  lenSC = len(SC) - 1
  ans = 0
  while i <= lenSC:
    if SC[i] >= 0:
      cnt = 0
      k = i + K * 2
      if k > lenSC:
        k = lenSC
      for j in range(i,k + 1):
        cnt += abs(SC[j])
      if ans < cnt:
        ans = cnt
    i += 1

  print(ans)

main()

Submission status: Time Limit Exceeded

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

n = []
t = 1
c = 0
for i in range(0, N):
    if (S[i] == str(t)):
        c += 1
    else:
        n.append(c)
        t = 1 - t
        c = 1
if c != 0:
    n.append(c) 

# 1で始まって1で終わるように調節
if len(n) % 2 == 0:
    n.append(0)   

s = 2 * K + 1
a = 0
for i in range(0, len(n), 2):
    tmp = 0 

    l = i 
    r = min(i + s, len(n))
    for j in range(l, r):
        tmp += n[j]

    a = max(a, tmp)

print(a)

Submission status: Time Limit Exceeded

Solution code:
n, k = map(int, input().split(' '))
s = input()
if k >= n:
  print(n)
  exit()
  
s10 = []
cnt = 0
s_prev = '1'
for si in s+'x':
  if si == s_prev:
    cnt += 1
  else:
    s10.append(cnt)
    cnt = 1
  s_prev = si
if s[-1] == '0':
  s10.append(0)
if len(s10) < 2*k + 1:
  print(n)
  exit()

sum_max= 0
for i in range(len(s10)//2+1):
  sum_i = sum(s10[2*i:2*i+2*k+1])
  sum_max = max(sum_i, sum_max)
  
print(sum_max)

Submission status: Time Limit Exceeded

Solution code:
n, k = map(int, input().split(' '))
s = input()
s10 = []
cnt = 0
s_prev = '1'
for si in s+'x':
  if si == s_prev:
    cnt += 1
  else:
    s10.append(cnt)
    cnt = 1
  s_prev = si
if s[-1] == '0':
  s10.append(0)

sum_max= 0
for i in range(len(s10)//2+1):
  sum_i = sum(s10[2*i:2*i+2*k+1])
  sum_max = max(sum_i, sum_max)
  
print(sum_max)

Submission status: Time Limit Exceeded

Solution code:
n,k = map(int,input().split())
s = list(input())
if s[0] == "1":
    cnt_list = []
else:
    cnt_list = [0]
cnt = 1
for i in range(n-1):
    if s[i] == s[i+1]:
        cnt += 1
    else:
        cnt_list.append(cnt)
        cnt = 1
cnt_list.append(cnt)
if s[-1] == '0':
    cnt_list.append(0)
#print(cnt_list)
sum_list = []
right = max(len(cnt_list)-2*k, 1)
for i in range(0,right,2):
    sum_list.append(sum(cnt_list[i:i+2*k+1]))
#print(sum_list)
print(max(sum_list))

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)

Submission status: Time Limit Exceeded

Solution code:
n,k = map(int,input().split(' '))
s = input()
l = [0]*2*k
ans=0
l_last = 1
count = 0
for s0 in s:
    if int(s0) == l_last:
        count += 1
    else:
        l.append(count)
        count = 1
        l_last = (l_last+1)%2
        if l_last==0:
            ans = max(ans,sum(l[-(2*k+1):]))
if count!=0:
    l.append(count)
if len(l)%2==0:
    l.append(0)
ans = max(ans,sum(l[-(2*k+1):]))
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n,k = map(int,input().split(' '))
s = input()
l = []
l_last = 1
count = 0
for s0 in s:
    if int(s0) == l_last:
        count += 1
    else:
        l.append(count)
        count = 1
        l_last = (l_last+1)%2
if count!=0:
    l.append(count)
if len(l)%2==0:
    l.append(0)
q=(len(l)-1)/2
if k >= q:
    ans = n
else:
    ans = 0
    j = 0
    while j <= q-k:
        ans = max(ans,sum(l[2*j:2*(j+k)+1]))
        j += 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n,k = map(int,input().split(' '))
s = input()
l = []
l_last = 1
count = 0
for s0 in s:
    if int(s0) == l_last:
        count += 1
    else:
        l.append(count)
        count = 1
        l_last = (l_last+1)%2
if count!=0:
    l.append(count)
if len(l)%2==0:
    l.append(0)
q=(len(l)-1)/2
if k >= q:
    ans = n
else:
    ans = 0
    j = 0
    while j <= q-k:
        if ans <= sum(l[2*j:2*(j+k)+1]):
            ans = sum(l[2*j:2*(j+k)+1])
        j += 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from itertools import islice
import numpy
 
init_data = list(map(int, input().split(" ")))
 
line = input()
line_number = []
 
current_direct = '1'
count = 0
for c in line:
    if c == current_direct:
        count += 1
    else:
        if current_direct == '0':
            current_direct = '1'
        else:
            current_direct = '0'
        line_number.append(count)
        count = 1
line_number.append(count)
if current_direct == '0':
    line_number.append(0)
 
max_operation = (len(line_number) - 1) // 2
if max_operation < init_data[1]:
    init_data[1] = max_operation
 
max_result = 0
for i in range(0, len(line_number) - init_data[1]*2, 2):
    result = sum(islice(line_number, i, i + init_data[1]*2 + 1))
    if max_result < result:
        max_result = result
print(max_result)

Submission status: Time Limit Exceeded

Solution code:
from itertools import islice

init_data = list(map(int, input().split(" ")))

line = input()
line_number = []

current_direct = '1'
count = 0
for c in line:
    if c == current_direct:
        count += 1
    else:
        if current_direct == '0':
            current_direct = '1'
        else:
            current_direct = '0'
        line_number.append(count)
        count = 1
line_number.append(count)
if current_direct == '0':
    line_number.append(0)

max_operation = (len(line_number) - 1) // 2
if max_operation < init_data[1]:
    init_data[1] = max_operation

max_result = 0
for i in range(0, len(line_number) - init_data[1]*2, 2):
    result = sum(islice(line_number, i, i + init_data[1]*2 + 1))
    if max_result < result:
        max_result = result
print(max_result)

Submission status: Time Limit Exceeded

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

line = input()
line_number = []

current_direct = '1'
count = 0
for c in line:
    if c == current_direct:
        count += 1
    else:
        if current_direct == '0':
            current_direct = '1'
        else:
            current_direct = '0'
        line_number.append(count)
        count = 1
line_number.append(count)
if current_direct == '0':
    line_number.append(0)

max_operation = (len(line_number) - 1) // 2
if max_operation < init_data[1]:
    init_data[1] = max_operation

results = []
for i in range(0, len(line_number) - init_data[1]*2, 2):
    results.append(sum(line_number[i:i + init_data[1]*2 + 1]))
print(max(results))

Submission status: Time Limit Exceeded

Solution code:
N,K = map(int, input().split())
S = input()
memo =[]
now = '1'
cnt = 0
for s in S:
    if s==now:
        cnt+=1
    else:
        memo.append(cnt)
        cnt=1
        now = '1' if now=='0' else '0'
else:
    memo.append(cnt)
if S[-1] == '0':
    memo.append(0)
ans = 0
add = K*2+1
for i in range(N)[::2]:
    if i < N:
        ans = max(ans, sum(memo[i:i+K*2+1]))
print(ans)

Submission status: Time Limit Exceeded

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

now = 1
cnt = 0
x = []

for i in range(n):
    if sss[i] == str(now):
        cnt += 1
    else:
        x.append(cnt)
        now = 1 - now
        cnt = 1
if cnt != 0:
    x.append(cnt)
if len(x) % 2 == 0:
    x.append(0)

Add = 2 * k + 1
ans = 0

for i in range(len(x))[::2]:
    tmp = 0
    left = i
    right = min(i + Add, len(x))
    for j in range(left, right):
        tmp += x[j]
    ans = max(tmp, ans)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
N,K = map(int,input().split())
S = list(map(int,input()))
tmp = 1
tbl = []
cont = 0
for s in S:
    if s == tmp:
        cont += 1
    else:
        tbl.append(cont)
        cont = 1
        tmp = abs(tmp-1)
tbl.append(cont)
if len(tbl) % 2 == 0:
    tbl.append(0)
max = 0
for i in range(0,len(tbl),2):
    if sum(tbl[i:i+K*2+1]) > max:
        max = sum(tbl[i:i+K*2+1])
print(max)

Submission status: Time Limit Exceeded

Solution code:
N,K = map(int,input().split())
S = input()
tmp = 1
tbl = []
cont = 0
for s in S:
    if int(s) == tmp:
        cont += 1
    else:
        tbl.append(cont)
        cont = 1
        tmp = abs(tmp-1)
tbl.append(cont)
if len(tbl) % 2 == 0:
    tbl.append(0)
max = 0
for i in range(0,len(tbl),2):
    if sum(tbl[i:i+K*2+1]) > max:
        max = sum(tbl[i:i+K*2+1])
print(max)

Submission status: Time Limit Exceeded

Solution code:
N,K = map(int,input().split())
S = input()
tmp = 1
tbl = []
cont = 0
for s in S:
    if int(s) == tmp:
        cont += 1
    else:
        tbl.append(cont)
        cont = 1
        tmp = abs(tmp-1)
tbl.append(cont)
if len(tbl) % 2 == 0:
    tbl.append(0)
max = 0
for i in range(0,len(tbl),2):
    if len(tbl) == 1:
        max = tbl[i]
        break
    if sum(tbl[i:i+K*2+1]) > max:
        max = sum(tbl[i:i+K*2+1])
print(max)

Submission status: Time Limit Exceeded

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

cnts = []
# denote whether the first block is 0 or 1
tmp, cnt = s[0], 0
flag = int(s[0])

for i in range(n):
    if s[i] == tmp:
        cnt += 1
    else: # here tmp should be cleared
        cnts.append(cnt)
        cnt = 1
    tmp = s[i]
cnts.append(cnt)
#print(cnts)

n_zero = (len(cnts) + 1) // 2 if flag == 0 else len(cnts) // 2
# print("# of zeros block: ", n_zero)
if n_zero <= k:
     ans = sum(cnts)
else:
    ans = 0
    for inx in range(flag, len(cnts), 2):
        start = max(0, inx - 1)
        end = min(inx + 2 * k, len(cnts))
        tmp = sum(cnts[start: end])
        ans = max(tmp, ans)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import math
import os
import random
import re
import sys
from collections import defaultdict

# sys.stdin = open('input.in', 'r')
# sys.stdout = open('output.out', 'w') 

def Fzero(l, k):
	max = 0
	if k*2 < len(l):
		max = sum(l[:k*2])
	else:
		max = sum(l)
	for i in range(1, len(l), 2):
		if i+k*2 < len(l):
			temp = sum(l[i:i+k*2+1])
		else:
			temp = sum(l[i:])
		if temp > max:
			max = temp

	return max

def Fone(l, k):
	max = 0
	for i in range(0, len(l), 2):
		if i+k*2 < len(l):
			temp = sum(l[i:i+k*2+1])
		else:
			temp = sum(l[i:])
		if temp > max:
			max = temp

	return max

def findmax(n, k, N):
	if n == 1:
		return 1
	sum = 0
	prev = N[0]
	length = []
	for i in N:
		if i==prev:
			sum += 1
		else:
			length.append(sum)
			sum = 1
		prev = i
	length.append(sum)

	if N[0] == 0:
		return Fzero(length, k)
	else:
		return Fone(length, k)



if __name__ == '__main__':
	n, k = map(int, input().rstrip().split())
	N = [int(d) for d in input()]
	print(findmax(n, k, N))