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())

l = list(map(int,input()))
ll = []
p = l[0]
co = 1
ppp = 0
su = 0
for i in range(n-1):
    if p == l[i+1]:
        co += 1
    else:
        if p == 0:
            ppp += 1
        ll.append([p,co])
        p = l[i+1]
        co = 1
if p == 0:
    ppp += 1
ll.append([p,co])
if len(ll) == 1:
    print(n)
else:
    if ppp <= k:
        print(n)
    else:
        ko = 0
        if ll[0][0] == 0:
            if ll[-1][0] == 0:
                for i in range(ppp-k):
                    if i == 0:
                        for j in range(2*k):
                            ko += ll[j][1]
                        su = max(su,ko)
                        ko = 0
                    elif i == co - k - 1:
                        for j in range(2*k):
                            ko += ll[-(j+1)][1]
                        su = max(su,ko)
                        ko = 0
                    else:
                        for j in range(2*k+1):
                            ko += ll[2*i+j][1]
                        su = max(su,ko)
                        ko = 0
            else:
                for i in range(ppp-k+1):
                    if i == 0:
                        for j in range(2*k):
                            ko += ll[j][1]
                        su = max(su,ko)
                        ko = 0
                    else:
                        for j in range(2*k+1):
                            ko += ll[2*i+j][1]
                        su = max(su,ko)
                        ko = 0

        else:
            if ll[-1][0] == 1:
                for i in range(ppp-k+1):
                    for j in range(2*k+1):
                        ko += ll[2*i+j][1]
                    su = max(su,ko)
                    ko = 0
            else:
                for i in range(ppp-k):
                    if i == co-k-1:
                        for j in range(2*k):
                            ko += ll[j][1]
                        su = max(su,ko)
                        ko = 0
                    else:
                        for j in range(2*k+1):
                            ko += ll[2*i+j][1]
                        su = max(su,ko)
                        ko = 0
        print(su)

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())
s = input()
if s.count("1") == n or s.count("0") == n:
    print(n)
    exit()
renzoku = []
count = 0
flag = 0
for i in range(n):
    if s[i] == "1":
        if flag:
            count += 1
        else:
            renzoku.append(count)
            count = 1
        flag = 1
    else:
        if flag:
            renzoku.append(count)
            count = 1
        else:
            count += 1
        flag = 0
renzoku.append(count)
renzoku_t = [0]
for i in range(len(renzoku)):
    renzoku_t.append(renzoku[i] + renzoku_t[i])
ans = 0
if s[0] == "0":
    ans = renzoku_t[0 + k + k] - renzoku_t[0]
for i in range(1, len(renzoku) - k - k + 1, 2):
    if i == 0 or i == len(renzoku) - k - k:
        ans = max(ans, renzoku_t[i + k + k] - renzoku_t[i])
    else:
        ans = max(ans, renzoku_t[i + k + k + 1] - renzoku_t[i])
print(ans)

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-

import sys
from itertools import accumulate

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7

N,K=MAP()
S=input()

if N==1:
    print(1)
    exit()

L=[]
if S[0]=='0':
    L.append(0)
    cur=start=0
else:
    cur=start=1
cnt=0
for i in range(N):
    if cur==0:
        if S[i]=='0':
            cnt+=1
        else:
            L.append(cnt)
            cnt=1
            cur=1
    else:
        if S[i]=='1':
            cnt+=1
        else:
            L.append(cnt)
            cnt=1
            cur=0
else:
    if cnt!=0:
        if cur==0:
            L.append(cnt)
        else:
            L.append(cnt)
if S[-1]=='0':
    L.append(0)

acc=[0]+list(accumulate(L))
acc+=[0]*100
mx=0
for i in range(0, len(acc)-10, 2):
    mx=max(mx, acc[i+(K*2+1)]-acc[i])

print(mx)

Submission status: Runtime Error

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

A = [0]
B = [0]
for s in S:
  if s == '1':
    A[-1] += 1
  elif A[-1] != 0:
    A.append(0)
  if s == '0':
    B[-1] += 1
  elif B[-1] != 0:
    B.append(0)

ans = 0

c,d,e,f = 0,0,0,0

for i in range(min(len(A),len(B))):
  a = 0
  if i == 0:
    if S[0] == '1':
      c,d = sum(A[i:min(i + K + 1,len(A))]),sum(B[i:min(i + K,len(B))])
      a = c + d
    else:
      c,d = sum(A[i:min(i + K,len(A))]) , sum(B[i:min(i + K,len(B))])
      a = c + d
  else:
    if S[0] == '1':
      if i + K <= len(A):
        c += A[min(i + K,len(A) - 1)] - A[i - 1]
        d += B[min(i + K - 1,len(A) - 1)] - B[i - 1]
        a = c + d
    else:
      a = sum(A[i - 1:min(i + K,len(A))]) + sum(B[i:min(i + K,len(B))])
      if i + K <= len(A):
        c += A[min(i + K,len(A) - 1)] - (0 if i == 1 else A[i - 2])
        d += B[min(i + K - 1,len(A) - 1)] - B[i - 1]
        a = c + d
      
  ans = max(ans,a)
print(ans)

Submission status: Runtime Error

Solution code:
import sys
input = sys.stdin.readline
N, K = map(int, input().split())
S = input()
c0, c1 = [], []
if S[0] == '1':
    c0.append(0)
i = 0
while i < N:
    p = S[i]
    c = 1
    i += 1
    while i < N and S[i] == p:
        i += 1
        c += 1
    if p == '0':
        c0.append(c)
    else:
        c1.append(c)
if S[N-1] == '0':
    c1.append(0)
l0 = len(c0)
for i in range(1, l0):
    c0[i] += c0[i-1]
    c1[i] += c1[i-1]
l = l0 - K + 1
ans = c0[K-1] + c1[K-1]
for i in range(1, l):
    tmp = c0[i+K-1] - c0[i-1] + c1[i+K-1] - c1[i-2]
    if ans < tmp:
        ans = tmp
print(ans)

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()
x=[len(list(v))for k,v in groupby(s)if k=="0"]
y=[len(list(v))for k,v in groupby(s)if k=="1"]
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(max(max(t+f),x3+y3))

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())
st = input() + "4"
cnt = 1
before = 3
line = []
ans = 0

for s in st:
    if s == before:
        cnt +=1
    else:
        line.append(cnt)
        cnt = 1
        before = s

if st[0] == "1":
    start = 1
else:
    start = 2
    ans = line[1:2*k+1]

for i in range(start, len(line)-2*k):
    ans = max(ans, sum(line[i:i+2*k+1])) 

print(ans)

Submission status: Runtime Error

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

if s[0] == '0':
  t += '0'
c = 1
for i in range(1, n):
  if s[i-1] == '0' and s[i] == '0':
    c += 1
  if s[i-1] == '1' and s[i] == '0':
    t += str(c)
    c = 1
  if s[i-1] == '0' and s[i] == '1':
    t += str(c)
    c = 1
  if s[i-1] == '1' and s[i] == '1':
    c += 1
t += str(c)
t = list(map(int, t))
print(t)

# 範囲
r = 2 * k + 1
gs = []
for i in range(0,n,2):
  if i + r < n:
    gs.append(sum(t[i:i+r]))
print(max(gs))

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)
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==1:
    Acc+=stack[-1]
    acc.append(Acc)
    Acc2+=stack[-2]
    Acc2+=stack[-1]
    acc2.append(Acc2)
  for i in range(len(acc)-k-1):
    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)-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==1:
    Acc+=stack[-1]
    acc.append(Acc)
    Acc2+=stack[-2]
    Acc2+=stack[-1]
    acc2.append(Acc2)
  for i in range(len(acc)-k-1):
    ans=max(ans,acc2[i+k]-acc[i])
print(ans)

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
s=input()+"d"
d=[]
r=s[0]
c=0
for i in range(n+1):
    if r==s[i]:
        c+=1
    else:
        d.append(c)
        r=s[i]
        c=1
if s[0]=="0":
        d=[0]+d
if s[-2]=="0":
    d+=[0]
if len(d)<=2*k+1:
    print(n)
else:
    l=sum(d[:2*k+1])
    p=sum(d[:2*k+1])
    for i in range(len(d)-2*k-3):
        p=p-d[2*i]-d[2*i+1]+d[2*i+2*k+1]+d[2*i+2*k+2]
        l=max(l,p)
    print(l)

Submission status: Runtime Error

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

prev = S[0]
if prev == "0":
  fst_is_0 = True
else:
  fst_is_0 = False

cnt = 0
cnt_0 = 0

cnt_lst = []
for idx, s in enumerate(S):
  if s == prev:
    cnt += 1
  else:
    cnt_lst.append(cnt)
    if s == "0":
      cnt_0 += 1
    prev = s
else:
  cnt_lst.append(cnt)
#print(cnt_lst)


if cnt_0 <= K:
  print(len(S)-2)
else:
  l = [cnt_lst[2*idx+2*K+1] -cnt_lst[idx*2] for idx in range( int( (len(cnt_lst)+1)/2) -K)   ]
  #print(l)
  ss = []
  for idx,sss in enumerate(l):
    if idx == 0:
      ss.append(sss-1)
    elif idx == len(l)-1:
      
      ss.append(sss-1)
    else:
      ss.append(sss)
      
  print(max(ss))

Submission status: Runtime Error

Solution code:
from itertools import accumulate

N, K = map(int, input().split())
Ss = list(map(int, input()))

consecutive01s = [[], [0]]
turn = 0
num = 0
for S in Ss+[9]:
    if S == turn:
        num += 1
    else:
        consecutive01s[turn].append(num)
        turn = 1-turn
        num = 1
if turn == 0:
    consecutive01s[0].append(0)
consecutive01s[1].append(0)

N0, N1 = len(consecutive01s[0]), len(consecutive01s[1])

acc0s = list(accumulate([0]+consecutive01s[0]))
acc1s = list(accumulate([0]+consecutive01s[1]))

len0s = [acc0s[i+K]-acc0s[i] for i in range(N0-K+1)]
len1s = [acc1s[i+K+1]-acc1s[i] for i in range(N1-K)]

ans = max([len0+len1 for len0, len1 in zip(len0s, len1s)])
print(ans)

Submission status: Runtime Error

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

S = str(input())

L = [0]
if len(S) != 1:
    for i in range(len(S)-1):
    #     print(S[i],S[i+1])
        if S[i] != S[i+1]:
            L.append(i+1)
    L.append(len(S))
# print(L)

ans = 1
for i in range(len(L)-K*2-1):
#     print(i, i+K*3-1)
#     print(L[i+K*3-1]-L[i])
    ans = max(ans, L[i+K*3-1]-L[i])
print(ans)

Submission status: Runtime Error

Solution code:
import sys
input = sys.stdin.readline
N, K = map(int, input().split())
S = input()
c0, c1 = [], []
if S[0] == '1':
    c0.append(0)
i = 0
while i < N:
    p = S[i]
    c = 1
    i += 1
    while i < N and S[i] == p:
        i += 1
        c += 1
    if p == '0':
        c0.append(c)
    else:
        c1.append(c)
if S[N-1] == '0':
    c1.append(0)
l0 = len(c0)
for i in range(1, l0):
    c0[i] += c0[i-1]
    c1[i] += c1[i-1]
l = l0 - K + 1
ans = c0[K] + c1[K]
for i in range(1, l):
    tmp = c0[i+K-1] - c0[i-1] + c1[i+K-1] - c1[i-2]
    if ans < tmp:
        ans = tmp
print(ans)

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)
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==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)-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==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:
n,k=map(int,input().split())
s=input()
def func(k,ans,tmp,q):
    if q>=n:
        return max(ans,tmp)
    if s[q]=='1':
        return func(k,ans,tmp+1,q+1)
    if k==0:
        return max(ans,tmp)
    t=n
    for i in range(q,n):
        if s[i]=='1':
            t=i
            break
    if t==n:
        return max(ans,n-q+tmp)
    a=func(k,max(ans,tmp),0,t)
    b=func(k-1,ans,t-q+tmp,t)
    return max(a,b)
print(func(k,0,0,0))

Submission status: Runtime Error

Solution code:
import sys
input = sys.stdin.readline
def ii(): return int(input())
def mi(): return map(int, input().rstrip().split())
def lmi(): return list(map(int, input().rstrip().split()))
def li(): return list(input().rstrip())
# template #


N, K = mi()
strS = li()
S = [int(i) for i in strS]
S.append(0)
# S.insert(0, 1)
# print(S)

cnt = 0
lis0 = []
if S[0] == 1:
    listart = [0]
else:
    listart = []
liend = []
for i in range(1, N):
    if S[i] == 0:
        cnt += 1
    if S[i] == 0 and S[i + 1] == 1:
        listart.append(i + 1)
        lis0.append(i)
    if S[i-1] == 1 and S[i] == 0:
        liend.append(i-1)
if S[N-1] == 0:
    lis0.append(N)
if S[N-1] == 1:
    liend.append(N-1)
# lis.append(N)
# lis.insert(0, -1)
# print(lis0)
# print(listart)
# print(liend)
ma = max(liend[K - 1] + 1, N - listart[-K])
# print(ma)
if K >= len(lis0):
    print(N)
else:
    for i in range(len(listart) - K):
        ma = max(ma, liend[i+K]-listart[i]+1)
    print(ma)

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())
s = input()
ans_0 = []
ans_1 = []
cnt = 0

if s.count("1") == 1:
    print(max(n - 1 - s.index("1"), s.index("1") + 1))
else:
    s = list(s)
    for i in range(n):
        if s[i] == "1" and cnt == 0:
            ans_0.append(i)
            cnt += 1
        elif s[i] == "0":
            cnt = 0
    cnt = 0
    for i in range(n):
        if s[n-1 - i] == "1" and cnt == 0:
            ans_1.append(n-1-i)
            cnt += 1
        elif s[n-1 - i] == "0":
            cnt = 0
    ans_1.sort()
    ans_list = []
    for i in range(len(ans_0) - k):
        ans_list.append(ans_1[i+k] - ans_0[i])
    print(max(ans_list) + 1)

Submission status: Runtime Error

Solution code:
N, K = map(int,input().split())
S = input()
nowL = 0
if S[0] == '0':
    nowL = 1
L = [0] * N
for i in range(N-1):
    if S[i] == S[i+1]:
        L[nowL] = L[nowL] + 1
    else:
        L[nowL] = L[nowL] + 1
        nowL = nowL + 1
# 終端処理
L[nowL] = L[nowL] + 1
L.remove(0)

ans = 0
for j in range(len(L)):
    ans = max(ans,sum(L[2*j:2*j+2*K+1]))

print(ans)

Submission status: Runtime Error

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

sentous = []
sentou = 0
counts = [0]
count = 0
prev = 0
for s in S:
    count += 1
    if s == prev and count > 0:
        sentou += 1
    elif s != prev and s == '1':
        sentou += 1
    elif s != prev and s == '0':
        sentous.append(sentou + 1)
        sentou = 0
        if K > 0:
            K -= 1
        else:
            count -= sentous.pop(0)
        counts.append(count)
    prev = s

print(max(*counts, count))

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
S = input()
*S, = map(int, list(S))
zeros = []
ones = []
cnt = 1
now = S[0]
for i in range(1, N):
    if now == S[i]:
        cnt += 1
    else:
        if now == 0:
            zeros.append(cnt)
        else:
            ones.append(cnt)
        cnt = 1
        now = S[i]
if now == 0:
    zeros.append(cnt)
else:
    ones.append(cnt)

if len(zeros) <= K:
    print(len(S))
else:
    ans = 0
    for i in range(len(zeros) - K+1):
        aaa = sum(zeros[i:i + K]) + sum(ones[i:i + K])
        if S[0] == 1:
            aaa += ones[i+K]
        ans = max(aaa, ans)
    print(ans)

Submission status: Runtime Error

Solution code:
def solve():
  n,k = (int(i) for i in input().split())
  s = input()
  one = []
  zero = []  
  for i in range(n):
    if i == 0:
      if s[i] == "0":
        zero.append(1)
      else:
        one.append(1)
    
    else:     
     if s[i] == s[i-1]:
      if s[i] == "0":
        zero[-1]+= 1
      else:
        one[-1] += 1
     else:
      if s[i] == "0":
        #zero.append(0)
        zero.append(1)
      else:
        #one.append(0)
        one.append(1)
  ans = 0 
  if len(zero) <= k:
    ans = n
  elif len(one) > len(zero):
    tmp = 0
    for i in range(k+1):
     tmp += one[i]
    for i in range(k):
     tmp += zero[i]
    for i in range(len(zero)-k):
       
       tmp -=  one[i]
       tmp +=  one[i+k+1]
       tmp -=  zero[i]
       tmp +=  zero[i+k]
       ans = max(ans,tmp)
  else:
    tmp = 0
    for i in range(k):
      tmp += one[i]
    for i in range(k):
      tmp += zero[i]
    ans = max(ans,tmp)
    tmp -= zero[i]
    tmp += zero[i+1]
    ans = max(ans,tmp)
    for i in range(len(zero)-k-1):
       
       tmp -=  one[i]
       tmp +=  one[i+k+1]
       tmp -=  zero[i+1]
       tmp +=  zero[i+2]
       ans = max(ans,tmp)
  print(ans)
    
solve()

Submission status: Runtime Error

Solution code:
N = list(map(int, input().split()))
a = list(input())
l = []
flg = "1"
tmp = 0

for i in range(len(a)):
    if a[i] == flg:
        tmp += 1
        if i+1 == len(a):
            l.append(tmp)
    else:
        l.append(tmp)
        tmp = 1
        if flg == "1":
            flg = "0"
        else:
            flg = "1"

b = N[1]*2+1

ll = [sum(l[i:i+b]) for i in range(0,len(l)-b+1,2)]

print(max(ll))

Submission status: Runtime Error

Solution code:
N,K = map(int,input().split())
S = list(map(int,input()))
blobs = []
if S[0] == 0:
    blobs.append(0)
cnt = 0
past_i = 0
for i in range(len(S)):
    if i == 0:
        past_i = S[i]
    if past_i == S[i]:
        cnt += 1
    else:
        blobs.append(cnt)
        cnt = 1
        past_i = S[i]
if S[-1] == 0:
    blobs.append(0)
sums = []
for i in range(len(blobs)//2):
    sums.append(sum(blobs[2*i:2*(i+K+1)]))
print(max(sums))

Submission status: Runtime Error

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

res = 0
left = 0
right = 0
# start
cnt = 0
for i in range(N-1):
    if S[i] == "0" and S[i+1] == "1":
        cnt += 1
    if cnt == K and S[i+1] == "0":
        right = i + 1
        break
    if i == N - 2:
        right = N
    
res = max(res, right - left)

while right < N:
    left += 1
    right += 1
    while left < N:
        if S[left] == "1" and S[left-1] == "0":
            break
        else:
            left += 1
    while right < N:
        if S[right] == "0" and S[right+1] == "1":
            break
        else:
            right += 1
    res = max(res, right - left)
    
print(res)

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
s=input()
s='0'+s+'0'
S=[]
E=[]
ans=[]
for i in range(n+1):
  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]))
  if s[1]=='0':
    ans.append(E[k-1]-1)
  if s[n]=='0':
    ans.append(S[len(S)-k]-1)
  if ans==[]:
    print(E[0])
  else:
    print(max(ans)+1)

Submission status: Runtime Error

Solution code:
n,k = (int(i) for i in input().split())
s = input()
x,y,f,num,ans = [],[],1,0,0
for i in range(n):
    if f and s[i]=="1":
        f = 0
        x.append(i)
    elif 1-f and s[i]=="0":
        f,num = 1,num+1
        y.append(i-1)
if x[0]: num+=1
if int(s[-1]): y.append(n-1)
if num<=k: print(n)
else:
    if x[0]: ans = y[k-1]+1
    for i in range(len(y)-k):
        if x[i]+1: ans = max(y[i+k]-x[i]+1,ans)
        else: ans = max(y[i+k]-x[i],ans)
    print(ans)

Submission status: Runtime Error

Solution code:
# -*- coding:utf-8 -*-

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

i = 0
while True:
        flag = False
        if i >= len(S): break
        if S[i] == "0" and K > 0:
            while S[i] == "0" and K > 0:
                count += 1
                i += 1
            if flag: K -= 1
        elif S[i] == "1":
            count += 1
            i += 1
        else:
            break

print(count)

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())
s = input()
temp = ""
ans = []
c = 0
for s_ in s:
    if temp != s_:
        if s_ == "0":
            ans.append(c)
            temp = "0"
            c = -1
        else:
            ans.append(c)
            temp = "1"
            c = 1
    else:
        if s_ == "0":
            c -= 1
        else:
            c += 1
ans.append(c)
ans = ans[1:]
c = []
t = [i for i in range(len(ans)) if ans[i] < 0]
aaa = []
for v in itertools.combinations(t, k):
    anst = ans.copy()
    for v_ in v:
        anst[v_] *= -1
    c_ = 0
    for s_ in anst:
        if s_ > 0:
            c_ += s_
        else:
            aaa.append(c_)
            c_ = 0
    aaa.append(c_)
if len(aaa) > 0:
    print(max(aaa))
else:
    print(len(s))

Submission status: Runtime Error

Solution code:
#include <cstdio>
const int MAXN=1E5+10;
int N,K,src[MAXN<<2];
int ansl,ansr;
struct wrc {
	int cnt,cl,cr;
	wrc(){cnt=cl=cr=0;}
	wrc(int a,int b,int c) {
		cnt=a;
		cl=b;cr=c;
	}
}w[MAXN<<2];
wrc operator + (const wrc&a,const wrc&b) {
	wrc c(a.cnt+b.cnt,a.cl,b.cr);
	if(a.cr==1&&b.cl==1)c.cnt-=1;
	return c;
}
void build(int root,int l,int r) {
	if(l==r) {
		w[root]=wrc(src[l],src[l],src[l]);
		return ;
	}
	int mid=(l+r)>>1;
	build(root<<1,l,mid);
	build(root<<1|1,mid+1,r);
	w[root]=w[root<<1]+w[root<<1|1];
}
wrc find(int root,int l,int r) {
	if(ansl<=l&&r<=ansr) return w[root];
	int mid=(l+r)>>1;
	if(ansr<=mid)return find(root<<1,l,mid);
	if(ansl> mid)return find(root<<1|1,mid+1,r);
	return find(root<<1,l,mid)+find(root<<1|1,mid+1,r);
}
int clac(int l,int r) {
	ansl=l;ansr=r;
	return find(1,1,N).cnt;
}
int doubleDivide(int len) {
	for(int i=1;i<=N;++i) {
		if(i+len-1>N)break;
		if(clac(i,i+len-1)<=K)
			return true;
	}return false;
}
int main() {
	scanf("%d%d",&N,&K);
	for(int i=1;i<=N;++i) {
		scanf("%1d",&src[i]);
		src[i]^=1;
	}
	build(1,1,N);
	int ans=0; 
	double l=0,r=1E6;
	for(int t=1;t<=32;++t) {
		double mid=(l+r)/2;
		//printf("%lf (%lf) %lf \n",l,mid,r);
		if(doubleDivide(mid)) {
			ans=mid;
			l=mid;
		}else r=mid;
	} 
	printf("%d\n",ans);
	return 0;
}

Submission status: Runtime Error

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

zero_positions = []
flag = False
zeros = []
for i in range(n):
    if s[i] == '0':
        zeros.append(i)
        flag = True
    if s[i] == '1' and flag:
        flag = False
        zero_positions.append(zeros)
        zeros = []
if zeros != []:
    zero_positions.append(zeros)

def get_one_positions(s):
    one_positions_len = []
    flag = False
    ones_len = 0
    for i in range(n):
        if s[i] == '1':
            ones_len += 1
            flag = True
        if s[i] == '0' and flag:
            flag = False
            one_positions_len.append(ones_len)
            ones_len = 0
    if ones_len != []:
        one_positions_len.append(ones_len)
    return(max(one_positions_len))

one_positions_lens = []
for i in range(len(zero_positions) - k + 1):
    choice = []
    for j in range(k):
        choice.extend(zero_positions[i + j])

    # reverse
    s_new = []
    for l in range(n):
        if l in choice:
            s_new.append('1')
        elif s[l] == '1':
            s_new.append('1')
        elif s[l] == '0':
            s_new.append('0')


    # count
    one_positions_len = get_one_positions(s_new)
    one_positions_lens.append(one_positions_len)

print(max(one_positions_lens))

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
S = input()
last_s = "0"
count = 0
A = []
for i, s in enumerate(S):
	if i == 0:
		count += 1
	else:
		if s != last_s:
			A.append(count)
			count = 0
		count += 1
	last_s = s

if count != 0:
	A.append(count)

B = []

for i in range(len(S)-(2*K+1)-1):
	B.append(sum(A[2*i:2*i+2*K+1]))

print(max(B))

Submission status: Runtime Error

Solution code:
#include<bits/stdc++.h>
#define INF 1e9
#define llINF 1e18
#define MOD 1000000007
#define pb push_back
#define mp make_pair 
#define F first
#define S second
#define ll long long
#define ull unsigned long long
#define vi vector<ll>
#define vvi vector<vi>
#define DBG_N(hoge) cerr<<" "<<(hoge)<<endl;
#define DBG cerr<<"!"<<endl;
#define BITLE(n) (1LL<<((ll)n))
#define BITCNT(n) (__builtin_popcountll(n))
#define SUBS(s,f,t) ((s).substr((f)-1,(t)-(f)+1))
#define ALL(a) (a).begin(),(a).end()
using namespace std;
ll rui[211111];
int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll n,k;cin>>n>>k;
  string s;
  cin>>s;
  vi num;
  vi num2;
  for(int i=0;i<s.size();i++){
    ll cnt=0;
    while(i<s.size()&&s[i]=='0'){
      cnt++;i++;
    }
    num.pb(cnt);
    cnt=0;
    while(i<s.size()&&s[i]=='1'){
      cnt++;i++;
    }
    i--;
    num2.pb(cnt);
  }
  ll ans=0;
  for(int i=0;i<num.size();i++){
    rui[i*2+1]+=rui[i*2]+num[i];
    rui[i*2+2]+=rui[i*2+1]+num2[i];
  }
  for(int i=1;i<111111;i++){
   
    if(i%2==1)
      ans=max(ans,rui[i+k*2]-rui[i-1]);
    else
      ans=max(ans,rui[i+k*2+1]-rui[i-1]);
  }
  cout<<ans<<endl;
  return 0;
}

Submission status: Runtime Error

Solution code:
N,K = list(map(int,input().split()))
S = input()
n = []
p = S[0]
c = 1
for s in S[1:]:
    if s != p:
        n.append(c)
        p = s
        c = 0
    c+=1
n.append(c)
ans = n[0]
if S[0]=="0":
    for i in range(1,len(n)-2*K+1,2):
        ans = max(ans,sum(n[i:i+2*K+1]))
    n[1]+=n[0]
    n.pop(0)
    K -=1
for i in range(0,len(n)-2*K,2):
    ans = max(ans,sum(n[i:i+2*K+1]))
print(ans)

Submission status: Runtime Error

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

S = input()

xs = []

i = 0

while i < N :
    l = 0
    while i < N and S[i] == '1':
        i += 1
        l += 1
    xs.append(l)
    l = 0
    while i < N and S[i] == '0':
        i += 1
        l += 1
    xs.append(l)

xs += [0] * (K * 2)

#print(xs)

result = 0

l = (K * 2) + 1
result = r = sum(xs[0:l])
#print("*", result, r)

i = 0
while i + l + 1 < N:
    r = r - xs[i] - xs[i+1] + xs[i+l] + xs[i+l+1]
    result = max(result, r)
    #print(result, r)
    i += 2

print(result)

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
import math

list=input().split(" ")
n=int(list[0])
k=int(list[1])
s=input()

list2=[]
renzoku=0

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

for x in range(n-1):
    renzoku+=1
    if int(s[x])!=int(s[x+1]):
        list2.append(renzoku)
        renzoku=0

list2.append(renzoku+1)

#print(list2)

kaisu=math.ceil(len(list2)/2)-k
max=0
if kaisu==0:
    gokei=len(s)

for y in range(kaisu):
    #print(list2[y*2:y*2+(2*k+1)])
    gokei=sum(list2[y*2:y*2+(2*k+1)])
    if gokei>max:
        max=gokei
print(gokei)