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())
row = []
for i in range(N):
	if i == 0:
		judge = S[i]
		row.append(1)
	else:
		if judge == S[i]:
			row[-1] += 1
		else:
			judge = S[i]
			row.append(1)

max_row = 2 * K + 1

if max_row >= len(row):
	print(N)
else:
	if S[-1] == "0":
		row.append(0)
	if S[0] == "0":
		row.append(0)
	sum_count = []
	temp_sum = 0
	for i in range(max_row):
		temp_sum += row[i]
	sum_count.append(temp_sum)
	i = max_row
	while i < len(row):
		sum_count.append(sum_count[-1] + row[i] + row[i+1] - row[i-max_row] - row[i-max_row+1])
		i += 2
	print(max(sum_count))
	
	

N , K = map(int,input().split())
S = list(input())
row = []
for i in range(N):
	if i == 0:
		row.append([S[i],1])
	else:
		if row[-1][0] == S[i]:
			row[-1][1] += 1
		else:
			row.append([S[i],1])
			

max_row = 2 * K + 1
row_len = len(row)
if row[0][0] == "0":
	if row[row_len - 1][0] == "0":
		row.insert(0,["1",0])
		row.append(["1",0])
	else:
		row.insert(0,["1",0])
else:
	if row[row_len - 1][0] == "0":
		row.append(["1",0])
j = 0
max_ans = 0
if row_len > max_row: 
	while len(row) - max_row +1 > j:
		temp_sum = 0
		for k in range(max_row):
			temp_sum += row[j + k][1]

		if temp_sum > max_ans:
			max_ans = temp_sum
		j += 2
	print(max_ans)
else:
	print(N)
	

N , K = map(int,input().split())
S = list(input())
row = []
for i in range(N):
	if i == 0:
		judge = S[i]
		row.append(1)
	else:
		if judge == S[i]:
			row[-1] += 1
		else:
			judge = S[i]
			row.append(1)

max_row = 2 * K + 1


if max_row >= len(row):
	print(N)
else:
	if S[-1] == "0":
		row.append(0)
	if S[0] == "0":
		row.insert(0,0)
	sum_count = []
	temp_sum = 0
	for i in range(max_row):
		temp_sum += row[i]
	sum_count.append(temp_sum)
	i = max_row
	while i < len(row):
		sum_count.append(sum_count[-1] + row[i] + row[i+1] - row[i-max_row] - row[i-max_row+1])
		i += 2
	print(max(sum_count))
	
	

N , K = map(int,input().split())
S = list(input())
row = []
for i in range(N):
	if i == 0:
		judge = S[i]
		row.append(1)
	else:
		if judge == S[i]:
			row[-1] += 1
		else:
			judge = S[i]
			row.append(1)
if S[-1] == "0":
	row.append(0)
if S[0] == "0":
	row.insert(0,0)
max_row = 2 * K + 1

if max_row >= len(row):
	print(N)
else:
	sum_count = []
	temp_sum = 0
	for i in range(max_row):
		temp_sum += row[i]
	sum_count.append(temp_sum)
	i = max_row
	while i < len(row):
		sum_count.append(sum_count[-1] + row[i] + row[i+1] - row[i-max_row] - row[i-max_row+1])
		i += 2
	print(max(sum_count))
	
	

Verdicts: Wrong Answer

Wrong Answer

Wrong Answer

Accepted