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 = [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 = cal_l(S, N)
    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 = 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_s] = 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:
N = int(input())
S = input()
n = list(map(int, str(S)))
count = 1
mas = []
for i in range(N-1):
    if n[i] == n[i + 1]:
        count += 1
    else:
        mas.append(count)
        count = 1
        if i == N-2:
            mas.append(1)
        
l = len(mas)
mas2 = []
for m in K:
    

    if l%2==0:
            for k in range(l//2):
                mas2.append(mas[2*k+1] + mas[2*k] + mas[2*k+2])
        else:
            for k in range(l/2-1):
                mas2.append(mas[2*k+1] + mas[2*k] + mas[2*k+2])
            mas.append(mas[l-2] + mas[l-1])
    
    else:
        else:
            mas2.append(mas[0] + mas[1])
            for k in range(1, l//2):
                mas2.append(mas[2*k-1] + mas[2*k] + mas[2*k+1])
        else:
            mas2.append(mas[0] + mas[1])
            for k in range(1, l//2-1):
                mas2.append(mas[2*k+1] + mas[2*k] + mas[2*k-1])
            mas.append(mas[l-2] + mas[l-1])

            
            
print(max(mas2))

Submission status: Runtime Error

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

## 連続するものをlistとして保持する
import re

def nchars(s,n):
    reg = re.compile("(.)\\1{%d,}" % (n-1))
    while True:
        m = reg.search(s)
        if not m:
            break
        yield m.group(0)
        s =s[m.end():]
        
candidates = list(nchars(S,1))
## いい具合にnumpyで操作
import numpy as np
count_0 = np.zeros((N))
count_1 = np.zeros((N))

for i,candidate in enumerate(candidates):
  if int(candidate[0]) == 0:
    count_0[i] += len(candidate)
  else:
    count_1[i] += len(candidate)
    
## 最小の試行回数
min_Try = len(np.where(count_0 != 0)[0])

change_ = np.where(count_0 != 0)[0]
## 最小の試行回数以下の時どうするかが厳しいな...
## 連続しないので、とりあえず大きい順に入れていけばオッケー
## 個数分だけ全部足して、indexで見たときに一番大きいやつが正解！！
if min_Try <= K :
  print(K)
else:
  count_ = count_0 + coount_1
  change_list = []
  for k in range(min_Try-K):
    change_list.append(np.sum(count[change_[k:k+K]]))
  print(max(change_list))

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
S = input()
S = S + "2"
chokuritsu = []
sakadachi = []
flag = S[0]
count = 1
for c in S:
    if flag != c:
        if c == "1":
            chokuritsu.append(count)
        elif c == "0":
            sakadachi.append(count)
        elif c == "2":
            if flag == "1":
                sakadachi.append(count-1)
            else:
                chokuritsu.append(count-1)

        count = 1
        flag = c
    else:
        count += 1
ans = 0
sub_ans = 0

if S[0] == "1":
    for i in range(min(K, len(sakadachi) - 1)):
        sub_ans += sakadachi[i] + chokuritsu[i]
    sub_ans += sakadachi[min(K-1, len(sakadachi)-1)]
    ans = max(ans, sub_ans)
    for i in range(len(sakadachi) - K):
        sub_ans += sakadachi[i+K] + chokuritsu[i+K-1]
        sub_ans -= sakadachi[i] + chokuritsu[i-1]
        ans = max(ans, sub_ans)

    if S[-1] == "0":
        sub_ans = chokuritsu[-1]
        for i in ragne(len(sakadachi)-1, len(sakadachi) - 1 - min(K -1, len(sakadachi)), -1):
            sub_ans += sakadachi[i] + chokuritsu[i]
    ans = max(ans, sub_ans)

else:
    sub_ans = chokuritsu[0]
    for i in range(min(K - 1, len(sakadachi) - 1)):
        sub_ans += sakadachi[i] + chokuritsu[i]
    ans = max(ans, sub_ans)
    sub_ans = 0
    for i in range(min(K, len(sakadachi) - 1)):
        sub_ans += sakadachi[i] + chokuritsu[i+1]
    sub_ans += sakadachi[min(K-1, len(sakadachi)-1)]

    for i in range(len(sakadachi) - K):
        sub_ans += sakadachi[i+K] + chokuritsu[i+K]
        sub_ans -= sakadachi[i] + chokuritsu[i]
        ans = max(ans, sub_ans)

    if S[-1] == "0":
        sub_ans = chokuritsu[-1]
        for i in ragne(len(sakadachi)-1, len(sakadachi) - 1 - min(K -1, len(sakadachi)), -1):
            sub_ans += sakadachi[i] + chokuritsu[i]
    ans = max(ans, sub_ans)

print(ans)

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 s == "0" :
    print(1)
else :
    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:
import numpy as np
n,k=map(int,input().split())
s=input()
A=[]
B=[]
a=0
ans=0
s+="2"
for i in range(n):
    if s[i]!=s[i+1]:
        A.append(i-a)
        B.append(i-a+B[i-1])
        a+=i
Amax=max(A)
index=A.index(max(A))
if (len(A)-1)/2<k:
    ans=n
dp=np.zeros((n,2))
dp[0][0]=A[0]
for j in range(n):
    for i in range(23*k+1):
        dp[j+1][i+1]=max(dp[j][i]+A[i],dp[j][i])
print(dp[len(A)][2*k+1])

Submission status: Runtime Error

Solution code:
NK = input().split()
s = input()

K = int(NK[1])

zero = ['0']
one = ['1']


if "0" in s:
    #make 0配列
    for i in range(len(s)):

        if i > 0:
            zero.append('0' + zero[i - 1])
            one.append('1' + one[i - 1])

    for k in range(K):
        for i in range(len(zero)):
            if(zero[len(zero) - i - 1] in s):

                s = s.replace(zero[len(zero) - i - 1], one[len(one) - i - 1])

                break

    for i in range(len(zero)):
        if(one[len(one) - i - 1] in s):
            result = i



    print(result )
else:
    print(len(s))

Submission status: Runtime Error

Solution code:


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))
 
else:
  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 = list(map(int, input().split()))
T = input()
S = [int(x) for x in list(str(T))]

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

if S[0] == 0:
    cand = L[0] + L[1]
    l = len(L)
    for i in range(1, (l+1)//2):
        length = L[2*i - 1] + L[2*i] + L[2*i + 1]
        cand = max(cand, length)
    print(cand)

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
S = input()
width,l,r = [],[],[]

for i in range(K):
  if "0" not in S:
    break()
  for j in range(N-1):
    if j == 0:
      sw = 1
      l.append(0)
      for k in range(j,N-1-j):
        if S[k] == "0" and S[k+1] == "1":
          r.append(k+1) #r自体は1
          sw = 2
        if sw == 2 and S[k] == "1" and S[k+1] == "0":
          width.append(k-j)
          break
    if S[j] == "0" and S[j+1] == "1":
      sw = 1
      for k in range(j+1,N-1-j):
        if S[k] == "1" and S[k+1] == "0":
          l.append(k+1)
        if S[k] == "0" and S[k+1] == "1":
          r.append(k+1) #r自体は1
          sw = 2
        if sw == 2 and S[k] == "1" and S[k+1] == "0":
          width.append(k-j)
          break
  idx = width.index(max(width))
  ans = max(width)
  S = S[:l[idx]] + "1"*(r[idx]-l[idx]) + S[r[idx]:]
print(ans)

Submission status: Runtime Error

Solution code:
import numpy as np

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


def func3(arr_sign, list_set):
    list_t = []
    for pos in np.where(arr_sign==0)[0]:
#         print(pos)
        if pos == 0:
            list_t.append([[list_set[0] + list_set[1]] + list_set[2:], arr_sign[1:]])
        elif pos == len(list_set)-1:
            list_t.append([list_set[:-2] + [list_set[-2] + list_set[-1]], arr_sign[:-1]])
        else:
            list_t.append(list_set[:pos-1] + [list_set[pos-1] + list_set[pos] + list_set[pos+1]] + list_set[pos+2:], [arr_sign[:-2]])

    max_t = max([max(t[0]) for t in list_t])
    return [t[1] for t in list_t if max(t[0]) == max_t], [list(t[0]) for t in list_t if max(t[0]) == max_t]


arr_s = np.array([int(x) for x in list(str(s))])

list_set = []
tmp = 1
for i in range(1,arr_s.shape[0]):
    if arr_s[i-1] == arr_s[i]:
        tmp += 1
    else:
        list_set.append(tmp)
        tmp = 1
list_set.append(tmp)
list_set = [list_set]

arr_sign = np.array([0 if i%2 == int(s[0]) else 1 for i in range(len(list_set))])

for turn in range(k):
    l = []
    al = []
    for s in list_set:
        arr_sign_tmp, list_set_tmp = func3(arr_sign, s)
        l += list_set_tmp
        al += arr_sign_tmp
    list_set = l
    if max([max(s) for s in  list_set]) == n:
        print(n)
        exit()
    
    
print(max([max(s) for s in  list_set]))

Submission status: Runtime Error

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

now = "0"
nl = []
for i, s in enumerate(S):
    if s != now:
        now = s
        nl.append(i)

nl_even = nl[0::2]
nl_odd = nl[1::2]
if s[-1] == "1":
    nl_odd.append(len(S))
    y = min(k, len(nl_even)-1)
if s[0] == "0":
    nl_odd.insert(0, 0)
    y = min(k, len(nl_odd))
if len(nl_odd)>1:
    x = nl_odd[1]
else:
    x = len(S)

y = min(k, len(nl_even)-1)
for i in range(len(nl_even)-y):
    x_ = nl_odd[i+y] - nl_even[i]
    x = max(x, x_)

print(x)

Submission status: Runtime Error

Solution code:
import sys
n,k = map(int,input().split())
st = input()
s = []
for i in st:
    s.append(i)
s.append('-1')
man = 1 if s[0] == '0' else 0

check_ls = [];check_num = 0;tyokuritu = []
for i in range(len(s)):
    check_num += 1
    if s[i] != s[i+1]:
        check_ls.append(check_num)
        check_num = 0
        if s[i] == '0':
            tyokuritu.append(True)
    if s[i+2] == '-1':break
if k >= len(tyokuritu):print(n)
else:
    ios_check = [0]+[check_ls[0]]*(len(check_ls))
    for i in range(len(check_ls)-1):
        ios_check[i+1+1] = check_ls[i+1] + ios_check[i+1]
    ans = 0
    for i in range(0,10**5,2):
        if (k*2+1+i+man) > (len(ios_check)-1):break
        ans = max(ans,ios_check[k*2+1+i+man] - ios_check[i+man])
    if man == 1:ans = max(ans,ios_check[k*2])
    print(ans)

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())
s = input()
memo = []
c = 0
ps = ''
lens = len(s) - 1
b = 0
if s[0] == '1':
    b = 1

for i in range(lens + 1):
    if s[i] != ps:
        memo.append(c)
        c = 1
        ps = s[i]
        if i == lens:
            memo.append(1)
        continue
    if i == lens:
        memo.append(c+1)
    c += 1
    ps = s[i]
sum = 0
if  len(memo) < k * 2 + b + 1:
    print(sum(memo))
    else:
    sum = sum(memo[: k * 2 + b + 1])
    temp = [sum]
    memo.append(0)
    # print(memo)
    # print(len(memo))
    for i in range(0, len(memo) - 2 * k  - 1, 2):
        sum = sum - memo[i] - memo[i + 1] + memo[i + 2 * k  + 1] + memo[i + 2 * k + 2]
        temp.append(sum)
    # print(temp)
    print(max(temp))

Submission status: Runtime Error

Solution code:
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 + 2):
            t += s_list[i - j]

        counter += [t]
        temp_count = 1
        t = 0

print(max(counter))

Submission status: Runtime Error

Solution code:
N,K=input().split()
N = int(N)
K = int(K)
S = input()
ini = 0
i = 0
while S[i] == "0":
    ini = ini+1
    i += 1
S = int(S)
len_list=[]
one_count = 0
zero_count = 0
def ten_am(a):
    global zero_count
    global one_count
    x = a//10
    y = a%10
    if y == 0:
        zero_count +=1
        if one_count != 0:
            len_list.append(one_count)
        one_count=0
    else:
        one_count +=1
        if zero_count != 0:
            len_list.append(zero_count)
        zero_count =0
    if x != 0:
        ten_am(x)
    else:
        len_list.append(max([one_count,zero_count]))
    return len_list
len_list = ten_am(S)
if ini !=0:
    len_list.append(ini)
si = len(len_list)
ans = sum(len_list[0:2*K])
if K+1 > si :
    print (sum(len_list))
else:
    for i in range(si-2*K):
        s = sum(len_list[i+1:i+2*K+2])
        if s > ans:
            ans = s
    print(ans)

Submission status: Runtime Error

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

arr = []
cnt = 0
comp = S[0]
for s in S:
    if s==comp:
        cnt += 1
    else:
        if comp=='0':
            arr.append(-cnt)
        else:
            arr.append(cnt)
        cnt = 1
        comp = s
if comp=='0':
    arr.append(-cnt)
else:
    arr.append(cnt)

def iter(A, cnt):
    if cnt>=K or len(A)<2:
        return max(A)
    elif len(A)==2:
        return abs(A[0]) + abs(A[1])
    m = -A[0]+A[1]
    arg = -1
    if A[0]>0:
        i=0
    else:
        i=1
    while i<len(A)-2:
        s3 = A[i] - A[i+1] + A[i+2]
        if m<s3:
            m = s3
            arg = i
        i += 2
    s3 = A[-2]-A[-1]
    if m<s3:
        m = s3
        arg = len(A)-1
    if arg==-1:
        B = [-A[0]+A[1]] + A[2:]
    elif arg==len(A)-1:
        B = A[:-2] + [A[-2]-A[-1]]
    else:
        B = A[:arg] + [A[arg] - A[arg+1] + A[arg+2]] + A[arg+3:]
    return iter(B, cnt+1)


print(iter(arr, 0))

Submission status: Runtime Error

Solution code:

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

X=[]
cnt=0
if len(s)>=2:
    for i in range(n-1):
        cnt+=1
        if s[i]=='0' and s[i+1]=='1':
            X.append(cnt)
            cnt=0
        elif s[i]=='1' and s[i+1]=='0':
            X.append(cnt)
            cnt=0
        elif i==n-2:
            X.append(cnt)
    
    if s[-1]!=s[-2]:
        X.append(1)
    else:
        X[-1]+=1
else:
    X=[s]


if s[0]=='1':
    if k>=len(X[1::2]):
        print(n)
    else:
        z=X[1::2]
        o=X[::2]
        ans=sum(o[:k+1])+sum(z[:k])
        for i in range(len(z)-k):
            ans=max(ans,ans-z[i]-o[i]+z[i+k]+o[i+k+1])
        print(ans)
else:
    if k>=len(X[0::2]):
        print(n)
    else:
        z=X[::2]
        o=X[1::2]
        ans=sum(z[:k])+sum(o[:k])
        for i in range(len(o)-k):
            ans=max(ans,ans-o[i]-z[i]+o[i+k]+z[i+k+1])
        print(ans)

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
S = input()
s = []
count = []
sum = 0
save = 0
for i in range(len(S)):
    s.append(S[i])

print(s.index('0'))
for j in range(s.count('0')):
    for i in range(K):
        point = s.index('0', save)
        sum += point + 1 - check
        save = point
        check = sum
    count[j] = sum
    
print(max(count))

Submission status: Runtime Error

Solution code:
n,k = map(int,input().split())
s = input()
c = [int(s[i]) for i in range(n)]
count = []
cost = 0
if c[0] == 0:
  cost += -1
else:
  cost += 1
for i in range(1,n):
  if c[i] == c[i-1]:
    cost += -1 if cost < 0 else 1
  else:
    count.append(cost)
    cost = -1 if cost > 0 else 1
count.append(cost)
i = 0
M = 0
l = 0
r = 0
for j in range(len(count)):
  if count[j] < 0:
    M += -count[j]
    i += 1
  else:
    M += count[j]
  if i == k:
    if j < len(count)-2:
      M += count[j+1]
      r = j + 1
    else:
      r = j
    break
tmp = M
if r < len(count) - 2:
  if count[0] < 0:
    tmp += count[0]
    l += 1
    r += 1
    tmp += -count[r]
    if r+1 < k:
      tmp += count[r + 1]
      r += 1
    M = max(M,tmp)
  while r + 1 < K -1:
    tmp -= count[l]
    tmp += count[l+1]
    l += 2
    r += 1
    tmp += -count[r]
    if r+1 < k:
      tmp += count[r + 1]
      r += 1
    M = max(M,tmp)
print(M)

Submission status: Runtime Error

Solution code:
NK = input().split()
s = input()

K = int(NK[1])

zero = ['0']
one = ['1']


if "0" in s:
    #make 0配列
    for i in range(len(s)):

        if i > 0:
            zero.append('0' + zero[i - 1])
            one.append('1' + one[i - 1])

    for k in range(K):
        for i in range(len(zero)):
            if(zero[len(zero) - i - 1] in s):
                print(zero[len(zero) - i - 1])
                print(one[len(one) - i - 1])
                s = s.replace(zero[len(zero) - i - 1], one[len(one) - i - 1])

                print(s)
                break

    for i in range(len(zero)):
        if(one[len(one) - i - 1] in s):
            result = i



    print(result )
else:
    print(len(s))

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-2,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:
import numpy as np
n, k = (int(i) for i in input().split())  
s = input()

i = 0
count_list = []
starts_from = s[0]
tmp = s[0]
count = 0

while i < n:
  if s[i] == tmp: # still same 0/1
    count += 1
  else:
    count_list.append(count)
    tmp = s[i]
    count = 1
  i += 1
count_list.append(count)  # last

sum_list = []

sum_list = []
for j, count in enumerate(count_list):
  if starts_from == "0":
    if j == 0:
      sum_list.append(count_list[0] + count_list[1])
    elif j % 2 == 0:
      sum_list.append(count_list[j-1] + count_list[j] + count_list[j+1])
  else:
    if j + 1 > len(count_list):
      sum_list.append(count_list[j-1] + count_list[j])
    else:
      sum_list.append(count_list[j-1] + count_list[j] + count_list[j+1])
      
i = 0
sum = 0

while i < k:
  if i == 0:
    sum += max(sum_list)
    before_idx = np.argmax(sum_list)
    max_idx = np.argmax(sum_list) 
  else:
    max_idx = np.argmax(sum_list)
    if max_idx - before_idx == 1:
      if starts_from == "0":
        sum -= count_list[before_idx * 2 - 1]
      else:
        sum -= count_list[before_idx * 2]
      sum += max(sum_list)
    else:
      sum += max(sum_list)
    before_idx = np.argmax(sum_list) 
  sum_list[max_idx] = 0
  i += 1

print(sum)

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 re

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

def popcnt(n):
  return bin(n).count("1")

cands = []
for xor in range(2 ** len(S)):
  if len(re.split('1+', bin(xor))) != 2:
    continue
  cands.append(xor)

for _ in range(K):
  best_score = max([len(v) for v in re.findall(r'1+', S)])
  best_xor = 0
  for xor in cands:
    res = bin(intS ^ xor)
	matchs = re.findall(r'1+', res)      
    if matchs:
      score = max([len(v) for v in matchs])
    else:
      score = 0
    
    if score > best_score:
      best_score = score
      best_xor = xor
      # print(xor, bin(xor), res, bin(res), _popcnt)
        
  intS = intS ^ best_xor
    
print(max([len(v) for v in re.findall(r'1+', bin(intS))]))

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 and K+1+i > len_zero_list:
                tmp_length += one_list[K+1+i]+zero_list[K+i]
            elif K+i < len_zero_list:
                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 = 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()

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-2,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 = []
ans = 0
for i in range(len(s)-1):
    ans += 1
    if s[i+1] != s[i]:
        l.append(ans)
        ans = 0
l.append(ans+1)

k = min(len(l)-1,k)
if s[0] == '1' or n%2 == 0:
    print(max([sum(l[j:j+k*k+1]) for j in range(len(l)-k*k)]))
else:
    print(max([sum(l[j:j+k*k+2]) for j in range(len(l)-k*k-1)]))

Submission status: Runtime Error

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

if (p[0] == '1') or (len(a) == 1):
     kaisi = 0
else:
     kaisi = 1

ans = 0
anslist = []

for i in range(kaisi,min(2*k+1+kaisi,len(a))):
     ans += a[i]
anslist.append(ans)

for i in range(kaisi+2,len(a),2):
     if i+2*k <= len(a)-1:
          ans = ans-a[i-2]-a[i-1]+a[i+2*k]+a[i+2*k-1]
          anslist.append(ans)
     else:
          break
ans = 0
if (kaisi != 0) and (k != 0):
     for i in range(0,2*k):
          ans += a[i]
     anslist.append(ans)

print(max(anslist))