Problem:


Score : 400 points

Problem Statement

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.

Constraints

N is an integer satisfying 1 \leq N \leq 10^5.

K is an integer satisfying 1 \leq K \leq 10^5.

The length of the string S is N.

Each character of the string S is 0 or 1.

Input

Input is given from Standard Input in the following format:

N K
S

Output

Print the maximum possible number of consecutive people standing on hands after at most K directions.

Sample Input 1

5 1
00010

Sample Output 1

4

We can have four consecutive people standing on hands, which is the maximum result, by giving the following direction:

Give the direction with l = 1, r = 3, which flips the first, second and third persons from the left.

Sample Input 2

14 2
11101010110011

Sample Output 2

8

Sample Input 3

1 1
1

Sample Output 3

1

No directions are necessary.



Sample input:
5 1
00010


Sample output:
4

Submission status: Wrong Answer

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

nums = []
if S[0] != '1':
  nums.append(0)

current = S[0]
count = 1
for i, s in enumerate(list(S)):
  if i == len(S)-1:
    nums.append(count)
    break
  if S[i+1] != s:
    nums.append(count)
    count = 1
    current = s
  else:
    count += 1

if S[len(S)-1] != '1':
  nums.append(0)

sums = [0]
for index, i in enumerate(nums):
  sums.append(sums[index] + i)
  
res = 0
for left in range(0, len(sums), 2):
  right = left + K*2 + 1
  if right > len(sums):
    right = len(sums) - 1
  print(left, right)
  res = max(res, sums[right] - sums[left])
  
print(res)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
S = input()
T = []
count = 1
for n in range(N - 1):
    s0, s1 = S[n], S[n + 1]
    if s0 == s1:
        count += 1
    else:
        T.append(count)
        count = 1
T.append(count)
if S[0] == '0':
    T = [0] + T
if S[-1] == '0':
    T = T + [0]
L = len(T)
if 2 * K + 1 >= L:
    print(N)
    exit()

# 偶数番目から始まる連続する2K+1個の和の最大値
ans = sum(T[0: 2 * K + 2])
for l in range(2, L - (2 * K + 1), 2):
    cand = ans
    cand -= T[l - 2]
    cand -= T[l - 1]
    cand += T[l + 2 * K - 1]
    cand += T[l + 2 * K]
    if cand > ans:
        ans = cand
print(ans)

Submission status: Wrong Answer

Solution code:
from itertools import groupby
from numpy import cumsum


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

if len(set(S)) == 1:
    print(N)
    exit()

G = []
for k, v in groupby(S):
    G.append(len(list(v)))
if S[0] == '0':
    G.append(0)
if S[-1] == '0':
    G.append(0)

cum = [0]
cum.extend(list(cumsum(G)))
cum.append(0)

ans = 0
gc = len(G)
w = min(gc, 3+(K-1)*2)
for i in range(0, gc-w+2, 2):
    ans = max(ans, cum[i+w]-cum[i])
print(ans)

Submission status: Wrong Answer

Solution code:
from itertools import groupby
from numpy import cumsum


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

if len(set(S)) == 1:
    print(N)
    exit()

G = []
if S[0] == '0':
    G.append(0)
for k, v in groupby(S):
    G.append(len(list(v)))

cum = [0]
cum.extend(list(cumsum(G)))

ans = 0
gc = len(G)
w = min(gc, 3+(K-1)*2)
for i in range(0, gc-w+1, 2):
    ans = max(ans, cum[i+w]-cum[i])
print(ans)

Submission status: Wrong Answer

Solution code:
from itertools import groupby
from numpy import cumsum


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

if set(S) == {'1'}:
    print(N)
    exit()

G = []
if S[0] == '0':
    G.append(0)
for k, v in groupby(S):
    G.append(len(list(v)))

cum = [0]
cum.extend(list(cumsum(G)))

ans = 0
gc = len(G)
w = min(gc, 3+(K-1)*2)
for i in range(0, gc-w+1, 2):
    ans = max(ans, cum[i+w]-cum[i])
print(ans)

Submission status: Wrong Answer

Solution code:
from itertools import groupby

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

if set(S) == {'1'}:
    print(N)
    exit()

G = []
if S[0] == '0':
    G.append(0)
for k, v in groupby(S):
    G.append(len(list(v)))

ans = 0
gc = len(G)
w = min(gc, 3+(K-1)*2)
for i in range(0, gc-w+1, 2):
    ans = max(ans, sum(G[i:i+w]))
print(ans)

Submission status: Wrong Answer

Solution code:
from itertools import groupby

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

if set(S) == {'1'}:
    print(N)
    exit()

G = []
if S[0] == '0':
    G.append(0)
for k, v in groupby(S):
    G.append(len(list(v)))

ans = 0
gc = len(G)
w = 3 + (K - 1) * 2
for i in range(0, gc-w+1, 2):
    ans = max(ans, sum(G[i:i+w]))
print(ans)

Submission status: Wrong Answer

Solution code:
from sys import exit
def groupby(a):
    a2=[[a[0],1]]
    for i in range(1,len(a)):
        if a2[-1][0]==a[i]:
            a2[-1][1]+=1
        else:
            a2.append([a[i],1])
    return a2

n,k=map(int,input().split())
s=list(input())
x=groupby(s)
l=len(x)
y=[x[i][1] for i in range(l)]

if x[0][0]=="0":
    su=sum(y[:2*k])
    ans=su
    y.pop(0)
    l-=1
else:
    ans=0
su=sum(y[:2*k+1])
ans=max(ans,su)
l_sub=(l-(2*k+1))//2
if l_sub<=0:
    print(ans)
    exit()
for i in range(l_sub):
    su-=y[2*i]
    su-=y[2*i+1]
    su+=y[2*k+1+(2*i)]
    su+=y[2*k+1+(2*i+1)]
    ans=max(su,ans)
#最後のところの判定っぽいな
'''
if 2*k+1+2*l_sub<l:
    su-=y[2*l_sub]
    su+=y[2*k+1+2*l_sub]
    ans=max(su,ans)
'''
print(ans)

Submission status: Wrong Answer

Solution code:
from sys import exit
def groupby(a):
    a2=[[a[0],1]]
    for i in range(1,len(a)):
        if a2[-1][0]==a[i]:
            a2[-1][1]+=1
        else:
            a2.append([a[i],1])
    return a2

n,k=map(int,input().split())
s=list(input())
x=groupby(s)
l=len(x)
y=[x[i][1] for i in range(l)]

if x[0][0]=="0":
    su=sum(y[:2*k])
    ans=su
    y.pop(0)
    l-=1
else:
    ans=0
su=sum(y[:2*k+1])
ans=max(ans,su)
l_sub=(l-(2*k+1))//2
if l_sub<=0:
    print(ans)
    exit()
for i in range(l_sub):
    su-=y[2*i]
    su-=y[2*i+1]
    su+=y[2*k+1+(2*i)]
    su+=y[2*k+1+(2*i+1)]
    ans=max(su,ans)
if 2*k+1+2*l_sub<l:
    su-=y[2*l_sub]
    su+=y[2*k+1+2*l_sub]
    ans=max(su,ans)
print(ans)

Submission status: Wrong Answer

Solution code:
from sys import exit
def groupby(a):
    a2=[[a[0],1]]
    for i in range(1,len(a)):
        if a2[-1][0]==a[i]:
            a2[-1][1]+=1
        else:
            a2.append([a[i],1])
    return a2

n,k=map(int,input().split())
s=list(input())
x=groupby(s)
l=len(x)
y=[x[i][1] for i in range(l)]
if x[0][0]=="1":
    su=sum(y[:2*k+1])
    ans=su
    l_sub=(l-(2*k+1))//2
    for i in range(l_sub):
        su-=y[2*i]
        su-=y[2*i+1]
        su+=y[2*k+1+(2*i)]
        su+=y[2*k+1+(2*i+1)]
        ans=max(su,ans)
    if l%2==0:
        su-=y[2*l_sub]
        su+=y[2*k+1+2*i]
        ans=max(su,ans)
    print(ans)
else:
    su=sum(y[:2*k])
    ans=su
    #print(ans)
    su-=y[0]
    if 2*k+1>l-1:
        print(ans)
        exit()
    su+=y[2*k+1]
    ans=max(su,ans)
    l-=1
    l_sub=(l-(2*k+1))//2
    for i in range(l_sub):
        su-=y[1+2*i]
        su-=y[1+2*i+1]
        su+=y[1+2*k+1+(2*i)]
        su+=y[1+2*k+1+(2*i+1)]
        ans=max(su,ans)
    if l%2==0:
        su-=y[1+2*l_sub]
        su+=y[1+2*k+1+2*i]
        ans=max(su,ans)
    print(ans)

'''
if 2*k+1>=l:
    print(n)
else:
    su=sum(y[:2*k+1])
    ans=su
    for i in range(l-(2*k+1)):
        su-=y[i]
        su+=y[2*k+1+i]
        ans=max(su,ans)
    print(ans)
'''

Submission status: Wrong Answer

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

ss = []
pre = S[0]
num = 0
if S[0]=='0':ss.append(0)
  
for i in range(1, N):
    if S[i]!=pre:
        ss.append(len(S[num:i]))
        num = i
        pre = S[i]
ss.append(len(S[num: N]))

sss = []
sss.append(ss[0])
for i in range(1, len(ss)):
    sss.append(sss[i-1]+ss[i])

ans = []
ans.append(sum(ss[0:2*K+1]))

for i in range(2,len(ss), 2):
    if i+2*K+1> len(ss):break
    ans.append(sss[i+2*K]-sss[i-1])

print(max(ans))

Submission status: Wrong Answer

Solution code:
n, nFlips = map(int, input().split())
s = input().rstrip()

psums = [0]
for i, ch in enumerate(s):
    psums.append(psums[-1])
    if ch == '0' and (i == 0 or s[i - 1] == '1'):
        psums[-1] += 1
ret = 0
for i in range(n + 1):
    maxNeed = psums[i] + nFlips
    left, right = i - 1, n + 1
    while left + 1 < right:
        mid = (left + right) >> 1
        if psums[mid] > maxNeed:
            right = mid
        else:
            left = mid
    if left != i - 1:
        ret = max(ret, left - i)
print(ret)

Submission status: Wrong Answer

Solution code:
n, k = [int(i) for i in input().split()]
s = input() + '2'
top = int(s[0])
targetIdx = 1 if top % 2 == 0 else 0
target = top
ret = 0
tmpSum = 0
lst = [0] * (k * 2 + 1)
for v in s:
	if int(v) == target:
		tmpSum += 1
	else:
		lst.append(tmpSum)
		lst = lst[1:]
		if int(v) == 0:
			ret = max(sum(lst), ret)
		if int(v) == 2 and int(s[-2]) == 1:
			ret = max(sum(lst), ret)
		tmpSum = 1
		target = int(v)
print(ret)

Submission status: Wrong Answer

Solution code:
n, k = [int(i) for i in input().split()]
s = input() + '2'
top = int(s[0])
targetIdx = 1 if top % 2 == 0 else 0
target = top
ret = 0
tmpSum = 0
lst = [0] * (k * 2 + 1)
for i, v in enumerate(s):
	if int(v) == target:
		tmpSum += 1
	else:
		lst.append(tmpSum)
		lst = lst[1:]
		if int(v) == 0 or int(s[-2]) == 1:
			ret = max(sum(lst), ret)
		tmpSum = 1
		target = int(v)
print(ret)

Submission status: Wrong Answer

Solution code:
n, k = list(map(int, input().split(' ')))
s = input()
ll = []
prev = ''
temp = 0
for c in s:
  t = int(c)
  if t != prev:
    prev = t
    if temp:
      ll.append(abs(temp))
    temp = 0
  temp += 1 if t else -1
if temp:
  ll.append(abs(temp))
ans = 0
temps = [abs(ll[0])]
for i in range(1, len(ll)):
  temps.append(temps[i-1] + ll[i])
  if i >= 2 * k + 1 or (s[0] == '0' and i == 2 * k):
    temps[-1] -= ll[min(0, i - 2 * k - 1)]
print(max(temps))

Submission status: Wrong Answer

Solution code:
n, k = list(map(int, input().split(' ')))
s = input()
ll = []
prev = ''
temp = 0
for c in s:
  t = int(c)
  if t != prev:
    prev = t
    if temp:
      ll.append(temp)
    temp = 0
  temp += 1 if t else -1
if temp:
  ll.append(temp)
print(ll)
ans = 0
for i in range(len(ll)):
  temp = 0
  if i == 0 and ll[i] < 0:
    for j in range(min(2*k, len(ll))):
      temp += abs(ll[j])
  elif ll[i] > 0:
    for j in range(i, min(i + 2*k + 1, len(ll))):
      temp += abs(ll[j])
  ans = max(ans, temp)

print(ans)

Submission status: Wrong Answer

Solution code:
import os, sys, re, math

(N, K) = [int(n) for n in input().split()]
S = input()

bunches = []
for s in S:
    if not bunches:
        bunches.append(1 if s == '1' else -1)
    else:
        if s == '1':
            if bunches[-1] > 0:
                bunches[-1] += 1
            else:
                bunches.append(1)
        else:
            if bunches[-1] < 0:
                bunches[-1] -= 1
            else:
                bunches.append(-1)

max_count = 0

if len(bunches) == 1:
    print(1)
    sys.exit(0)

for i in range(1, K + 1):
    nstart = 0 if bunches[0] < 0 else 1
    mcount = 0
    while nstart < len(bunches):
        nidx = nstart
        count = 0 if nstart == 0 else bunches[nstart - 1]
        for j in range(i):
            if nidx >= len(bunches):
                break
            count -= bunches[nidx]
            if nidx < len(bunches) - 1:
                count += bunches[nidx + 1]
            nidx += 2
        mcount = max(count, mcount)
        nstart += 2
    max_count = max(mcount, max_count)

print(max_count)

Submission status: Wrong Answer

Solution code:
import os, sys, re, math

(N, K) = [int(n) for n in input().split()]
S = input()

bunches = []
for s in S:
    if not bunches:
        bunches.append(1 if s == '1' else -1)
    else:
        if s == '1':
            if bunches[-1] > 0:
                bunches[-1] += 1
            else:
                bunches.append(1)
        else:
            if bunches[-1] < 0:
                bunches[-1] -= 1
            else:
                bunches.append(-1)

max_count = 0

if len(bunches) == 1:
    print(1)
    sys.exit(0)

for i in range(1, K + 1):
    nstart = 0 if bunches[0] < 0 else 1
    mcount = 0
    while nstart < len(bunches):
        nidx = nstart
        count = 0 if nstart == 0 else bunches[nstart - 1]
        for j in range(i):
            if nidx >= len(bunches):
                break
            count -= bunches[nidx]
            if nidx < len(bunches) - 1:
                count += bunches[nidx + 1]
            nidx += 2
        mcount = max(count, mcount)
        nstart += 2
    max_count = max(mcount, max_count)
    print(max_count)

print(max_count)

Submission status: Wrong Answer

Solution code:
n, k = list(map(int, input().split(' ')))
s = input()
ll = []
prev = ''
temp = 0
for c in s:
  t = int(c)
  if t != prev:
    prev = t
    if temp:
      ll.append(temp)
    temp = 0
  temp += 1 if t else -1
if temp:
  ll.append(temp)
# print(ll) -> [2, -1, 1, -1, 1, -1, 1, -2, 2, -1, 1]
# (2*k + 1)個合体させたもので最大のものが答えのはず
ans = 0
for i in range(len(ll)):
  temp = 0
  for j in range(i, min(i + 2*k + 1, len(ll))):
    temp += abs(ll[j])
  ans = max(ans, temp)

print(ans)

Submission status: Wrong Answer

Solution code:
n, k = list(map(int, input().split(' ')))
s = input()
ll = []
prev = ''
temp = 0
for c in s:
  t = int(c)
  if t != prev:
    prev = t
    if temp:
      ll.append(temp)
    temp = 0
  temp += 1 if t else -1
if temp:
  ll.append(temp)
# TLEかも
ans = 0
for l in [ll[:], ll[::-1]]:
  for i in range(k):
    index = -1,
    mmm = -1
    for j in range(len(l)):
      if l[j] < 0:
        temp = -l[j] + (l[j-1] if j >= 1 else 0) + (l[j+1] if j+1 < len(l) else 0)
        if temp > mmm:
          mmm = temp
          index = j
    if len(l) == 1:
      pass
    elif index == 0:
      l = [mmm] + l[2:]
    elif index == 1:
      l = [mmm] + l[3:]
    elif index == len(l)-1:
      l = l[:-2] + [mmm]
    elif index == len(l)-2:
      l = l[:-3] + [mmm]
    else:
      l = l[:index-1] + [mmm] + l[index+2:]
  ans = max(ans, max(l))
print(ans)

Submission status: Wrong Answer

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

ss = []
pre = S[0]
num = 0
if S[0]=='0':ss.append(0)
  
for i in range(1, N):
    if S[i]!=pre:
        ss.append(len(S[num:i]))
        num = i
        pre = S[i]
ss.append(len(S[num: N]))

ans = []
ans.append(sum(ss[0:2*K+1]))

for i in range(2,len(ss), 2):
    if i+2*K+1> len(ss):break
    ans.append(ans[-1]+sum(ss[i+2*K-1:i+2*K+1])-sum(ss[i-2:i]))

print(max(ans))

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
S=input()
#Aは0の左端、Bは0の右端
A=[]
B=[]
for i in range(N-1):
  if S[i]=='0'and S[i+1]=='1':
    B.append(i)
if S[-1]=='0':
  B.append(N-1)
if S[0]=='0':
  A.append(0)
for i in range(N-1):
  if S[i]=='1'and S[i+1]=='0':
    A.append(i+1)
print(A)
print(B)
if len(A)<=K:
  print(N)
else:
  y=A[K]
  z=N-B[-1*(K+1)]-1
  l=0
  ans=0
  for i in range(len(A)-K-1):
    l=A[i+K+1]-B[i]-1
    ans=max(l,ans)
    print(l)
  print(max(l,y,z))

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
S=input()
#Aは1の右端、Bは1の左端
A=[]
B=[]
if S[0]=='1':
  A.append(0)
for i in range(N-1):
  if S[i]=='0' and S[i+1]=='1':
    A.append(i+1)
  elif S[i]=='1' and S[i+1]=='0':
    B.append(i)
if S[-1]=='1':
  B.append(N-1)

a=len(A)-K
ans=0
for i in range(a):
  l=B[i+K]-A[i]+1
  ans=max(l,ans)
print(ans)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
s = list(map(int,input()))
count = [0]
flag = s[0]
for i in range(n):
    if flag==s[i]:
        count[-1] +=1
    else:
        flag = s[i]
        count.append(1)
c = [0]*len(count)
c[0] = count[0]
for i in range(1,len(count)):
    c[i] = c[i-1]+count[i]
if len(count)%2==0:
    m = len(count)//2
else:
    m = len(count)//2 + abs(s[0]-1)
if k>=m:
    print(len(s))
    exit()
if s[0]==1:
    ans = c[min(len(c)-1,k*2)]
    if s[-1]==1:
        for i in range(2,len(c),2):
            if k*2+i-1>=len(c):
                break
            else:
                ans = max(ans,c[i+k*2]-c[i-1])
    else:
        for i in range(2,len(c),2):
            if k*2+i-1>=len(c):
                break
            else:
                ans = max(ans,c[i+k*2-1]-c[i-1])   
else:
    ans = c[min(len(c)-1,k*2-1)]
    if s[-1]==1:
        for i in range(2,len(c),2):
            if k*2+i-1>=len(c):
                break
            else:
                ans = max(ans,c[i+k*2-1]-c[i-2])
    else:
        for i in range(2,len(c),2):
            if k*2+i-1>=len(c):
                break
            elif i+(k-1)*2==n-1:
                ans = max(ans,c[i+(k-1)*2]-c[i-2])
            else:
                ans = max(ans,c[i+k*2-1]-c[i-2])

print(ans)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
s = list(map(int,input()))
count = [0]
flag = s[0]
for i in range(n):
    if flag==s[i]:
        count[-1] +=1
    else:
        flag = s[i]
        count.append(1)
c = [0]*len(count)
c[0] = count[0]
for i in range(1,len(count)):
    c[i] = c[i-1]+count[i]
if len(count)%2==0:
    m = len(count)//2
else:
    m = len(count)//2 + abs(s[0]-1)
if k>=m:
    print(len(s))
    exit()
if s[0]==1:
    ans = c[min(len(c)-1,k*2)]
    for i in range(2,len(c),2):
        if k*2+i-1>=len(c):
            break
        else:
            ans = max(ans,c[i+k*2-1]-c[i-1])
else:
    ans = c[min(len(c)-1,k*2-1)]
    for i in range(2,len(c),2):
        if k*2+i-1>=len(c):
            break
        else:
            ans = max(ans,c[i+k*2-1]-c[i-2])
print(ans)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
s = list(map(int,input()))
count = [0]
flag = s[0]
for i in range(n):
    if flag==s[i]:
        count[-1] +=1
    else:
        flag = s[i]
        count.append(1)
c = [0]*len(count)
c[0] = count[0]
for i in range(1,len(count)):
    c[i] = c[i-1]+count[i]
if len(count)%2==0:
    m = len(count)//2
else:
    m = len(count)//2 + abs(s[0]-1)
if k>=m:
    print(len(s))
    exit()
if s[0]:
    ans = c[min(len(c)-1,k*2)]
    for i in range(2,len(c),2):
        if k*2+i>=len(c):
            break
        else:
            ans = max(ans,c[i+k*2]-c[i-1])
else:
    ans = c[min(len(c)-1,k*2-1)]
    for i in range(2,len(c),2):
        if k*2+i>=len(c):
            break
        else:
            ans = max(ans,c[i+k*2-1]-c[i-2])
print(ans)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
s = list(map(int,input()))
count = [0]
flag = s[0]
for i in range(n):
    if flag==s[i]:
        count[-1] +=1
    else:
        flag = s[i]
        count.append(1)
c = [0]*len(count)
c[0] = count[0]
for i in range(1,len(count)):
    c[i] = c[i-1]+count[i]
if len(count)%2==0:
    m = len(count)//2
else:
    m = len(count)//2 + abs(s[0]-1)
if k>=m:
    print(len(s))
    exit()
if s[0]:
    ans = c[min(n-1,k*2)]
    for i in range(2,len(c),2):
        if k*2+i>=len(c):
            break
        else:
            ans = max(ans,c[i+k*2]-c[i-1])
else:
    ans = c[min(n-1,k*2-1)]
    for i in range(2,len(c),2):
        if k*2+i>=len(c):
            break
        else:
            ans = max(ans,c[i+k*2]-c[i-2])
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline

def main():
    N, K = [int(x) for x in input().split()]
    S = input().strip()

    count, right, ans = 0, 1, 0
    if S[0] == '0':
        count += 1
    for i, v in enumerate(S):
        if i != 0 and S[i - 1] == '0' and S[i] == '1':
            count -= 1
        while count <= K and right < len(S) - 1:
            if S[right - 1] == '1' and S[right] == '0':
                count += 1
            right += 1
        ans = max(ans, right - i)

    print(ans)







if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline

def main():
    N, K = [int(x) for x in input().split()]
    S = input().strip()

    count, right, ans = 0, 0, 0
    if S[0] == '0':
        count += 1
    for i, v in enumerate(S):
        if i != 0 and S[i - 1] == '0' and S[i] == '1':
            count -= 1
        while count <= K and right < len(S) - 1:
            right += 1
            if right != len(S) - 1 and S[right] == '1' and S[right + 1] == '0':
                count += 1
            elif right == len(S) - 1 and S[right] == '0':
                count += 1
        ans = max(ans, right - i + 1)

    print(ans)







if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline

def main():
    N, K = [int(x) for x in input().split()]
    S = input().strip()

    count, right, ans = 0, 0, 0
    if S[0] == '0':
        count += 1
    for i, v in enumerate(S):
        if i != 0 and S[i - 1] == '0' and S[i] == '1':
            count -= 1
        while count <= K and right < len(S) - 1:
            right += 1
            if right != len(S) - 1 and S[right] == '1' and S[right + 1] == '0':
                count += 1
        ans = max(ans, right - i + 1)

    print(ans)







if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
P = input()

# 0又は1の連続する回数をリストに格納する
continuityList = []
statusList = []
cnt = 0
status = P[0]
for tmp in P:
    if tmp == status:
        cnt += 1
    else:
        continuityList.append(cnt)
        statusList.append(status)
        cnt = 1
        if status == "0":
            status = "1"
        else:
            status = "0"
continuityList.append(cnt)
statusList.append(status)
# print(continuityList)

# 先頭から順番に反転した時の連続する値を取得する
startValue = 0
ans = 0
if P[0] == "1":
    for tmp in continuityList[:K*2+1]:
        startValue += tmp

    ans = startValue
    for i, tmp in enumerate(continuityList[K*2+1:]):
        startValue = startValue + tmp - continuityList[i]
        ans = max(ans, startValue)

else:
    for tmp in continuityList[:K*2]:
        startValue += tmp

    ans = startValue
    for i, tmp in enumerate(continuityList[K*2+1:]):
        startValue = startValue + tmp - continuityList[i]
        if i == 0:
            startValue+=continuityList[K*2+1]
        ans = max(ans, startValue)


print(ans)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
P = input()

# 0又は1の連続する回数をリストに格納する
continuityList = []
cnt = 0
status = P[0]
for tmp in P:
    if tmp == status:
        cnt += 1
    else:
        continuityList.append(cnt)
        cnt = 1
        if status == "0":
            status = "1"
        else:
            status = "0"
continuityList.append(cnt)
# print(continuityList)

# 先頭から順番に反転した時の連続する値を取得する
startValue = 0
startIndex = 0
if P[0] == "1":
    startIndex = K*2+1
    for tmp in continuityList[:K*2+1]:
        startValue += tmp
else:
    startIndex = K*2
    for tmp in continuityList[:K*2]:
        startValue += tmp
# print(startValue)

ans = startValue
for i, tmp in enumerate(continuityList[startIndex:]):
    startValue = startValue + tmp - continuityList[i]
    ans = max(ans, startValue)

print(ans)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline

def main():
    N, K = [int(x) for x in input().split()]
    S = input().strip()
    ans = []
    prev = '0'
    if S[0] == '1':
        ans.append(0)
        prev = '1'

    count = 0
    for s in S:
        if s == prev:
            count += 1
        else:
            prev = s
            ans.append(count)
            count = 1
    ans.append(count)

    ans2 = 0
    tmp = 0
    count = 0
    r = 0
    l = 0
    if ans[0] == 0:
        l = 2
    for i, v in enumerate(ans):
        r = i
        if v == 0:
            continue
        tmp += v
        if count == K:
            break
        if not i & 1:
            count += 1

    ans2 = max(tmp, ans2)

    while r <= len(ans) - 3:
        if l == 0:
            tmp -= ans[0]
        else:
            tmp -= ans[l]
            tmp -= ans[l - 1]

        l += 2
        r += 2
        tmp += ans[r]
        tmp += ans[r - 1]

        ans2 = max(tmp, ans2)

    print(ans2)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

ans=0
history=[0]*(2*K+1)
sum_recent=0
idx=0
c=1
for i in range(1,N+1):
    #print(ans,sum_recent,history,idx,c)
    if i<N and S[i]==S[i-1]:
        c+=1
        continue
    sum_recent=sum_recent+c-history[idx]
    history[idx]=c
    idx=(idx+1)%(2*K+1)
    c=1
    if S[i-1]=='1':
        ans=max(ans,sum_recent)

print(ans)

Submission status: Wrong Answer

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

ans=0
pre1=[0]*K
pre0=[0]*K
idx=0
c=1
for i in range(1,N+1):
    if i<N and S[i]==S[i-1]:
        c+=1
        continue
    if S[i-1]=='1':
        ans=max(ans,sum(pre1)+sum(pre0)+c)
        pre1[idx]=c
        idx=(idx+1)%K
    else:
        pre0[idx]=c
    c=1

print(ans)

Submission status: Wrong Answer

Solution code:
from itertools import accumulate

def run_length(data, init):
    tmp = init
    cnt = 0
    length = []
    for c in data:
        if tmp != c:
            length.append(cnt)
            cnt = 0
        tmp = c
        cnt += 1
    length.append(cnt)
    return length

n,k = map(int, input().split())
s = input()
length = run_length(s, '')
if s[0] == '0':
    acc = [0] + list(accumulate(length))
else :
    acc = list(accumulate(length))
acc.append(acc[-1])

ans = acc[0]
if k * 2 + 1 > n:
    k = n // 2
for i in range(0, n,2):
    if i + k * 2 + 1 >= len(acc):
        break
    if ans < acc[i + k * 2 + 1] - acc[i]:
        ans = acc[i + k * 2 + 1] - acc[i]
print(ans)

Submission status: Wrong Answer

Solution code:
# 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)

#lは1から始まることが前提
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)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
sl = list(input())

l = []
l.append(0)
ans = 0
before = ""
count = 0
p = 2*k + 1
if sl[0] == "1":
   p = 2*k + 2
for idx,s in enumerate(sl):
   if s != before:
      before = s
      if idx > 0:
         l.append(idx)
         if len(l) == p:
            ans = max(l[-1] - l[0], ans)
            l.pop(0)
            l.pop(0)
l.append(n)
ans = max(l[-1] - l[0], ans)
print(ans)

Submission status: Wrong Answer

Solution code:
N,K = map(int, input().split())
S = str(input())

S_list = []
before = S[0]
num = 0
for s in S:
    if s == before:
        num += 1
    else:
        before = s
        S_list.append(num)
        num = 1
S_list.append(num)

ans = 0
a = 0
if S[0] == '1':
    a = 1
for i in range(0,len(S_list),2*K+a):
    num = min(len(S_list),i+2*K+a)
    ans = max(ans, sum(S_list[i:num]))

a = 0
if S[N-1] == '1':
    a = 1
num = min(len(S_list),2*K+a)
ans = max(ans, sum(S_list[-num:]))

print(ans)

Submission status: Wrong Answer

Solution code:
from itertools import groupby, accumulate
from operator import sub
N, K = map(int, input().split())
S = input()
A = list(map(lambda p: (p[0], len(list(p[1]))), groupby(S, lambda s: s=="1")))
M = len([() for t,_ in A if not t])
B = [0] + list(accumulate(l for _,l in A))
print(max(map(sub, B[K * 2 + int(A[0][0])::2], B[::2])) if M > K else len(S))

Submission status: Wrong Answer

Solution code:

import numpy as np
import sys
sys.setrecursionlimit(100000)

directions = np.array([[1, 0], [0, 1], [-1, 0], [0, -1]])


def acinput():
    return list(map(int, input().split(" ")))


sys.setrecursionlimit(100000)


directions = [[1, 0], [0, 1], [-1, 0], [0, -1]]
directions = list(map(np.array, directions))


def acinput():
    return list(map(int, input().split(" ")))


N, K = acinput()
s = list(map(int, list(input())))

nums = [1]
for i in range(1, N):
    if s[i] == s[i-1]:
        nums[-1] += 1
    else:
        nums.append(1)


nums_cs = np.cumsum(nums)

if len(nums_cs) > 1:
       res = nums_cs[min(len(nums_cs)-1,2*K-1)]
else:
    res=nums_cs[0]

#print(res)
if s[0]==1:
    for i in range(1,len(nums_cs)-2*K-1):
        if i%2==0:
            res=max(res,nums_cs[i+2*K+1]-nums_cs[i-1])
else:    

    for i in range(1,len(nums_cs)-2*K-1):
        if i%2==1:
            res=max(res,nums_cs[i+2*K+1]-nums_cs[i])


print(res)

Submission status: Wrong Answer

Solution code:
from collections import Counter,defaultdict,deque
import sys
from itertools import permutations, combinations
from heapq import heappop, heappush
# input = sys.stdin.readline
sys.setrecursionlimit(10**8)
mod = 10**9+7
    
def inp(): # n=1
    return int(input())
def inpm(): # x=1,y=2
    return map(int,input().split())
def inpl(): # a=[1,2,3,4,5,...,n]
    return list(map(int, input().split()))
def inpls(): # a=['1','2','3',...,'n']
    return list(input().split())
def inplm(n): # x=[] 複数行
    return list(int(input()) for _ in range(n))
def inpll(n): # [[1,1,1,1],[2,2,2,2],[3,3,3,3]]
    return sorted([list(map(int, input().split())) for _ in range(n)])


def main():
    n,k = inpm()
    s = list(input())
    cnt = []
    be = ''
    c = 0
    for i in range(n):
        if i == 0:
            be = s[i]
            c = 1                        
        elif s[i] != be:
            if be == '0':
                cnt.append((c,0))
            else:
                cnt.append((c,1))
            be = s[i]
            c = 1
        else:
            c += 1
    cnt.append((c,int(be)))
    ans = []
    length = len(cnt)
    if length//2 + (1-cnt[0][1]) <= k:
        print(n)
        return
    l = 0
    r = 2*k + cnt[0][1]
    s = 0
    for i in range(l,r):
        s += cnt[i][0]
    ans.append(s)
    l += 1
    key = 1
    while r < length:
        if cnt[l][1] == 0:
            l += 1
            r += 1
        else:
            if l-2 >= 0:
                s -= cnt[l-2][0]
            s -= cnt[l-1][0]
            if key:
                s += cnt[r-1][0]
            s += cnt[r][0]
            ans.append(s)
            l += 2
            r += 2
            if r == length:
                r -= 1
                key = 0
    print(max(ans))

if __name__ == "__main__":
    main()