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: Runtime Error

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

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

print(calc_list)

def zeros(calc_list):
    zero_list = [1 if calc_list[0] == 0 else 0]
    for i in range(1, len(calc_list)):
        if calc_list[i] == 0:
            zero_list.append(zero_list[i-1] + 1)
        else:
            zero_list.append(0)

    return zero_list


max_times = max(calc_list)
for _ in range(K):
    for i in range(len(calc_list)):
        if calc_list.count(max_times) == 1:
            if calc_list[i] == max_times:
                for j in range(max_times):
                    calc_list[i-j] = 0
                break
        else:
            zero_list = zeros(calc_list))
            index = zero_list.index(max(zero_list))

    print('calc', calc_list)
    print('zeros', zeros(calc_list))
    max_times = max(calc_list)

Submission status: Runtime Error

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

nagasa = len(S)

zero_counter = 0
if S[0] == 0:
    zero_counter += 1


for i in range(nagasa-1):
    if int(S[i+1]) - int(S[i]) == -1:
        zero_counter += 1


start_i_list = [0 for _ in range(zero_counter - K + 1)]
end_i_list = [0 for _ in range(zero_counter - K + 1)]


list_index_s = 0
list_index_e = 0
k_counter = 0
if S[0] == 0:
    k_counter += 1


for i in range(nagasa-1):
    if int(S[i+1]) - int(S[i]) == 1:
        start_i_list[list_index] = i
        list_index_s += 1
    if int(S[i+1]) - int(S[i]) == -1:
        k_counter += 1
        if k_counter == K+1:
            k_counter -= 1
            end_i_list[list_index_e] = i
            list_index_e += 1

ans_list = [0 for i in range(len(end_i_list))]

for i in range(len(end_i_list)):
    ans_list[i] = end_i_list[i] - start_i_list[i]

print(max(ans_list))

Submission status: Runtime Error

Solution code:
import sys

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

ones_list = S.split('0')
zeros_list = S.split('1')

ones = []
if S[0] == '0':
    ones.append(0)
for k in range(len(ones_list)):
    if ones_list[k] != '':
        ones.append(len(ones_list[k]))
if S[-1] == '0':
    ones.append(0)

zeros = []
for k in range(len(zeros_list)):
    if zeros_list[k] != '':
        zeros.append(len(zeros_list[k]))

if K >= len(zeros):
    print(len(S))
    sys.exit

if len(zeros) == 0:
    print(len(S))
    sys.exit

if len(ones) == 0:
  	print(len(S))
    sys.exit

result = []
maximum = 0
for k in range(K):
    maximum += ones[k]
    maximum += zeros[k]
maximum += ones[K]
result.append(maximum)


for j in range(len(ones) - K - 1):
    maximum += zeros[K+j] + ones[K+1+j] - ones[j] - zeros[j]
    result.append(maximum)
print(max(result))

Submission status: Runtime Error

Solution code:
N, K = [int(i) for i in input().split()]
s = input()
S = [int(s[i]) for i in range(len(s))]
 
def cal_l(S, N):
    m = 0
    max_f = 0
    for i in range(N):
        if (S[i]==1):
            max_f += 1
        elif(S[i]==0):
            m = max(m, max_f)
            max_f = 0
    m = max(m, max_f)
    return m
  
for i in range(K):
    pair = []
    temp = 0
    for i in range(N):
        if i < N-1:
            if (S[i]==0 and S[i+1]==0):
                temp += 1
            elif(S[i]==0):
                pair.append([i-temp,temp])
                temp = 0
        else:
            if(S[i]==0):
                pair.append([i,temp])
                temp = 0
    max_m = 0
    max_s = []
    for i in range(len(pair)):
        temp = S.copy()
        s, l = pair[i]
        for j in range(l+1):
            temp[s+j]=1
        m = cal_l(temp, N)
        if (m > max_m):
            max_m = m
            max_s = temp.copy()
    S = max_s.copy()
print (max_m)

Submission status: Runtime Error

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

S2 = []
x = 1
for i in range(1,N):
    if S[i-1] == S[i]:
        x += 1
    elif S[i-1] != S[i] or i == N-1:
        S2.append(x)
        x = 1

if S[0] == "0":
    for i in range(K):
        y = S2.index(max(S2[1::1]))
        S2[y] += S2[y-1]+S2[y+1]
        S2.remove(S2[y-1])
        S2.remove(S2[y+1])
    else:
        ans = max(S2)
else:
    for i in range(K):
        y = S2.index(max(S2[0::1]))
        S2[y] += S2[y-1]+S2[y+1]
        S2.remove(S2[y-1])
        S2.remove(S2[y+1])
    else:
        ans = max(S2)

print(ans)

Submission status: Runtime Error

Solution code:
N,K = map(int,input().split())
S = input()
S_0 = list(S.split("1"))
S_1 = list(S.split("0"))

print(S_0,S_1)

S_1_len = [len(i) for i in S_1]
S_1_len = [j for j in S_1_len if j != 0]

S_0_len = [len(i) for i in S_0]
S_0_len = [j for j in S_0_len if j != 0]

print(S_1_len)
print(S_0_len)
if S_0_len:
    max0_index = S_0_len.index(max(S_0_len))
else:
    max0_index = 0

print(max0_index)
count1 = 0

if S[0] == "1":
        #S_1_len[max0_index] = S_0_len[max0_index] + S_1_len[max0_index] + S_1_len[max0_index+1]
    count += (S_0_len[max0_index] + S_1_len[max0_index] + S_1_len[max0_index+1])
    for i in range(K-1):
        count += (S_0_len[max0_index-i] + S_1_len[max0_index-i])
else:
    count += (S_0_len[max0_index] + S_1_len[max0_index] + S_1_len[max0_index-1])
    for i in range(K-1):
        count += (S_0_len[mex0_index-i] + S_1_len[max0_index-i])

print(count)

Submission status: Runtime Error

Solution code:
import numpy as np
import re
N, K = map(int, input().split())
i = input()
s = ''.join(map(str, list(i)))
c = np.array(list(map(len, re.findall('0*', s))))
index = np.where(c>0)[0]+1
if len(index) > 0:
  index[0] = 1
if len(index) > 1:
  index[len(index)-1] = N

d = -index[:-len(index)//2] + index[len(index)//2:]
if len(d) > 0:
  print(np.max(d))
else:
  print(1)

Submission status: Runtime Error

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

arr=[0]
if s[0]==0:
    arr.append(0)
target=s[0]
for i in range(len(s)):
    if s[i]==target:
        arr[len(arr)-1]+=1
    else:
        target=s[i]
        arr.append(1)
if s[len(s)-1]==0:
    arr.append(0)

for i in range(k):
    l=[]
    for j in range(1,len(arr), 2):
        l.append(arr[j-1] + arr[j] + arr[j+1])
    m=max(l)
    idx=l.index(m)*2
    arr=arr[0:idx] + [m] + arr[(idx+3):(len(arr))]
print(max(arr))

Submission status: Runtime Error

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

if s[0]=='0':
    flag=0
else:
    flag=1

l=[]
i=0
x=0
while (i<n):
    if flag==0:
        if s[i]=='0':
            x+=1
        else:
            l.append(x)
            x=1
            flag=1
    else:
        if s[i]=='1':
            x+=1
        else:
            l.append(x)
            x=1
            flag=0
    i+=1
l.append(x)

m=len(l)
t=2*k+1
if s[0]=='1':
    if m<=t:
        ans=n
    else:
        ans=0
        for i in range(t):
            ans+=l[i]
        if m%2==1:
            x=ans
            for i in range(0,m-t,2):
                x+=(l[i+t+1]+l[i+t]-l[i+1]-l[i])
                if ans<x:
                    ans=x
        else:
            x=ans
            for i in range(0,m-t-1,2):
                x+=(l[i+t+1]+l[i+t]-l[i+1]-l[i])
                if ans<x:
                    ans=x
            y=0
            for i in range(m-2*k,m):
                y+=l[i]
            if ans<y:
                ans=y
else:
    if m<=2*k:
        ans=n
    else:
        ans=0
        for i in range(2*k):
            ans+=l[i]
        if m%2==0:
            for i in range(1,1+t):
                x+=l[i]
            if ans<x:
                ans=x
            for i in range(1,m-t-1,2):
                x+=(l[i+t+1]+l[i+t]-l[i+1]-l[i])
                if ans<x:
                    ans=x
        else:
            for i in range(1,1+t):
                x+=l[i]
            if ans<x:
                ans=x
            for i in range(1,m-t-1,2):
                x+=(l[i+t+1]+l[i+t]-l[i+1]-l[i])
                if ans<x:
                    ans=x
            y=0
            for i in range(m-2*k,m):
                y+=l[i]
            if ans<y:
                ans=y
print(ans)

Submission status: Runtime Error

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

L=[]

P=S[0]
cnt=1
for i in range(1,N):
    if S[i]==P:
        cnt+=1
    else:
        if P=="1":
            L.append(cnt)
            cnt=1
            P="0"
        else:
            L.append(cnt*-1)
            cnt=1
            P="1"
if P=="1":
    L.append(cnt)
else:
    L.append(cnt*-1)
X=[0 for i in range(len(L))]
L=X+L+X
#print(L)
ANS=[]
NUM=0
for i in range(1,2*K-1):
    NUM+=abs(L[i])
ANS=[NUM]
#print(ANS)

SUM=[0]
for i in range(1,len(L)):
    SUM.append(SUM[i-1]+abs(L[i]))

ans=0
if len(SUM)-len(X)<2*K+1:
    print(SUM[len(SUM)-1])
else:    
    for i in range(len(SUM)-len(X)):
        if L[i]<0:
            if (SUM[2*K+1+i]-SUM[i])>ans:
                ans=(SUM[2*K+1+i]-SUM[i])
        else:
            if (SUM[2*K+i]-SUM[i])>ans:
                ans=(SUM[2*K+i]-SUM[i])
            #print(i)
    print(ans)

Submission status: Runtime Error

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

cnt_arr = [0]
idx = 0
cnt_seq_0 = 1
cnt_seq_1 = 0
cnt_seq_0_arr = []
cnt_seq_1_arr = []
idx_seq_0 = []

prev = 1
for i, s_i in enumerate(S):
    if s_i == 0:
        if prev == 0:
            cnt_seq_0 += 1
        elif prev == 1:
            cnt_seq_1_arr.append(cnt_seq_1)
            cnt_seq_1 = 1
        if prev != 0:
            idx_seq_0.append(i)
        if i == len(S) - 1:
            cnt_seq_0_arr.append(cnt_seq_0)
            cnt_seq_1_arr.append(0)
    elif s_i == 1:
        if prev == 1:
            cnt_seq_1 += 1
        elif prev == 0:
            cnt_seq_0_arr.append(cnt_seq_0)
            cnt_seq_0 = 1
        if i == len(S) - 1:
            cnt_seq_1_arr.append(cnt_seq_1)
    prev = s_i

r_cnt = {}

import heapq
sum_arr = []

if len(cnt_seq_0_arr) == 0:
    print(len(cnt_seq_1_arr))
else:
    sum_0 = cnt_seq_1_arr[0]
    for i in range(K):
        sum_0 += cnt_seq_0_arr[i] + cnt_seq_1_arr[i + 1]

    heapq.heappush(sum_arr, -sum_0)
    sum_i = sum_0

    for i in range(0, len(cnt_seq_0_arr) - K):
        sum_i += -cnt_seq_0_arr[i] - cnt_seq_1_arr[i] + cnt_seq_0_arr[i + K] + cnt_seq_1_arr[i + K + 1]
        heapq.heappush(sum_arr, -sum_i)

    print(-heapq.heappop(sum_arr))

Submission status: Runtime Error

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

one_list = []
zero_list = []

cur_char = S[0]
char_num = 0
for i in range(N):
    if S[i] == cur_char:
        char_num += 1
    else:
        if cur_char == '0':
            zero_list.append(char_num)
        else:
            one_list.append(char_num)
        cur_char = S[i]
        char_num = 1
if cur_char == '0':
    zero_list.append(char_num)
else:
    one_list.append(char_num)

len_zero_list = len(zero_list)
len_one_list = len(one_list)
# print(len_one_list)
# print(len_zero_list)

cont_length_list = []

if K >= len(zero_list):
    ans = N
else:
    if S[0] == '1':
        tmp_length = sum(one_list[:K+1])+sum(zero_list[:K])
        cont_length_list.append(tmp_length)
        for i in range(min(len_zero_list,len_one_list)-K):
            if K+1+i<len_one_list:
                tmp_length += one_list[K+1+i]+zero_list[K+i]
            else:
                tmp_length += zero_list[K+i]
            tmp_length -= one_list[i]+zero_list[i]
            cont_length_list.append(tmp_length)
        # print(cont_length_list)
    else:
        tmp_length = sum(one_list[:K])+sum(zero_list[:K])
        cont_length_list.append(tmp_length)
        if K < len_zero_list:
            tmp_length += zero_list[K]+one_list[K]
            tmp_length -= zero_list[0]
            cont_length_list.append(tmp_length)
            for i in range(1, min(len_zero_list,len_one_list)-K):
                if K+i<len_one_list and K+i < len_zero_list:
                    tmp_length += one_list[K+i]+zero_list[K+i]
                elif K+i<len_one_list:
                    tmp_length += one_list[K+i]
                else:
                    tmp_length += zero_list[K+i]
                tmp_length -= one_list[i-1]+zero_list[i]
                cont_length_list.append(tmp_length)
        # print(cont_length_list)
    # print(cont_length_list)
    ans = max(cont_length_list)

print(ans)

Submission status: Runtime Error

Solution code:
import sys

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


tmp1 = s[0]
count = 1
data = []
zeros = 0

for i in range(1,len(s)):
    tmp2 = s[i]
    if tmp1 != tmp2:
        data.append((tmp1, count))
        if tmp1 == '0':
            zeros += 1
        tmp1 = tmp2
        count = 1
    else:
        count += 1

data.append((tmp1,count))
if tmp1 == '0':
    zeros += 1

ans = 0

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

if data[0][0] == '0':
    data.insert(0, (1,0))

if data[-1][0] == '0':
    data.append((1,0))

anstmp = 0
for i in range(2*k + 1):
    anstmp += data[i][1]

ans = max(ans, anstmp)

for i in range(zeros - k):
    anstmp -= data[2*i][1]
    anstmp -= data[2*i + 1][1]
    anstmp += data[2*k + 2*i + 1][1]
    anstmp += data[2*k + 2*i + 2][1]

    ans = max(ans, anstmp)


print(ans)

Submission status: Runtime Error

Solution code:
l,k=map(int,input().split())
s=input()
import re
c=lambda i,n:[len(x.group()) for x in re.finditer(str(n)+'+',i)]
l1=c(s,1)
l0=c(s,0)
f=max(l1)
a=[]
for i in range(len(l0)-k+1):
  a.append(sum(l0[i:i+k]))
b=a
for j in range(len(l0)-k+1):
  i=a.index(max(b))
  b.pop(b.index(max(b)))
  
  if s[0]=='1':
    p=l1[i:i+k+1]
  else:
    p=l1[i:i+k]
  m=sum(l0[i:i+k])+sum(p)
  if f<m:f=m
print(f)

Submission status: Runtime Error

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

count = 0
countList = []
zeroCounter = 0

for i in range(len(S)):
  if i == 0:
    now = S[0]
    count += 1
  else:
    if now == S[i]:
      count += 1
    else:
      countList.append([now, count])
      if now == "0":
        zeroCounter += 1
      now = S[i]
      count = 1
countList.append([now, count])
if now == "0":
  zeroCounter += 1
  
if zeroCounter <= K:
  print(N)
else:
  MAX = 0
  endCount = 0
  flag = 0
  index = 0
  for z in range(len(countList)):
    if countList[z][0] == "0":
      endCount += 1
      MAX += countList[z][1]
    else:
      MAX += countList[z][1]
    if endCount == K:
      MAX += countList[z+1][1]
      z += 1
      PRE = int(MAX)
      break
  while flag != 1:
    NOW = int(PRE) - int(countList[z-2*K][1]) - int(countList[z-2*K+1][1]) + int(countList[z+1][1]) + int(countList[z+2][1])
    z += 2
    PRE = NOW
    if NOW >= MAX:
      MAX = NOW
    if z >= len(countList) - 2:
      flag = 1
    
  


print(MAX)

Submission status: Runtime Error

Solution code:
n, k = map(int,input().split())
s = input()
ha = []
owa = []
ju = 0
for i in range(len(s)) :
    if s[i] == "1" :
        if ju == 0 :
            ha.append(i)
            ju = 1
    else :
        if ju == 1 :
            owa.append(i - 1)
            ju = 0
if ha[0] != 0 :
    ha.insert(0,0)
    owa.insert(0,0)
owa.append(len(s)-1)
if len(ha) != len(owa) :
    ha.append(len(s) - 1)
#print(ha,owa)
dai = 0
for i in range(len(owa) - k) :
    dai = max(dai,owa[i+k] - ha[i] + 1)
if len(ha) > k :
    print(dai)
else :
    print(len(s))

Submission status: Runtime Error

Solution code:
N, K = [int(i) for i in input().split()]
s = input()
S = [int(s[i]) for i in range(len(s))]
 
def cal_l(S, N):
    m = 0
    max_f = 0
    for i in range(N):
        if (S[i]==1):
            max_f += 1
        elif(S[i]==0):
            m = max(m, max_f)
            max_f = 0
    m = max(m, max_f)
    return m
  
for i in range(K):
    pair = []
    temp = 0
    for i in range(N):
        if i <= N-1:
            if (S[i]==0 and S[i+1]==0):
                temp += 1
            elif(S[i]==0):
                pair.append([i-temp,temp])
                temp = 0
        else:
            if(S[i]==0):
                pair.append([i,temp])
                temp = 0
    max_m = 0
    max_s = []
    for i in range(len(pair)):
        temp = S.copy()
        s, l = pair[i]
        for j in range(l+1):
            temp[s+j]=1
        m = cal_l(temp, N)
        if (m > max_m):
            max_m = m
            max_s = temp.copy()
    S = max_s.copy()
print (max_m)

Submission status: Runtime Error

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

zero_count_list = []
zero_count = 0
one_count_list = []
one_count = 0
s_size = len(s)

for index, i in enumerate(s):
    if i == "0":
        zero_count += 1
    else:
        if zero_count != 0:
            zero_count_list.append(zero_count)
        zero_count = 0
    if index == s_size - 1:
        if zero_count != 0:
            zero_count_list.append(zero_count)

for index, i in enumerate(s):
    if i == "1":
        one_count += 1
    else:
        if one_count != 0:
            one_count_list.append(one_count)
        one_count = 0
    if index == s_size - 1:
        if one_count != 0:
            one_count_list.append(one_count)
if k => len(zero_count_list):
    print(s_size)
    sys.exit(0)

Submission status: Runtime Error

Solution code:
import re
import numpy as np
N, K = [int(item) for item in re.split(r"\s", input())]
S = [int(item) for item in list(input())]

if sum(S) == len(S) or sum(S) == 0:
    print(len(S))
else:
    S = S + [2]

    index = [-1]
    for i in range(N):
        if S[i] != S[i + 1]:
            index.append(i)

    count = np.diff(np.array(index)).tolist()

    start = 0
    if S[0] == 0:
        start = 1

    # print(index)
    # print(count)

    cumsum = [0] + np.cumsum(count).tolist()
    c_len = len(cumsum)
    win = min(2 * K + 1, c_len - 1)
    print(max([cumsum[i + win] - cumsum[i] for i in range(start, c_len - win, 2)]))

Submission status: Runtime Error

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

one_list = []
zero_list = []

cur_char = S[0]
char_num = 0
for i in range(N):
    if S[i] == cur_char:
        char_num += 1
    else:
        if cur_char == '0':
            zero_list.append(char_num)
        else:
            one_list.append(char_num)
        cur_char = S[i]
        char_num = 1
if cur_char == '0':
    zero_list.append(char_num)
else:
    one_list.append(char_num)

len_zero_list = len(zero_list)
len_one_list = len(one_list)
# print(len_one_list)
# print(len_zero_list)

cont_length_list = []

if K >= len(zero_list):
    ans = N
else:
    if S[0] == '1':
        tmp_length = sum(one_list[:K+1])+sum(zero_list[:K])
        cont_length_list.append(tmp_length)
        for i in range(len_zero_list-K):
            if K+1+i<len_one_list:
                tmp_length += one_list[K+1+i]+zero_list[K+i]
            else:
                tmp_length += zero_list[K+i]
            tmp_length -= one_list[i]+zero_list[i]
            cont_length_list.append(tmp_length)
        # print(cont_length_list)
    else:
        tmp_length = sum(one_list[:K])+sum(zero_list[:K])
        cont_length_list.append(tmp_length)
        if K < len_zero_list:
            tmp_length += zero_list[K]+one_list[K]
            tmp_length -= zero_list[0]
            cont_length_list.append(tmp_length)
            for i in range(1, min(len_zero_list,len_one_list)-K):
                if K+i<len_one_list and K+i < len_zero_list:
                    tmp_length += one_list[K+i]+zero_list[K+i]
                elif K+i<len_one_list:
                    tmp_length += one_list[K+i]
                else:
                    tmp_length += zero_list[K+i]
                tmp_length -= one_list[i-1]+zero_list[i]
                cont_length_list.append(tmp_length)
        # print(cont_length_list)
    # print(cont_length_list)
    ans = max(cont_length_list)

print(ans)

Submission status: Runtime Error

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

center = []
side = [0]

last = '1'
for s in S:
    if s != last:
        if s == '0':
            center.append(0)
        else:
            side.append(0)
    if s == '0':
        center[-1] += 1
    else:
        side[-1] += 1
    last = s
if last == '0':
    side.append(0)

if len(center) <= K:
    print(N)
else:
    max_ = sum(center[:K]) + sum(side[:K+1])
    for i in range(len(center) - K + 1):
        max_ = max(max_, max_ + (center[K+i]+side[K+1+i]) - (center[i]+side[i]))
    print(max_)

Submission status: Runtime Error

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

ms = []
a1 = ''
fptrs = []
lptrs = []

flg = 0
i = 0

for i in range(N):
	if(S[i] == '0' and flg == 0):
		fptrs.append(i)
		flg = 1
	elif(S[i]== '1' and flg == 1):
		lptrs.append(i)
		flg = 0
	else:
		pass
	i = i + 1

m = 0
fptrss = []
lptrss = []

for i in range(len(fptrs) - K + 1):
	buf_f = []
	buf_l = []
	for j in range(K):
		buf_f.append(fptrs[i+j])
		buf_l.append(lptrs[i+j])
	fptrss.append(buf_f)
	lptrss.append(buf_l)

cntbufs = []
m = 0

for fptrs,lptrs in zip(fptrss,lptrss):
	bufs = []
	cntbufs = []
	for c in S:
		bufs.append(c)
	for fptr,lptr in fptrs,lptrs:
		for i in range(fptr,lptr):
			bufs[i] = '1'
	bufs = ''.join(bufs)
	cntbuf = 0
	for i,c in enumerate(bufs):
		if(c == '1' and i != len(bufs) - 1):
			cntbuf = cntbuf + 1
		elif(c == '1' and i == len(bufs) - 1):
			cntbuf = cntbuf + 1
			cntbufs.append(cntbuf)
		else:
			cntbufs.append(cntbuf)
			cntbuf = 0
	if(m < max(cntbufs)):
		m = max(cntbufs)

if fptrs == []:
	m = N

print(m)

Submission status: Runtime Error

Solution code:
import sys

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

ones_list = S.split('0')
zeros_list = S.split('1')

ones = []
if S[0] == '0':
    ones.append(0)
for k in range(len(ones_list)):
    if ones_list[k] != '':
        ones.append(len(ones_list[k]))
if S[-1] == '0':
    ones.append(0)

zeros = []
for k in range(len(zeros_list)):
    if zeros_list[k] != '':
        zeros.append(len(zeros_list[k]))

if K >= len(zeros):
    print(len(S))
    sys.exit

if (len(zeros) == 0) | (len(ones) == 0):
    print(len(S))
    sys.exit

result = []
maximum = 0
for k in range(K):
    maximum += ones[k]
    maximum += zeros[k]
maximum += ones[K]
result.append(maximum)


for j in range(len(ones) - K - 1):
    maximum += zeros[K+j] + ones[K+1+j] - ones[j] - zeros[j]
    result.append(maximum)
print(max(result))

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
s=input()
_tmp,tmp_=0,0
l=[]
r=[]
for i in range(n):
    if s[i]=='1':
        if _tmp==0:
            l.append(i)
            _tmp=1
    else:
        _tmp=0
    if s[n-i-1]=='1':
         if tmp_==0:
            r.append(n-i-1)
            tmp_=1
    else:
        tmp_=0
k=min(k,len(l)-1)
ans=0
for j in range(k):
    ans=max(ans,r[j]-l[k-j]+1)
ans=max(ans,n-1-l[k]+1)
ans=max(ans,r[k]+1)
if k>len(l):
    ans=n
print(ans)

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())
s = input()
s += "-"
s_list =[0]  * (k * 3)
counter = []
temp_count = 1

for i in range(n):

    if s[i] == s[i + 1]:
        temp_count += 1

    else:
        s_list += [temp_count]
        print(s_list)
        for j in range(k * 2):
            temp_count += s_list[i - j]

        counter += [temp_count]
        temp_count = 1

print(counter)

Submission status: Runtime Error

Solution code:
a,k=map(int,input().split())
ls=list(map(int,input()))
le=len(ls)
zl=[]
flag=False
for i in range(le):
    if ls[i]==0 and flag==False:
        f=i
        flag=True
    if ls[i]==1 and flag==True:
        l=i-1
        flag=False
        zl.append([f,l])
    if i==le-1 and flag==True:
        l=i
        flag=False
        zl.append([f,l])

zero=[]
one=[zl[0][0]]
for i in range(len(zl)):
    zero.append(zl[i][1]-zl[i][0]+1)
    if i<len(zl)-1:
        one.append(zl[i+1][0]-zl[i][1]-1)
one.append(le-zl[-1][1]-1)

ml=[]
m=0

q=len(zero)-k+1
max_list=[]
for i in range(q):
    ma=zero[i]+zero[i+1]+one[i]+one[i+1]+one[i+2]
    max_list.append(ma)
print(max(max_list))

Submission status: Runtime Error

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

num=0
A=[]
for i in range(n):
    if s[i]=="0":
        num+=1
        if i==n-1:
            A.append(num)
        else:
            if s[i+1]=="1":
                A.append(num)
                num=0
A.sort(reverse=True)

ans=0
ans+=s.count("1")
for j in range(k):
    ans=A[j]+ans
print(ans)
#print(type(A[0]))

Submission status: Runtime Error

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

one_list = []
zero_list = []

cur_char = S[0]
char_num = 0
for i in range(N):
    if S[i] == cur_char:
        char_num += 1
    else:
        if cur_char == '0':
            zero_list.append(char_num)
        else:
            one_list.append(char_num)
        cur_char = S[i]
        char_num = 1
if cur_char == '0':
    zero_list.append(char_num)
else:
    one_list.append(char_num)

len_zero_list = len(zero_list)
len_one_list = len(one_list)
# print(len_one_list)
# print(len_zero_list)

cont_length_list = []

if K >= len(zero_list):
    ans = N
else:
    if S[0] == '1':
        tmp_length = sum(one_list[:K+1])+sum(zero_list[:K])
        cont_length_list.append(tmp_length)
        for i in range(len_zero_list-K):
            if K+1+i<len_one_list:
                tmp_length += one_list[K+1+i]+zero_list[K+i]
            else:
                tmp_length += zero_list[K+i]
            tmp_length -= one_list[i]+zero_list[i]
            cont_length_list.append(tmp_length)
        # print(cont_length_list)
    else:
        tmp_length = sum(one_list[:K])+sum(zero_list[:K])
        cont_length_list.append(tmp_length)
        if K < len_zero_list:
            tmp_length += zero_list[K]+one_list[K]
            tmp_length -= zero_list[0]
            cont_length_list.append(tmp_length)
            for i in range(1, len_zero_list-K):
                if K+i<len_one_list and K+i < len_zero_list:
                    tmp_length += one_list[K+i]+zero_list[K+i]
                elif K+i<len_one_list:
                    tmp_length += one_list[K+i]
                else:
                    tmp_length += zero_list[K+i]
                tmp_length -= one_list[i-1]+zero_list[i]
                cont_length_list.append(tmp_length)
        # print(cont_length_list)
    # print(cont_length_list)
    ans = max(cont_length_list)

print(ans)

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())
s = input()
s = list(s)
ans = s
len = []
for i in range(n):
  l = 0
  if s[i] == '0':
    l += 1
    j = i
    while s[j+1] != '0':
      l += 1
  len.append(l)
print(len)

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
s=input()
s='01'+s+'10'
S=[]
E=[]
ans=[]
for i in range(n+3):
  if s[i:i+2]=='01':
    S.append(i+1)
  if s[i:i+2]=='10':
    E.append(i)
if S==[] and E==[]:
  print(n)
else:
  for j in range(len(E)-k):
    ans.append(abs(S[j]-E[j+k]))
  ans[0]-=1
  ans[len(ans)-1]-=1
  print(max(ans)+1)

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
s=input()
_tmp,tmp_=0,0
l=[]
r=[]
for i in range(n):
    if s[i]=='1':
        if _tmp==0:
            l.append(i)
            _tmp=1
    else:
        _tmp=0
    if s[n-i-1]=='1':
         if tmp_==0:
            r.append(n-i-1)
            tmp_=1
    else:
        tmp_=0
k=min(k,len(l)-1)
ans=0
for j in range(k):
    ans=max(ans,r[j]-l[k-j]+1)
ans=max(ans,n-1-l[k]+1)
ans=max(ans,r[k]+1)
print(ans)

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
s=input()
l=len(s)
word='1'*l
num=0
lst=[]
L=[]
for i in range(n):
    if i==0:
        if s[i]==0:
            num+=1
            lst.append(0)
            L.append(0)
        else:
            L.append(1)
    else:
        if s[i]=='0':
            L.append(0)
            if s[i-1]=='1':
                num+=1
                lst.append(i)
        if s[i]=='1':
            L.append(L[-1]+1) 
if num==0:
    print(n)
else:
    p=min(num-k,k)
    q=len(lst)
    ans=0
    r=q-p
    for i in range(r):
        if i+p-1!=q-1:
            ans=max(ans,L[lst[i]-1]+lst[i+p]-lst[i]+1)
        else:
            ans=max(ans,L[lst[i]-1]+max(lst[i+p],l)-lst[i]+1)
print(ans)

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
# スペース区切りの整数の入力
a, b = map(int, input().split())

# 整数の入力
humans = list(str(input()))
h_lis = []
temp = []
temp.append(humans[0])
for i in range(1, len(humans)):
    if humans[i-1] == humans[i]:
        temp.append(humans[i])
    else:
        h_lis.append(''.join(temp))
        temp = []
        temp.append(humans[i])
h_lis.append(''.join(temp))


#1にしてくっつけてまた分離して１だけ数えるを繰り返す
maxx = []
maxx_lis = []
for k in range(b):
    for i in range(len(h_lis)):
        ttt = int(h_lis[i])
        lll = h_lis.copy()
        if ttt == 0:
            temp = []
            for j in range(len(h_lis[i])):
                temp.append(str(1))
            lll[i] = ''.join(temp)
        h_t = ''.join(lll)
        maxx_lis.append(h_t)

        h_lis_t = []
        temp = []
        temp.append(h_t[0])
        for j in range(1, len(h_t)):
            if h_t[j-1] == h_t[j]:
                temp.append(h_t[j])
            else:
                h_lis_t.append(''.join(temp))
                temp = []
                temp.append(h_t[j])
        h_lis_t.append(''.join(temp))

        ##1の数え上げ
        max_t = 0
        for j in range(len(h_lis_t)):
            if int(h_lis_t[j]) != 0:
                if len(h_lis_t[j]) > max_t:
                    max_t = len(h_lis_t[j])
        maxx.append(max_t)
    max_t = maxx[0]
    index = 0
    for i in range(1, len(maxx)):
        if maxx[i] > max_t:
            max_t = maxx[i]
            index = i
    h_lis = maxx_lis[index]

list_a = list(h_lis)

print(h_lis.count('1'))
    

#1to0でひとくくり配列を作る、前の状態を保持して(for)０のそれぞれを１にした時の最大値を実行する、繰り返す