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:
def resolve():
    N, K = list(map(int, input().split()))
    S = input()
    S_small = ""
    cnt = 0
    for c in S:
        if c == "1":
            cnt += 1
        else:
            if cnt > 0:
                S_small += str(cnt)
                cnt = 0
            S_small += "0"
    if cnt > 0:
        S_small += str(cnt)
    S_small = list(S_small)
    #print(S_small)
    sizes = []
    opn = False
    cnt = 0
    idx = 0
    start = 0
    for end, c in enumerate(S_small):
        if c == "0":
            opn = True
            cnt += 1
        else:
            if opn:
                cnt += int(c)
                sizes.append((idx, cnt, start, end))
                idx += 1
                cnt = int(c)
                start = end
                opn = False
            else:
                cnt += int(c)
    if start != end:
        sizes.append((idx, cnt, start, None))
    #print(sizes)
    if sizes == []:
        print(N)
        return
    maxidx, maxcnt, _, __ = max(sizes, key=lambda x: x[1])
    leftidx = maxidx - 1 if maxidx > 0 else None
    rightidx = maxidx + 1 if maxidx < len(sizes)-1 else None
    ans = maxcnt
    for j in range(K-1):
        if leftidx is not None and leftidx >= 0:
            _, leftcnt, left_leftidx, left_rightidx = sizes[leftidx]
            left_leftedge, left_rightedge = int(S_small[left_leftidx]), int(S_small[left_rightidx])
            leftdiff = leftcnt - left_rightedge
        else:
            leftdiff = 0

        if rightidx is not None and rightidx <= len(sizes)-1:
            _, rightcnt, right_leftidx, right_rightidx = sizes[leftidx]
            right_leftedge, right_rightedge = int(S_small[right_leftidx]), int(S_small(right_rightidx))
            rightdiff = rightcnt - right_leftedge
        else:
            rightdiff = 0

        if leftidx < 0 and rightidx > len(sizes)-1:
            break

        if rightdiff < leftdiff:
            ans += leftdiff
            leftidx -= 1
        else:
            ans += rightdiff
            rightidx += 1
    print(ans)

    

if '__main__' == __name__:
    resolve()

Submission status: Runtime Error

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

s=list(input())

memo = [0]

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

if len(memo) == 1:
  print(n)
else:
  if s[-1] == s[-2]:
    memo.append(n)

  ans = 0
  print(memo)

  kyoukai_math = len(memo)

  for i in range(kyoukai_math):
    if s[i] == '0':
      hasi = min(kyoukai_math-1,i+k*2)
    else:
      hasi = min(kyoukai_math-1,i+k*2+1)
    ans = max(ans,memo[hasi]-memo[i])
print(ans)

Submission status: Runtime Error

Solution code:
h = len(nagasa)

if h // 2 <= k:
    print(n)
    exit()
lef = 0
rig = 2*k+1
now = sum(nagasa[lef:rig])
ans = now
while True:
    if rig == h:
        break
    now = now - nagasa[lef] - nagasa[lef+1] + nagasa[rig] + nagasa[rig+1]
    ans = max(ans,now)
    lef += 2
    rig += 2
print(ans)

Submission status: Runtime Error

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

s = '1' + s + '1'

cnt_1 = []
cnt_0 = []
stack_1 = 0
stack_0 = 0
for i in s:
    if i == '1':
        if stack_0 > 0:
            cnt_0.append(stack_0)
            stack_0 = 0
        stack_1 += 1
    else:
        if stack_1 > 0:
            cnt_1.append(stack_1)
            stack_1 = 0
        stack_0 += 1
if stack_1 > 0:
    cnt_1.append(stack_1)

if len(cnt_1) == 0 or len(cnt_0) == 0:
    print(n)
    exit()
cnt_1[0] -= 1
cnt_1[-1] -= 1
cum_1 = []
cum_0 = []
for i in range(len(cnt_1)-k):
    cum_1.append(sum(cnt_1[i:i+k+1]))
for i in range(len(cnt_0)-k+1):
    cum_0.append(sum(cnt_0[i:i+k]))

res = []
for v, w in zip(cum_1, cum_0):
    res.append(v+w)
print(max(res))

Submission status: Runtime Error

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

s = '1' + s + '1'

cnt_1 = []
cnt_0 = []
stack_1 = 0
stack_0 = 0
for i in s:
    if i == '1':
        if stack_0 > 0:
            cnt_0.append(stack_0)
            stack_0 = 0
        stack_1 += 1
    else:
        if stack_1 > 0:
            cnt_1.append(stack_1)
            stack_1 = 0
        stack_0 += 1
if stack_1 > 0:
    cnt_1.append(stack_1)
cnt_1[0] -= 1
cnt_1[-1] -= 1
if len(cnt_1) == 0 or len(cnt_0) == 0:
    print(n)
    exit()
cum_1 = []
cum_0 = []
for i in range(len(cnt_1)-k):
    cum_1.append(sum(cnt_1[i:i+k+1]))
for i in range(len(cnt_0)-k+1):
    cum_0.append(sum(cnt_0[i:i+k]))

res = []
for v, w in zip(cum_1, cum_0):
    res.append(v+w)
print(max(res))

Submission status: Runtime Error

Solution code:
import numpy as np
from collections import deque
from collections import defaultdict
import heapq
import collections
import itertools
import bisect

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

# 1の人をわざわざ0にする必要はない
# i番目の人から数えて何人いけるかを考える
ans = 0
prev = s[0]
change_points = [0]
for i in range(n):
    if prev != s[i]:
        change_points.append(i)
    prev = s[i]
change_points.append(n)
# 全部ひっくり返せる時
if (s[0] == '1'):
    if (len(change_points) - 2 <= 2 * k):
        print(n)
        exit()
else:
    if (len(change_points) - 1 <= 2 * k):
        print(n)
        exit()
ans = 0
# print(change_points)
for i in range(len(change_points)-(k*2)):
    if (s[i] == '1'):
        ans_kouho = change_points[i+2*k+1] - change_points[i]
        ans = max(ans, ans_kouho)
    else:
        ans_kouho = change_points[i+k*2] - change_points[i]
        ans = max(ans, ans_kouho)
    # print(i, ans_kouho)
print(ans)
# dp[i] = dp[i-1]

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
import numpy as np
from collections import deque
from collections import defaultdict
import heapq
import collections
import itertools
import bisect


mod = 10 ** 9 + 7
n, k = list(map(int, input().split()))
s = input()
dp = [1] * n
# 1の人をわざわざ0にする必要はない
# i番目の人から数えて何人いけるかを考える
ans = 0
prev = s[0]
change_points = [0]
for i in range(n):
    if prev != s[i]:
        change_points.append(i)
    prev = s[i]
# 全部ひっくり返せる時
if (((len(change_points))//2 <= k and s[0] == '1') or (len(change_points)//2 + 1 <= k)):
    print(n)
    exit()
change_points.append(n)
ans = 0
# print(change_points)
for i in range(len(change_points)-(k*2)):
    if (s[i] == '1'):
        ans_kouho = change_points[i+2*k+1] - change_points[i]
        ans = max(ans, ans_kouho)
    else:
        ans_kouho = change_points[i+k*2] - change_points[i]
        ans = max(ans, ans_kouho)
    # print(i, ans_kouho)
print(ans)
# dp[i] = dp[i-1]

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
import numpy as np
from collections import deque
from collections import defaultdict
import heapq
import collections
import itertools
import bisect


mod = 10 ** 9 + 7
n, k = list(map(int, input().split()))
s = input()
dp = [1] * n
# 1の人をわざわざ0にする必要はない
# i番目の人から数えて何人いけるかを考える
ans = 0
prev = s[0]
change_points = [0]
for i in range(n):
    if prev != s[i]:
        change_points.append(i)
    prev = s[i]
# 全部ひっくり返せる時
if ((len(change_points))//2 <= k and s[0] == '1'):
    print(n)
    exit()
change_points.append(n)
ans = 0
# print(change_points)
for i in range(len(change_points)-(k*2)):
    if (s[i] == '1'):
        ans_kouho = change_points[i+2*k+1] - change_points[i]
        ans = max(ans, ans_kouho)
    else:
        ans_kouho = change_points[i+k*2] - change_points[i]
        ans = max(ans, ans_kouho)
    # print(ans_kouho)
print(ans)
# dp[i] = dp[i-1]

Submission status: Runtime Error

Solution code:
N,K=map(int,input().split())
S=list(input())
T=[int(a) for a in S]
U=[]

tmp=T[0]
cnt=1
for i in range(1,N):
  if i==N-1:
    if T[i]==T[i-1]:
      cnt+=1
      U.append((tmp,cnt) )
    else:
      U.append((tmp,cnt) )
      U.append((T[i],1))
  else:
    if T[i]==T[i-1]:
      cnt+=1
    else:
      U.append((tmp,cnt) )
      tmp=T[i]
      cnt=1
    
print(U)

for _ in range(K):
  maxi=0
  for i,kumi in enumerate(U):
    if kumi[0]==0:
      if i==0:
        if len(U)>1:
          tmp = kumi[1]+U[i+1][1]
        else:
          tmp= kumi[1]
      elif 0<i<len(U)-1:
        tmp = U[i-1][1] + kumi[1]+ U[i+1][1]
      else: # i=len(U)-1
        tmp= U[i-1][1] + kumi[1]
      maxi=max(maxi,tmp)
  print("search:",maxi)
  for i,kumi in enumerate(U):
    if kumi[0]==0 and kumi[1]==maxi:
      if i==0:
        if len(U)>1:
          U[1][1] += kumi[1]
          U.pop(0)
        else:
          kumi[0]=1
      elif 0<i<len(U)-1:
        U[i-1][1] += kumi[1]+U[i+1][1]
        U.pop(i)
        U.pop(i)
      else: # i=len(U)-1
        U[i-1][1] += kumi[1]
        U.pop()
  print(U)

Submission status: Runtime Error

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

    res = []
    buf = '1' # start:1 kara NG->s[0]
    cnt = 0
    for i in range(n):
        if s[i] == buf:
            cnt += 1
        else:
            res.append(cnt)
            buf = s[i]
            cnt = 1
    if cnt != 0:
        res.append(cnt)

    if len(res)%2 == 0:
        res.append(0) # end:1 no kosu

    #from itertools import accumulate
    #rui = list(accumulate(res))
    rui = [0]*(len(res)+1)
    for i in range(len(rui)-1):
        rui[i+1] = rui[i] + res[i]

    for i in range(0,len(res),2): # 1 only
        left = i
        right = min(i+2*k+1, len(res))
        tmp = rui[right] - rui[left]
        ans = max(tmp,ans)
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
import sys, itertools

N, K = map(int, input().split())
S = sys.stdin.readline()[:-1]
A = []
if S[0] == '0':
    A.append(0)

num1 = 0
num0 = 0
for s in S:
    if s == '1':
        if num0 != 0:
            A.append(num0)
        num1 += 1
        num0 = 0
    else:
        if num1 != 0:
            A.append(num1)
        num1 = 0
        num0 += 1
if num1 != 0:
    A.append(num1)
if num0 != 0:
    A.append(num0)

ans = 0
n = len(A)
if n // 2 <= K:
    ans = N
else:
    A = [0] + list(itertools.accumulate(A))
    for i in range(0, n-2*K+1, 2):
        ans = max(A[i+2*K+1] - A[i], ans)

print(ans)

Submission status: Runtime Error

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

ns = []
now_num = s[0]
cnt = 0
for i in range(n):
    if now_num == s[i]:
        cnt += 1
    else:
        ns.append((now_num, cnt))
        now_num = s[i]
        cnt = 1
ns.append((now_num, cnt))
#print(ns)
len_ns = len(ns)
pre = [0] * (len_ns + 1)
for i in range(len_ns):
    pre[i + 1] = pre[i] + ns[i][1]
#print(pre)

ans = 1
for i in range(len_ns - 2*k + 1):
    temp = pre[i + 2*k] - pre[i]
    if ns[i - 1][0] == '1':temp += ns[i - 1][1]
    elif i + 2*k != len_ns and ns[i + 2*k][0] == '0':temp += ns[i + 2*k + 1][1]
    if temp > ans:ans = temp
print(ans if len_ns - 2*k + 1 > 0 else len(s))

Submission status: Runtime Error

Solution code:
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
from itertools import permutations, accumulate, combinations, combinations_with_replacement, groupby
from math import sqrt, ceil, floor, factorial
from bisect import bisect_left, bisect_right, insort_left, insort_right
from copy import deepcopy
from operator import itemgetter
from functools import reduce
from fractions import gcd
import sys
def input(): return sys.stdin.readline().rstrip()
def I(): return int(input())
def Is(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
def TI(): return tuple(map(int, input().split()))
def IR(n): return [I() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def TIR(n): return [TI() for _ in range(n)]
def S(): return input()
def Ss(): return input().split()
def LS(): return list(input())
def SR(n): return [S() for _ in range(n)]
def SsR(n): return [Ss() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
sys.setrecursionlimit(10**6)
MOD = 10**9+7
INF = float('inf')
# ----------------------------------------------------------- #

n, k = Is()
s = S()
A = []  # count of 1  (1, 0, 1, 0, ... , 1)
B = []  # count of 0  (1, 0, 1, 0, ... , 1)
if s[0] == "0":
    A.append(0)
for key, value in groupby(s):
    if key == "1":
        A.append(len(list(value)))
    else:
        B.append(len(list(value)))
if s[0] == "0":
    A.append(0)

# print(A)
# print(B)
# A: i ~ i+k  B: i ~ i+k-1  の和の最大が答え

accA = [0] + list(accumulate(A))
accB = [0] + list(accumulate(B))
# print(accA)
# print(len(accA))

if len(B) <= k:
    print(sum(A)+sum(B))
    exit()

ans = 0
for i in range(1, len(accA)-k):
    tmp = accA[i+k] - accA[i-1] + accB[i+k-1] - accB[i-1]
    ans = max(ans, tmp)
print(ans)

Submission status: Runtime Error

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

ans_list = []
if len(hand_list) > 2 * k:
    for i in range(len(hand_list) - 2 * k):
        if s[i] == '1':
            ans = hand_list[i + 2 * k + 1] - hand_list[i]
        else:
            ans = hand_list[i + 2 * k] - hand_list[i]
        ans_list.append(ans)
else:
    ans_list.append(n)
print(max(ans_list))

Submission status: Runtime Error

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

ans_list = []
for i in range(len(hand_list) - 2 * k):
    if s[i] == '1':
        ans = hand_list[i + 2 * k + 1] - hand_list[i]
    else:
        ans = hand_list[i + 2 * k] - hand_list[i]
    ans_list.append(ans)
print(max(ans_list))

Submission status: Runtime Error

Solution code:
def ranlength(A):
    num = list(A)
    count = 1
    result = ''
    table = []
    for i in range(len(num)):
        try:
            if num[i] == num[i+1]:
               count = count + 1
            else:
                result += str(num[i]) + str(count)
                table.append(count)
                count = 1
        except:
            result += str(num[i]) + str(count)
            table.append(count)
    return(table)
    
N,K=map(int,input().split())
S=input()

RL=ranlength(S)
#print(RL)

#しゃくとり法
if int(S[0])==1:
    cnt1=RL[0]
    ans1=RL[0]
    for i in range(len(RL)):
        if i%2==0 and i>=2:
            cnt1+=RL[i]
            cnt1+=RL[i-1]
            if i>=K*2+2:
                cnt1-=RL[i-(K*2+1)]
                cnt1-=RL[i-(K*2+2)]
            ans1=max(ans1,cnt1)
            #print(cnt1)
    print(ans1)
elif int(S[0])==0:
    cnt2=RL[0]+RL[1]
    ans2=RL[0]+RL[1]
    for i in range(len(RL)):
        if i%2==1 and i>=3:
            cnt2+=RL[i]
            cnt2+=RL[i-1]
            if i>=K*2+1:
                cnt2-=RL[i-(K*2+1)]
                cnt2-=RL[i-(K*2)]
            ans2=max(ans2,cnt2)
            #print(cnt2)
    print(ans2)

Submission status: Runtime Error

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

if(S[0] == "1"):
    s = []
else:
    s = [0]
cnt = 1
tmp = S[0]
for i in range(1, len(S)):
    if(tmp == S[i]):
        cnt += 1
    else:
        s.append(cnt)
        tmp = S[i]
        cnt = 1
s.append(cnt)
if(len(s)//2 <= K):
    ans = sum(s)        
else:
    ans = sum(s[:(2*K+1)])
    tmp = ans
    cnt = 0
    for i in range(2*K+1, len(s)-1, 2):
        if(ans < tmp-s[cnt]-s[cnt+1]+s[i]+s[i+1]):
            ans = tmp-s[cnt]-s[cnt+1]+s[i]+s[i+1]
            tmp = tmp-s[cnt]-s[cnt+1]+s[i]+s[i+1]
        else:
            tmp = tmp-s[cnt]-s[cnt+1]+s[i]+s[i+1]
        cnt += 2
    ss = s[::-1]
    if(S[-1] == "0"):
        tmp1 = sum(ss[:(2*K)])
    else:
        tmp1 = sum(ss[:(2*K+1)])
print(max(ans, tmp1))

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(10**8)
def ii(): return int(sys.stdin.readline())
def mi(): return map(int, sys.stdin.readline().split())
def li(): return list(map(int, sys.stdin.readline().split()))
def li2(N): return [list(map(int, sys.stdin.readline().split())) for _ in range(N)]
def dp2(ini, i, j): return [[ini]*i for _ in range(j)]
def dp3(ini, i, j, k): return [[[ini]*i for _ in range(j)] for _ in range(k)]
#import bisect #bisect.bisect_left(B, a)
#from collections import defaultdict #d = defaultdict(int) d[key] += value
#from itertools import accumulate #list(accumulate(A))

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

len_list = [0]*(10**5+100)
ind = 0
if S[0] == '0':
    len_list[0] = 0
    ind += 1

cnt = 1
for i in range(1, N):
    if S[i] != S[i-1]:
        len_list[ind] = cnt
        ind += 1
        cnt = 1
    else:
        cnt += 1
len_list[ind] = cnt

#print(len_list[:30])
i = 0
wa = 0
sum_l = sum(len_list[:2*K+1])
while True:
    if len_list[i+2*K+1] != 0:
        wa = max(wa, sum_l)
    else:
        wa = max(wa, sum_l)
        break
    sum_l -= (len_list[i] + len_list[i+1])
    sum_l += (len_list[i+2*K+1] + len_list[i+2*K+2])
    i += 2
print(wa)

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(10**8)
def ii(): return int(sys.stdin.readline())
def mi(): return map(int, sys.stdin.readline().split())
def li(): return list(map(int, sys.stdin.readline().split()))
def li2(N): return [list(map(int, sys.stdin.readline().split())) for _ in range(N)]
def dp2(ini, i, j): return [[ini]*i for _ in range(j)]
def dp3(ini, i, j, k): return [[[ini]*i for _ in range(j)] for _ in range(k)]
#import bisect #bisect.bisect_left(B, a)
#from collections import defaultdict #d = defaultdict(int) d[key] += value
#from itertools import accumulate #list(accumulate(A))

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

len_list = [0]*(10**5+10)
ind = 0
if S[0] == '0':
    len_list[0] = 0
    ind += 1

cnt = 1
for i in range(1, N):
    if S[i] != S[i-1]:
        len_list[ind] = cnt
        ind += 1
        cnt = 1
    else:
        cnt += 1
len_list[ind] = cnt

#print(len_list[:30])
i = 0
wa = 0
sum_l = sum(len_list[:2*K+1])
while True:
    if len_list[i+2*K+1] != 0:
        wa = max(wa, sum_l)
    else:
        wa = max(wa, sum_l)
        break
    sum_l -= (len_list[i] + len_list[i+1])
    sum_l += (len_list[i+2*K+1] + len_list[i+2*K+2])
    i += 2
print(wa)

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(10**8)
def ii(): return int(sys.stdin.readline())
def mi(): return map(int, sys.stdin.readline().split())
def li(): return list(map(int, sys.stdin.readline().split()))
def li2(N): return [list(map(int, sys.stdin.readline().split())) for _ in range(N)]
def dp2(ini, i, j): return [[ini]*i for _ in range(j)]
def dp3(ini, i, j, k): return [[[ini]*i for _ in range(j)] for _ in range(k)]
#import bisect #bisect.bisect_left(B, a)
#from collections import defaultdict #d = defaultdict(int) d[key] += value
#from itertools import accumulate #list(accumulate(A))

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

len_list = [0]*(10**5)
ind = 0
if S[0] == '0':
    len_list[0] = 0
    ind += 1

cnt = 1
for i in range(1, N):
    if S[i] != S[i-1]:
        len_list[ind] = cnt
        ind += 1
        cnt = 1
    else:
        cnt += 1
len_list[ind] = cnt

#print(len_list[:30])
i = 0
wa = 0
while True:
    if len_list[i+2*K+1] != 0:
        wa = max(wa, sum(len_list[i:i+2*K+1]))
    else:
        wa = max(wa, sum(len_list[i:]))
        break
    i += 2
print(wa)

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(10**8)
def ii(): return int(sys.stdin.readline())
def mi(): return map(int, sys.stdin.readline().split())
def li(): return list(map(int, sys.stdin.readline().split()))
def li2(N): return [list(map(int, sys.stdin.readline().split())) for _ in range(N)]
def dp2(ini, i, j): return [[ini]*i for _ in range(j)]
def dp3(ini, i, j, k): return [[[ini]*i for _ in range(j)] for _ in range(k)]
#import bisect #bisect.bisect_left(B, a)
#from collections import defaultdict #d = defaultdict(int) d[key] += value
#from itertools import accumulate #list(accumulate(A))

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

len_list = []
if S[0] == '1' and S[1] != '1':
    len_list.append(0)

cnt = 1
for i in range(1, N):
    if S[i] != S[i-1]:
        len_list.append(cnt)
        cnt = 1
    else:
        cnt += 1
len_list.append(cnt)

#print(len_list)
i = 0
wa = 0
while True:
    if i+2*K+1 < N:
        wa = max(wa, sum(len_list[i:i+2*K+1]))
    else:
        wa = max(wa, sum(len_list[i:]))
        break
    i += 2
print(wa)

Submission status: Runtime Error

Solution code:
// #include <bits/stdc++.h>
#include <iostream>  // cin, cout, string
#include <algorithm> // sort, min/max, upper/lower_bound, transform, count/copy_if
#include <vector>
#include <utility> // pair, swap
#include <functional>
#include <iomanip> // setprecision
#include <map>
#include <set> // multiset
#include <unordered_map>
#include <unordered_set>
#include <tuple>
#include <stack>
#include <queue> // priority_queue
#include <deque>
#include <bitset>
#include <array>
#include <list>
#include <forward_list>
#include <complex>
#include <ratio>
#include <cassert>
#include <numeric> // accumulate, itoa
#include <random>  // mt19937
#include <iterator>
#include <chrono>
#include <cstring>

using namespace std;

// #define int long long

using i64 = int64_t;
using ll = long long;
using ld = long double;

template <typename T>
using vec = vector<T>;
template <typename T>
using mat = vec<vec<T>>;
template <typename T>
using dic = unordered_map<string, T>;
template <typename T>
using pqg = priority_queue<T, vector<T>, greater<T>>; // dijkstra
// template<typename T, typename U> using P = pair<T, U>;
// template<typename... Args> using func = function<Args...>;
// template<typename T> using pq = priority_queue<T>;

using pii = pair<int, int>;
using ppi = pair<pii, int>;
using pip = pair<int, pii>;
using ppp = pair<pii, pii>;

using vi = vec<int>;
using mi = mat<int>; // Graph
using vp = vec<pii>;

constexpr int IINF = 0x3f3f3f3f;
constexpr long long LINF = 0x3f3f3f3f3f3f3f3fLL;
// constexpr int INF = sizeof(int) == sizeof(long long) ? LINF : IINF;
constexpr long long MOD = 1e9 + 7;

#define _overload(_1, _2, _3, name, ...) name
#define _rep1(i, a, b) for (int i = (a), _##i = (b); i < _##i; ++i)
#define _rep2(i, n) _rep1(i, 0, n)
#define _rep3(n) _rep2(i, n)
#define rep(...) _overload(__VA_ARGS__, _rep1, _rep2, _rep3)(__VA_ARGS__)
#define _rrep1(i, a, b) for (int i = (b)-1, _##i = (a); i >= _##i; --i)
#define _rrep2(i, n) _rrep1(i, 0, n)
#define _rrep3(n) _rrep2(i, n)
#define rrep(...) _overload(__VA_ARGS__, _rrep1, _rrep2, _rrep3)(__VA_ARGS__)

#define each(i, a) for (auto &&i : a)
#define each2(i, a) for (const auto &i : a)

#define pb push_back
// emplace_back(*) == push_back(constructor(*))
#define eb emplace_back
#define pob pop_back
#define pf push_front
#define pof pop_front

#define np next_permutation
#define pp prev_permutation

// make_pair(A, B) == pair<T, U>(A, B)
#define mp make_pair
// mt(...) == tie(...)
#define mt make_tuple

#define ft first
#define sd second

#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define sz(a) (int)(a).size()
#define rev(a) reverse(all(a))
#define rs(a, l) (a).resize(l)

#define upper(s) transform(all(s), (s).begin(), topper)
#define lower(s) transform(all(s), (s).begin(), tolower)

#define sum(a) accumulate(all(a), 0)
#define elif else if
#define unless(a) if (!(a))
#define qt(a) #a

#define endl '\n'
#define dec(n) fixed << setprecision(n)
#define zfl(n) setfill(‘0’) << setw(n)

template <typename T = int, T l = 0, T r = (T)MOD>
auto rnd = bind(uniform_int_distribution<T>(l, r), mt19937(chrono::steady_clock::now().time_since_epoch().count()));

template <typename T>
inline constexpr T gcd(T a, T b)
{
    if (a == 0 || b == 0)
        return 0;
    while (b)
    {
        a = a % b;
        swap(a, b);
    }
    return a;
}
template <typename T>
inline constexpr T extgcd2(T a, T b, T &x, T &y)
{
    x = 1;
    y = 0;
    while (b)
    {
        T t = a / b;
        a -= t * b;
        swap(a, b);
        x -= t * y;
        swap(x, y);
    }
    return a;
}
template <typename T>
inline constexpr T lcm(T a, T b) { return a / gcd(a, b) * b; }

template <typename T>
inline constexpr bool chmax(T &a, const T &b)
{
    if (a < b)
    {
        a = b;
        return true;
    }
    return false;
}
template <typename T>
inline constexpr bool chmin(T &a, const T &b)
{
    if (b < a)
    {
        a = b;
        return true;
    }
    return false;
}

template <typename A, typename B>
istream &operator>>(istream &is, pair<A, B> &p)
{
    is >> p.first;
    is >> p.second;
    return is;
}
template <typename A, typename B>
ostream &operator<<(ostream &os, const pair<A, B> &p)
{
    os << p.first << " " << p.second;
    return os;
}

template <typename T>
istream &operator>>(istream &is, vector<T> &v)
{
    for (auto &&x : v)
        is >> x;
    return is;
}
template <typename T>
ostream &operator<<(ostream &os, const vector<T> &v)
{
    for (int i = 0; i < (int)v.size(); ++i)
        cout << v[i] << (i + 1 != (int)v.size() ? " " : "");
    return os;
}

#define debug(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg>
void __f(const char *name, const Arg &arg)
{
    cerr << name << " : " << arg << endl;
}
template <typename Arg, typename... Args>
void __f(const char *names, const Arg &arg, const Args &... args)
{
    const char *comma = strchr(names + 1, ',');
    cerr.write(names, comma - names) << " : " << arg << " | ";
    __f(comma + 1, args...);
}

inline auto yes(const bool &c) { return c ? "yes" : "no"; }
inline auto Yes(const bool &c) { return c ? "Yes" : "No"; }
inline auto YES(const bool &c) { return c ? "YES" : "NO"; }
inline auto Possible(const bool &c) { return c ? "Possible" : "Impossible"; }
inline auto POSSIBLE(const bool &c) { return c ? "POSSIBLE" : "IMPOSSIBLE"; }

constexpr int lim = 100007; // 10^5+7

int n, k;
string s;

signed main()
{
    cin.tie(0);
    cout.tie(0);
    ios_base::sync_with_stdio(false);

    cin >> n >> k >> s;
    vec<int> ind;
    ind.eb(0);
    rep(i, 1, n - 1)
    {
        if (s.at(i) != s.at(i + 1))
            ind.eb(i + 1);
    }
    int ans = 0;
    // rep(ind.size()) cout << ind.at(i) << " ";
    // cout << endl;

    ind.eb(n);
    int hoge = ind.size() - 1;
    rep(ind.size() - 1)
    {
        int t;
        if (s.at(ind.at(i)) == '0')
        {
            int l = i + 2 * k;
            t = ind.at(min(l, hoge)) - ind.at(i);
        }
        else
        {
            int l = i + 2 * k + 1;
            t = ind.at(min(l, hoge)) - ind.at(i);
        }
        ans = max(t, ans);
    }
    // rep(ind.size()) cout << ind.at(i) << " ";
    // cout << endl;
    cout << ans << endl;
    return 0;
}

Submission status: Runtime Error

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

if s[0] == "0":
    res.append(0)

res.append(1)

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

if s[-1] == "0":
    res.append(0)

l = len(res)
su = [0] * (l + 1)
for i in range(l):
    su[i + 1] = su[i] + res[i]

mil = min(l, 2 * k + 1)

ans = 1
for i in range(0, l - mil + 1, 2):
    ans = max(ans, su[2 * k + 1 + i] - su[i])
print(ans)

Submission status: Runtime Error

Solution code:
N = int(input())

t = [[0] * 10 for _ in range(10)]
for i in range(1, N + 1):
    s = str(i)
    t[int(s[0])][int(s[-1])] += 1

result = 0
for i in range(1, 10):
    for j in range(1, 10):
        result += t[i][j] * t[j][i]
print(result)

Submission status: Runtime Error

Solution code:
n,k = map(int,input().split())
s = input()
a = []
c = 1
for i in range(n-1):
    if s[i]==s[i+1]:
        c+=1
    else:
        a.append(c)
        c = 1
a.append(c)
b = [0]
for i in range(len(a)):
    b.append(b[-1]+a[i])
b.append(b[-1])
l = 2*k+1
st=int(s[0])
ans = 0
for i in range(st,n,2):
    ans = max(ans,b[i]-b[max(0,i-l)])
print(ans)

Submission status: Runtime Error

Solution code:
n,k=map(int,input().strip().split())
s=input.strip()
l=r=0
c=0
while r<n and s[r]=='1': r+=1
while c<k:
 while r<n and s[r]=='0':r+=1
 while r<n and s[r]=='1':r+=1
 if r==n:print(n);exit()
 c+=1
ans=r-l
while r<n:
 while l<n and s[l]=='1':l+=1
 while l<n and s[l]=='0':l+=1 
 while r<n and s[r]=='0':r+=1
 while r<n and s[r]=='1':r+=1
 ans=max(ans,r-l)
print(ans)

Submission status: Runtime Error

Solution code:
def burst(S,k):
    S = "_" + S + "_"
    OP = []
    pt_l,pt_r = None,None
    for i in range(len(S)):
        if S[i] == k:
            if pt_l == None:
                pt_l = i-1
        elif pt_l != None:
            pt_r = i-2
            OP.append((pt_l,pt_r))
            pt_l,pt_r = None,None
    return OP

def merge(burst,k):
    if len(burst) <= k + 1:
        return burst[0][1] - burst[0][0] + 1
    else:
        output = 0
        for i in range(len(burst)-k):
            tmp = burst[k+i][1] - burst[i][0] + 1
            output = max(output,tmp)
        return output

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

burst1 = burst(S,"1")

if burst1[0][0] != 0:
    burst1 = [(0,0)] + burst1
if burst1[len(burst1)-1][1] != N-1:
    burst1 = burst1 + [(N-1,N-1)]

ans = merge(burst1,K)
print(ans)

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())
s = input()
one = [i.count('1') for i in s.split('0') if i.count('1') > 0]
zero = [i.count('0') for i in s.split('1') if i.count('0') > 0]
if s[0] == '0':
    a = sum(one[:k]+zero[:k])
    rslt = a
    for i in range(k, len(zero)):
        if len(zero) > len(one) and i == len(zero)-1:
            a += zero[i]
            a -= zero[i-k]
            rslt = max(rslt, a)
        else:
            a += one[i] + zero[i]       
            a -= one[i-k] + zero[i-k]
            rslt = max(rslt, a)
else:
    a = sum(one[:k+1]+zero[:k])
    rslt = a
    for i in range(k, len(zero)):
        a += one[i+1]+zero[i]
        a -= one[i-k] + zero[i-k]
        rslt = max(rslt, a)
print(rslt)