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

tmp_count = 1
count_list = []
for i in range(N-1):
    if S[i] == "0" and S[i+1] == "0":
        tmp_count += 1
    elif S[i] == "1" and S[i+1] == "1":
        tmp_count += 1
    elif S[i] == "0" and S[i+1] == "1":
        count_list.append(tmp_count)
        tmp_count = 1
    elif  S[i] == "1" and S[i+1] == "0":
        count_list.append(tmp_count)
        tmp_count = 1
count_list.append(tmp_count)

count_sakadati = 0
tmp_list = []
if S[0] == "0":
    for i in range(K):
        tmp_max = 0
        tmp_max_num = 1
        for j in range(0, len(count_list)-1, 2):
            if j == 0:
                tmp_max = count_list[j] + count_list[j+1]
                tmp_max_num = j
            else:
                count_list[j- 1] + count_list[j] + count_list[j+1] > tmp_max
                tmp_max = count_list[j- 1] + count_list[j] + count_list[j+1]
                tmp_max_num = j
        if tmp_max_num == 0:
            del count_list[0]
            del count_list[1]
            count_list.insert(0, tmp_max)
        else:
            del count_list[tmp_max_num-1]
            del count_list[tmp_max_num-1]
            del count_list[tmp_max_num-1]
            count_list.insert(tmp_max_num-1, tmp_max)
else:
    for i in range(K):
        tmp_max = 0
        tmp_max_num = 1
        for j in range(1, len(count_list)-1, 2):
            if count_list[j- 1] + count_list[j] + count_list[j+1] > tmp_max:
                tmp_max = count_list[j- 1] + count_list[j] + count_list[j+1]
                tmp_max_num = j
        del count_list[tmp_max_num-1]
        del count_list[tmp_max_num-1]
        del count_list[tmp_max_num-1]
        count_list.insert(tmp_max_num-1, tmp_max)
            
print(max(count_list))

Submission status: Runtime Error

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

tmp_count = 1
count_list = []
for i in range(N-1):
    if S[i] == "0" and S[i+1] == "0":
        tmp_count += 1
    elif S[i] == "1" and S[i+1] == "1":
        tmp_count += 1
    elif S[i] == "0" and S[i+1] == "1":
        count_list.append(tmp_count)
        tmp_count = 1
    elif  S[i] == "1" and S[i+1] == "0":
        count_list.append(tmp_count)
        tmp_count = 1
count_list.append(tmp_count)

print("countlist =", count_list)

count_sakadati = 0
tmp_list = []
if S[0] == "0":
    for i in range(K):
        tmp_max = 0
        tmp_max_num = 1
        for j in range(0, len(count_list)-1, 2):
            if j == 0:
                tmp_max = count_list[j] + count_list[j+1]
                tmp_max_num = j
            else:
                count_list[j- 1] + count_list[j] + count_list[j+1] > tmp_max
                tmp_max = count_list[j- 1] + count_list[j] + count_list[j+1]
                tmp_max_num = j
        if tmp_max_num == 0:
            del count_list[0]
            del count_list[1]
            count_list.insert(0, tmp_max)
        else:
            del count_list[tmp_max_num-1]
            del count_list[tmp_max_num-1]
            del count_list[tmp_max_num-1]
            count_list.insert(tmp_max_num-1, tmp_max)
else:
    for i in range(K):
        tmp_max = 0
        tmp_max_num = 1
        for j in range(1, len(count_list)-1, 2):
            if count_list[j- 1] + count_list[j] + count_list[j+1] > tmp_max:
                tmp_max = count_list[j- 1] + count_list[j] + count_list[j+1]
                tmp_max_num = j
        del count_list[tmp_max_num-1]
        del count_list[tmp_max_num-1]
        del count_list[tmp_max_num-1]
        count_list.insert(tmp_max_num-1, tmp_max)
            
print(max(count_list))

Submission status: Runtime Error

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

s.append(9)
ran = []
a = 0
i = 1
#ランレングス圧縮
while i < len(s):
    if s[a] == s[i]:
        i +=1
    else:
        ran.append(i-a)
        a = i
        i += 1
ran.append(0)
ran.append(0)
ran.append(0)
b = s[0]
result = []
i = 0
counter = 0
if len(ran) <= k*2:
    print(n)
else:
    while i+2*k <= len(ran)-3:
        if s[counter] == 1:
            result.append(sum(ran[i:2*k+i+1]))
        else:
            result.append(sum(ran[i:2*k+i]))
        counter += ran[i]
        i += 1
    print(max(result))

Submission status: Runtime Error

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

s.append(9)
ran = []
a = 0
i = 1
#ランレングス圧縮
while i < len(s):
    if s[a] == s[i]:
        i +=1
    else:
        ran.append(i-a)
        a = i
        i += 1
ran.append(0)
ran.append(0)
ran.append(0)
b = s[0]
result = []
i = 0
counter = 0
if n/2 <= k:
    print(n)
else:
    while i+2*k <= len(ran)-3:
        if s[counter] == 1:
            result.append(sum(ran[i:2*k+i+1]))
        else:
            result.append(sum(ran[i:2*k+i]))
        counter += ran[i]
        i += 1
    print(max(result))

Submission status: Runtime Error

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

s.append(9)
ran = []
a = 0
i = 1
#ランレングス圧縮
while i < len(s):
    if s[a] == s[i]:
        i +=1
    else:
        ran.append(i-a)
        a = i
        i += 1
ran.append(0)
ran.append(0)
ran.append(0)
b = s[0]
result = []
i = 0
counter = 0
if n/2 <= k:
    print(n)
else:
    while i+2*k <= len(ran):
        if s[counter] == 1:
            result.append(sum(ran[i:2*k+i+1]))
        else:
            result.append(sum(ran[i:2*k+i]))
        counter += ran[i]
        i += 1
    print(max(result))

Submission status: Runtime Error

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,queue,copy

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7
dd=[(-1,0),(0,1),(1,0),(0,-1)]
ddn=[(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return input()

# Summarize count of factor within list
def summarize_list(l):
  sl=l

  a=sl[0]
  c=1
  res=[]

  for x in sl[1:]:
    if x==a:
      c+=1
    else:
      res.append(c)
      a=x
      c=1
  res.append(c)

  return res

def main():
  n,k=LI()
  s=list(S())

  l=summarize_list(s)

  for i in range(len(l)-1):
    l[i+1]+=l[i]
  l=[0]+l

  mx=-inf
  if s[0]=='1':
    haba=2*k+1
    for i in range(len(l)-haba+1):
      if i%2==0:
        mx=max(mx,l[i+haba]-l[i])
  else:
    a

  return mx

# main()
print(main())

Submission status: Runtime Error

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

a = []
j = 0
temp = 1
prev = s[0]
for i in s[1:]:
    if i == prev:
        prev = i
        temp += 1
    else:
        a.append(temp)
        prev = i        
        temp = 1
a.append(temp)

b = []
if s[0] == "1":
    b.append(sum(a[:2*k+1]))
    for i in range(len(a) - 2*k - 1):
        b.append(b[-1] - a[i] + a[2*k+i+1])
    print(max(b))
else:
    t = a[1:]
    b.append(sum(t[:2*k+1]))
    for i in range(len(a) - 2*k - 1):
        b.append(b[-1] - t[i] + t[2*k+i+1])
    b.append(sum(a[:2*k]))
    print(max(b))

Submission status: Runtime Error

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

a = [0]
tmp = 1
for i in range(n-1):
    if s[i] == s[i+1]:
        tmp += 1
    else:
        a.append(tmp)
        tmp += 1
a.append(tmp)
m = len(a)
ans = 1
i = 1
if len(a) == 2 or m-2*k < 1:
    ans = a[m-1]
elif s[0] == "1":
    while (i <= m-2*k):
        if i % 2 != 0:        
            ans = max(ans,a[2*k+i]-a[i-1])
        else:
            ans = max(ans,a[2*k+i-1]-a[i-1])
        i += 1
else:
    while (i < m-2*k):
        if i % 2 == 0:        
            ans = max(ans,a[2*k+i]-a[i-1])
        else:
            ans = max(ans,a[2*k+i-1]-a[i-1])
        i += 1
print(ans)

Submission status: Runtime Error

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

a = [0]
tmp = 1
for i in range(n-1):
    if s[i] == s[i+1]:
        tmp += 1
    else:
        a.append(tmp)
        tmp += 1
a.append(tmp)
m = len(a)
ans = 1
if len(a) == 2 or m-2*k < 1:
    ans = a[m-1]
elif s[0] == "1":
    for i in range(1,m-2*k+1):
        if i % 2 != 0:        
            ans = max(ans,a[2*k+i]-a[i-1])
        else:
            ans = max(ans,a[2*k+i-1]-a[i-1])
else:
    for i in range(1,m-2*k):
        if i % 2 == 0:        
            ans = max(ans,a[2*k+i]-a[i-1])
        else:
            ans = max(ans,a[2*k+i-1]-a[i-1])

print(ans)

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
S = list(input())
bw = [0] * N
ind = 0
for i in range(N):
  bw[ind] += S[i] == S[i + 1]
  if S[i] != S[i + 1]:
    bw[ind] += 1
    ind += 1
ind = 0
res = 0
cumsumbw = [0] + [bw[0]] + [0] * (N - 1)
for i in range(1, N):
  cumsumbw[i + 1] = cumsumbw[i] + bw[i]
if N - 2 * K <= 0:
  print(N)
  exit(0)
for i in range(N - 2 * K):
  if S[i] == "1":
    res = max(res, cumsumbw[i + 2 * K + 1] - cumsumbw[i])
  else:
    res = max(res, cumsumbw[i + 2 * K] - cumsumbw[i])
print(res)

Submission status: Runtime Error

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


def count_zero(s, i):
    count = 0
    for j in range(i, len(s)):
        if s[j] == 0:
            count += 1
        else:
            break
    return count


def dfs(s, i, k, count):
    if i == len(s):
        return count
    elif s[i] == 1:
        count += 1
        return dfs(s, i + 1, k, count)
    else:
        if k > 0:
            c = count_zero(s, i)
            count += c
            return dfs(s, i + c, k - 1, count)
        else:
            return count


nums = []
for i in range(n):
    count = 0
    nums.append(dfs(s, i, k, count))
print(max(nums))

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(10 ** 6)
N, K = map(int, input().split())
S = list(map(int, list(input())))
def solve(l, k):
  if k == K:
    temp = 1
    thrbl = 0
    for i in range(l + 1, N):
      temp += 1
      if thrbl == 0 and S[i] == 1:
        thrbl = 1
      elif thrbl == 1 and S[i] == 0:
        temp -= 1
        break
    #print(l, temp, k)
    return temp
  temp = 0
  for i in range(l + 1, N):
    if S[i] == 0:
      temp = solve(i, k + 1)
      thrbl = 0
      for j in range(l + 1, i):
        temp += 1
        if thrbl == 0 and S[j] == 1:
          thrbl = 1
        elif thrbl == 1 and S[j] == 0:
          temp -= 1
          break
      if k == 1:
        thrbl = 0
        for j in range(l, -1, -1):
          temp += 1
          #print(j)
          if j == l:
            continue
          if thrbl == 0 and S[j] == 1:
            thrbl = 1
          elif thrbl == 1 and S[j] == 0:
            temp -= 1
            break
      #print(l, i, temp, k)
      return temp
res = 0
for i in range(N):
  if S[i] == 0:
    if i > 0 and S[i - 1] != 0:
      res = max(solve(i, 1), res)
    elif i == 0:
      res = max(solve(i, 1), res)
print(res) if res != 0 else print(N)

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(1000000)
N, K = map(int, input().split())
S = list(input())
X = []
cnt = 1
for i in range(1, N):
    if S[i] != S[i-1]:
        X.append(cnt)
        cnt = 0
    cnt += 1
    if i == N-1:
        X.append(cnt)

l = len(X)
if S[0] == '0' and (l+1)//2 <= k:
    print(N)
    exit()
if S[0] == '1' and l//2 <= k:
    print(N)
    exit()

ans = []
if S[0] == '1': 
    sum0 = sum(X[0:2*K+1])
    ans.append(sum0)
    i = 1
    while 2*K+2*i-1 < len(X):
        ans.append(ans[i-1] + X[2*K+2*i-1] + X[2*K+2*i] - X[2*i] - X[2*i-1])
        i += 1
else:
    sum0 = sum(X[0:2*K])
    ans.append(sum0)
    i = 1
    while 2*K+2*i <= len(X):
        if i == 1:
            ans.append(ans[i-1] + X[2*K+2*i-2] + X[2*K+2*i-1] - X[i-1])
            i += 1
        else:
            ans.append(ans[i-1] + X[2*K+2*i-2] + X[2*K+2*i-1] - X[2*i] - X[2*i-1])
            i += 1
print(max(ans))

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(1000000)
N, K = map(int, input().split())
S = list(input())
l = len(S)
X = []
cnt0 = 0
cnt1 = 0
for i in S:
    if i == '0':
        if cnt1 != 0:
            X.append(cnt1)
            cnt1 = 0
            cnt0 += 1
        else:
            cnt0 += 1
    else:
        if cnt0 != 0:
            X.append(cnt0)
            cnt0 = 0
            cnt1 += 1
        else:
            cnt1 += 1
if cnt1 != 0:
    X.append(cnt1)
elif cnt0 != 0:
    X.append(cnt0)
ans = []
if S[0] == '1': #Xの奇数は1
    sum0 = sum(X[0:2*K+1])
    ans.append(sum0)
    i = 1
    while 2*K+2*i-1 < len(X):
        ans.append(ans[i-1] + X[2*K+2*i-1] + X[2*K+2*i] - X[i] - X[i-1])
        i += 1
else:
    sum0 = sum(X[0:2*K])
    ans.append(sum0)
    i = 1
    while 2*K+2*i <= len(X):
        if i == 1:
            ans.append(ans[i-1] + X[2*K+2*i-2] + X[2*K+2*i-1] - X[i-1])
            i += 1
        else:
            ans.append(ans[i-1] + X[2*K+2*i-2] + X[2*K+2*i-1] - X[i] - X[i-1])
            i += 1
print(max(ans))

Submission status: Runtime Error

Solution code:
import numpy
n,k = map(int,input().split())
s = input()
tmp = s[0]
cnt = 1
ans = 0
ss = []
res = [0]
sum = 0
if s[0] == '0':
    ss.append(0)

for i in range(1,n):
    if s[i] == tmp:
        cnt += 1
    else:
        tmp = s[i]
        ss.append(cnt)
        cnt = 1
ss.append(cnt)
if s[-1] == '0':
    ss.append(0)
ll = len(ss)
for i in range(ll):
    sum += ss[i]
    res.append(sum)

if ll//2 <= k:
    print(n)
else:
    for i in range(ll-k*2)[::2]:
        ans = max(ans, res[i+k*2+1]-res[i])
    print(ans)

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
S = list(input())
l = len(S)
X = []
cnt0 = 0
cnt1 = 0
for i in S:
    if i == '0':
        if cnt1 != 0:
            X.append(cnt1)
            cnt1 = 0
            cnt0 += 1
        else:
            cnt0 += 1
    else:
        if cnt0 != 0:
            X.append(cnt0)
            cnt0 = 0
            cnt1 += 1
        else:
            cnt1 += 1
if cnt1 != 0:
    X.append(cnt1)
elif cnt0 != 0:
    X.append(cnt0)
ans = []
if S[0] == '1': #Xの奇数は1
    sum0 = sum(X[0:2*K+1])
    ans.append(sum0)
    i = 1
    while 2*K+2*i-1 < len(X):
        ans.append(ans[i-1] + X[2*K+2*i-1] + X[2*K+2*i] - X[i] - X[i-1])
        i += 1
else:
    sum0 = sum(X[0:2*K])
    ans.append(sum0)
    i = 1
    while 2*K+2*i <= len(X):
        if i == 1:
            ans.append(ans[i-1] + X[2*K+2*i-2] + X[2*K+2*i-1] - X[i-1])
            i += 1
        else:
            ans.append(ans[i-1] + X[2*K+2*i-2] + X[2*K+2*i-1] - X[i] - X[i-1])
            i += 1
print(max(ans))

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
S = list(input())
l = len(S)
X = []
cnt0 = 0
cnt1 = 0
for i in S:
    if i == '0':
        if cnt1 != 0:
            X.append(cnt1)
            cnt1 = 0
            cnt0 += 1
        else:
            cnt0 += 1
    else:
        if cnt0 != 0:
            X.append(cnt0)
            cnt0 = 0
            cnt1 += 1
        else:
            cnt1 += 1
if cnt1 != 0:
    X.append(cnt1)
elif cnt0 != 0:
    X.append(cnt0)
ans = []
if S[0] == '1': #Xの奇数は1
    sum0 = sum(X[0:2*K+1])
    ans.append(sum0)
    i = 1
    while 2*K+2*i-1 < len(X):
        ans.append(ans[i-1] + X[2*K+2*i-1] + X[2*K+2*i] - X[i] - X[i-1])
        i += 1
else:
    sum0 = sum(X[0:2*K])
    ans.append(sum0)
    i = 1
    while 2*K+2*i < len(X):
        if i == 1:
            ans.append(ans[i-1] + X[2*K+2*i+1] + X[2*K+2*i] - X[i-1])
            i += 1
        else:
            ans.append(ans[i-1] + X[2*K+2*i+1] + X[2*K+2*i] + X[i] - X[i] - X[i-1])
            i += 1
print(max(ans))

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
S = list(input())
l = len(S)
X = []
cnt0 = 0
cnt1 = 0
for i in S:
    if i == '0':
        if cnt1 != 0:
            X.append(cnt1)
            cnt1 = 0
            cnt0 += 1
        else:
            cnt0 += 1
    else:
        if cnt0 != 0:
            X.append(cnt0)
            cnt0 = 0
            cnt1 += 1
        else:
            cnt1 += 1
if cnt1 != 0:
    X.append(cnt1)
elif cnt0 != 0:
    X.append(cnt0)
ans = []
if S[0] == '1': #Xの奇数は1
    sum0 = sum(X[0:2*K+1])
    ans.append(sum0)
    i = 1
    while 2*K+2*i-1 < len(X):
        ans.append(ans[i-1] + X[2*K+2*i-1] + X[2*K+2*i] - X[i] - X[i-1])
        i += 1
        print(ans)
else:
    sum0 = sum(X[0:2*K])
    ans.append(sum0)
    i = 1
    while 2*K+2*i < len(X):
        if i == 1:
            ans.append(ans[i-1] + X[2*K+2*i+1] + X[2*K+2*i] - X[i-1])
            i += 1
        else:
            ans.append(ans[i-1] + X[2*K+2*i+1] + X[2*K+2*i] + X[i] - X[i] - X[i-1])
            i += 1
print(max(ans))

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
S = list(input())
l = len(S)
X = []
cnt0 = 0
cnt1 = 0
for i in S:
    if i == '0':
        if cnt1 != 0:
            X.append(cnt1)
            cnt1 = 0
            cnt0 += 1
        else:
            cnt0 += 1
    else:
        if cnt0 != 0:
            X.append(cnt0)
            cnt0 = 0
            cnt1 += 1
        else:
            cnt1 += 1
if cnt1 != 0:
    X.append(cnt1)
elif cnt0 != 0:
    X.append(cnt0)
ans = []
if S[0] == '1': #Xの奇数は1
    sum0 = sum(X[0:2*K+1])
    ans.append(sum0)
    i = 0
    if 2*K+1 >= len(X):
        ans = sum(X)
    else:
        while 2*K+i+1 < len(X):
            ans.append(ans[i] + X[2*K+i+1] - X[i])
            i += 1
else:
    sum0 = sum(X[0:2*K])
    ans.append(sum0)
    i = 0
    while 2*K+i < len(X):
        ans.append(ans[i] + X[2*K+i] - X[i])
        i += 1
print(max(ans))

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
s=input()
if "0" not in s:
	print(len(s))
	exit()
p=s[0]
a=[]
d=[]
b=[]
cnt_1=0
cnt_0=0
p=s[0]
for i in range(len(s)):
	if p!=s[i]:
		if p=="0":
			a.append(cnt_0)
			cnt_0=0
			p="1"
		else:
			b.append(cnt_1)
			cnt_1=0
			p="0"
	if s[i]=="0":
		cnt_0+=1
	else:
		cnt_1+=1
if cnt_0>0:
	a.append(cnt_0)
else:
	b.append(cnt_1)
if len(a)<=len(b):
	k+=1
	if len(a)==len(b):
		if s[0]=="0":
			b.insert(0,0)
		else:
			b.insert(-1,0)
	for i in range(len(b)-k+1):
		d.append(sum(b[i:i+k])+sum(a[i:i+k-1]))
else:
	b.insert(0,0)
	b.insert(len(b),0)
	for i in range(len(b)-k+1):
		k+=1
		d.append(sum(b[i:i+k])+sum(a[i:i+k-1]))
print(max(d))

Submission status: Runtime Error

Solution code:
n, k = list(map(int, input().split(' ')))
s = list(map(int, list(input())))
pres = []
i = 0
while i < n:
    ka = 0
    while s[i] == s[i+ka]:
        ka += 1
        if ka+i >= n:
            break
    pres.append([s[i],ka])
    i += ka
ia = 0
result = []
while ia < len(pres):
    h = 0
    if pres[ia][0] == 0:
        if ia + 2*k <= len(pres):
            for p in range(2*k):
                h += pres[p+ia][1]
            result.append(h)
        else:
            break
    else:
        if ia + 2*k+1 <= len(pres):
            for p in range(2*k+1):
                h += pres[ia+p][1]
            result.append(h)
        else:
            break
    ia += 1
print(max(result))

Submission status: Runtime Error

Solution code:
import sys
from collections import deque
stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()  # ignore trailing spaces

n, k = na()
s = input()
s = s + "2"
li_0 = []
li_1 = []
ct = 1
for i in range(n):
    if s[i] != s[i+1]:
        if s[i] == '0':
            li_0.append(ct)
            ct = 1
        else:
            li_1.append(ct)
            ct = 1
    else:
        ct += 1
if len(li_0) <= k:
    print(len(s)-1)
else:
    li_0 = deque(li_0)
    li_1 = deque(li_1)
    if s[0] == "0":
        li_1.appendleft(0)
    li_1.append(0)
    Sum_0 = sum(li_0[i] for i in range(k))
    Sum_1 = sum(li_1[i] for i in range(k+1))
    Max = Sum_0 + Sum_1
    for i in range(len(li_1)-k-1):
        Sum_0 = Sum_0 - li_0[i] + li_0[i+k]
        Sum_1 = Sum_1 - li_1[i] + li_1[i+k+1]
        Max = max(Max, Sum_0 + Sum_1)
    print(Max)

Submission status: Runtime Error

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

compress = []
now = '1'
count = 0
for s in S:
    if s != now:
        compress.append(count)
        now = s
        count = 1
    else:
        count += 1
compress.append(count)

length = len(compress)
candidate = sum(compress[:2*k+1])
ans = candidate
if length <= 2*k+1:
    print(ans)
    exit()

for i in range(0, length-2*k-1, 2):
    if 2+k+2+i < length:
        candidate += compress[2*k+1+i] + compress[2*k+2+i] - compress[i] - compress[i+1]
    else:
        candidate += compress[2*k+1+i] - compress[i] - compress[i+1]
    ans = max(ans, candidate)

print(ans)

Submission status: Runtime Error

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

A=[]

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

cnt=1
pre=s[0]
for i in s[1:]:
	if pre!=i:
		A.append(cnt)
		cnt=1
		pre=i
	else:
		cnt+=1

if (s[-1]=='1' and len(A)%2==0) or (s[-1]=='0' and len(A)%2!=0):
	A.append(cnt)


if s[-1]!='0':
	A.append(0)
A.append(0)

dp=[0]*(len(A)//2-K+1)

for i in range(2*K+1):dp[0]+=A[i]

i=1
idx=0
while len(dp)>i:
	dp[i]=dp[i-1]-A[idx]-A[idx+1]+A[idx+2*(K+1)]+A[idx-1+2*(K+1)]
	idx+=2
	i+=1

print(max(dp))

Submission status: Runtime Error

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


    cnt = 0
    right = 0
    flag = 0
    j = k + 1 if s[0] == '1' else k
    if len(s) == 1:
        if k == 0 and s[0] == '0':
            return 0
        flag = 1
    for i in range(n - 1):
        if i + 1 >= n:
            flag = 1
            break
        if s[i]  == '1' and s[i + 1] == '0':
            cnt += 1
        if cnt == j:
            right = i + 1
            break


    if flag:
        return n


    left = 0
    ret = right - left
    flag = 0

    while True:
        while True:
            if right + 1 >= n:
                flag = 1
                break
            if s[right] == '1' and s[right + 1] == '0':
                right += 1
                break
            right += 1
        while True:
            if s[left] == '0' and s[left + 1] == '1':
                left += 1
                break
            left += 1
        if flag:
            right += 1
        ret = max(ret, right - left)
        if flag:
            break
    return ret


print(main())

Submission status: Runtime Error

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

if s[0] == "1":
    s_first = 1
else:
    s_first = 0

S_count = []
t = 1
for i in range(n-1):
    if s[i] == s[i+1]:
        t += 1
    else:
        S_count.append(t)
        t = 1
S_count.append(t)

t = len(S_count)
ans_list = []
if n-2*k <= 0:
    print(len(s))
else:    
    if s_first == 1:
        for i in range(t-2*k):
            ans_list.append(sum(S_count[i:i+2*k+1]))
    else:
        for i in range(t-2*k):
            ans_list.append(sum(S_count[i:i+2*k]))
    print(S_count)
    print(ans_list)
    ans = max(ans_list)
    print(ans)

Submission status: Runtime Error

Solution code:
#### import ####
import sys
import math
from collections import defaultdict

#### 設定 ####
sys.setrecursionlimit(10**7)
def input():
    return sys.stdin.readline()[:-1]

#### 定数 ####
mod = 10**9 + 7

#### 読み込み ####
def I(): return int(input())
def II(): return map(int, input().split())
def III(): return list(map(int, input().split()))
def Line(N):
    read_all = [tuple(map(int, input().split())) for _ in range(N)]
    return map(list,zip(*read_all))

#################

N,K = II()
S = str(input())

num = 0
for i in range(N):
    if S[i]=='0' and S[i+1]=='1':
        num += 1
        place = i+1
    if num==K:
        break

if num<K:
    print(N)
    exit()

for i in range(place+1,N):
    if i==N-1 and S[i]=='1':
        print(N)
        exit()
    if S[i]=='0':
        break

place = i-1

ans = place+1

for i in range(1,N):
    if S[i-1]=='0' and S[i]=='1':
        val = N-1
        count = 0
        for j in range(place+2,N):
            val = j
            if S[j]=='1':
                count += 1
            if count==2:
                break
        if val-i+1>ans:
            ans = val-i+1
        place = val

print(ans)

Submission status: Runtime Error

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

mxl = []
Sl = []
c = 0
if S[0] == "0":
  Sl.append(0)
for i in range(N-1):
  c += 1
  if S[i] != S[i+1]:
    Sl.append(c)
    c = 0
if S[N-1] == "0":
  c += 1
  Sl.append(c)
  Sl.append(0)
else:
  c += 1
  Sl.append(c)

Kl = K*2+1

Ru = [0 for i in range(len(Sl)+1)]

if len(Sl) ==1:
  mxl.append(Sl[0])
else:
  for i in range(1,len(Sl)+1):
    Ru[i] = Ru[i-1]+Sl[i-1] 
  mxl.append(Ru[Kl])
  
for i in range(2,len(Sl)+1-Kl,2):
  mxl.append(Ru[Kl+i]-Ru[i])

print(max(mxl))

Submission status: Runtime Error

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

mxl = []
Sl = []
c = 0
if S[0] == "0":
  Sl.append(0)
for i in range(N-1):
  c += 1
  if S[i] != S[i+1]:
    Sl.append(c)
    c = 0
if S[N-1] == "0":
  c += 1
  Sl.append(c)
  Sl.append(0)
else:
  c += 1
  Sl.append(c)

Kl = K*2+1

Ru = [0 for i in range(len(Sl)+1)]
Ru[0] = 0

if len(Sl) ==1:
  mxl.append(Sl[0])
else:
  for i in range(1,len(Sl)+1):
    Ru[i] = Ru[i-1]+Sl[i-1] 
  mxl.append(Ru[Kl])
  
for i in range(3,len(Sl)+1-Kl,2):
  mxl.append(Ru[Kl+i]-Ru[i-1])

print(max(mxl))

Submission status: Runtime Error

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

mxl = []
Sl = []
c = 0
if S[0] == "0":
  Sl.append(0)
for i in range(N-1):
  c += 1
  if S[i] != S[i+1]:
    Sl.append(c)
    c = 0
if S[N-1] == "0":
  c += 1
  Sl.append(c)
  Sl.append(0)
else:
  c += 1
  Sl.append(c)

Kl = K*2+1

if len(Sl) ==1:
  mxl.append(Sl[0])

#for i in range(0,len(Sl)+1-Kl,2):
#  mxl.append(sum(Sl[i:Kl+i]))

print(max(mxl))

Submission status: Runtime Error

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

mxl = []
Sl = []
c = 0
if S[0] == "0":
  Sl.append(0)
for i in range(N-1):
  c += 1
  if S[i] != S[i+1]:
    Sl.append(c)
    c = 0
if S[N-1] == "0":
  c += 1
  Sl.append(c)
  Sl.append(0)
else:
  c += 1
  Sl.append(c)

Kl = K*2+1

if len(Sl) ==1:
  mxl.append(Sl[0])

for i in range(0,len(Sl)+1-Kl,2):
  mxl.append(sum(Sl[i:Kl+i]))

print(max(mxl))

Submission status: Runtime Error

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

mxl = []
if S == ['1']:
  mxl.append(1)
Sl = []
c = 0
if S[0] == "0":
  Sl.append(0)
for i in range(N-1):
  c += 1
  if S[i] != S[i+1]:
    Sl.append(c)
    c = 0
if S[N-1] == "0":
  c += 1
  Sl.append(c)
  Sl.append(0)
else:
  c += 1
  Sl.append(c)

Kl = K*2+1

for i in range(0,len(Sl)+1-Kl,2):
  mxl.append(sum(Sl[i:Kl+i]))

print(max(mxl))

Submission status: Runtime Error

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

Sl = []
c = 0
if S[0] = "0":
  Sl.append(0)
for i in range(N-1):
  c += 1
  if S[i] != S[i+1]:
    Sl.append(c)
    c = 0
if S[N-1] == "0":
  c += 1
  Sl.append(c)
  Sl.append(0)
else:
  c += 1
  Sl.append(c)

Kl = K*2+1

mxl = []
for i in range(len(Sl)+1-Kl):
  mxl.append(sum(Sl[i:Kl+i]))

print(max(mxl))

Submission status: Runtime Error

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

judge=[0]
max_=0
for i in range(n-1):
  if s[i]!=s[i+1]:
    judge.append(i+1)
judge.append(n)

if len(judge)//2<=k:
  print(len(s))
else:
  for i in range(len(judge)-2*k):
    if s[judge[i]]=='0':
      ans=judge[i+2*k]-judge[i]
    else:
      ans=judge[i+2*k+1]-judge[i]
    if ans>max_:
      max_=ans
  print(max_)

Submission status: Runtime Error

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


if s[0] == '1':
    length_01 = [0]
else:
    length_01 = [0,0]

for i in range(n-1):
    if s[i] != s[i+1]:
        length_01.append(i+1)

if s[n-1] == '0':
    length_01.append(n-1)

num_0 = int((len(length_01)+1)/2)
if num_0 - 1 >= k:
    answer = 0
    for i in range(num_0):
        if answer < length_01[2*(i+k)] - length_01[2*i] + 1:
            answer = length_01[2*(i+k)] - length_01[2*i] + 1
else:
    answer = n

print(answer)

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
s=input()
s+="2";c=1
p=[]
for i in range(len(s)-1):
  if s[i]!=s[i+1]:
    p.append(c)
    c=1
    continue
  c+=1
if s[0]=="1":
  d=sum(p[:min(2*k+1,len(p))])
  ans=sum(p[:min(2*k+1,len(p))])
  if 2*k+1>=len(p):
    print(ans)
  else:
    m=len(p)-2*k-1
    for i in range(m):
      d-=p[i]
      d+=p[i+m]
      if d>ans:ans=d
    print(ans)
else:
  p=[1]+p
  flag=0
  d=sum(p[:min(2*k+1,len(p))])
  ans=sum(p[:min(2*k+1,len(p))])
  if 2*k+1>=len(p):
    print(ans-1)
  else:
    m=len(p)-2*k-1
    for i in range(m):
      d-=p[i]
      d+=p[i+m]
      if d>ans:
        ans=d
        flag=1
    if flag==0:
      print(ans-1)
    else:
      print(ans)

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
s=list(map(int,str(input())))
if s[0]==1:
  one_st=[0]+[i+1 for i in range(n-1) if s[i]==0 and s[i+1]==1]
  zero_st=[i+1 for i in range(n-1) if s[i]==1 and s[i+1]==0]
else:
  one_st=[i+1 for i in range(n-1) if s[i]==0 and s[i+1]==1]
  zero_st=[0]+[i+1 for i in range(n-1) if s[i]==1 and s[i+1]==0]
if s[n-1]==1:
  one_en=[i for i in range(n-1) if s[i]==1 and s[i+1]==0]+[n-1]
  zero_en=[i for i in range(n-1) if s[i]==0 and s[i+1]==1]
else:
  one_en=[i for i in range(n-1) if s[i]==1 and s[i+1]==0]
  zero_en=[i for i in range(n-1) if s[i]==0 and s[i+1]==1]+[n-1]
if len(zero_st) <= k:
  x=n
else:
  y=[one_en[i+k]-one_st[i]+1 for i in range(len(zero_st)-k+1) if i+k+1<=len(one_en)]
  y=y+[one_en[k-1]+1,n-one_st[len(zero_st)-k]]
  x=max(y)
print(x)