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 = []
before = ''
cnt = 0
len_l = 0
for i in range(N):
  if before == '0' and S[i] == '0'
  	cnt += 1
  elif before == '0' and S[i] == '1'
  	before = '1'
    L.append(cnt)
    len_l += 1
    cnt = 0
  elif before == '1' and S[i] == '0'
    before == '0'
    cnt += 1
if cnt != 0:
  L.append(cnt)
  len_l += 1

L.sort(reverse=True)

if len_l > K:
  print(sum(L[:K])
else:
  print(sum(L))

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

before = S[0]
cnt = 0
same_nums = []
for i in range(N):
  if S[i] == before:
    cnt += 1
  else:
    same_nums.append(cnt)
    before = S[i]
    cnt = 1
same_nums.append(cnt)
if S[0] == '0':
  sum_temp = sum(same_nums[:2*K+1])
  ans = sum_temp
  for i in range(0, K+1, 2):
    sum_temp = sum_temp - sum(same_nums[i:i+2]) + sum(same_nums[i+2*K+1:i+2*K+3])
    ans = max(ans, sum_temp)
  print(ans)
else:
  sum_temp = sum(same_nums[:2*K])
  ans = sum_temp
  sum_temp = sum_temp - same_nums[1] + sum(same_nums[2*K:2*K+2])
  ans = max(ans, sum_temp)
  for i in range(3, K+1, 2):
    sum_temp = sum_temp - sum(same_nums[i:i+2]) + sum(same_nums[i+2*K:i+2*K+2])
    ans = max(ans, sum_temp)
  print(ans)
  


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

L = []
before = ''
cnt = 0
len_l = 0
for i in range(N):
    if before == '0' and S[i] == '0':
        cnt += 1
    elif before == '0' and S[i] == '1':
        before = '1'
        L.append(cnt)
        len_l += 1
        cnt = 0
    elif before == '1' and S[i] == '0':
        before == '0'
        cnt += 1
if cnt != 0:
    L.append(cnt)
    n_l += 1

L.sort(reverse=True)

if len_l > K:
    print(sum(L[:K])
else:
    print(sum(L))

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

L = []
before = ''
cnt = 0
len_l = 0
for i in range(N):
  if before == '0' and S[i] == '0':
  	cnt += 1
  elif before == '0' and S[i] == '1':
  	before = '1'
    L.append(cnt)
    len_l += 1
    cnt = 0
  elif before == '1' and S[i] == '0':
    before == '0'
    cnt += 1
if cnt != 0:
  L.append(cnt)
  len_l += 1

L.sort(reverse=True)

if len_l > K:
  print(sum(L[:K])
else:
  print(sum(L))


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

L = []
before = ''
cnt = 0
len_l = 0
for i in range(N):
    if before == '0' and S[i] == '0':
        cnt += 1
    elif before == '0' and S[i] == '1':
        before = '1'
        L.append(cnt)
        len_l += 1
        cnt = 0
    elif before == '1' and S[i] == '0':
        before == '0'
        cnt += 1
if cnt != 0:
    L.append(cnt)
    n_l += 1

L.sort(reverse=True)

if len_l > K:
    print(sum(L[:K]))
else:
    print(sum(L))

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

before = S[0]
zeros = []
ones = []
zero_cnt = 0
one_cnt = 0
for char in S:
  if char == before:
    if char == '0':
      zero_cnt += 1
    else:
      one_cnt += 1
    before = char
  else:
    if char == '0':
      ones.append(one_cnt)
      zero_cnt = 1
    else:
      zeros.append(zero_cnt)
      one_cnt = 1
    before = char
if S[-1] == '0':
  zeros.append(zero_cnt)
elif S[-1] == '1':
  ones.append(one_cnt)
if len(zeros) <= K:
  print(N)
else:
  if S[1] == '0':
    sum_temp = sum(zeros[:K]) + sum(ones[:K])
    sum_o = sum_temp
    for i in range(len(zeros)-K+1):
      sum_temp = sum_temp - zeros[i] + zeros[i+K] - ones[i] + ones[i+K]
      sum_o = max(sum_o, sum_temp)
    print(sum_o)
  else:
    sum_temp = sum(zeros[:K]) + sum(ones[:K+1])
    sum_o = sum_temp
    for i in range(len(zeros)-K+1):
      sum_temp = sum_temp - zeros[i] + zeros[i+K] - ones[i] + ones[i+K+1]
      sum_o = max(sum_o, sum_temp)
    print(sum_o)
  
    


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

before = S[0]
cnt = 0
same_nums = []
if S[0] == '0':
  same_nums.append(0)
for i in range(N):
  if S[i] == before:
    cnt += 1
  else:
    same_nums.append(cnt)
    before = S[i]
    cnt = 1
same_nums.append(cnt)
if S[-1] == '0':
  same_nums.append(0)
  
sum_nums = sum(same_nums[:2*K+1])
ans = sum_nums
for i in range(0, len(same_nums)-2*K, 2):
  sum_nums = sum_nums - sum(same_nums[i:i+2]) + sum(same_nums[i+2*K+1:i+2*K+3])
  ans = max(ans, sum_nums)
print(ans)
  


Verdicts: Runtime Error

Runtime Error

Runtime Error

Runtime Error

Wrong Answer

Runtime Error

Accepted