TnT/Multi_CodeNet4Repair huggingface dataset:
Problem: 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.

Submission attempts (in order):
from collections import deque

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

count = 0
flag = True

la = deque([])
for c in S:
    if int(flag) == c:
        count += 1
    else:
        la.append(count)
        count = 1
        flag = not flag

la.append(count)

print(la)

ltmp = deque([])
sum_la = 0
for _ in range((K-1)*2 + 1):
    tmp = la.popleft()
    sum_la += tmp
    ltmp.append(tmp)

last_a = [sum_la]
while len(la) > 0 :
    tmp = la.popleft()
    sum_la += tmp
    ltmp.append(tmp)
    if len(la) > 0:
        tmp = la.popleft()
        sum_la += tmp
        ltmp.append(tmp)

    last_a.append(sum_la)

    if len(ltmp) > 0:
        tmp = ltmp.popleft()
        sum_la -= tmp
    if len(ltmp) > 0:
        tmp = ltmp.popleft()
        sum_la -= tmp
    last_a.append(sum_la)


#     last_a.append(sum(l[i-K:i+K+1]))


# K = K-1

#     last_b.append(sum(l[i-K:i+K+1]))

print(max(last_a))

#     l = 



from collections import deque

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

count = 0
flag = True

la = deque([])
for c in S:
    if int(flag) == c:
        count += 1
    else:
        la.append(count)
        count = 1
        flag = not flag

la.append(count)

if K >= (len(la) / 2):
    print(N)
    exit()

ltmp = deque([])
sum_la = 0
for _ in range((K-1)*2 + 1):
    tmp = la.popleft()
    sum_la += tmp
    ltmp.append(tmp)

last_a = [sum_la]
while len(la) > 0 :
    tmp = la.popleft()
    sum_la += tmp
    ltmp.append(tmp)
    if len(la) > 0:
        tmp = la.popleft()
        sum_la += tmp
        ltmp.append(tmp)

    last_a.append(sum_la)

    if len(ltmp) > 0:
        tmp = ltmp.popleft()
        sum_la -= tmp
    if len(ltmp) > 0:
        tmp = ltmp.popleft()
        sum_la -= tmp
    last_a.append(sum_la)

print(max(last_a))


Verdicts: Runtime Error

Accepted
Problem: 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.

Submission attempts (in order):
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))

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 = 0
result = 0
if S[0] == '0':
    for j in range(2*K):
        result += X[j]
    ans = result
    for j in range(2*K, l):
        if j == 2*K:
            result -= X[0]
        elif j%2 == 0:
            result = result - X[j-2*K] - X[j-2*K-1]
        result += X[j]
        ans = max(ans,result)
else:
    for j in range(2*K+1):
        result += X[j]
    ans = result
    for j in range(2*K+1, l):
        if j%2 == 1:
            result = result - X[j-2*K-1] - X[j -2*K]
        result += X[j]
        ans = max(ans, result)
print(ans)

Verdicts: Runtime Error

Accepted
Problem: 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.

Submission attempts (in order):
N, K = map(int, input().split())
S = input();

mans = [];
seq = 0;
now = S[0];
if(now == "0"):
    mans.append(0);

for i in S:
    if(i == now):
        seq += 1;
    else:
        mans.append(seq);
        seq = 1;
        if(now=="1"):
            now="0"
        else:
            now="1"

if(seq!=0):
    mans.append(seq);
    if(now=="1"):
        now="0"
    else:
        now="1"
if(now=="0"):
    mans.append(0);

mans.append(0);

shakutori = [0]
shakunum = 0;

for i in mans:
    shakunum += i;
    shakutori.append(shakunum);

print(shakutori);

if(len(shakutori) <= 2*K+1):
    print(shakutori[-1]);
else:
    answer = 0;
    for i in range(1,len(shakutori),2):
        if(len(shakutori) <= i+2*K):
            break;
        else:
            if(shakutori[i+2*K]-shakutori[i-1] > answer):
                answer = shakutori[i+2*K]-shakutori[i-1];
    print(answer);


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

mans = [];
seq = 0;
now = S[0];
if(now == "0"):
    mans.append(0);

for i in S:
    if(i == now):
        seq += 1;
    else:
        mans.append(seq);
        seq = 1;
        if(now=="1"):
            now="0"
        else:
            now="1"

if(seq!=0):
    mans.append(seq);
    if(now=="1"):
        now="0"
    else:
        now="1"
if(now=="0"):
    mans.append(0);

mans.append(0);

shakutori = [0]
shakunum = 0;

for i in mans:
    shakunum += i;
    shakutori.append(shakunum);

if(len(shakutori) <= 2*K+1):
    print(shakutori[-1]);
else:
    answer = 0;
    for i in range(1,len(shakutori),2):
        if(len(shakutori) <= i+2*K):
            break;
        else:
            if(shakutori[i+2*K]-shakutori[i-1] > answer):
                answer = shakutori[i+2*K]-shakutori[i-1];
    print(answer);


Verdicts: Wrong Answer

Accepted
Problem: 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.

Submission attempts (in order):
N,K=map(int,input().split())
S=input()

A=[]
B=[]
if S[0]=='1':
  A.append(0)
for i in range(N-1):
  if S[i]=='0' and S[i+1]=='1':
    A.append(i+1)
  elif S[i]=='1' and S[i+1]=='0':
    B.append(i)
if S[-1]=='1':
  B.append(N-1)

a=len(A)-K
ans=0
for i in range(a):
  l=B[i+K]-A[i]+1
  ans=max(l,ans)
print(ans)

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

A=[]
B=[]
for i in range(N-1):
  if S[i]=='0'and S[i+1]=='1':
    B.append(i)
if S[-1]=='0':
  B.append(N-1)
if S[0]=='0':
  A.append(0)
for i in range(N-1):
  if S[i]=='1'and S[i+1]=='0':
    A.append(i+1)
print(A)
print(B)
if len(A)<=K:
  print(N)
else:
  y=A[K]
  z=N-B[-1*(K+1)]-1
  l=0
  ans=0
  for i in range(len(A)-K-1):
    l=A[i+K+1]-B[i]-1
    ans=max(l,ans)
    print(l)
  print(max(l,y,z))

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

A=[]
B=[]
for i in range(N-1):
  if S[i]=='0'and S[i+1]=='1':
    B.append(i)
if S[-1]=='0':
  B.append(N-1)
if S[0]=='0':
  A.append(0)
for i in range(N-1):
  if S[i]=='1'and S[i+1]=='0':
    A.append(i+1)

if len(A)<=K:
  print(N)
else:
  y=A[K]
  z=N-B[-1*(K+1)]-1
  l=0
  ans=0
  for i in range(len(A)-K-1):
    l=A[i+K+1]-B[i]-1
    ans=max(l,ans)
  print(max(ans,y,z))

Verdicts: Wrong Answer

Wrong Answer

Accepted
Problem: 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.

Submission attempts (in order):
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))

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

bl = []
if S[0] == "0":
    bl.append(0)
bl.append(0)
for i in range(1, N):
    if S[i-1] != S[i]:
        bl.append(i)
bl.append(N)
bl.append(N)

nbl = len(bl)
m = [bl[i+2 * K+1] - bl[i] 
     for i in range(nbl - 2*K-1) if i%2 == 0]
if len(m) == 0:
    print(N)
else:
    print(max(m))

Verdicts: Runtime Error

Accepted
Problem: 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.

Submission attempts (in order):
# input

#s = input()
n=14
k=2
s='11101010110011'

# solve

##. number of consecutive 0 groups and 1 groups
##. ex:

##.       0: 0, 0, 0, 00
##.       1: 111, 1, 1, 11, 11

##.  ones = [3, 1, 1, 2, 2]
##.   (always count the edges of s as 1)
zeros = []
ones = []
_c = '1'
count = 0
for c in s:
	if _c == c:
		count += 1
	else:
		_c = c
		if c == '0':
			zeros.append(count)
		else:
			ones.append(count)
		count = 1

if c == '0':
	zeros.append(count)
	ones.append(0)
else:
	ones.append(count)


ans = 0
i = 0
while i + k <= len(zeros):
	_ans = sum(zeros[i:i+k]) + sum(ones[i:i+k+1])
	ans = max(ans, _ans)
	i += 1

print(ans)
	

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

# solve

##. number of consecutive 0 groups and 1 groups
##. ex:

##.       0: 0, 0, 0, 00
##.       1: 111, 1, 1, 11, 11

##.  ones = [3, 1, 1, 2, 2]
##.   (always count the edges of s as 1)
zeros = []
ones = []
_c = '1'
count = 0
for c in s:
	if _c == c:
		count += 1
	else:
		if _c == '0':
			zeros.append(count)
		else:
			ones.append(count)
		_c = c
		count = 1

if c == '0':
	zeros.append(count)
	ones.append(0)
else:
	ones.append(count)

if len(zeros) <= k:
	## flip all 0's
	ans = len(s)
else:
  
  ans = 0

  i = 0
  _ans = sum(zeros[i:i+k]) + sum(ones[i:i+k+1])
  ans = _ans

  i = 1
  while i + k <= len(zeros):
#	  _ans = sum(zeros[i:i+k]) + sum(ones[i:i+k+1])
	  _ans += zeros[i+k-1] - zeros[i-1] + ones[i+k] - ones[i-1]
	  ans = max(ans, _ans)
	  i += 1

print(ans)

Verdicts: Wrong Answer

Accepted
Problem: 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.

Submission attempts (in order):
N, K = map(int, input().split())
S = input()
L=[]
n=0
for i in range(len(S)-1):
    n+=1
    if(S[i]!=S[i+1]):
        L.append(n)
        n=0
if S[0]=="0":
    L.insert(0,0)

max_r = 0
for i in range(len(L)-K-2):
    R = sum(L[2*(i+1)-2:2*(i+1)-1+2*K+1])
    #print(R)
    if max_r < R:
        max_r = R
print(max_r)


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

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

if S[0]=='0':
    L.insert(0,0)
if S[-1]=='0':
    L.append(N)

max_r = 0
step = K*2+1
if len(L)-step<1:
    step=len(L)-1
for i in range(0, len(L)-step,2):
    max_r = max(max_r, L[i+step]-L[i])

print(max_r)


Verdicts: Wrong Answer

Accepted
Problem: 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.

Submission attempts (in order):
n, k = map(int, input().split())
s = input()
n = len(s)

if n == 1:
    print(1)
    exit(0)

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

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

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

ans = 0
if 2 * k + 1 > len(l):
    sum(l[i * 2 : min(i * 2 + 2 * k + 1, len(l)) : 1])

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


for i in range((len(l) + 1)):
    if i * 2 + 2 * k + 1 > len(l):
        break
    ans = max(ans, b[min(i * 2 + 2 * k + 1, len(l))] - b[i * 2 - 1])
print(ans)


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

if n == 1:
    print(1)
    exit(0)

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

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

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

ans = 0

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


if 2 * k + 1 > len(l) - 1:
    print(b[len(l) - 1])
    exit(0)
else:
    ans = b[2 * k]


for i in range(1, len(l)):
    if i * 2 + 2 * k > len(l) - 1:
        break
    ans = max(ans, b[min(i * 2 + 2 * k, len(l) - 1)] - b[i * 2 - 1])
print(ans)


Verdicts: Runtime Error

Accepted
Problem: 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.

Submission attempts (in order):
from sys import stdin
sN,sK = stdin.readline().rstrip().split()
N,K = int(sN), int(sK)
S = stdin.readline().rstrip()

# print(S)

k = len(S.split("1"))
# print("k:",k)
num = 0
for itr in range(0,N):
    if K < k:
        # print(S[:N-itr].split("1"))
        list1 = S[:N-itr].split("1")
        c = 0
        for itr2 in list1:
            if itr2:
                c += 1
        k = c
    else:
        num = itr
        break
ans_str = S[:N-num].replace("0","1") + S[N-num:]
# print("num:",num)
# print("ans_str:",ans_str)
ans = 0
for itr in range(0,N):
    if ans_str[itr] == "1":
        ans += 1
print("ans:",ans)


from sys import stdin
sN,sK = stdin.readline().rstrip().split()
N,K = int(sN), int(sK)
S = stdin.readline().rstrip()

# print(S)
nums = []
now = 1
cnt = 0
for itr in range(0,N):
    if S[itr] == str(now):
        cnt += 1
    else:
        nums.append(cnt)
        now = 1 - now
        cnt = 1
if(cnt != 0):
    nums.append(cnt)
if(len(nums)%2 == 0):
    nums.append(0)
add = 2*K+1
ans = 0
left = 0
right = 0
tmp = 0
for itr in range(0,len(nums),2):
    nextleft = itr
    nextright = min(itr + add, len(nums))
    while(right < N and right < nextright):
        tmp += nums[right]
        right += 1
    while(left < N and left < nextleft):
        tmp -= nums[left]
        left += 1
    ans = max(tmp, ans)
print(ans)

Verdicts: Wrong Answer

Accepted
Problem: 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.

Submission attempts (in order):
# -*- coding: utf-8 -*-
import itertools
import sys
import math
from functools import lru_cache


# n = int(input())


from queue import Queue
from operator import mul
from functools import reduce
from queue import Queue
from operator import mul
from functools import reduce
from functools import lru_cache

input = sys.stdin.readline


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

s = input() + '2'


# n = int(input())

ret = 0



def next0(l: int):
    if l >= n:
        return l
    l += 1
    while s[l] != '2' and s[l] != '0':
        l += 1
    return min(l, n)



def next1(l: int):
    if l >= n:
        return l
    l += 1
    while s[l] != '2' and s[l] != '1':
        l += 1
    return min(l, n)


start = 0
end = 0
ret = 0

end = next0(end)
for i in range(k):
    end = next1(end)
    end = next0(end)

ret = end - start
#print(start, end, ret)

while end < n:
    start = next1(next0(start))
    end = next0(next1(end))
    ret = max(ret, end - start)
    print(start, end, ret)
print(ret)


# -*- coding: utf-8 -*-
import itertools
import sys
import math
from functools import lru_cache


# n = int(input())


from queue import Queue
from operator import mul
from functools import reduce
from queue import Queue
from operator import mul
from functools import reduce
from functools import lru_cache

input = sys.stdin.readline


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

s = input() + '2'


# n = int(input())

ret = 0



def next0(l: int):
    if l >= n:
        return l
    l += 1
    while s[l] != '2' and s[l] != '0':
        l += 1
    return min(l, n)



def next1(l: int):
    if l >= n:
        return l
    l += 1
    while s[l] != '2' and s[l] != '1':
        l += 1
    return min(l, n)


start = 0
end = 0
ret = 0

end = next0(end)
for i in range(k):
    end = next1(end)
    end = next0(end)

ret = end - start
#print(start, end, ret)

while end < n:
    start = next1(next0(start))
    end = next0(next1(end))
    ret = max(ret, end - start)
    #print(start, end, ret)
print(ret)


Verdicts: Wrong Answer

Accepted
Problem: 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.

Submission attempts (in order):
import numpy as np


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

# search where 1
arr = np.array([0] + s_arr.tolist() + [0])
diff = np.diff(arr)
heads = np.where(diff == 1)[0]
tails = np.where(diff == -1)[0]
assert heads.size == tails.size

ans = 0
for i, head in enumerate(heads):
    idx = min(i + K, len(tails) - 1)
    tail = tails[idx]
    ans = max(ans, tail - head)

print(ans)


import numpy as np


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

# search where 1
arr = np.array([0] + s_arr.tolist() + [0])
diff = np.diff(arr)
heads = np.where(diff == 1)[0]
tails = np.where(diff == -1)[0]
assert heads.size == tails.size

if s_arr[0] == 0:
    heads = np.array([0] + heads.tolist())
    tails = np.array([0] + tails.tolist())

if s_arr[-1] == 0:
    heads = np.array(heads.tolist() + [len(s_arr)])
    tails = np.array(tails.tolist() + [len(s_arr)])

ans = 0
for i, head in enumerate(heads):
    idx = min(i + K, len(tails) - 1)
    tail = tails[idx]
    ans = max(ans, tail - head)

print(ans)


Verdicts: Wrong Answer

Accepted
Problem: 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.

Submission attempts (in order):
#include<bits/stdc++.h>
using namespace std;

int main()
{
    int n,k;
    cin >> n >> k;
    string s;
    cin >> s;

    int i=0;
    int j=0;

    int ans = 0;

    while(j<n)
    {
        while(j<n && s[j]=='1')
        {
            j++;
        }
        ans = max(ans,j-i);
        while(j<n && s[j]=='0')
        {
            j++;
        }
        k--;
        if(k<0)
        {
            while(i<n && s[i]=='1')
            {
                i++;
            }
            while(i<n && s[i]=='0')
            {
                i++;
            }
        }
        ans = max(ans,j-i);
    }
    cout << ans << endl;
}

def main():
    ans = 0
    n, k = map(int, input().split())
    s = input()
    arr = []
    if s[0] == '0':
        arr.append(0)
    cnt = 1
    i = 1
    while i != n:
        if s[i] != s[i - 1]:
            arr.append(cnt)
            cnt = 1
        else:
            cnt += 1
        i += 1
    arr.append(cnt)
    if s[-1] == '0':
        arr.append(0)
    ans = 0
    curr = arr[0]
    j = 1
    if len(arr) - 1 <= 2 * k:
        print(n)
        return 0
    for i in range(1, len(arr), 2):
        curr += arr[i] + arr[i + 1]
        if i - j == 2 * k:
            curr -= arr[j] + arr[j - 1]
            j += 2
        ans = max(ans, curr)
    print(ans)
    return 0
main()

Verdicts: Runtime Error

Accepted
Problem: 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.

Submission attempts (in order):
n, k = map(int, input().split())
s = list(map(int, list(input())))

i = 0
a = []
while i < n:
    l = 1
    while i+1 < n and s[i]==s[i+1]:
        l+=1
        i+=1
    a.append(l)
    i+=1

n = len(a)
if s[0] == 0:
    st = 0
else:
    st = 1

acum = [0]*(n+1)
for i in range(n):
    acum[i+1] = acum[i]+a[i]

ans = 0
if s[0] == 1:
    st = 0
else:
    st = 1
for i in range(st, n+1):
    end = min(i+2*k+1, n)
    ans = max(ans, acum[end] - acum[i])
print(ans)

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

i = 0
a = []
while i < n:
    l = 1
    while i+1 < n and s[i]==s[i+1]:
        l+=1
        i+=1
    a.append(l)
    i+=1

if s[0] == 0:
    a = [0] + a
if s[-1] == 0:
    a = a + [0]
n = len(a)

acum = [0]*(n+1)
for i in range(n):
    acum[i+1] = acum[i]+a[i]

ans = 0
for i in range(0, n, 2):
    end = min(i+2*k+1, n)
    ans = max(ans, acum[end] - acum[i])
print(ans)

Verdicts: Wrong Answer

Accepted
Problem: 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.

Submission attempts (in order):
n,k = map(int,input().split())
s = tuple(input())

now = 1 
cnt = 0 
nums =[]

for i in range(n):
    if s[i] == '0'+str(now):
        cnt += 1
    else:
        nums.append(cnt)
        now ^= 1    
        cnt += 1

if cnt != 0:
    nums.append(cnt)

if len(nums) % 2 == 0:
    nums.append(0)

add = 2 * k + 1
ans = 0
left = 0
right = 0
r_l_sum = 0
for i in range(len(nums))[::2]:
    next_left = i
    next_right = min(i+add,len(nums))
    
    while next_left > left:
        r_l_sum -= nums[left]
        left += 1
    while next_right > right:
        r_l_sum += nums[right]
        right += 1
    ans = max(r_l_sum,ans)

print(ans)

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

now = 1 
cnt = 0 
nums =[]

for i in range(n):
    if s[i] == '0'+str(now):
        cnt += 1
    else:
        nums.append(cnt)
        now ^= 1    
        cnt += 1

if cnt != 0:
    nums.append(cnt)

if len(nums) % 2 == 0:
    nums.append(0)

add = 2 * k + 1
ans = 0
left = 0
right = 0
r_l_sum = 0
for i in range(len(nums))[::2]:
    next_left = i
    next_right = min(i+add,len(nums))
    
    while next_left > left:
        tmp -= nums[left]
        left += 1
    while next_right > right:
        tmp += nums[right]
        right += 1
    ans = max(tmp,ans)

print(ans)

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

now = 1 
cnt = 0 
nums =[]

for i in range(n):
    if s[i] == str(now):
        cnt += 1
    else:
        nums.append(cnt)
        now ^= 1    
        cnt = 1

if cnt != 0:
    nums.append(cnt)

if len(nums) % 2 == 0:
    nums.append(0)

add = 2 * k + 1
ans = 0
left = 0
right = 0
tmp = 0

for i in range(len(nums))[::2]:
    next_left = i
    next_right = min(i+add, len(nums))
    
    while next_left > left:
        tmp -= nums[left]
        left += 1
    while next_right > right:
        tmp += nums[right]
        right += 1
    ans = max(tmp,ans)

print(ans)


Verdicts: Wrong Answer

Runtime Error

Accepted
Problem: 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.

Submission attempts (in order):
n,k = map(int,input().split())
s = list(input())
s_0_1 = [1]*(k+int(s[0]))
s_1_0 = []
for i in range(n-1):
    if s[i] == "0" and s[i+1] == "1":
        s_0_1.append(i+2)
    elif s[i] == "1" and s[i+1] == "0":
        s_1_0.append(i+1)
s_1_0.extend([n]*(k+int(s[-1])))
print(s_0_1)
print(s_1_0)

ans = 0
for i in range(len(s_0_1)):
    ans = max(ans, s_1_0[i]-s_0_1[i]+1)
print(ans)




n,k = map(int,input().split())
s = list(input())
s_0_1 = [1]*(k+int(s[0]))
s_1_0 = []
for i in range(n-1):
    if s[i] == "0" and s[i+1] == "1":
        s_0_1.append(i+2)
    elif s[i] == "1" and s[i+1] == "0":
        s_1_0.append(i+1)
s_1_0.extend([n]*(k+int(s[-1])))
ans = 0
for i in range(len(s_0_1)):
    ans = max(ans, s_1_0[i]-s_0_1[i]+1)
print(ans)




Verdicts: Wrong Answer

Accepted
Problem: 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.

Submission attempts (in order):
# coding: utf-8
import sys
import itertools

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))


streak = [0, 0]
N, K = lr()
S = sr() + '2'
cur = 1
for i in range(N):
    if S[i] != S[i+1]:
        streak.append(cur)
        cur = 1
    else:
        cur += 1

streak_cum = list(itertools.accumulate(streak))
streak_cum.extend([streak_cum[-1], streak_cum[-1]])
if S[0] == '0':
    start = 0
else:
    start = 1
answer = 0

for i in range(start, len(streak_cum), 2):
    result = streak_cum[i+2*K+1] - streak_cum[i]
    if result > answer:
        answer = result

print(answer)
# 52


# coding: utf-8
import sys
import itertools

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))


streak = [0, 0]
N, K = lr()
S = sr() + '2'
cur = 1
for i in range(N):
    if S[i] != S[i+1]:
        streak.append(cur)
        cur = 1
    else:
        cur += 1

streak_cum = list(itertools.accumulate(streak))
streak_cum.extend([streak_cum[-1], streak_cum[-1]])
if S[0] == '0':
    start = 0
else:
    start = 1
answer = 0

limit = len(streak_cum) - 1
for i in range(start, len(streak_cum), 2):
    result = streak_cum[min(limit, i+2*K+1)] - streak_cum[i]
    if result > answer:
        answer = result

print(answer)
# 52

Verdicts: Runtime Error

Accepted
Problem: 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.

Submission attempts (in order):
N, K = map(int, input().split())
S = input()

zo_count = [1]
for n in range(1, N):
	if S[n] == S[n - 1]:
		zo_count[-1] += 1
	else:
		zo_count += [1]

zo_sum = [0]
for zo in zo_count:
	zo_sum += [zo_sum[-1] + zo]

if N <= K + 2:
	print(zo_sum[-1])
	exit()

print(zo_sum)

zo_max = 0
for n in range(K * 2 + 1, len(zo_sum)):
	zo_max = max(zo_max, zo_sum[n] - zo_sum[n - (K * 2 + 1)])

print(zo_max)



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

    counts = [0]
    last = "1"
    for s in S:
        if s == last:
            counts[-1] += 1
        else:
            counts += [1]
            last = s
    if S[-1] == "0":
        counts += [0]
    l, r = 0, K * 2 + 1
    max_c = sum(counts[l:r])
    current_c = max_c
    while r < len(counts):
        current_c -= (counts[l] + counts[l + 1])
        l += 2
        current_c += (counts[r] + counts[r + 1])
        r += 2
        max_c = max(max_c, current_c)
    print(max_c)

main()


Verdicts: Wrong Answer

Accepted
Problem: 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.

Submission attempts (in order):
n,k=map(int,input().split())
s=input()
if s[0]=="0":
  a=[0]
else:a=[]
i=0
c=0
while True:
  c+=1
  if s[i]!=s[i+1]:
    a.append(c)
    c=0
  i+=1
  if i==len(s)-1:
    a.append(len(s)-sum(a))
    break
l=len(a)
if 2*k+1>=l:print(l)
else:
  c=sum(a[:2*k+1])
  ans=sum(a[:2*k+1])
  for i in range(l-2*k-1):
    c-=a[i]-a[i+2*k+1]
    if ans<c:
      ans=c
  print(ans)

n,k=map(int,input().split())
s=input()
s+="6"
c=0
l=[]
zcnt=0
for i in range(n):
  if s[i]!=s[i+1]:
    if s[i]=="0":zcnt+=1
    l.append((s[i],i+1-c))
    c=i+1
L=len(l)
sl=[0]*(L+1)
for i in range(L):
    sl[i+1]=sl[i]+l[i][1]
if k>=zcnt:print(n);exit()
ans=0
if s[0]=="0":
    for i in range(zcnt-k+1):
        ans=max(ans,sl[min(L,2*k+i*2)]-sl[max(0,i*2-1)])
    print(ans)
else:
    for i in range(zcnt-k+1):
        ans=max(ans,sl[min(L,2*k+i*2+1)]-sl[i*2])
    print(ans)

Verdicts: Runtime Error

Accepted
Problem: 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.

Submission attempts (in order):
n,k=map(int,input().split())
S=input()

T='0'+S+'0'

ans,cnt=0,0
for i,(a,b) in enumerate(zip(S,S[1:])):
  if a=='0' and b=='1':
    cnt+=1
  elif cnt==k and a=='1' and b=='0':
    ans=max(ans,i+1)
    break
else:
  print(n)
  exit()
  
S,cnt=[::-1],0
for i,(a,b) in enumerate(zip(S,S[1:])):
  if a=='0' and b=='1':
    cnt+=1
  elif cnt==k and a=='1' and b=='0':
    ans=max(ans,i+1)
    break
else:
  print(n)
  exit()
  
x,y=[],[]
for i,(a,b) in enumerate(zip(T,T[1:])):
  if a==b: continue
  if a=='0':
    x.append(i)
  else:
    y.append(i)
for a,b in zip(x,y[k:]):
  ans=max(ans,b-a)
print(ans)

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

T='0'+S+'0'
ans,cnt=0,0
for i,(a,b) in enumerate(zip(S,S[1:])):
  if a=='0' and b=='1':
    cnt+=1
  elif cnt==k and a=='1' and b=='0':
    ans=max(ans,i+1)
    break
else:
  print(n)
  exit()
  
S,cnt=S[::-1],0
for i,(a,b) in enumerate(zip(S,S[1:])):
  if a=='0' and b=='1':
    cnt+=1
  elif cnt==k and a=='1' and b=='0':
    ans=max(ans,i+1)
    break
else:
  print(n)
  exit()
  
x,y=[],[]
for i,(a,b) in enumerate(zip(T,T[1:])):
  if a==b: continue
  if a=='0':
    x.append(i)
  else:
    y.append(i)
for a,b in zip(x,y[k:]):
  ans=max(ans,b-a)
print(ans)

Verdicts: Runtime Error

Accepted
Problem: 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.

Submission attempts (in order):
n, k = list(map(int, input().split()))
s = input()

def findmax(s,k):
    if len(s) == 1:
        return 1
    begin = s[0]
    l = [0]
    cur = 0
    for i in range(len(s)):
        if s[i]==begin:
            l[cur]+=1
        else:
            if begin == '0' : begin ='1'
            else: begin = '0'
            cur+=1
            l.append(1)
    r = 0
    if s[0] == '0':
        l = [0] + l
        r = 2*k
    else:
        r = 2*k+1
    sumup = [0]
    for i in range(len(l)):
        sumup.append(sumup[i]+l[i])
    return (max([sumup[i+r]-sumup[i] for i in range(0,len(sumup)-r,2)]))
print (findmax(s,k))

n, k = list(map(int, input().split()))
s = input()
begin = s[0]
l = [begin,0]
cur = 1
for i in range(len(s)):
    if s[i] == begin:
        l[cur]+=1
    else:
        if begin == '0':
            begin = '1'
        else: begin = '0'
        l.append(begin)
        l.append(1)
        cur+=2
max = 0
for i in range(1,len(l),2):
    sumall = 0
    if l[i-1] == '0':
        if (i+k*2*2 <= len(l)+1):
            for j in range (k*2):
                sumall+=l[i+j*2]
            if sumall > max: max = sumall
    else:
        if (i+(k*2+1)*2 <= len(l)+1):
            for j in range (k*2+1):
                sumall+=l[i+j*2]
            if sumall > max: max = sumall
    print (i,max)


from itertools import accumulate

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


def findmax():
    cur=0
    l=[]
    prev = '1'
    for c in s:
        if prev ==c:
            cur+=1
        else:
            l.append(cur)
            cur = 1
        prev=c
    l.append(cur)
    if prev=='0':
        l.append(0)
    w = 2*k+1
    if len(l)<=w:
        return n
    sumup = [0]+list(accumulate(l))
    res = 0
    for i in range(0, len(l)-w+1,2):
        res = max(res,sumup[i+w]-sumup[i])
    return res
print(findmax())


Verdicts: Runtime Error

Wrong Answer

Accepted
Problem: 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.

Submission attempts (in order):
s = list(input())
is_count_zero = True
sl = [0]
sl_ptr = 0
for c in s:
    if is_count_zero and c == '1':
        sl.append(1)
        sl_ptr += 1
        is_count_zero = False
    elif not is_count_zero and c == '0':
        sl.append(1)
        sl_ptr += 1
        is_count_zero = True
    else:
        sl[sl_ptr] += 1

# print(sl)
# print("len(sl):{} len(sl)-1)//2:{}".format(len(sl), (len(sl)-1)//2))

if k > (len(sl)-1)//2:
    print(sum(sl))
elif k == (len(sl)-1)//2:
    if sl[0] == 0:
        print(sum(sl))
    else:
        print(max(sum(sl[1:]), sum(sl[:-1])))
else:
    ans = 0
    for i in range((len(sl)+1)//2-(k-1)):
        if i == 0:
            ans = max(ans, sum(sl[:k*2-1]))
            # print("sl[:k*2-1]:{} sum:{}".format(sl[:k*2-1], sum(sl[:k*2-1])))
        else:
            ans = max(ans, sum(sl[i*2-1:(i+k)*2]))
            # print("sl[i*2-1:(i+k)*2-1]: {}".format(sl[i*2-1:(i+k)*2]))
    print(ans)


# import sys
# input = sys.stdin.readline

_, k = map(int, input().split())
s = list(map(int, list(input())))
# print(s)
is_count_zero = True
s0 = [0]
s1 = []
s0_ptr = 0
s1_ptr = -1
for c in s:
    if is_count_zero and c == 0:
        s0[s0_ptr] += 1
    elif not is_count_zero and c == 0:
        s0.append(1)
        s0_ptr += 1
        is_count_zero = True
    elif is_count_zero and c == 1:
        s1.append(1)
        s1_ptr += 1
        is_count_zero = False
    else: # not is_count_zero and c == 1
        s1[s1_ptr] += 1

# print(s0,s1)
is_end_one = True if len(s0) == len(s1) else False

if k >= len(s0):
    print(sum(s0)+sum(s1))

#     if s1 == []:
#         print(0)
#     else:
#         print(max(s1))
else:
    ans = 0
    sum_range = 0
    n = len(s0)-k+1
    for i in range(n):
        if i == 0:
            sum_range = sum(s0[:k]) + sum(s1[:k])
            ans = max(ans, sum_range)
        elif i == n-1:
            sum_range = sum(s0[i:]) + sum(s1[i-1:])
            ans = max(ans, sum_range)
        elif i == 1:
            sum_range = sum_range - s0[0] + s0[k] + s1[k]
            ans = max(ans, sum_range)
        elif i != 0:
            sum_range = sum_range - s0[i-1] + s0[i+k-1] - s1[i-2] + s1[i+k-1]
            ans = max(ans, sum_range)
    print(ans)


Verdicts: Runtime Error

Accepted