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()
nowL = 1
if S[0] == '0':
    nowL = 2
L = [0] * (N+1)
for i in range(N-1):
    L[nowL] = L[nowL] + 1
    if S[i] != S[i+1]:
        L[nowL+1] = L[nowL]
        nowL = nowL + 1
# 終端処理
L[nowL] = L[nowL] + 1

ans = 0
for j in range(N):
    if 2*j+2*K+1 > len(L) or L[2*j+2*K+1] == 0:
        break
    ans = max(ans,L[2*j+2*K+1]-L[2*j])
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]
    args = [-1]
    for i in range(len(A)-2):
        s3 = A[i] - A[i+1] + A[i+2]
        if m<s3:
            m = s3
            args = [i]
        elif m==s3:
            args.append(i)
    s3 = A[-2]-A[-1]
    if m<s3:
        m = s3
        args = [len(A)-1]
    elif m==s3:
        args.append(len(A)-1)
    cands = []
    for arg in args:
        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:]
        cands.append(iter(B, cnt+1))
    return max(cands)


print(iter(arr, 0))

Submission status: Runtime Error

Solution code:
def init(a):
    cf=a[0]
    cc=0
    z=[]
    for it in a:
        if(it==cf):
            cc+=1
        else:
            if(cf=='0'):
                z.append(-cc)
            else:
                z.append(cc)
            cf=it
            cc=1
    z.append(cc)
    return z

p=[]
def solve(a,K):

    if(K==0):
        p.append(max(a))
        return
    N=len(a)
    if(K%2==0):
        if(a[0]>0):
            b=[]
            b.append(a[1]-a[0])
            for j in range(2,N):
                b.append(a[j])
            solve(b,K-1)
        if(a[N-1]>0):
            b=[]
            for j in range(2,N):
                b.append(a[j])
            b.append(a[N-2]-a[N-1]) 
            solve(b,K-1)            
        for i in range(1,N-1):
            if(a[i]>0):
                b=[]
                for j in range(0,i-1):
                    b.append(a[j])
                b.append(a[i-1]-a[i]+a[i+1])
                for j in range(i+2,N):
                    b.append(a[j])
                solve(b,K-1)
    else:
        if(a[0]<0):
            b=[]
            b.append(a[1]-a[0])
            for j in range(2,N):
                b.append(a[j])
            solve(b,K-1)
        if(a[N-1]<0):
            b=[]
            for j in range(2,N):
                b.append(a[j])
            b.append(a[N-2]-a[N-1]) 
            solve(b,K-1)            
        for i in range(1,N-1):
            if(a[i]<0):
                b=[]
                for j in range(0,i-1):
                    b.append(a[j])
                b.append(a[i-1]-a[i]+a[i+1])
                for j in range(i+2,N):
                    b.append(a[j])
                solve(b,K-1)

N,K=list(map(int,input().split(" ")))
a=str(input())
z=init(a)
L=len(a)
Q=0
if(z[0]<0):
    Q=N//2+1
else:
    Q=N//2
if(K>=Q):
    print(N)
else:

    solve(z,K)
    print(max(p))

Submission status: Runtime Error

Solution code:
N, K = map(int,input().split())
S = list(map(int,input().strip()))
cur = S[0]
tmp = [] if cur == 1 else [0]
count = 1
def ngram(x,n):
  return [x[i:i+n] for i in range(0,len(x) - n + 1)]

for s in S[1:]:
  if s != cur:
    tmp.append(count)
    cur = s
    count = 0
  count += 1

tmp.append(count)
if cur == 0:
  tmp.append(0)
  
if len(tmp) == 1:
  print(tmp[0])
else:
  odd = tmp[::2]
  even = tmp[1::2]
  print(max([sum(a) + sum(b) for a,b in zip(ngram(odd,K+1),ngram(even,K))]))

Submission status: Runtime Error

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

arr = [0, 0]
prv = '0'
cnt = 0
for c in s:
    if c == prv:
        cnt += 1
    else:
        arr.append(cnt)
        prv = c
        cnt = 1
arr.append(cnt)
if prv == '1':
    arr.append(0)
arr.append(0)
arr.append(0)
cur = 0
for i in range(0, min(2 * k, len(arr)), 2):
    cur += arr[i]
    cur += arr[i + 1]
ans = cur
for i in range(0, len(arr), 2):
    if len(arr) <= i + 2 * k + 1:
        break
    if 0 < i:
        cur -= arr[i - 1]
    cur -= arr[i]
    cur += arr[i + 2 * k]
    cur += arr[i + 2 * k + 1]
    ans = max(ans, cur)
print(ans)

Submission status: Runtime Error

Solution code:
import sys
stdin = sys.stdin 
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline()
N,K=na();S=ns()
l=[]
c1=0
c0=0
x0=0
first=True
if(S[0]=="1"):
    c1=1
else:
    c0=1
for i in range(1,N):
    if(S[i]=="1"):
        if(S[i]!=S[i-1]):
            l.append(c0)
            c0=0
        c1+=1
    else:
        if(S[i]!=S[i-1]):
            x0+=1
            l.append(c1)
            c1=0
        c0+=1
if(c1):
    l.append(c1)
else:
    x0+=1
    l.append(c0)
#print(l)
#print(x0)
if(x0<=K):
    print(N)
    exit()
else:
    res=0
    if(int(S[0])>0):
        if(int(S[-1])>0):
            right = 0
            left = 2*K
            while(True):
                if(left>=len(l)):
                    print(res)
                    exit()
                res=max(sum(l[right:left+1]),res)
                #print(res,right,left)
                right+=2
                left+=2
        else:
            right = 0
            left = 2*K
            #print("llll")
            res=max(sum(l[len(l)-(1+(K-1)*2)-1:]),res)
            #print(len(l)-(1+(K-1)*2))
            while(True):
                if(left>=len(l)-1):
                    print(res)
                    exit()
                res=max(sum(l[right:left+1]),res)
                #print(res,right,left)
                right+=2
                left+=2
    else:
        if(int(S[-1])>0):
            res=max(res,sum(l[0:2*K]))
            right = 1
            left = 2*K+1
            while(True):
                if(left>=len(l)):
                    print(res)
                    exit()
                res=max(sum(l[right:left+1]),res)
                #print(res,right,left)
                right+=2
                left+=2
        else:
            right = 0
            left = 2*K
            #最後
            res=max(sum(l[len(l)-(1+(K-1)*2)-1:]),res)
            #最初
            res=max(res,sum(l[0:2*K]))
            right = 1
            left = 2*K+1
            while(True):
                if(left>=len(l)-1):
                    print(res)
                    exit()
                res=max(sum(l[right:left+1]),res)
                #print(res,right,left)
                right+=2
                left+=2

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())
s = input()
if n == 1:
    print(1)
if n != 1:
    nums = []
    cnt = 1
    for i in range(1, n):
        if s[i - 1] != s[i]:
            nums.append([s[i - 1], cnt])
            cnt = 1
            if i == n - 1:
                nums.append([s[i], cnt])
        else:
            cnt += 1
            if i == n - 1:
                nums.append([s[i - 1], cnt])

    candi = 0
    ansli = []
    p = 0
    for i in range(len(nums)):
        if nums[i][0] == "0":
            p += 1
    k = min(k, p)

    i1 = 0
    j1 = 0
    while i1 <= k:
        if i1 == k and nums[j1][0] == "0":
            pass
        else:
            candi += nums[j1][1]
        if nums[j1][0] == "0":
            i1 += 1
        j1 += 1
    ansli.append(candi)

    a = 0
    b = j1 - 1
    while b < len(nums):
        for i in range(2):
            if nums[a + i][0] == "1":
                candi -= nums[a + i][1]
            elif nums[a + i][0] == "0":
                candi -= nums[a + i][1]
                a += i + 1
                break
        for i in range(2):
            if b + i >= len(nums):
                ansli.append(candi)
                b += 1
                break
            else:
                if nums[b + i][0] == "1":
                    candi += nums[b + i][1]
                    b += i + 1
                    ansli.append(candi)
                    break
                elif nums[b + i][0] == "0":
                    candi += nums[b + i][1]

    print(max(ansli))

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)

elif s[0] == '0' and s[-1] == '0':
    anstmp = 0
    for i in range(2*k):
        anstmp += data[i][1]
    
    # print('初期',anstmp)
    ans = max(ans, anstmp)

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

        # print('次',anstmp)
        ans = max(ans, anstmp)

    i = (zeros - k - 1)
    anstmp -= data[2*i][1]
    anstmp += data[2*k + 2*i][1]

    # print('次',anstmp)
    ans = max(ans, anstmp)


if s[0] == '1' and s[-1] == '0':
    anstmp = 0
    for i in range(2*k + 1):
        anstmp += data[i][1]
    
    # print('初期',anstmp)
    ans = max(ans, anstmp)

    for i in range(zeros - k - 1):
        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]
    
        # print('次',anstmp)
        ans = max(ans, anstmp)

    i = (zeros - k - 1)
    anstmp -= data[2*i][1]
    anstmp -= data[2*i + 1][1]
    anstmp += data[2*k + 2*i][1]
 
    # print('次',anstmp)
    ans = max(ans, anstmp)


if s[0] == '1' and s[-1] == '1':
    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)

if s[0] == '0' and s[-1] == '1':
    anstmp = 0
    for i in range(2*k):
        anstmp += data[i][1]
    
    ans = max(ans, anstmp)

    i = 0
    anstmp -= data[2*i + 1][1]
    anstmp += data[2*k + 2*i][1]
    anstmp += data[2*k + 2*i + 1][1]

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

        ans = max(ans, anstmp)

print(ans)

Submission status: Runtime Error

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

tmpu = []

count = 0
onecount = 0
for i in S:
    if i == '0':
        count += 1
    if i == '1':
        tmpu.append(count)
        count=0
        onecount += 1


tmpu.sort(reverse=True)
for n in range(K):
    onecount += tmpu[n]

print(onecount)

Submission status: Runtime Error

Solution code:
from itertools import*
from numpy import*
n,k=map(int,input().split())
k2=k+1
s=input()
if len(s)==1:
    if s=="1":
        print(1)
    else:
        print(0)
    exit()
p=[len(list(v))for k,v in groupby(s)if k=="0"]
q=[len(list(v))for k,v in groupby(s)if k=="1"]
if len(p)==1 and len(q)==0 and k>0:
    print(sum(p))
    exit()
elif len(q)==1 and len(p)==0:
    print(sum(q))
    exit()
if s[0]=="1" and s[-1]=="0":
    x=[0]+p
    y=[0]+q+[0]
elif s[0]=="0" and s[-1]=="1":
    x=[0]+p+[0]
    y=[0]+q
else:
    x=[0]+p+[0]
    y=[0]+q+[0]
x1=array(x[:-k])
x2=array(x[k:])
x3=sum(x[:k])
t=x3+cumsum(x2-x1)
y1=array(y[:-k2])
y2=array(y[k2:])
y3=sum(y[:k2])
f=y3+cumsum(y2-y1)
#print(x,y)
#print(t,f)
print(max(max(t+f),x3+y3))

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python3

import math

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

S_list = list(S)
ans_sub = []

if S_list[0] == "0":
    ans_sub.append(0)#直立
else:
    ans_sub.append(1)#逆立ち

num = 0
for i in range(1,N):
    num += 1
    if i != N-1:
        if S_list[i-1] != S_list[i]:
            ans_sub.append(num)
            num = 0
    elif i == N-1:
        if S_list[i-1] != S_list[i]:
            ans_sub.append(num)
            ans_sub.append(1)
        else:
            ans_sub.append(num+1)


if ans_sub[0] == 0:
    if math.ceil(len(ans_sub)-1) <= K:
        print(len(S_list))

    else:
        


    
elif ans_sub[0] == 1:
    if math.floor(len(ans_sub)-1) <= K:
        print(len(S_list))

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
s=list(input())
s=list(map(int,s))
stack=[]
tmp=1
for i in range(1,len(s)):
  if s[i]!=s[i-1]:
    stack.append(tmp)
    tmp=1
  else:
    tmp+=1
stack.append(tmp)
if s[0]==1:
  if len(s)//2<=k:
    print(len(s))
    exit()
if s[0]==0:
  if (len(s)+1)//2<=k:
    print(len(s))
    exit()
acc=[]
acc2=[]
ans=0
if s[0]==1:
  Acc=stack[0]
  Acc2=0
  acc.append(Acc)
  acc2.append(0)
  for i in range(1,len(stack)-len(stack)%2,2):
    Acc+=stack[i]
    Acc+=stack[i+1]
    acc.append(Acc)
    Acc2+=stack[i-1]
    Acc2+=stack[i]
    acc2.append(Acc2)
  if len(stack)%2==0:
    Acc2+=stack[i+1]
    Acc2+=stack[i+2]
    acc2.append(Acc2)
  if len(stack)%2==1:
    Acc+=stack[-1]
    acc.append(Acc)
    Acc2+=stack[-2]
    Acc2+=stack[-1]
    acc2.append(Acc2)
  for i in range(len(acc)-k):
    ans=max(ans,acc[i+k]-acc2[i])
if s[0]==0:
  Acc=stack[0]
  Acc2=0
  acc.append(0)
  acc.append(Acc)
  acc2.append(0)
  for i in range(1,len(stack)-1-len(stack)%2,2):
    Acc+=stack[i]
    Acc+=stack[i+1]
    acc.append(Acc)
    Acc2+=stack[i-1]
    Acc2+=stack[i]
    acc2.append(Acc2)
  if len(stack)%2==0:
    Acc2+=stack[i+1]
    Acc2+=stack[i+2]
    acc2.append(Acc2)
  if len(stack)%2==1:
    Acc+=stack[-1]
    acc.append(Acc)
    Acc2+=stack[-2]
    Acc2+=stack[-1]
    acc2.append(Acc2)
  for i in range(len(acc)-k):
    ans=max(ans,acc2[i+k]-acc[i])
print(ans)

Submission status: Runtime Error

Solution code:
import re, copy

n, k = input().split(" ")
k = int(k)
input_str = input()
input_list = list(input_str)


pattern_0 = re.compile(r"(0+)")
pattern_1 = re.compile(r"(1+)")
list_0 = []

def get_longest_1_in_str(string):
    res = pattern_1.findall(string)
    res_list = [len(s) for s in res]
    return max(res_list)

for r in pattern_0.finditer(input_str):
    list_0.append([r.start(),len(r.group())])


length_list = []
for i in range(0,len(list_0)):
    new_string_list = copy.copy(input_list)
    for j in range(k):
        if i + j >= len(list_0): break
        new_string_list[list_0[i+j][0]:list_0[i+j][0] + list_0[i+j][1]] = ['1' for j in range(0, list_0[i+j][1])]


    m = get_longest_1_in_str("".join(new_string_list))
    length_list.append(m)

print(max(length_list))

Submission status: Runtime Error

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

list0 = list(map(len, S.split("1")))
list1 = list(map(len, S.split("0")))
list0 = [x for x in list0 if x != 0]
list1 = [x for x in list1 if x != 0]

if S[0] == "0":
    list1.insert(0, 0)
else:
    list0.append(0)
# print(list0, list1)

ans = 0

if K >= len(list0):
    ans = N
else:
    for i in range(len(list0)- K + 1):
        num1 = sum(list1[i:i+K])
        num0 = sum(list0[i:i+K])
        ex = 0
        if i+K < len(list0):
            ex = list1[i+K]
        ans = max(ans, num0 + num1 + ex)
    
print(ans)

Submission status: Runtime Error

Solution code:
N,K=map(int,input().split())
S=input()
N=int(len(S))
s=list(S)
c=[]
sum=0
cnt1=0
cnt0=0
d=K*2+1
for i in range(N):
  if s[i]=="1":
    if cnt0!=0:
      c.append(cnt0)
    cnt1=cnt1+1
    cnt0=0
  else:
    if cnt1!=0:
      c.append(cnt1)
    cnt0=cnt0+1
    cnt1=0
if cnt0!=0:
  c.append(cnt0)
if cnt1!=0:
  c.append(cnt1)
C=int(len(c))
for i in range(d):
  sum=sum+c[i]
big=sum
for i in range(C-d):
  sum=sum+c[i+d]-c[i]
  if big<=sum:
    big=sum
print(big)

Submission status: Runtime Error

Solution code:
N, K = list(map(int, input().split()))
S = input()
ct = 1
li0 = []
li1 = []
cap = S[0]
su = 0
if N != 1:
    for i in range(N - 1):
        if S[i + 1] == S[i]:
            ct += 1
        else:
            if S[i] == '0':
                li0.append(ct)
            else:
                li1.append(ct)
            ct = 1
    if S[i+1] == '0':
        li0.append(ct)
    else:
        li1.append(ct)
    for i in range(len(li0) - K + 1):
        num = sum(li0[i:i + K])
        if su < num:
            su = num
            idx = i
    print(max((su + sum(li1[idx:idx + K+1])), max(li1)))
else:
    print(1)

Submission status: Runtime Error

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

tmpu = []

count = 0
onecount = 0
for i in S:
    if i == '0':
        count += 1
    if i == '1':
        tmpu.append(count)
        count=0
        onecount += 1
if not tmp[]:
  tmp.append(0)
tmpu = sorted(tmpu)

for n in range(K):
    onecount += tmpu[n]

print(onecount)

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+1] == '-1':break
if k >= len(tyokuritu):print(len(s)-1);sys.exit()
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):
    ans = max(ans,ios_check[k*2+1+i+man] - ios_check[i+man])
    if k*2+1+i+man+2 >= len(check_ls):break
if man == 1:ans = max(ans,ios_check[k*2-1])
print(ans)

Submission status: Runtime Error

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

data = []
cnt = 0
curr = '1'
for i in range(len(s)):
    if s[i] != curr:
        data.append(cnt)
        curr = s[i]
        cnt = 1
    else:
        cnt += 1
data.append(cnt)
data.append(0)
data.append(0)

current_sum = sum(data[0:0 + 2*k+1])
ret = current_sum
for start in range(2, len(data)-k-2, 2):
    diff = -data[start-2]-data[start-1]+data[start + 2*k]+data[start + 2*k-1]
    current_sum += diff

#     print(start, val)
    if current_sum > ret:
        ret = current_sum
print(ret)

Submission status: Runtime Error

Solution code:
x,y,z,k=list(map(int,input().split()))
a=list(map(int,input().split()))
b=list(map(int,input().split()))
c=list(map(int,input().split()))
m=[]
for i in range(x):
  for j in range(y):
    for l in range(z):
      m.append(a[i]+b[j]+c[l])
m.sort(reverse=True)
for i in range(k):
  print(m[i])

Submission status: Runtime Error

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

li = []
size = []

now = S[0]
cnt = 1

for i in range(1,N,1):
  before = now
  now = S[i]
  if now == before:
    cnt += 1
  else:
    li.append(before)
    size.append(cnt)
    cnt = 1

li.append(now)
size.append(cnt)

if 3*K > len(size):
  print(sum(size))
  END = True

max_sum = 0

if END:
  pass
else:
  for i in range(K):
    try:
      now_sum = sum(size[i:i+3*K])
      if max_sum < now_sum:
          max_sum = now_sum
    except:
      print(i)

  print(max_sum)

Submission status: Runtime Error

Solution code:
def init(a):
    cf=a[0]
    cc=0
    z=[]
    for it in a:
        if(it==cf):
            cc+=1
        else:
            if(cf=='0'):
                z.append(-cc)
            else:
                z.append(cc)
            cf=it
            cc=1
    z.append(cc)
    return z

p=[]
def solve(a,K):

    if(K==0):
        p.append(max(a))
        return
    N=len(a)
    if(K%2==0):
        if(a[0]>0):
            b=[]
            b.append(a[1]-a[0])
            for j in range(2,N):
                b.append(a[j])
            solve(b,K-1)
        if(a[N-1]>0):
            b=[]
            for j in range(2,N):
                b.append(a[j])
            b.append(a[N-2]-a[N-1]) 
            solve(b,K-1)            
        for i in range(1,N-1):
            if(a[i]>0):
                b=[]
                for j in range(0,i-1):
                    b.append(a[j])
                b.append(a[i-1]-a[i]+a[i+1])
                for j in range(i+2,N):
                    b.append(a[j])
                solve(b,K-1)
    else:
        if(a[0]>0):
            b=[]
            b.append(-a[1]+a[0])
            for j in range(2,N):
                b.append(a[j])
            solve(b,K-1)
        if(a[N-1]>0):
            b=[]
            for j in range(2,N):
                b.append(a[j])
            b.append(-a[N-2]+a[N-1]) 
            solve(b,K-1)            
        for i in range(1,N-1):
            if(a[i]>0):
                b=[]
                for j in range(0,i-1):
                    b.append(a[j])
                b.append(-a[i-1]+a[i]-a[i+1])
                for j in range(i+2,N):
                    b.append(a[j])
                solve(b,K-1)


N,K=list(map(int,input().split(" ")))
a=str(input())
z=init(a)
L=len(a)
Q=0
if(z[0]<0):
    Q=N//2+1
else:
    Q=N//2
if(K>=Q):
    print(N)
else:

    solve(z,K)
    print(max(p))

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]
            tmp_length -= zero_list[0]
            cont_length_list.append(tmp_length)
            for i in range(1, len_zero_list-K):
                if K+1+i<len_one_list:
                    tmp_length += one_list[K+1+i]+zero_list[K+i+1]
                else:
                    tmp_length += one_list[K+i]
                tmp_length -= one_list[i]+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:
def kakaka():
    a = list(map(int, input().split()))
    b = map(int, input().split())
    c = int(input())

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

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

    ans = 0
    ans_data = 0
    count_0 = 0
    if data[0] == '0':
        flg = 0
    else:
        flg= 1

    if len(data_la) <= 2:
        print(sum(data_la))
    else:
        pre_data1, pre_data2 = data_la[0], data_la[1]
        pre_data1_ind, pre_data2_ind = 0, 1
        # print(data_la)
        for i in range(len(data_la)):
            if i % 2 == flg:
                count_0 += 1
            if count_0 == inst_num + 1:
                ans = ans - pre_data1 - pre_data2
                pre_data1_ind += 2
                pre_data2_ind += 2
                pre_data1, pre_data2 = data_la[pre_data1_ind], data_la[pre_data2_ind]
                count_0 -= 1
            ans += data_la[i]
            # print(ans)
            if ans_data < ans:
                ans_data = ans






        print(ans_data)






if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
n ,k = map(int,input().split())
s = list(str(input()))
for i in range(n):
  s[i] = int(s[i])
for i in range(n):
  s[i] = s[i]*2 - 1
for i in range(n-1,0,-1):
  if s[i] * s[i-1] > 0:
    s[i-1] += s[i]
    del s[i]
n0 = s[::2]
n1 = s[1::2]
count = []
co = 0
if s[0] < 0:
  for i in range(len(n0)):
    for j in range(i,n):
      co += n0[j]*(-1)
      if len(n1)>j:
        co += n1[j]
      j += 1
      if j >= k:
        break
    count.append(co)
    co = 0
else:
  for i in range(len(n1)):
    co = n0[0]
    for j in range(i,n):
      co += n1[j]*(-1) + n0[j+1]
      if j >= k:
        break
    count.append(co)
count.sort()
print(count[len(count)-1])

Submission status: Runtime Error

Solution code:
#import time

N,K = [int(x) for x in input().split()]

#t1 = time.time() 
S = input()
a= S[0]
b = int(S[0])
table = [0 for i in range(N)]
c = 0
l = 0
for s in S:
    if int(s) == b:
        c += 1
    else:
        b = not(b)
        table[l]=c
        c = 1
        l+=1

table[l]=c
#t2 = time.time() 
if a =='0':
    N = 2*K+1
    ans = sum(table[0:2*K])
    for i in range(1,l,2):
        ans = max(ans,sum(table[i:i+N]))

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

t3 = time.time() 
#print(table)
print(ans)
#print(t2-t1)
#print(t3-t2)

Submission status: Runtime Error

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

b = []
l = []
count = 1

for i in range(len(s)):
  try:
    if s[i] == s[i+1]:
      count = count + 1
    else:
      b.append(int(s[i]))
      l.append(count)
      count = 1
  except:
    b.append(int(s[i]))
    l.append(count)

if len(b) / 2 <= k:
  print(sum(l))
else:
  m = 0 
  if b[0] == 1:
    for i in range(len(b) // 2 - k + 1):
      st = max(0, i * 2)
      en = min(i * 2 + k * 2, len(b)) 
      m = max(m, sum(l[st:en]))

  else:
    for i in range(len(b + 1) // 2 - k + 1):
      st = max(0, i * 2 - 1)
      en = min(i * 2 + k * 2 - 1, len(b)) 
      m = max(m, sum(l[st:en]))

  print(m)

Submission status: Runtime Error

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

tmpu = []

count = 0
onecount = 0
for i in S:
    if i == '0':
        count += 1
    if i == '1':
        tmpu.append(count)
        count=0
        onecount += 1

tmpu = sorted(tmpu)

for n in range(K):
    onecount += tmpu[n]

print(onecount)

Submission status: Runtime Error

Solution code:
length, change_cnt = map(int, input().split())
state = input()

state_list = {0: [], 1: []}
count = 0
pre_x = None
for x in state:
    x = int(x)
    if pre_x is not None and x != pre_x:
        state_list[pre_x].append(count)
        count = 0
    count += 1
    pre_x = x
state_list[pre_x].append(count)
max_ans = max(state_list[1])
for i in range(len(state_list[0]) - change_cnt + 1):
    max_ans = max(max_ans, sum(state_list[0][i:i+change_cnt]) + sum(state_list[1][i:i+change_cnt+1]))
print(max_ans)

Submission status: Runtime Error

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

store = []
start = -1
end = -1

if 1 == len(S):
    print(1)
else:
    for i in range(N):
        if '0' == S[i] and -1 == start:
            start = i

        if '1' == S[i] and -1 != start or len(S)-1 == i:
            end = i

        if -1 != start and -1 != end:
            store.append([start, end, len(S[start:end])])
            start = end = -1

    store.sort(key=lambda x: x[2])
    store.reverse()

    li = list(S)
    for i in range(K):
        start = store[i][0]
        end = store[i][1]
        count = store[i][2]
        li[start:end] = '1' * count

    temp = 0
    count = 0
    prev = ''
    for i in range(N):
        if 0 == i:
            prev = li[i]

        if '1' == li[i] and '1' == prev:
            temp += 1

        if '0' == li[i] or len(S)-1 == i:
            count = max(temp, count)
            temp = 0
    print(count)