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):
def main():
	N, K = [int(x) for x in input().split(" ")]
	S = list(input())
	cnt = [0] # idx = 0, 2, 4 ... -> handstand
	a = 0
	for i in range(len(S)):
		if S[i] == "0":
			if a % 2 == 0:
				a += 1
				cnt.append(1)
			else:
				cnt[-1] += 1
		elif S[i] == "1":
			if a % 2 == 0:
				cnt[-1] += 1
			else:
				a += 1
				cnt.append(1)
	if S[-1] == "0":
		cnt.append(0)

	if K >= (len(cnt) - 1) / 2:
		print(N)
		return 0

	T = [sum(cnt[:(2 * K + 1)])]
	for i in range(0, len(cnt) - 2 * K - 2, 2):
		T.append(T[-1] - cnt[i] - cnt[i + 1] + cnt[i + 2 * K + 1] + cnt[i + 2 * K + 2])
	print(T)

main()

def main():
	N, K = [int(x) for x in input().split(" ")]
	S = list(input())
	cnt = [0] # idx = 0, 2, 4 ... -> handstand
	a = 0
	for i in range(len(S)):
		if S[i] == "0":
			if a % 2 == 0:
				a += 1
				cnt.append(1)
			else:
				cnt[-1] += 1
		elif S[i] == "1":
			if a % 2 == 0:
				cnt[-1] += 1
			else:
				a += 1
				cnt.append(1)
	if S[-1] == "0":
		cnt.append(0)

	if K >= (len(cnt) - 1) / 2:
		print(N)
		return 0

	T = [sum(cnt[:(2 * K + 1)])]
	for i in range(0, len(cnt) - 2 * K - 2, 2):
		T.append(T[-1] - cnt[i] - cnt[i + 1] + cnt[i + 2 * K + 1] + cnt[i + 2 * K + 2])
	print(max(T))

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() + "#"
count = 1
group = 0
L = []
for i in range(n):
  if s[i] == s[i+1]:
    count += 1
  else:
    L.append(count)
    group += 1
    count = 1
ans = 0
if k >= group//2:
  ans = n
else:
  temp = sum(L[:2*k+1])
  ans = temp
  for i in range(2*k+1, group):
    temp -= L[i-(2*k+1)]
    temp += L[i]
    ans = max(temp, ans)
print(ans)

n, k = map(int, input().split())
s = "#" + input()
B = []
L = []
for i in range(n):
  if s[i] != s[i+1]:
    B.append(int(s[i+1]))
    L.append(i+1)
ans = 0
for j, b in enumerate(B):
  if b == 0:
    if j+2*k >= len(L):
      ans = max(ans, n+1 - L[j])
    else:
      ans = max(ans, L[j+2*k] - L[j])
  else:
    if j+2*k+1 >= len(L):
      ans = max(ans, n+1 - L[j])
    else:
      ans = max(ans, L[j+2*k+1] - L[j])
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;
  string S;
  cin >> N >> K >> S;
  S.push_back('2');
  vector<int> A;
  if(S[0] == '0') A.push_back(0);
  int cnt = 1;
  rep(i, 1, S.size()) {
    if(S[i] == S[i-1]) cnt++;
    else {
      A.push_back(cnt);
      cnt = 1;
    }
  }
  if(S.back() == '0') A.push_back(0);
  int ans = 0;
  int r = 0;
  int sum = 0;
  for(int l = 0; l < A.size(); l++){
    while(r < A.size() && r - l < 2 * K + 1){
      sum += A[r];
      r++;
    }
    if(l % 2 == 0) ans = max(ans, sum);
    if(l == r) r++;
    else sum -= A[l];
  }
  cout << ans << endl;
  return 0;
}


N, K = map(int, input().split())
S = input()
A = []
if S[0] == '0': A.append(0)
left = 0
while left < N:
  right = left
  while right < N and S[left] == S[right]:
    right += 1
  A.append(right - left)
  left = right
ans = 0
tot = 0
right = 0
for left in range(len(A)):
  while right < len(A) and right - left < 2 * K + 1:
    tot += A[right]
    right += 1
  if left % 2 == 0:
    ans = max(ans, tot)
  if left == right:
    right += 1
  else:
    tot -= A[left]
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()
X = [-1]*N
k = 0
i = 1
c = 0
for s in S:
	if s == str(i):
		c += 1
	else:
		X[k] =c
		i = (i+1)%2
		c = 1
		k+= 1
X[k] = c
for n in range(N):
	if X[N-n-1] != -1:
		b = N-n
		break
X = X[:b]
M = len(X)
ko = M //2
if M == 2*ko:
	X.append(0)
#print(X)
if ko <= K:
	print(N) 
else:
	now = sum(X[0:2*K+1])
	print(now)
	maxm = now
	for t in range(ko-K):
		a = X[2*t] +X[2*t+1]
		b = X[2*(K+t)+1]+X[2*(K+t)+2]
		now += b - a
		#print(now, a, b)
		maxm = max(maxm, now)
	print(maxm)

N, K = map(int,input().split())
S = input()
X = [-1]*N
k = 0
i = 1
c = 0
for s in S:
	if s == str(i):
		c += 1
	else:
		X[k] =c
		i = (i+1)%2
		c = 1
		k+= 1
X[k] = c
for n in range(N):
	if X[N-n-1] != -1:
		b = N-n
		break
X.append(-1)
X = X[:b]
M = len(X)
ko = M //2
if ko <= K:
	print(N) 
else:
	now = sum(X[0:2*K+2])
	print(now)
	maxm = now
	for t in range(ko-K-1):
		a = X[2*t] +X[2*t+1]
		b = X[2*(K+t)+1]+X[2*(K+t)+2]
		now += b - a
		maxm = max(maxm, now)
	print(maxm)

N, K = map(int,input().split())
S = input()
X = [-1]*N
k = 0
i = 1
c = 0
for s in S:
	if s == str(i):
		c += 1
	else:
		X[k] =c
		i = (i+1)%2
		c = 1
		k+= 1
X[k] = c
for n in range(N):
	if X[N-n-1] != -1:
		b = N-n
		break
X = X[:b]
M = len(X)
ko = M //2
if M == 2*ko:
	X.append(0)
#print(X)
if ko <= K:
	print(N) 
else:
	now = sum(X[0:2*K+1])
	#print(now)
	maxm = now
	for t in range(ko-K):
		a = X[2*t] +X[2*t+1]
		b = X[2*(K+t)+1]+X[2*(K+t)+2]
		now += b - a
		#print(now, a, b)
		maxm = max(maxm, now)
	print(maxm)

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

## make 'next_lst'
## S='1100010000' --> next_lst=[2,3,1,4,1]
next_lst = []
pre_state = S[0]
cnt = 1
for i in range(1,len(S)):
  if S[i] == pre_state:
    cnt += 1
  else:
    next_lst.append(cnt)
    pre_state = S[i]
    cnt = 1
else:
  next_lst.append(cnt)
if S[0] == '0':
  next_lst = [0] + next_lst
if S[-1] == '0':
  next_lst = next_lst + [0]
#print(next_lst)

ans = 0
if len(next_lst) <= 2*K+1:
  ans = sum(next_lst)
  print(ans)
  exit()
for i in range((len(next_lst)-2*K+1)//2):
  var = sum(next_lst[2*i:2*i+(2*K+1)])
  ans = max(ans, var)
  #print(i, var, next_lst[2*i:2*i+(2*K+1)])
  print(ans)
  



import numpy as np

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

## pesmistic input (8916 ms)
#N = 100000
#K = 10000
#S = '10'*50000

## make 'next_lst'
## S='1100010000' --> next_lst=[2,3,1,4,1]
next_lst = []
pre_state = S[0]
cnt = 1
for i in range(1,len(S)):
  if S[i] == pre_state:
    cnt += 1
  else:
    next_lst.append(cnt)
    pre_state = S[i]
    cnt = 1
else:
  next_lst.append(cnt)
if S[0] == '0':
  next_lst = [0] + next_lst
if S[-1] == '0':
  next_lst = next_lst + [0]


if len(next_lst) <= 2*K+1:
  ans = sum(next_lst)
  print(ans)
  exit()


ans = 0
lst = np.cumsum(np.array([0]+next_lst))
for i in range((len(next_lst)-2*K+1)//2):
  var = lst[2*i+2*K+1] - lst[2*i]
  ans = max(ans, var)

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())
a = input()

one = 0
zero = 0
z = 0
flg = False
num_list = []

if a[0] == '0':
    flg = True
for i in a:
    if one >= 0 and zero == 0 and i == '1':
        one += 1
    elif one == 0 and zero >= 0 and i == '0':
        zero += 1
        z += 1
    elif zero > 0 and i == '1':
        num_list.append(zero)
        one += 1
        zero = 0
    elif one > 0 and i == '0':
        num_list.append(one)
        zero += 1
        one = 0
        z += 1
num_list.append(max(one, zero))

if a[0] == '0':
    num_list.insert(0,0)
num_list.insert(0,0)
num_list.append(0)

if z <= k:
    print(n)
    exit()

n_list = []

sum = 0
for i in range(len(num_list)):
    sum += num_list[i]
    n_list.append(sum)

width = 2*k
if width >= len(num_list):
    print(n)
    exit()

max_num = 0

for i in range(1, len(n_list)-width, 2):
    print(i)
    num = n_list[i+width] - n_list[i-1]
    if num > max_num:
        max_num = num

print(max_num)


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

one = 0
zero = 0
z = 0
flg = False
num_list = []

if a[0] == '0':
    flg = True
for i in a:
    if one >= 0 and zero == 0 and i == '1':
        one += 1
    elif one == 0 and zero >= 0 and i == '0':
        zero += 1
        z += 1
    elif zero > 0 and i == '1':
        num_list.append(zero)
        one += 1
        zero = 0
    elif one > 0 and i == '0':
        num_list.append(one)
        zero += 1
        one = 0
        z += 1
num_list.append(max(one, zero))

if a[0] == '0':
    num_list.insert(0,0)
num_list.insert(0,0)
num_list.append(0)

if z <= k:
    print(n)
    exit()

n_list = []

sum = 0
for i in range(len(num_list)):
    sum += num_list[i]
    n_list.append(sum)

width = 2*k
if width >= len(num_list):
    print(n)
    exit()

max_num = 0

for i in range(1, len(n_list)-width, 2):
    num = n_list[i+width] - n_list[i-1]
    if num > max_num:
        max_num = num

print(max_num)


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())
sss = input()

now = 1
cnt = 0
x = []

for i in range(n):
    if sss[i] == str(now):
        cnt += 1
    else:
        x.append(cnt)
        now = 1 - now
        cnt = 1
if cnt != 0:
    x.append(cnt)
if sss[-1] == 0:
    x.append(0)

Add = 2 * k + 1
ans = 0

y = [0]
for i in range(len(x)):
    y.append(y[i] + x[i])
print(y)

for i in range(len(x))[::2]:
    left = i
    right = min(i + Add, len(x))
    tmp = y[right] - y[left]
    
    ans = max(tmp, ans)

print(ans)


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

now = 1
cnt = 0
x = []

for i in range(n):
    if sss[i] == str(now):
        cnt += 1
    else:
        x.append(cnt)
        now = 1 - now
        cnt = 1
if cnt != 0:
    x.append(cnt)
if sss[-1] == 0:
    x.append(0)

Add = 2 * k + 1
ans = 0

y = [0]
for i in range(len(x)):
    y.append(y[i] + x[i])

for i in range(len(x))[::2]:
    left = i
    right = min(i + Add, len(x))
    tmp = y[right] - y[left]
    
    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):
#include<bits/stdc++.h>
using namespace std;

int main()
{
    int n, k;
    cin >> n >> k;
    string s;
    cin >> s;
    
    vector<int> arr;
    if (s[0] == '0') arr.push_back(0);
    int cnt = 1;
    for (int i = 1; i != n; ++i)
    {
        if (s[i] != s[i - 1])
        {
            arr.push_back(cnt);
            cnt = 1;
        }
        else ++cnt;
    }
    arr.push_back(cnt);
    if (s[n - 1] == '0') arr.push_back(0);
    
    int ans = 0;
    int curr = arr[0];
    int j = 1;
    for (int i = 1; i != arr.size(); i += 2)
    {
        curr += arr[i] + arr[i + 1];
        if (i - j == k << 1)
        {
            curr -= arr[j] + arr[j - 1];
            j += 2;
        }
        ans = max(ans, curr);
    }
    cout << ans << endl;
    return 0;
}

def main():
    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=input()
First=S[0]
Now=First
cnt=0
A=[0]
for s in S:
    if Now=='1' and s=='0':
        A+=[cnt]
        Now='0'
    elif Now=='0' and s=='1':
        A+=[cnt]
        Now='1'
    cnt+=1
A+=[N]
dp=2*K+1
ans=[]

if First=="0":
    p=1
    ans.append(A[min(dp-1,len(A)-1)])
else:
    p=0
    ans.append(A[min(dp,len(A)-1)])

while p+dp<len(A):
	ans.append(A[p+dp]-A[p])
	p+=2

#print(A)
print(ans)

N,K=map(int,input().split())
S=input()
First=S[0]
Now=First
cnt=0
A=[0]
for s in S:
    if Now=='1' and s=='0':
        A+=[cnt]
        Now='0'
    elif Now=='0' and s=='1':
        A+=[cnt]
        Now='1'
    cnt+=1
A+=[N]
dp=2*K+1
ans=[]

if First=="0":
    p=1
    ans.append(A[min(dp-1,len(A)-1)])
else:
    p=0
    ans.append(A[min(dp,len(A)-1)])

while p+dp<len(A):
	ans.append(A[p+dp]-A[p])
	p+=2
ans.append(N-A[p])
#print(A)
print(ans)

N,K=map(int,input().split())
S=input()
First=S[0]
Now=First
cnt=0
A=[0]
for s in S:
    if Now=='1' and s=='0':
        A+=[cnt]
        Now='0'
    elif Now=='0' and s=='1':
        A+=[cnt]
        Now='1'
    cnt+=1
A+=[N]
dp=2*K+1
ans=[]

if First=="0":
    p=1
    ans.append(A[min(dp-1,len(A)-1)])
else:
    p=0
    ans.append(A[min(dp,len(A)-1)])

while p+dp<len(A):
	ans.append(A[p+dp]-A[p])
	p+=2

print(A)
print(max(ans))

N,K=map(int,input().split())
S=input()
First=S[0]
Last=S[N-1]

Now=First
cnt=0
A=[0,0]
for s in S:
    if Now=='1' and s=='0':
        A+=[cnt]
        Now='0'
    elif Now=='0' and s=='1':
        A+=[cnt]
        Now='1'
    cnt+=1
A+=[cnt,cnt]


print(S)
print(A)
ans=0
dp=2*K+1
if First=="0":
    p=0
else:
    p=1
while p+dp<len(A):
   if A[p+dp]-A[p]>ans:ans=A[p+dp]-A[p]
   p+=2
print(ans)

N,K=map(int,input().split())
S=input()
First=S[0]
Now=First
cnt=0
A=[0]
for s in S:
    if Now=='1' and s=='0':
        A+=[cnt]
        Now='0'
    elif Now=='0' and s=='1':
        A+=[cnt]
        Now='1'
    cnt+=1
A+=[N]
dp=2*K+1
ans=[]

if First=="0":
    p=1
    ans.append(A[min(dp-1,len(A)-1)])
else:
    p=0
    ans.append(A[min(dp,len(A)-1)])

while p+dp<len(A):
	ans.append(A[p+dp]-A[p])
	p+=2
ans.append(N-A[p])
#print(A)
print(max(ans))

Verdicts: Wrong Answer

Wrong Answer

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):
import itertools

def getint(): return int(input())
def getints(): return list(map(int, input().split()))

n,k=getints()
s=input()
n=len(s)

rl = []
now, cnt = '0', 0

for i in range(n):
    if now != s[i]:
        rl.append(cnt)
        now = s[i]
        cnt = 1
    else:
        cnt += 1
if cnt > 0:
    rl.append(cnt)
acc=[0]*len(rl)
acc[0]=rl[0]
for i in range(1, len(rl)):
    acc[i] = acc[i-1]+rl[i]

# print(rl)
# print(acc)

res = 0
for i in range(len(acc)):
    if i % 2 == 0:
        i0 = i - 2
        j  = min(i + 2 * k - 1, len(acc) - 1)
    else:
        i0 = i - 1
        j  = min(i + 2 * k, len(acc) - 1)
    print(i,i0,j)
    tmp1 = acc[i0] if i0 >= 0      else 0
    tmp2 = acc[j]  if j < len(acc) else 0
    res = max(res, tmp2-tmp1)


print(res)


import itertools

def getint(): return int(input())
def getints(): return list(map(int, input().split()))

n,k=getints()
s=input()
n=len(s)

rl = []
now, cnt = '0', 0

for i in range(n):
    if now != s[i]:
        rl.append(cnt)
        now = s[i]
        cnt = 1
    else:
        cnt += 1
if cnt > 0:
    rl.append(cnt)
acc=[0]*len(rl)
acc[0]=rl[0]
for i in range(1, len(rl)):
    acc[i] = acc[i-1]+rl[i]

# print(rl)
# print(acc)

res = 0
for i in range(len(acc)):
    if i % 2 == 0:
        i0 = i - 2
        j  = min(i + 2 * k - 1, len(acc) - 1)
    else:
        i0 = i - 1
        j  = min(i + 2 * k, len(acc) - 1)
    #print(i,i0,j)
    tmp1 = acc[i0] if i0 >= 0      else 0
    tmp2 = acc[j]  if j < len(acc) else 0
    res = max(res, tmp2-tmp1)


print(res)


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()
left=[]
right=[]
for i in range(len(S)):
	
	
	
	
	
	if len(left)>len(right):
		if S[i]=='0':
			right.append(i-1)
	elif len(left)==len(right):
		if S[i]=='1':
			left.append(i)



if len(left)>len(right):
	right.append(N-1)

left = [0]*K + left + [N-1]*K
right = [0]*K + right + [N-1]*K

print(left)
print(right)

length = [ right[i+K]-left[i]+1 for i in range(len(left)-K) ]

print(max(length))

N,K=map(int,input().split(' '))
S=input()
left=[]
right=[]
for i in range(len(S)):
	
	
	
	
	
	if len(left)>len(right):
		if S[i]=='0':
			right.append(i-1)
	elif len(left)==len(right):
		if S[i]=='1':
			left.append(i)



if len(left)>len(right):
	right.append(N-1)

section=[ [left[i],right[i]] for i in range(len(left)) ]

section=[[0, 0]] + section + [[N-1, N-1]]
#print(section)

length=[]

if K+1>=len(section):
	print(N)
else:
	for i in range(len(section)-K):
		length.append(section[i+K][1] - section[i][0] + 1)
	print(max(length))

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()+"8"
F = ["1","0"]
d = {"0":[],"1":[]}
f = {"0":"1","1":"0"}
k = []
kk = [0]
S = s[0]
t = 1
for i in s[1:]:
    if S == i:t+=1
    else:
        k.append(t)
        d[S].append(t)
        t = 1
        S = f[S]
kk[0]=k[0]
for i ,n in enumerate(k[1:]):
    kk.append(kk[i]+n)
print(d)

n,k=map(int,input().split())
s=input()
g=[]
t=1
for i in range(1,n):
    if s[i]==s[i-1]:
        t+=1
    else:
        g.append(t)
        t=1
g.append(t)
for i in range(1,len(g)):
    g[i]+=g[i-1]
g=[0]+g+[g[-1]]
a=0
for i in range(s[0]=='1',len(g),2):
    a=max(a,g[i]-g[max(0,i-2*k-1)])
print(a)


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 = 13, 1
S = "0000011100110"

# N, K = 2, 2
# S = "00"


# N, K = 1, 1
# S = "1"
#
#
# N, K = 5, 1
# S = "00010"
#
N, K = map(int,input().split())
S = input()

def calculate(n, k, s):
    s = list(s)

    result = [{'start': 0, 'end': 0}]
    for i in range(n):

        previous = s[i - 1]
        current = s[i]

        if i == 0:
            if s[i] == '1':
                result.append({'start': 0})
        else:
            if (current == '0') and (previous == '1'):
                res = result[-1]
                res.__setitem__('end', i)
                result[-1] = res

            if (current == '1') and (previous == '0'):
                result.append({'start': i})

            if i == n - 1 and s[i] == '1':
                res = result[-1]
                res.__setitem__('end', i + 1)
                result[-1] = res

    result.append({'start': n, 'end': n})
    maxValue = 0
    if len(result) > k:
        for i in range(len(result) - k):
            val = result[i + k]['end'] - result[i]['start']
            maxValue = max(val, maxValue)
    else:
        maxValue = max(maxValue, result[-1]['end'] - result[0]['start'])
    print(result)
    print(maxValue)


calculate(N, K, S)


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

def calculate(n, k, s):
    s = list(s)

    result = [{'start': 0, 'end': 0}]
    for i in range(n):

        previous = s[i - 1]
        current = s[i]

        if i == 0:
            if s[i] == '1':
                result.append({'start': 0})
        else:
            if (current == '0') and (previous == '1'):
                res = result[-1]
                res.__setitem__('end', i)
                result[-1] = res

            if (current == '1') and (previous == '0'):
                result.append({'start': i})

        if i == n - 1 and s[i] == '1':
            res = result[-1]
            res.__setitem__('end', i + 1)
            result[-1] = res

    result.append({'start': n, 'end': n})


    # print(result)
    maxValue = 0
    if len(result) > k:
        for i in range(len(result) - k):
            val = result[i + k]['end'] - result[i]['start']
            maxValue = max(val, maxValue)
    else:
        maxValue = max(maxValue, result[-1]['end'] - result[0]['start'])


    print(maxValue)


calculate(N, K, S)


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):
# coding: utf-8
# Your code here!
N,K=map(int,input().split())
S=list(map(int,list(input())))

l=[]
count=1
for i in range(N-1):
    if S[i]==S[i+1]:
        count+=1
    else:
        l.append(count)
        count=1
l.append(count)

ans=0
if S[0]==0:
    l=[0]+l

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

tgt=sum(l[:2*K+1])
ans=max(ans,tgt)


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

# coding: utf-8
# Your code here!
N,K=map(int,input().split())
S=list(map(int,list(input())))

l=[]
count=1
for i in range(N-1):
    if S[i]==S[i+1]:
        count+=1
    else:
        l.append(count)
        count=1
l.append(count)

ans=0
if S[0]==0:
    l=[0]+l

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

tgt=sum(l[:2*K+1])
ans=max(ans,tgt)


for i in range(0,len(l)-(2*K+1))[::2]:
    tgt-=l[i]
    tgt-=l[i+1]
    tgt+=l[2*K+1+i]
    tgt+=l[2*K+2+i]
    ans=max(ans,tgt)
    
#print(l)    
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=list(map(int,input()))
sp_list=[]
sum=0
sum_0=0
for i in range(n):
  if s[i]==1:
    sum+=1
    if sum_0>0:
      sp_list.append(sum_0)
    sum_0=0
    if i==(n-1):
      sp_list.append(sum)
  else:
    if i==0:
      sp_list.append(0)
    if i>0 and sum>0:
      sp_list.append(sum)
    sum_0+=1
    sum=0
    if i==(n-1):
      sp_list.append(sum_0)
      sp_list.append(0)
sum_list=[0]
sum=0
for i in range(len(sp_list)):
  sum+=sp_list[i]
  sum_list.append(sum)
length=min(k,len(sum_list))
if 2*k+1>=len(sum_list):
  print(sum_list[-1])
else:
  for i in range((len(sum_list)-2*k)//2):
    temp=0
    if temp<(sum_list[2*i+2*k+1]-sum_list[2*i]):
      temp=(sum_list[2*i+2*k+1]-sum_list[i*2])
  print(temp)

n,k=map(int,input().split())
s=list(map(int,input()))
sp_list=[]
sum=0
sum_0=0
for i in range(n):
  if s[i]==1:
    sum+=1
    if sum_0>0:
      sp_list.append(sum_0)
    sum_0=0
    if i==(n-1):
      sp_list.append(sum)
  else:
    if i==0:
      sp_list.append(0)
    if i>0 and sum>0:
      sp_list.append(sum)
    sum_0+=1
    sum=0
    if i==(n-1):
      sp_list.append(sum_0)
      sp_list.append(0)
sum_list=[0]
sum=0
for i in range(len(sp_list)):
  sum+=sp_list[i]
  sum_list.append(sum)
length=min(k,len(sum_list))
if 2*k+1>=len(sum_list):
  print(sum_list[-1])
else:
  for i in range((len(sum_list)-2*k)//2):
    temp=0
    if temp<(sum_list[2*i+2*k+1]-sum_list[2*i]):
      temp=(sum_list[2*i+2*k+1]-sum_list[i*2])
  print(temp)

n,k=map(int,input().split())
s=list(map(int,input()))
sp_list=[]
sum=0
sum_0=0
for i in range(n):
  if s[i]==1:
    sum+=1
    if sum_0>0:
      sp_list.append(sum_0)
    sum_0=0
    if i==(n-1):
      sp_list.append(sum)
  else:
    if i==0:
      sp_list.append(0)
    if i>0 and sum>0:
      sp_list.append(sum)
    sum_0+=1
    sum=0
    if i==(n-1):
      sp_list.append(sum_0)
      sp_list.append(0)
sum_list=[0]
sum=0
for i in range(len(sp_list)):
  sum+=sp_list[i]
  sum_list.append(sum)
length=min(k,len(sum_list))
if 2*k+1>=len(sum_list):
  print(sum_list[-1])
else:
  temp=0
  for i in range((len(sum_list)-2*k)//2):
    if temp<(sum_list[2*i+2*k+1]-sum_list[2*i]):
      temp=(sum_list[2*i+2*k+1]-sum_list[i*2])
  print(temp)

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()
ss = []

cnt = 0
for i in range(n):
    if i == 0 and s[i] == '0':
        ss.append(0)

    if i > 1 and s[i] != s[i-1]:
        ss.append(cnt)
        cnt = 0

    cnt += 1

ss.append(cnt)

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

#print(ss)
ans = 0
i = 0
while i+2*k+1 < n:
    #print(ans, i, sum(ss[i:i+2*k+1]))
    ans = max(ans, sum(ss[i:i+2*k+1]))

    i += 2 * k

print(1 if k == 1 else ans)


n, k = map(int, input().split())
s = input()
ss = []

cnt = 0

if s[0] == '0': ss.append(0)
i = 0
while i < n:
    j = i
    while j < n and s[i] == s[j]: j += 1
    ss.append(j-i)
    i = j
if s[-1] == '0': ss.append(0)

right = 2*k+1
left = 0
val = sum(ss[:right])
ans = val

#print(ss)
while right < len(ss):
    for i in [1, 2]:
        #print('for', ss[left], ss[right])
        val += ss[right]
        val -= ss[left]
        left += 1
        right += 1
    #print(left, right, val)
    ans = max(ans, val)
    
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):
"""
map(int,input().split())

"""
N,K = map(int,input().split())
S = input()
s = []
for i in range(0,len(S)):
    s.append(S[i])


Nums = []
now = 1
cnt = 0
for i in range(0,N):
    if(s[i] == 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
W = []

wsm = 0
for i in range(0,len(Nums)):
    wsm = Nums[i] + wsm
    W.append(wsm)

W.append(wsm)
W.append(wsm)

for i in range(0,len(Nums),2):
    tmp = 0
    left = i
    right = min(i + Add , len(Nums))

    tmp = W[right] - W[left-1]
    ans = max(tmp,ans)

print(ans)


"""
map(int,input().split())

"""
N,K = map(int,input().split())
S = input()
s = []
for i in range(0,len(S)):
    s.append(S[i])


Nums = []
now = 1
cnt = 0
for i in range(0,N):
    if(s[i] == 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
W = []

wsm = 0
for i in range(0,len(Nums)):
    wsm = Nums[i] + wsm
    W.append(wsm)

W.append(wsm)
W.append(wsm)

for i in range(0,len(Nums),2):
    tmp = 0
    left = i
    if left == 0:
        left = 1
    right = min(i + Add , len(Nums))
    if left-1<=0:
        tmp = W[right-1]
    else:
        tmp = W[right-1] - W[left-1]
    #print(tmp,left,right)
    ans = max(tmp,ans)
if  N == 1:
    print(1)
else:
    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 = list(input())

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

i = 0
while i < n:
    j = i
    while j < n and s[j] == s[i]:
        j += 1
    cnt.append(j- i)
    i = j
if s[-1] == '0': cnt.append(0)

cs = [0] * (len(cnt)+1)
for i in range(len(cnt)):
    cs[i+1] = cs[i] + cnt[i]

ans = 0
m = 2 * k + 1
for i in range(0, len(cnt), 2):
    ans = max(ans, cs[i+m] - cs[i])

print(ans)


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

cnt = []
if s[0] == '0': cnt.append(0)
i = 0
while i < n:
    j = i
    while j < n and s[j] == s[i]:
        j += 1
    cnt.append(j- i)
    i = j
if s[-1] == '0': cnt.append(0)

m = len(cnt)
cs = [0] * (m+1)
for i in range(m):
    cs[i+1] = cs[i] + cnt[i]

ans = 0
for l in range(0, m+1, 2):
    r = l + 2 * k + 1
    if r > m: r = m
    ans = max(ans, cs[r] - cs[l])

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 = list(map(int,input().split()))
tmp = 1
tbl = []
cont = 0
for s in S:
    if s == tmp:
        cont += 1
    else:
        tbl.append(cont)
        cont = 1
        tmp = abs(tmp-1)
tbl.append(cont)
if len(tbl) % 2 == 0:
    tbl.append(0)
max = 0
for i in range(0,len(tbl),2):
    if sum(tbl[i:i+K*2+1]) > max:
        max = sum(tbl[i:i+K*2+1])
print(max)

N,K = map(int,input().split())
S = list(map(int,input()))
tmp = 1
nums = []
cont = 0
for s in S:
    if int(s) == tmp:
        cont += 1
    else:
        nums.append(cont)
        cont = 1
        tmp = 1 - tmp
if cont != 0:
    nums.append(cont)
if len(nums) % 2 == 0:
    nums.append(0)
cml = [0]
for n in nums:
    cml.append(cml[-1]+n)
ans = []
for i in range(0,len(cml),2):
    ans.append(cml[min(len(cml)-1,i+K*2+1)]-cml[i])
print(max(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 = list(map(int,input().strip()))

data = [[],[]]
key = 1
cnt = 0
for i in range(n):
    if s[i] == key:
        cnt += 1
        if i == n-1:
            data[key].append(cnt)
    else:
        data[key].append(cnt)
        key ^= 1
        cnt = 1
    
if len(data[0]) == len(data[1]):
    data[1].append(0)

m = len(data[0])

if m <= k:
    print(n)
    exit()

tmp = data[1][0]
for i in range(k):
    tmp += data[0][i]
    tmp += data[1][i+1]

ans = tmp
for i in range(k-1,m):
    tmp -= data[0][i-k+1]
    tmp -= data[1][i-k+1]
    tmp += data[0][i]
    tmp += data[1][i+1]
    ans = max(ans,tmp)

print(ans)


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

data = [[],[]]
key = 1
cnt = 0
for i in range(n):
    if s[i] == key:
        cnt += 1
    
    else:
        data[key].append(cnt)
        key ^= 1
        cnt = 1
        
    if i == n-1:
        data[key].append(cnt)
    
if len(data[0]) == len(data[1]):
    data[1].append(0)

m = len(data[0])


if m <= k:
    print(n)
    exit()

tmp = data[1][0]
for i in range(k):
    tmp += data[0][i]
    tmp += data[1][i+1]

ans = tmp

for i in range(k,m):
    tmp -= data[0][i-k]
    tmp -= data[1][i-k]
    tmp += data[0][i]
    tmp += data[1][i+1]
    ans = max(ans,tmp)

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 = [0]
X = ['1']
pos = 0
for i in range(N):
  if S[i] != X[pos]:
    L.append(1)
    X.append(S[i])
    pos += 1
  else:
    L[pos] += 1
while pos < 2 * K + 4:
  L.append(0)
  X.append('1')
  pos += 1
L.append(0)
X.append('1')
n = len(L)
for i in range(1, n):
  L[i] += L[i-1]
M = L[2*K]
for i in range(1, n-2*K):
  if X[i] == '1':
    M = max(L[2*K+i] - L[i-1], M)

N, K = map(int, input().split())
S = input()
L = [0]
X = ['1']
pos = 0
for i in range(N):
  if S[i] != X[pos]:
    L.append(1)
    X.append(S[i])
    pos += 1
  else:
    L[pos] += 1
while pos < 2 * K + 4:
  L.append(0)
  X.append('1')
  pos += 1
L.append(0)
X.append('1')
n = len(L)
for i in range(1, n):
  L[i] += L[i-1]
M = L[2*K]
for i in range(1, n-2*K):
  if X[i] == '1':
    M = max(L[2*K+i] - L[i-1], M)
print(M)

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=list(input())
S=list(map(int,S))
s=[]
s.append(0)
for i in range(1,N):
  if S[i]==1 and S[i-1]==0:
    s.append(i-1)
  elif i==N-1 and S[i]==0:
    s.append(i)
s.append(N)
if len(s)<=K+2:
  print(len(S))
  exit(0)
ans=0
for i in range(1,max(len(s)-K,0)):
  ans=max(s[i+K]-(s[i-1])-1,ans)

print(ans)

N,K=map(int,input().split())
S=list(input())
S=list(map(int,S))
s=[]
tmp=0

for i in range(N):
  if S[i]==1:
    if tmp!=0:
      s.append((tmp,i))
    tmp=0
  else:
    tmp+=1
    if i ==N-1:
      s.append((tmp,i))
if len(s)<=K:
  print(N)
  exit(0)
ans=0
for i in range(len(s)-K+1):
  tmp=0
  if i == len(s)-K:
    tmp= N
  else:
    tmp=s[i+K][1]-s[i+K][0]+1
  
  if i !=0:
    print(tmp,s[i-1][1])
    tmp-=s[i-1][1]
  ans=max(ans,tmp)
print(s)
print(ans)


N,K=map(int,input().split())
S=list(input())
S=list(map(int,S))
s=[]
s.append(0)
for i in range(1,N):
  if S[i]==1 and S[i-1]==0:
    s.append(i-1)
  elif i==N-1 and S[i]==0:
    s.append(i)
s.append(N)
if len(s)<=K+2:
  print(len(S))
  exit(0)
ans=0
print(s)
for i in range(1,max(len(s)-K,0)):
  ans=max(s[i+K]-(s[i-1])-1,ans)

print(ans)

N, K = map(int, input().split())
S = list(input())
S = list(map(int, S))
s = []
tmp = 0

for i in range(N):
    if S[i] == 1:
        if tmp != 0:
            s.append((tmp, i-1))
        tmp = 0
    else:
        tmp += 1
        if i == N-1:
            s.append((tmp, i))
if len(s) <= K:
    print(N)
    exit(0)
ans = 0
for i in range(len(s)-K+1):
    tmp = 0
    if i+K == len(s):
        tmp = N-1
    else:
        tmp = s[i+K][1]-s[i+K][0]

    if i != 0:
        tmp -= s[i-1][1]
    else:
        tmp += 1
    ans = max(ans, tmp)

print(ans)


Verdicts: Wrong Answer

Wrong Answer

Wrong Answer

Accepted