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())
zeros=[0]
ones=[0]
S=list(input())
now=S[0]
one=0
zero=0
for i,s in enumerate(S):
  if s==now:
    if s=="0":
      zero+=1
    else:
      one+=1
  else:
    if s=="0":
      zero+=1
      ones.append(one)
      now=s
    else:
      one+=1
      zeros.append(zero)
      now=s
  if i==N-1:
    if s=="0":
      zeros.append(zero)
    else:
      ones.append(one)
print(zeros, ones)
ans=0
lone=len(ones)
lzero=len(zeros)
if lzero==0:
  print(N)

if S[0]=="0":
  
  for i in range(lzero-K):
    idx=min(i+K, lzero-1)
    idx2=min(idx, lone-1)
    idx3=min(i, lone-1)
    ans=max(ans, zeros[idx]-zeros[i]+ones[idx2]-ones[i])
    
else:
  for i in range(lzero-K):
    idx=min(i+K, lzero-1)
    idx2=min(idx+1, lone-1)
    ans=max(ans, zeros[idx]-zeros[i]+ones[idx2]-ones[i])

print(ans)
    

N, K=map(int, input().split())
zeros=[0]
ones=[0]
S=list(input())
now=S[0]
one=0
zero=0
for i,s in enumerate(S):
  if s==now:
    if s=="0":
      zero+=1
    else:
      one+=1
  else:
    if s=="0":
      zero+=1
      ones.append(one)
      now=s
    else:
      one+=1
      zeros.append(zero)
      now=s
  if i==N-1:
    if s=="0":
      zeros.append(zero)
    else:
      ones.append(one)
print(zeros, ones)
ans=0
lone=len(ones)
lzero=len(zeros)
if S[0]=="0":
  
  for i in range(lzero-K):
    idx=min(i+K, lzero-1)
    idx2=min(idx, lone-1)
    idx3=min(i, lone-1)
    ans=max(ans, zeros[idx]-zeros[i]+ones[idx2]-ones[i])
    
else:
  for i in range(lzero-K):
    idx=min(i+K, lzero-1)
    idx2=min(idx+1, lone-1)
    ans=max(ans, zeros[idx]-zeros[i]+ones[idx2]-ones[i])

print(ans)
    

N, K=map(int, input().split())
zeros=[0]
ones=[0]
S=list(input())
now=S[0]
one=0
zero=0
for i,s in enumerate(S):
  if s==now:
    if s=="0":
      zero+=1
    else:
      one+=1
  else:
    if s=="0":
      zero+=1
      ones.append(one)
      now=s
    else:
      one+=1
      zeros.append(zero)
      now=s
  if i==N-1:
    if s=="0":
      zeros.append(zero)
    else:
      ones.append(one)
print(zeros, ones)
ans=0
lone=len(ones)
lzero=len(zeros)
if lzero==0:
  print(N)

if S[0]=="0":
  
  for i in range(lzero-K+1):
    idx=min(i+K, lzero-1)
    idx2=min(idx, lone-1)
    idx3=max(i-1, 0)
    ans=max(ans, zeros[idx]-zeros[i]+ones[idx2]-ones[idx3])
    
else:
  for i in range(lzero-K+1):
    idx=min(i+K, lzero-1)
    idx2=min(idx+1, lone-1)
    idx3=max(i-1, 0)
    ans=max(ans, zeros[idx]-zeros[i]+ones[idx2]-ones[i])

print(ans)
    

N, K=map(int, input().split())
zeros=[0]
ones=[0]
S=list(input())
now=S[0]
one=0
zero=0
for i,s in enumerate(S):
  if s==now:
    if s=="0":
      zero+=1
    else:
      one+=1
  else:
    if s=="0":
      zero+=1
      ones.append(one)
      now=s
    else:
      one+=1
      zeros.append(zero)
      now=s
  if i==N-1:
    if s=="0":
      zeros.append(zero)
    else:
      ones.append(one)

ans=0
lone=len(ones)
lzero=len(zeros)
if lzero==0:
  print(N)
  exit()
if lzero<=K:
  print(N)
  exit()

if S[0]=="0":
  
  for i in range(lzero-K+1):
    idx=min(i+K, lzero-1)
    idx2=min(idx, lone-1)
    idx3=max(i-1, 0)
    ans=max(ans, zeros[idx]-zeros[i]+ones[idx2]-ones[idx3])
    
else:
  for i in range(lzero-K+1):
    idx=min(i+K, lzero-1)
    idx2=min(idx+1, lone-1)
    
    ans=max(ans, zeros[idx]-zeros[i]+ones[idx2]-ones[i])

print(ans)
    

Verdicts: Wrong Answer

Wrong Answer

Wrong Answer

Accepted