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:
import copy
def main():
    n, k = map(int, input().rstrip().split(' '))
    s = [c == '1' for c in input().rstrip()]

    candidates = []
    windows = Windows()
    current_window = None
    for i, c in enumerate(s):
        if not c:
            if current_window  == None:
                current_window = Window(False)
            elif current_window.one:
                windows.windows.append(current_window)
                if windows.count_zero_windows() == k:
                    """we have k zero windows. shifting one to see new candiate"""
                    candidates.append(windows)
                    windows = windows.slide()
                current_window = Window(False)
            current_window.count += 1
        else:
            if current_window  == None:
                current_window = Window(True)
            elif not current_window.one:
                windows.windows.append(current_window)
                current_window = Window(True)
            current_window.count += 1
    windows.windows.append(current_window)
    candidates.append(windows)
    print(max([w.count_ones() for w in candidates]))



class Window:
    def __init__(self, one_or_zero):
        self.one = one_or_zero
        self.count = 0
    
class Windows:
    def __init__(self):
        self.windows = []
    
    def slide(self):
        """Remove leftmost zero window and returns new windows"""
        next = Windows()
        if not self.windows[0].one:
            next.windows = copy.deepcopy(self.windows[1:])
        else:
            next.windows =  copy.deepcopy(self.windows[2:])
        return next
    
    def count_zero_windows(self):
        if len(self.windows) % 2 == 0 or self.windows[0].one:
            return len(self.windows) // 2
        else:
            return len(self.windows) // 2 + 1
    
    def count_ones(self):
        return sum([w.count for w in self.windows])




if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
N,K = map(int, input().split())
S = list(str(input()))
l1 = [0]
l0 = [0]
count = 0
flg = int(S[0])#今0or1のどれについて数えているか
if flg == 1:
    l1[0] = 1
elif flg == 0:
    l0[0] = 1
else:
    print ("ERROR-1")
index = 1
#文字列分割が謎
while True:
    if index == len(S):
        #終了条件
        break
        
    if flg == 1:
        #前の文字が1
        if int(S[index]) == flg:
            #l1のケツの数字をインクリメント
            l1[len(l1)-1] += 1
        else:
            l0.append(1)
            flg = 0
            
    else:
        #前の文字が0
        if int(S[index]) == flg:
            #l0のケツをインクリメント
            l0[len(l0)-1] += 1
        else:
            l1.append(1)
            flg = 1
            
    index += 1

#テスト済み　4/19 15.00
#初めの文字でない方は0になる
#これはlist.remove(0)で消せる

#K個のl0の連続する要素とその間のindexのl1を足して保存する
#その最大値を出す

import numpy as np


#そもそもlen(l0) <= Kなら普通に全て足してOK
if len(l0) <= K:
    print(np.array(l0).sum() + np.array(l1).sum())
else:
    index0 = 0
    index1 = 0
    sum_tmp = 0
    tmp = [] #最大値を入れておく
    if l0[0] == 0:
        l0.remove(0)
        #初めが1スタートの時
        #l1からK+1こ、l0からKこ
        while index0 + K -1 < len(l0) and index1+K < len(l1):
            
            #値計算
            for k in range(K):
                sum_tmp +=  l1[index1 + k] + l0[index0 + k]
            sum_tmp += l1[index1 + K]

            tmp.append(sum_tmp)
            sum_tmp = 0
            index0 += 1
            index1 += 1
            #語尾に0がある場合
        if index0 + K -1 < len(l0):
            for k in range(K):
                sum_tmp += l0[index0 + k] + l1[index1 + k]
            tmp.append(sum_tmp)
    
    else:
        #そんな変わらないからif文の分岐ここじゃなくていいかも
        #初めが0スタート
        l1.remove(0)
        #初めだけl0からKこ、l1からKこ　0-1-0-1
        #他は上と同様
        for k in range(K):
            sum_tmp +=  l0[index0 + k] + l1[index1 + k]

        tmp.append(sum_tmp)
        sum_tmp = 0
        index0 += 1#初めの0だけ使わない

        while index0 + K-1 < len(l0) and index1 + K < len(l1):

            #値計算
            for k in range(K):
                sum_tmp +=  l1[index1 + k] + l0[index0 + k]
            sum_tmp += l1[index1 + K]

            tmp.append(sum_tmp)
            sum_tmp = 0
            index0 += 1
            index1 += 1
            
        #語尾に0がある場合
        if index0 + K -1 < len(l0):
            for k in range(K):
                sum_tmp += l0[index0 + k] + l1[index1 + k]
                
            tmp.append(sum_tmp)

    print(np.array(tmp).max())

Submission status: Time Limit Exceeded

Solution code:
class CreateList:
    def __init__(self, flg, cnt):
        self.flg = flg
        self.cnt = cnt
        
        

def main():
    
    n, k = map(int, input().split())
    s = list(map(int, input()))
    
    slist = []      #Sを使いやすいように加工した配列
    bkflg = s[0]    #一つ前の連続する値が「1」か「0」かを持ってるやつ
    scnt = 0        #「1」「0」の連続する数
    ecnt = 0        #終了判定のカウンター
    endlength = len(s)
    
    #配列作成
    for i in s:
        ecnt += 1
        
        #同じ数字が続く場合はカウントアップ
        if i == bkflg:
           scnt += 1
        
        #数字が変わったら配列にこれまでの合計数を入れてカウンターを初期化
        else:
            slist.append(CreateList(bkflg, scnt))
            scnt = 1
            bkflg = i
        
        #最後の1回が拾えないのでここで強引に配列に突っ込む
        if ecnt == endlength:
            slist.append(CreateList(bkflg, scnt))
    
    #本ちゃんで使う勇者たち
    maxsum = 0      #最大合計値の変数
    skipcnt = k     #連続する「0」を「1」としてカウント出来る回数
    cursor = 0      #Sの配列変数の開始位置
    cursorsum = 0   #一時的な「1」の合計数
    tmpcnt = 0      #一時的なカーソル位置
    endlength = len(slist) - 1
    
    #本ちゃんスタート
    while tmpcnt <= endlength:
        #Sの配列を左からスライドしながら数字を拾ってくので、最初にカウンターとカーソル位置を初期化
        tmpcnt = cursor
        skipcnt = k
        cursorsum = 0
        
        #ここで「1」の合計をSの配列の左からスライドさせながらカウントしていく
        while tmpcnt <= endlength:
            if slist[tmpcnt].flg == 0:
                if skipcnt != 0:
                    skipcnt -= 1
                else:
                    break
            
            cursorsum = cursorsum + slist[tmpcnt].cnt
            tmpcnt += 1
        
        #これまでの最大合計数を拾う
        if maxsum < cursorsum:
            maxsum = cursorsum
        
        cursor += 1
        
    print(maxsum)
    
    
if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
class CreateList:
    def __init__(self, flg, cnt):
        self.flg = flg
        self.cnt = cnt
        
        

def main():
    
    n, k = map(int, input().split())
    s = list(map(int, input()))
    
    slist = []      #Sを使いやすいように加工した配列
    bkflg = s[0]    #一つ前の連続する値が「1」か「0」かを持ってるやつ
    scnt = 0        #「1」「0」の連続する数
    ecnt = 0        #終了判定のカウンター
    
    #配列作成
    for i in s:
        ecnt += 1
        
        #同じ数字が続く場合はカウントアップ
        if i == bkflg:
           scnt += 1
        
        #数字が変わったら配列にこれまでの合計数を入れてカウンターを初期化
        else:
            slist.append(CreateList(bkflg, scnt))
            scnt = 1
            bkflg = i
        
        #最後の1回が拾えないのでここで強引に配列に突っ込む
        if ecnt == len(s):
            slist.append(CreateList(bkflg, scnt))
    
    #本ちゃんで使う勇者たち
    maxsum = 0      #最大合計値の変数
    skipcnt = k     #連続する「0」を「1」としてカウント出来る回数
    cursor = 0      #Sの配列変数の開始位置
    cursorsum = 0   #一時的な「1」の合計数
    tmpcnt = 0      #一時的なカーソル位置
    endlength = len(slist) - 1
    
    #本ちゃんスタート
    while tmpcnt <= endlength:
        #Sの配列を左からスライドしながら数字を拾ってくので、最初にカウンターとカーソル位置を初期化
        tmpcnt = cursor
        skipcnt = k
        cursorsum = 0
        
        #ここで「1」の合計をSの配列の左からスライドさせながらカウントしていく
        while tmpcnt <= endlength:
            if slist[tmpcnt].flg == 0:
                if skipcnt != 0:
                    skipcnt -= 1
                else:
                    break
            
            cursorsum = cursorsum + slist[tmpcnt].cnt
            tmpcnt += 1
        
        #これまでの最大合計数を拾う
        if maxsum < cursorsum:
            maxsum = cursorsum
        
        cursor += 1
        
    print(maxsum)
    
    
if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
class CreateList:
    def __init__(self, flg, cnt):
        self.flg = flg
        self.cnt = cnt
        
        

def main():
    
    n, k = map(int, input().split())
    s = list(map(int, input()))
    
    slist = []      #Sを使いやすいように加工した配列
    bkflg = s[0]    #一つ前の連続する値が「1」か「0」かを持ってるやつ
    scnt = 0        #「1」「0」の連続する数
    ecnt = 0        #終了判定のカウンター
    
    #配列作成
    for i in s:
        ecnt += 1
        
        #同じ数字が続く場合はカウントアップ
        if i == bkflg:
           scnt += 1
        
        #数字が変わったら配列にこれまでの合計数を入れてカウンターを初期化
        else:
            slist.append(CreateList(bkflg, scnt))
            scnt = 1
            bkflg = i
        
        #最後の1回が拾えないのでここで強引に配列に突っ込む
        if ecnt == len(s):
            slist.append(CreateList(bkflg, scnt))
    
    #本ちゃんで使う勇者たち
    maxsum = 0      #最大合計値の変数
    skipcnt = k     #連続する「0」を「1」としてカウント出来る回数
    cursor = 0      #Sの配列変数の開始位置
    cursorsum = 0   #一時的な「1」の合計数
    tmpcnt = 0      #一時的なカーソル位置
    
    #本ちゃんスタート
    while tmpcnt <= len(slist) - 1:
        #Sの配列を左からスライドしながら数字を拾ってくので、最初にカウンターとカーソル位置を初期化
        tmpcnt = cursor
        skipcnt = k
        cursorsum = 0
        
        #ここで「1」の合計をSの配列の左からスライドさせながらカウントしていく
        while tmpcnt <= len(slist) - 1:
            if slist[tmpcnt].flg == 0:
                if skipcnt != 0:
                    skipcnt -= 1
                else:
                    break
            
            cursorsum = cursorsum + slist[tmpcnt].cnt
            tmpcnt += 1
        
        #これまでの最大合計数を拾う
        if maxsum < cursorsum:
            maxsum = cursorsum
        
        cursor += 1
        
    print(maxsum)
    
    
if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
class CreateList:
    def __init__(self, flg, cnt):
        self.flg = flg
        self.cnt = cnt
        
        

def main():
    
    n, k = map(int, input().split())
    s = list(map(int, input()))
    
    slist = []      #Sを使いやすいように加工した配列
    bkflg = s[0]    #一つ前の連続する値が「1」か「0」かを持ってるやつ
    scnt = 0        #「1」「0」の連続する数
    ecnt = 0        #終了判定のカウンター
    
    #配列作成
    for i in s:
        ecnt += 1
        
        #同じ数字が続く場合はカウントアップ
        if i == bkflg:
           scnt += 1
        
        #数字が変わったら配列にこれまでの合計数を入れてカウンターを初期化
        else:
            slist.append(CreateList(bkflg, scnt))
            scnt = 1
            bkflg = i
        
        #最後の1回が拾えないのでここで強引に配列に突っ込む
        if ecnt == len(s):
            slist.append(CreateList(bkflg, scnt))
    
    #本ちゃんで使う勇者たち
    maxsum = 0      #最大合計値の変数
    skipcnt = k     #連続する「0」を「1」としてカウント出来る回数
    cursor = 0      #Sの配列変数の開始位置
    cursorsum = 0   #一時的な「1」の合計数
    tmpcnt = 0      #一時的なカーソル位置
    
    #本ちゃんスタート
    while tmpcnt <= len(slist) - 1:
        #Sの配列を左からスライドしながら数字を拾ってくので、最初にカウンターとカーソル位置を初期化
        tmpcnt = cursor
        skipcnt = k
        cursorsum = 0
        
        #ここで「1」の合計をSの配列の左からスライドさせながらカウントしていく
        while tmpcnt <= len(slist) - 1:
            if slist[tmpcnt].flg == 0:
                if skipcnt == 0:
                    break
                else:
                    skipcnt -= 1
            
            cursorsum = cursorsum + slist[tmpcnt].cnt
            tmpcnt += 1
        
        #これまでの最大合計数を拾う
        if maxsum < cursorsum:
            maxsum = cursorsum
        
        cursor += 1
        
    print(maxsum)
    
    
if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
q, k = list(map(int, input().split()))
list = [int(i) for i in input()]

# 0 ,1 のまとまりを作る
sum01 = []
pre = 1
cnt = 0
# 111010
# 001010
for i, e in enumerate(list):
    if e != pre:
        sum01.append(cnt)
        cnt = 1
        pre = e
    else:
        cnt += 1
if cnt != 0:
    sum01.append(cnt)

if len(sum01) % 2 == 1:
    sum01.append(0)

#print(sum01)
Add = 2 * k + 1
answer = 0
for i in range(0, len(sum01), 2):
    temp = 0
    left = i
    right = min(i + Add, len(sum01))
    for j in range(left, right, 1):
        temp += sum01[j]
    answer = max(temp, answer)

print(answer)

Submission status: Time Limit Exceeded

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

ct = 0
flg = s[0]
s_ct = []

for s_ in s:
  if s_ == flg:
    ct += 1
  else:
    flg = s_
    s_ct.append(ct)
    ct = 1
s_ct.append(ct)

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

Submission status: Time Limit Exceeded

Solution code:
N, K = list(map(int, input().split()))
S = input()
 
S += "X"
j = 0
count = 1
change = [] if S[0] != "0" else [0]
for j in range(len(S)-1):
  if S[j:j+2] == "10":
    change.append(count)
    count = 0
  elif S[j:j+2] == "01":
    change.append(count)
    count = 0
  count += 1

change.append(count-1)
#print(change)
max_len = max([sum(change[i:i+2*K+1]) for i in range(0, len(change), 2)])
  
print(max_len)

Submission status: Time Limit Exceeded

Solution code:
N, K = list(map(int, input().split()))
S = input()
 
S += "X"
j = 0
count = 1
change = [] if S[0] != "0" else [0]
for j in range(len(S)-1):
  if S[j:j+2] == "10":
    change.append(count)
    count = 0
  elif S[j:j+2] == "01":
    change.append(count)
    count = 0
  count += 1

change.append(count-1)
#print(change)
max_len = 0
for i in range(0, len(change), 2):
  #print(i, sum(change[i:i+2*K+1]))
  max_len = max([sum(change[i:i+2*K+1]), max_len])
  
print(max_len)

Submission status: Time Limit Exceeded

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

S += "X"
j = 0
count = 1
change = [] if S[0] != "0" else [0]
while j < len(S)-1:
  if S[j:j+2] == "10":
    change.append(count)
    count = 0
  elif S[j:j+2] == "01":
    change.append(count)
    count = 0
    
  j += 1
  count += 1

change.append(count-1)
i = 0
max_len = 0
while i < len(change):
  max_len = max([sum(change[i:i+2*K+1]), max_len])
  i += 2
  
print(max_len)

Submission status: Time Limit Exceeded

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

S += "X"
j = 0
count = 1
change = [] if S[0] != "0" else [0]
while True:
  if S[j:j+2] == "10":
    change.append(count)
    count = 0
  elif S[j:j+2] == "01":
    change.append(count)
    count = 0
    
  j += 1
  if j == len(S)-1:
    change.append(count)
    break
  count += 1

i = 0
max_len = 0
while i < len(change):
  max_len = max([sum(change[i:i+2*K+1]), max_len])
  i += 2
  
print(max_len)

Submission status: Time Limit Exceeded

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

seq = []

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

prev = s[0]
seq_val = 1
last_seq_length = n
for i in range(1, n):
    if s[i] == prev:
        seq_val += 1
    else:
        last_seq_length -= seq_val
        seq.append(seq_val)
        seq_val = 1
        prev = s[i]

seq += [last_seq_length]

ans = 0
for i in range(len(seq)):
    if i % 2 == 0:
        ans = max(ans, sum(seq[i:i+2*k]))
    else:
        ans = max(ans, sum(seq[i:i+2*k+1]))

print(ans)

Submission status: Time Limit Exceeded

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

seq = []

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

prev = s[0]
seq_val = 1
last_seq_length = n
for i in range(1, n):
    if s[i] == prev:
        seq_val += 1
    else:
        last_seq_length -= seq_val
        seq.append(seq_val)
        seq_val = 1
        prev = s[i]

seq += [last_seq_length]

ans = 0
for i in range(n):
    if i % 2 == 0:
        ans = max(ans, sum(seq[i:i+2*k]))
    else:
        ans = max(ans, sum(seq[i:i+2*k+1]))

print(ans)

Submission status: Time Limit Exceeded

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

seq = []

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

prev = s[0]
seq_val = 1
for i in range(1, n):
    if s[i] == prev:
        seq_val += 1
    else:
        seq.append(seq_val)
        seq_val = 1
        prev = s[i]

seq += [n-sum(seq)]

ans = 0
for i in range(n):
    if i % 2 == 0:
        # print(seq[i:i+2*k])
        ans = max(ans, sum(seq[i:i+2*k]))
    else:
        # print(seq[i:i+2*k])
        ans = max(ans, sum(seq[i:i+2*k+1]))

print(ans)

Submission status: Time Limit Exceeded

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

seq = []

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

prev = s[0]
i = 0
while i < n:
    f = True
    for j in range(i+1, n):
        if s[j] != prev:
            seq.append(j-i)
            f = False
            prev = s[j]
            break
    if f:
        i += 1
    else:
        i = j

last_length = 1
prev = s[-1]
for i, c in enumerate(s[::-1][1:]):
    if c != prev:
        last_length += i
        break
seq += [last_length]

ans = 0
for i in range(n):
    if i % 2 == 0:
        # print(seq[i:i+2*k])
        ans = max(ans, sum(seq[i:i+2*k]))
    else:
        # print(seq[i:i+2*k])
        ans = max(ans, sum(seq[i:i+2*k+1]))

print(ans)

Submission status: Time Limit Exceeded

Solution code:

import sys

N, K = map(int, sys.stdin.readline().split())

S_origin = sys.stdin.readline()

countk = 0
start = 0
max = 0
for i in range(N):
    S = list(S_origin)

    process_flag = 0
    countk = 0#呪文のカウンタ
    for j in range(start,N):
        if K == countk:
            break
        if S[j] == "0":
            S[j] ="1"
            process_flag = 1
        elif process_flag == 1:
            countk +=1
            process_flag = 0

    #1が最大で何個並んでいるか
    count1 = 0
    current_max = 0
    for j in range(N):
        if S[j] == "1":
            count1 +=1
            if max < count1:
                max = count1
        else:
            count1 = 0
    if max < current_max:
        max = current_max
    start += 1

print(max)

Submission status: Time Limit Exceeded

Solution code:
n,k=map(int,input().split())
s=input()
s_list=[]
first=s[0]
last=s[n-1]
state=first
length=1
for i in range(1,n):
    if (state=='0' and s[i]=='0') or (state=='1' and s[i]=='1'):
        state=s[i]
        length=length+1
    else:
        state=s[i]
        s_list.append(length)
        length=1
s_list.append(length)
if first=='0' and last=='0':
    if (len(s_list)+1)/2<=k:
        Max=n
    else:
        p_list=s_list[0:2*k]
        Max=sum(p_list)
        i=1
        while i+2*k+1<n-1:
            p_list=s_list[i:i+2*k+1]
            if sum(p_list)>Max:
                Max=sum(p_list)
            i=i+2
        p_list=s_list[n-2*k:]
        if sum(p_list)>Max:
            Max=sum(p_list)
elif first=='0' and last=='1':
    if len(s_list)/2<=k:
        Max=n
    else:
        p_list=s_list[0:2*k]
        Max=sum(p_list)
        i=1
        while i+2*k+1<=n-1:
            p_list=s_list[i:i+2*k+1]
            if sum(p_list)>Max:
                Max=sum(p_list)
            i=i+2
elif first=='1' and last=='0':
    if len(s_list)/2<=k:
        Max=n
    else:
        p_list=s_list[0:2*k+1]
        Max=sum(p_list)
        i=2
        while i+2*k+1<n-1:
            p_list=s_list[i:i+2*k+1]
            if sum(p_list)>Max:
                Max=sum(p_list)
            i=i+2
        p_list=s_list[n-2*k:]
        if sum(p_list)>Max:
            Max=sum(p_list)
elif first=='1' and last=='1':
    if (len(s_list)-1)/2<=k:
        Max=n
    else:
        p_list=s_list[0:2*k+1]
        Max=sum(p_list)
        i=2
        while i+2*k+1<=n-1:
            p_list=s_list[i:i+2*k+1]
            if sum(p_list)>Max:
                Max=sum(p_list)
            i=i+2
print(Max)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

def main():
    n,k = map(int,input().split())
    s = input()
    li = np.array([])
    count = 1


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

    m = len(li)

    if s[0] == '1':
        t = int(m/2)
        if k >= t:
            print(int(sum(li)))
        else:
            max1 = 0
            for i in range(t-k+1):
                max1 = max(np.sum(li[2*i:2*(i+k)+1]),max1)
            print(int(max1))
    else:
        t = int(m/2+1)
        if k >= t:
            print(int(sum(li)))
        else:
            max1 = sum(li[:2*k])
            for i in range(t-k):
                max1 = max(np.sum(li[2*i+1:2*(i+k)+2]),max1)
            print(int(max1))
main()

Submission status: Time Limit Exceeded

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

def search(first, count):
  change = 0
  past = "1"
  for i in range(first, len(S)):
    now = S[i]
    if past != now:
      change += 1
      if change == count*2:
        #print(S[first], S[i])
        for j in range(i, len(S)):
          if "1" != S[j]:
            #print(j, first)
            return j-first
    past = S[i]
  #print(len(S), first)
  return len(S)-first

max_len = 0
past = ""
for i in range(len(S)):
  now = S[i]
  if past != now:
    #print(i, S[i], search(i, K))
    max_len = max([search(i, K), max_len])
  past = S[i] 
  

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

Submission status: Time Limit Exceeded

Solution code:
argN,argK = list(map(int,input().split()))
argS = list(input())
wkCount = 0
wkCheck = argS[0]
aryS = []
for i in range(len(argS)):
  if wkCheck == argS[i]:
    wkCount += 1
  else:
    aryS.append(wkCount)
    wkCount = 1
    wkCheck = argS[i]
if wkCount > 0:
  aryS.append(wkCount)

#print(aryS)

maxnum = 0
startVal = int(argS[0])
for j in range(len(aryS)):
  if (j + startVal) % 2 == 0:
    if j == 0:
      startIdx = 0
    else:
      startIdx = j - 1
    endIdx = min(j + argK * 2 - 1,len(aryS))
  else:
    startIdx = j
    endIdx = min(j + argK * 2,len(aryS))
  maxnum = max(maxnum,sum(aryS[startIdx:endIdx + 1]))
  if len(aryS) <= endIdx:
    j = len(aryS)

print(maxnum)

Submission status: Time Limit Exceeded

Solution code:
argN,argK = list(map(int,input().split()))
argS = list(input())
wkCount = 0
wkCheck = argS[0]
aryS = []
for i in range(len(argS)):
  if wkCheck == argS[i]:
    wkCount += 1
  else:
    aryS.append(wkCount)
    wkCount = 1
    wkCheck = argS[i]
if wkCount > 0:
  aryS.append(wkCount)

#print(aryS)

maxnum = 0
startVal = int(argS[0])
for j in range(len(aryS)):
  if (j + startVal) % 2 == 0:
    if j == 0:
      startIdx = 0
    else:
      startIdx = j - 1
    endIdx = min(j + argK * 2 - 1,len(aryS)) + 1
  else:
    startIdx = j
    endIdx = min(j + argK * 2,len(aryS)) + 1
  maxnum = max(maxnum,sum(aryS[startIdx:endIdx]))
print(maxnum)

Submission status: Time Limit Exceeded

Solution code:
n,k=map(int,input().split())
s=input()
s_list=[]
first=s[0]
last=s[n-1]
state=first
length=1
for i in range(1,n):
    if (state=='0' and s[i]=='0') or (state=='1' and s[i]=='1'):
        state=s[i]
        length=length+1
    else:
        state=s[i]
        s_list.append(length)
        length=1
s_list.append(length)
if first=='0':
    if last=='0':
        number0=(len(s_list)+1)/2
    else:
        number0=len(s_list)/2
else:
    if last=='0':
        number0=len(s_list)/2
    else:
        number0=(len(s_list)-1)/2
if first=='0' and last=='0':
    if number0<=k:
        Max=n
    else:
        p_list=s_list[0:2*k]
        Max=sum(p_list)
        i=1
        while i+2*k+1<n-1:
            p_list=s_list[i:i+2*k+1]
            if sum(p_list)>Max:
                Max=sum(p_list)
            i=i+2
        p_list=s_list[n-2*k:]
        if sum(p_list)>Max:
            Max=sum(p_list)
elif first=='0' and last=='1':
    if number0<=k:
        Max=n
    else:
        p_list=s_list[0:2*k]
        Max=sum(p_list)
        i=1
        while i+2*k+1<=n-1:
            p_list=s_list[i:i+2*k+1]
            if sum(p_list)>Max:
                Max=sum(p_list)
            i=i+2
elif first=='1' and last=='0':
    if number0<=k:
        Max=n
    else:
        p_list=s_list[0:2*k+1]
        Max=sum(p_list)
        i=2
        while i+2*k+1<n-1:
            p_list=s_list[i:i+2*k+1]
            if sum(p_list)>Max:
                Max=sum(p_list)
            i=i+2
        p_list=s_list[n-2*k:]
        if sum(p_list)>Max:
            Max=sum(p_list)
elif first=='1' and last=='1':
    if number0<=k:
        Max=n
    else:
        p_list=s_list[0:2*k+1]
        Max=sum(p_list)
        i=2
        while i+2*k+1<=n-1:
            p_list=s_list[i:i+2*k+1]
            if sum(p_list)>Max:
                Max=sum(p_list)
            i=i+2
print(Max)

Submission status: Time Limit Exceeded

Solution code:
from functools import reduce
from operator import add

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

current_status = "1"
s = [0]
for h in s_law:
    if h == current_status:
        s[-1] += 1
    else:
        s.append(1)
        current_status = h

join_num = k * 2 + 1

max_rev = 0
for i, state in enumerate(s):
    if i % 2 == 1:
        continue
    max_rev = max(reduce(add, s[i:i + join_num]), max_rev)

print(max_rev)

Submission status: Time Limit Exceeded

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

postureL=[]
countL=[]
index=0
count=0
bef=0
while index < len(listS):
    if index == 0:
        count=1
        bef=listS[index]
    else:
        if listS[index] == bef:
            count+=1
        else:
            postureL.append(bef)
            countL.append(count)
            count=1
            bef=listS[index]
    index+=1
postureL.append(bef)
countL.append(count)

sumL=[]
index=0
sumA=0
while index < len(countL):
    sumA+=countL[index]
    sumL.append(sumA)
    index+=1

scoreL=[]
index=0
while index < len(postureL):
    sum=0
    missionC=0
    index2=index
    while index2 < len(postureL):
        if postureL[index2] == "0":
            missionC+=1
            if missionC == k+1:
                break
        index2+=1
    if index == 0:
        sum = sumL[index2-1]
    else:
        sum=sumL[index2-1]-sumL[index-1]
    scoreL.append(sum)
    index+=1
print(max(scoreL))

Submission status: Time Limit Exceeded

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

postureL=[]
countL=[]
index=0
count=0
bef=0
while index < len(listS):
    if index == 0:
        count=1
        bef=listS[index]
    else:
        if listS[index] == bef:
            count+=1
        else:
            postureL.append(bef)
            countL.append(count)
            count=1
            bef=listS[index]
    index+=1
postureL.append(bef)
countL.append(count)

scoreL=[]
index=0
while index < len(postureL):
    sum=0
    missionC=0
    index2=index
    while index2 < len(postureL):
        if postureL[index2] == "1":
            sum+=countL[index2]
        else:
            missionC+=1
            if missionC > k:
                break
            else:
                sum+=countL[index2]
        index2+=1
    scoreL.append(sum)
    index+=1
print(max(scoreL))

Submission status: Time Limit Exceeded

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

from itertools import groupby

def gen(s):
  if s[:1] == '0':
  	yield 0
  for ke, g in groupby(s):
    yield len(list(g))
  if s[-1:] == '0':
    yield 0


mc = 0
aaa = list(gen(s))
r = 2 * k + 1
if len(aaa) <= r:
  mc = sum(aaa)
else:
  for i in range(0, len(aaa) - r + 1 , 2):
    mc = max(mc, sum(aaa[i:i+r]))
print(mc)

Submission status: Time Limit Exceeded

Solution code:
import re

input_str=input()
N=int(input_str.split()[0])
K=int(input_str.split()[1])
str_teams = str(input())
sub_str_teams = str_teams

def next_check_val(check_val):
  if check_val == '1':
    return '0'
  else:
    return '1'

cur_check_val = str_teams[0]
teams_group = []
teams_state = ''

## 直立/逆立ちのグループに分ける処理
for i in range(N):
  person = str_teams[i]
  if str_teams[i] == cur_check_val:
    teams_state = teams_state + str_teams[i]
  else:
    teams_group.append(teams_state)
    teams_state = str_teams[i]
    cur_check_val = next_check_val(cur_check_val)

teams_group.append(teams_state)
#print(teams_group)

## 0グループの個数がK以下の場合
## 人数を回答にしてexitする
count_0 = 0
for i in range(len(teams_group)):
  if teams_group[i].startswith('0'):
    count_0 += 1
if count_0 <= K:
  print(N)
  exit()

sum_handstand = [] ## 累積和を格納するリスト

for i in range(len(teams_group)):
  handstands = 0     ## 各累積和を一時的に格納する変数
  if ('0' in teams_group[i]):
    for k in range(K*2):
      try:
        handstands += len(teams_group[i+k])
      except IndexError:
        pass
  else:
    for k in range(K*2+1):
      try:
        handstands += len(teams_group[i+k])
      except IndexError:
        pass
  sum_handstand.append(handstands)


### 最初の累積和を計算する処理
### 先頭が0か1かによって処理を分割している
#if ('0' in teams_group[0]):
#  for k in range(K*2):
#    handstands += len(teams_group[k])
#else:
#  for k in range(K*2+1):
#    handstands += len(teams_group[k])
#
#sum_handstand.append(handstands)
#
### 途中の累積和を計算する処理
#for i in range(1,len(teams_group)-(K*2)):
#  ## 先頭が0か1かによって処理を分割している
#  if ('0' in teams_group[i]):
#    #print(sum_handstand[i-1] , teams_group[i-1])
#    handstands = sum_handstand[i-1] - len(teams_group[i-1])
#  else:
#    #print(sum_handstand[i-1] , teams_group[i-1] , teams_group[i+K+1] , teams_group[i+K+2])
#    handstands = sum_handstand[i-1] - len(teams_group[i-1]) + (len(teams_group[i+K+1])+len(teams_group[i+K+2]))
#  sum_handstand.append(handstands)
#
#handstands = 0
### 最後の累積和を計算する処理
#if ('0' in teams_group[-1]):
#  for k in range(0,K*2):
#    handstands += len(teams_group[len(teams_group)-2*K+k])
#  sum_handstand.append(handstands)


print(max(sum_handstand))

Submission status: Time Limit Exceeded

Solution code:
n,k = map(int,input().split())
s = input()
li = []
count = 1
max1 = 0

for i in range(len(s)-1):
    if s[i] == s[i+1]:
        count += 1
    else:
        li.append(count)
        count = 1
li.append(count)

m = len(li)

if s[0] == '1':
    t = int(m/2) # ０の塊の数
    if k >= t:
        print(sum(li))
    else:
        for i in range(t-k+1):
            a = sum(li[2*i:2*(i+k)+1])
            max1 = max(a,max1)
        print(max1)
else:
    t = int(m/2+1)
    if k >= t:
        print(sum(li))
    else:
        max1 = max(max1,sum(li[:2*k]))
        for i in range(t-k):
            a = sum(li[2*i+1:2*(i+k)+2])
            max1 = max(max1,a)
        print(max1)