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

# print(n, k, s)

prevcCt = 0 
nextCnt = 0 



val = 0
vals = []
for i in range(len(s)):
	if s[i] == "0":
		val -= 1
	else:
		val += 1

	#print(i, val, s[i], s[i+1])
	# print(i,len(s))
	if i == len(s) - 1 or s[i] != s[i+1]:
		vals.append(val)
		val = 0

print(vals)


if vals[0] < 0:
	vals.insert(0,0)

vals.append(0)
vals.append(0)

print(vals)



max = 0
for i in range(len(vals) - k):
	
	if i%2 == 0:
		# print(vals[i:i + 2*k + 1])

		sum = 0
		for e in vals[i:i + 2*k + 1]:
			sum += abs(e)
			if sum > max:
				max = sum

print(max)

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

# print(n, k, s)

prevcCt = 0 
nextCnt = 0 



val = 0
vals = []
for i in range(len(s)):
	if s[i] == "0":
		val -= 1
	else:
		val += 1

	#print(i, val, s[i], s[i+1])
	# print(i,len(s))
	if i == len(s) - 1 or s[i] != s[i+1]:
		vals.append(val)
		val = 0

# print(vals)

def getMAXArray(arr):
	maxVal = 0 
	maxConvCol = 0 
	maxArr = arr[:]

	for i in range(len(arr)):
		if arr[i] < 0:
			copy = arr[:]

			
			copy[i] = copy[i] * -1

			# print(copy)

			を確認
			tempMax = 0
			for e in copy:
				if e > 0:
					tempMax += e
					if tempMax > maxVal:
						maxVal = tempMax
						maxConvCol = i
						maxArr = copy
				else:
					
					tempMax = 0

	# print(maxVal, maxConvCol)
	return(maxArr)


# print(getMAXArray(vals))
# print(getMAXArray(getMAXArray(vals)))
# print(getMAXArray(getMAXArray(getMAXArray(vals))))

for i in range(k):
	vals = getMAXArray(vals)

# 	print(vals)

をカウント
max = 0
temp = 0
for i in range(len(vals)):
	if vals[i] > 0:
		temp += vals[i]
	else:
		temp = 0
	
	if temp > max:
		max = temp

print(max)




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

# print(n, k, s)

prevcCt = 0 
nextCnt = 0 



val = 0
vals = []
for i in range(len(s)):
	if s[i] == "0":
		val -= 1
	else:
		val += 1

	#print(i, val, s[i], s[i+1])
	# print(i,len(s))
	if i == len(s) - 1 or s[i] != s[i+1]:
		vals.append(val)
		val = 0

#print(vals)


if vals[0] < 0:
	vals.insert(0,0)

vals.append(0)
vals.append(0)

#print(vals)

vals = list(map(abs, vals))
#print(vals)



max = 0

loopCnt = len(vals) - k
if  loopCnt < 1:
	loopCnt = 1



sumVal = sum(vals[0:2*k + 1])
max = sumVal

# print(vals)
# print(max)

for i in range(loopCnt):
	
	
	if i%2 == 0 and i>0:
		
		outVal = sum(vals[i-2:i])
		
		inVal = sum(vals[i + 2*k -1:i + 2*k + 1])
	
		sumVal = sumVal - outVal + inVal

		#print(i, vals[i-2:i], outVal, vals[i + 2*k -1:i + 2*k + 1], inVal, sumVal)
		# print(vals[i:i + 2*k + 1])

		#sumVal = sum(vals[i:i + 2*k + 1])
		
		# 	sumVal += abs(e)
		if sumVal > max:
			max = sumVal

print(max)

Verdicts: Wrong Answer

Wrong Answer

Accepted