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 = list(map(int, input().split()))
S = input()

def search(first, count):
  change = 0
  past = S[first]
  for i in range(first, len(S)):
    now = S[i]
    if past != now:
      change += 1
      if change == count*2:
        for j in range(i, len(S)):
          if S[first] != S[j]:
            return j-first
        return len(S)-first
    past = S[i]
  return len(S)-first

max_len = 0
past = ""
for i in range(len(S)):
  now = S[i]
  if past != now:
    #print(i, S[i], search(i, K))
    max_len = max([search(i, K), max_len])
  past = S[i] 
  
print(max_len)

Submission status: Wrong Answer

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

def search(first, count):
  change = 0
  past = S[first]
  for i in range(first, len(S)):
    now = S[i]
    if past != now:
      change += 1
      if change == count*2:
        for j in range(i, len(S)):
          if S[first] != S[j]:
            return j-first
        return len(S)-first
    past = S[i]
  return len(S)-first

max_len = 0
past = ""
for i in range(len(S)):
  now = S[i]
  if past != now:
    #print(i, S[i], search(i, K))
    max_len = max([search(i, K), max_len])
  past = S[i] 
  

#print(N, K, S)
print(max_len)

Submission status: Wrong Answer

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

def search(first, count):
  change = 0
  past = S[first]
  for i in range(first, len(S)):
    now = S[i]
    if past != now:
      change += 1
      if change == count*2:
        for j in range(i-1, len(S)):
          if S[first] != S[j]:
            return j-first
    past = S[i] 
  return len(S)-first

max_len = 0
past = ""
for i in range(len(S)):
  now = S[i]
  if past != now:
    #print(i, S[i], search(i, K))
    max_len = max([search(i, K), max_len])
  past = S[i] 
  

#print(N, K, S)
print(max_len)

Submission status: Wrong Answer

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

def search(first, count):
  change = 0
  past = S[first]
  for i in range(first, len(S)):
    now = S[i]
    if past != now:
      change += 1
      if change == count*2:
        for j in range(i+1, len(S)):
          if S[first] != S[j]:
            return j-first
    past = S[i] 
  return len(S)-first

max_len = 0
past = ""
for i in range(len(S)):
  now = S[i]
  if past != now:
    #print(i, S[i], search(i, K))
    max_len = max([search(i, K), max_len])
  past = S[i] 
  

#print(N, K, S)
print(max_len)

Submission status: Wrong Answer

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

def search(first, count):
  change = 0
  past = S[first]
  for i in range(first, len(S)):
    now = S[i]
    if past != now:
      change += 1
      if change == count*2:
        for j in range(i, len(S)):
          if S[first] != S[j]:
            return j-first
    past = S[i] 
  return len(S)-first

max_len = 0
past = ""
for i in range(len(S)):
  now = S[i]
  if past != now:
    #print(i, S[i], search(i, K))
    max_len = max([search(i, K), max_len])
  past = S[i] 
  

#print(N, K, S)
print(max_len)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
s = input().rstrip()
ans = 1
i = 1
left = 0
right = 1
while i < n:
    if k > 0:
        pos = s.find('01', i)
        if pos == -1:
            i = n
            right = n - 1
        else:
            pos = s.find('10', pos + 1)
            if pos == -1:
                i = n
                right = n - 1
            else:
                right = pos
                i = pos + 1
        k = k - 1
        ans = max(ans, right - left + 1)
        #print(ans)
    else:
        pos = s.find('01', left)
        left = pos + 1
        k = k + 1

print(ans)

Submission status: Wrong Answer

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

l_switch = []
l_ans = []

#print(S)


#N = 5
#K = 1
#S = [1,1,1,0,1,0,1,0,1,1,0,0,1,1]
#S = [0,0,0,1,0,1,0,1,0,0,1,1,0,0]
#S = [0,0,0,1,0]

if not 0 in S:
    l_ans.extend([1])

if S[0] == 1:
    S.insert(0,0)
elif S[0] == 0:
    S.insert(0,1)
if S[-1] == 1:
    S.extend([0])
elif S[-1] == 0:
    S.extend([1])

#print(S)



for i in range(1,len(S)):
    if S[i-1] == 0 and S[i] == 1:
        l_switch.extend([i])
    elif S[i-1] == 1 and S[i] == 0:
        l_switch.extend([i])
#print(l_switch)
min = min(2*K+1,len(l_switch))
if S[1] == 1:

    for j in range(0,len(l_switch)-(min),2):
        #print("J: " + str(l_switch[j]),"   2*K+1+j: " + str(l_switch[2*K+1+j]))
        #print(l_switch[2*K+1+j]-l_switch[j])
        l_ans.extend([l_switch[min+j]-l_switch[j]])

if S[1] == 0:
    #print("J: " + str(l_switch[0]),"   2*K+j: " + str(l_switch[2*K+0]))
    #print(l_switch[2*K+0]-l_switch[0])
    l_ans.extend([l_switch[min-1]-l_switch[0]])
    for j in range(1,len(l_switch)-(min),2):
        #print("J: " + str(l_switch[j]),"   2*K+1+j: " + str(l_switch[2*K+1+j]))
        #   sprint(l_switch[2*K+1+j]-l_switch[j])
        l_ans.extend([l_switch[min+j]-l_switch[j]])



print(max(l_ans))

Submission status: Wrong Answer

Solution code:
from functools import reduce
from operator import add

n, k = map(int, input().split())
s_law = input()

current_status = "1"
s = [0]
for h in s_law:
    if h == current_status:
        s[-1] += 1
    else:
        s.append(1)
        current_status = h

join_num = k * 2 + 1

max_rev = 0
for i, state in enumerate(s):
    max_rev = max(reduce(add, s[i:i + join_num]), max_rev)

print(max_rev)

Submission status: Wrong Answer

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

L = [0]
res = '1'
#print(S)
for index,char in enumerate(S):
    #计算重复次数
    if char != res:
        L.append(index)           
    res = char
L.append(index)
maxnum = 0
count = 0 
#print(L)
if L[-1]-L[-2] == 0:
    for m in range(len(L)-2,-1,-1):
        if m < 2 * K + 1 :
            maxnum = max(maxnum,L[m])
            break
        else: 
            maxnum = max(maxnum,L[m]-L[m-2*K-1])
else:
    for m in range(len(L)-1,-1,-1):
        if m < 2*K +1:
            maxnum = max(maxnum,L[m])
            break
        else:
            if count % 2 == 0:
                maxnum = max(maxnum,L[m]-L[m-2*K])
            else:
                maxnum = max(maxnum,L[m]-L[m-2*K-1])
            count += 1 
print(maxnum)

Submission status: Wrong Answer

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

L = [0]
res = '1'
print(S)
for index,char in enumerate(S):
    #计算重复次数
    if char != res:
        L.append(index)           
    res = char
L.append(index+1)
maxnum = 0
print(L)
if L[-1]-L[-2] == 1:
    for m in range(len(L)-1,-1,-1):
        if m < 3 * K:
            maxnum = max(maxnum,L[m])
            break
        else:
            maxnum = max(maxnum,L[m]-L[m-2*K-1])
print(maxnum)

Submission status: Wrong Answer

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

L = [0]
res = '1'
print(S)
for index,char in enumerate(S):
    #计算重复次数
    if char != res:
        L.append(index)           
    res = char
L.append(index)
maxnum = 0
print(L)
for m in range(len(L)-1,-1,-1):
    if m < 3 * K:
        maxnum = max(maxnum,L[m])
        break
    else:
        maxnum = max(maxnum,L[m]-L[m-3*K+1])
print(maxnum)

Submission status: Wrong Answer

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

L = [0]
res = '1'
#print(S)
for index,char in enumerate(S):
    #计算重复次数
    if char != res:
        L.append(index)           
    res = char
L.append(index+1)
maxnum = 0
#print(L)
for m in range(len(L)-2,-1,-1):
    if m < 3 * K:
        maxnum = max(maxnum,L[m])
        break
    else:
        maxnum = max(maxnum,L[m]-L[m-3*K+1])
print(maxnum)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
s=list(input())
li=[]
if s[0]=="0":
  a=0
  s.insert(0,"p")
  len_s=len(s)
  for i in range (1,len_s):
    if s[i-1]==s[i]:
      a=a+1
    else:
      li.append(a)
      a=1
  li.append(a)
else:
  a=1
  len_s=len(s)
  for j in range (1,len_s):
    if s[j-1]==s[j]:
      a=a+1
    else:
      li.append(a)
      a=1
  li.append(a)
len_li=len(li)
lis=[0]
t=0
for l in range (0,len_li):
  t=t+li[l]
  lis.append(t)
if 2*k+1>=len_li:
  print(n)
elif k==0:
  import math
  o=math.ceil(len_li/2)
  le=[]
  for f in range (0,o):
    le.append(li[2*f])
    le.sort()
  print(le[len(le)-1])
else:
  import math
  u=0
  r=len(li)-2*k-2
  r=math.floor(r/2)
  for m in range (0,r+1):
    d=lis[2*m+2*k+1]-lis[2*m]
    if u<=d:
      u=d
  print(u)

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
S=input()
po=[0]
p=0
ans=0
left,right=1,0
q=0
for i in range(1,N):
	p=S[i-1]+S[i]
	if (p=='10')|(p=='01'):
		po.append(i)
po.append(len(S))
right=2*K+1
for i in range(len(po)-K+2):
	if K*2>=len(po):
		ans=po[-1]
		break
	elif (S[0]=='0')&(left<=1):
		q=po[2*K]
		right=2*K
	elif right<len(po):
		q=po[right]-po[left]
		left+=2
		right+=2
	elif S[-1]=='0':
		right=len(po)-1
		left=right-2*K+1
		q=po[right]-po[left]
	else:
		right=len(po)-1
		left=right-2*K-1
		q=po[right]-po[left]
	ans=max(q,ans)
print(ans)

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
S=input()
po=[0]
p=0
ans=0
left,right=1,0
q=0
for i in range(1,N):
	p=S[i-1]+S[i]
	if (p=='10')|(p=='01'):
		po.append(i)
po.append(len(S))
right=2*K+1
print(po)
for i in range(len(po)-K+2):
	if K*2>=len(po):
		ans=po[-1]
		break
	elif (S[0]=='0')&(left<=1):
		q=po[2*K]
		right=2*K
	elif right<len(po):
		q=po[right]-po[left]
		left+=2
		right+=2
	elif S[-1]=='0':
		right=len(po)-1
		left=right-2*K+1
		q=po[right]-po[left]
	else:
		right=len(po)-1
		left=right-2*K-1
		q=po[right]-po[left]
	ans=max(q,ans)
print(ans)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
s=list(input())
li=[]
if s[0]=="0":
  a=0
  s.insert(0,"p")
  len_s=len(s)
  for i in range (1,len_s):
    if s[i-1]==s[i]:
      a=a+1
    else:
      li.append(a)
      a=1
  li.append(a)
else:
  a=1
  len_s=len(s)
  for j in range (1,len_s):
    if s[j-1]==s[j]:
      a=a+1
    else:
      li.append(a)
      a=1
  li.append(a)
len_li=len(li)
lis=[0]
t=0
for l in range (0,len_li):
  t=t+li[l]
  lis.append(t)
if 2*k+1>=len_li:
  print(n)
else:
  import math
  u=0
  r=len(li)-2*k-2
  r=math.floor(r/2)
  for m in range (0,r+1):
    d=lis[2*m+2*k+1]-lis[2*m]
    if u<=d:
      u=d
  print(u)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
s=list(input())
li=[]
if s[0]=="0":
  a=0
  s.insert(0,"p")
  len_s=len(s)
  for i in range (1,len_s):
    if s[i-1]==s[i]:
      a=a+1
    else:
      li.append(a)
      a=1
  li.append(a)
else:
  a=1
  len_s=len(s)
  for j in range (1,len_s):
    if s[j-1]==s[j]:
      a=a+1
    else:
      li.append(a)
      a=1
  li.append(a)
len_li=len(li)
lis=[0]
t=0
for l in range (0,len_li):
  t=t+li[l]
  lis.append(t)
if 2*k+1>=len_li:
  print(len(s))
else:
  import math
  u=0
  r=len(li)-2*k-2
  r=math.floor(r/2)
  for m in range (0,r+1):
    d=lis[2*m+2*k+1]-lis[2*m]
    if u<=d:
      u=d
  print(u)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
s=list(input())
li=[]
if s[0]=="0":
  a=0
  s.insert(0,"p")
  len_s=len(s)
  for i in range (1,len_s):
    if s[i-1]==s[i]:
      a=a+1
    else:
      li.append(a)
      a=1
  li.append(a)
else:
  a=1
  len_s=len(s)
  for j in range (1,len_s):
    if s[j-1]==s[j]:
      a=a+1
    else:
      li.append(a)
      a=1
  li.append(a)
len_li=len(li)
lis=[0]
t=0
for l in range (0,len_li):
  t=t+li[l]
  lis.append(t)
if 2*k+1>=len_li:
  print(n)
else:
  import math
  u=0
  r=len(lis)-2*k-2
  r=math.floor(r/2)
  for m in range (0,r+1):
    d=lis[2*m+2*k+1]-lis[2*m]
    if u<=d:
      u=d
  print(d)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
s=list(input())
li=[]
if s[0]=="0":
  a=0
  s.insert(0,"p")
  len_s=len(s)
  for i in range (1,len_s):
    if s[i-1]==s[i]:
      a=a+1
    else:
      li.append(a)
      a=1
  li.append(a)
else:
  a=1
  len_s=len(s)
  for j in range (1,len_s):
    if s[j-1]==s[j]:
      a=a+1
    else:
      li.append(a)
      a=1
  li.append(a)
len_li=len(li)
lis=[0]
t=0
for l in range (0,len_li):
  t=t+li[l]
  lis.append(t)
print(li)
print(lis)
if 2*k+1>=len_li:
  print(n)
else:
  import math
  u=0
  r=len(lis)-2*k-2
  r=math.floor(r/2)
  for m in range (0,r+1):
    d=lis[2*m+2*k+1]-lis[2*m]
    if u<=d:
      u=d
  print(d)

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
S=input()
po=[]
p=0
ans=0
left,right=0,0
q=0
for i in range(1,N):
	p=S[i-1]+S[i]
	if (p=='10')|(p=='01'):
		po.append(i)
po.append(len(S))
right=2*K
for i in range(len(po)-K+2):
	if K*2>=len(po):
		ans=po[-1]
		break
	elif (S[0]=='0')&(left<1)&(2*K-1<=len(po)):
		right=2*K-1
		q=po[right]
		left+=1
	elif (S[0]=='0')&(left<1)&(2*K-1>len(po)):
		q=po[-1]
		break
	elif right<len(po):
		q=po[right]-po[left]+1
		left+=2
		right+=2
	else:
		right=len(po)-1
		left=right-2*K+1
		q=po[right]-po[left]+1
		break
	ans=max(q,ans)
print(ans)

Submission status: Wrong Answer

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

# 累積和で実装

now = 1
cnt = 0
nums = []
for i in range(N):
    if int(S[i]) == now:
        cnt += 1
    else:
        nums.append(cnt)
        cnt = 1
        now = 1 - now
nums.append(cnt)
if int(S[-1]) == 0:
    nums.append(0)

# print(nums)

cumulative_sum = []
M = len(nums)
sum = 0
for i in range(M):
    sum += nums[i]
    cumulative_sum.append(sum)

# print(cumulative_sum)

ans = 0
start = 0
final = 2 * K
answers = []

# M = len(nums) = len(cumulative_sum)
if final <= M-1:
    while(final <= M-1):
        if start == 0:
            answers.append(cumulative_sum[final])
        else:
            ans = cumulative_sum[final] - cumulative_sum[start-1]
            answers.append(ans)

        # print(start, final, ans)
        start += 2
        final += 2
    print(max(answers))
else:
    print(cumulative_sum[0])

# print(answers)

Submission status: Wrong Answer

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

# 累積和で実装

now = 1
cnt = 0
nums = []
for i in range(N):
    if int(S[i]) == now:
        cnt += 1
    else:
        nums.append(cnt)
        cnt = 1
        now = 1 - now
nums.append(cnt)
if int(S[-1]) == 0:
    nums.append(0)

print(nums)

cumulative_sum = []
M = len(nums)
sum = 0
for i in range(M):
    sum += nums[i]
    cumulative_sum.append(sum)

print(cumulative_sum)

ans = 0
start = 0
final = 2 * K
answers = []

# M = len(nums) = len(cumulative_sum)
if final <= M-1:
    while(final <= M-1):
        if start == 0:
            answers.append(cumulative_sum[final])
        else:
            ans = cumulative_sum[final] - cumulative_sum[start-1]
            answers.append(ans)

        print(start, final, ans)
        start += 2
        final += 2
    print(max(answers))
else:
    print(cumulative_sum[0])

print(answers)

Submission status: Wrong Answer

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

S=input()

left = 0
right = 0

max_len = N

count0 =0
henka = 0
first = int(S[0])
for i in range(1,N):
    if S[i-1]!=S[i]:
        henka +=1
    if S[i]=='0'and henka == (2*K+first):
        right = i-1
        max_len = i
        break

    

# print(max_len)  
right_count = 3
left_count = 3

while(1):
    # print(left,right)
    if right_count <= 2: break
    if max_len == N:break
    #３変化する前までrightを動かす
    right_count = 0
    while(1):
        if right+1 >= N: break
        if S[right+1] !=  S[right]:
            right_count += 1
        if right_count >2 : break
        right += 1
    left_count = 0
    #leftを動かす
     
    if S[left] =='0':
        left_count = 1
    else:
        left_count = 0
    while(1):
        if right_count == 0:break
        if S[left+1] !=  S[left]:
            left_count += 1
        if left_count >2 : break
        left += 1
    

    
    max_len = max(right-left+1,max_len)

print(max_len)

Submission status: Wrong Answer

Solution code:
def ret_arr(N, K, S):
  a0 = str(S).split('1')
  remove_specified_values(a0, '')
  a1 = str(S).split('0')
  remove_specified_values(a1, '')

  a = [2] * (len(a1) + len(a0))
  if int(S[0]) == 0:
    for i in range(0, len(a0)):
      if i > (len(a0) - 2) and int(S[-1]) == 0:
        a[i * 2] = a0[i]
      else:
        a[i * 2] = a0[i]
        a[i * 2 + 1] = a1[i]
  else:
    for i in range(0, len(a1)):
      if i > (len(a1) - 2) and int(S[-1]) == 1:
        a[i * 2] = a1[i]
      else:
        a[i * 2] = a1[i]
        a[i * 2 + 1] = a0[i]
  return a


def remove_specified_values(arr, value):
  while value in arr:
    arr.remove(value)


def ret_arr_len(arr):
  ret_a = []
  for i in arr:
    ret_a.append(len(i))
  return ret_a


def count(arr, K):
  k = K * 2 + 1
  c_a = []
  for i in range(0, len(arr) - (k - 1)):
    c = 0
    for _k in range(0, k):
      c = c + arr[(i + _k)]
      c_a.append(c)
  try:
    m = max(c_a)
  except Exception as e:
    m = 0
  return m


def main(N, K, S):
  a = ret_arr(N, K, S)
  len_a = ret_arr_len(a)
  c_a = []
  for i in range(0, K + 1):
    c = count(len_a, i)
    c_a.append(c)
  return max(c_a)
  
if __name__ == '__main__':
  input_str=input()
  N=int(input_str.split()[0])
  K=int(input_str.split()[1])
  str_teams = str(input())
  print(main(N, K, str_teams))

Submission status: Wrong Answer

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

S=input()

#0の位置がK回きたら次の1がきた時点まででカウント終了
#そこからまたカウントスタートするけど前回の終了いちを忘れるな

# max_len = 0
# now_len = 0
# count0 = 0
# last0_ind = -1
# for i in range(N):
#     now_len += 1
#     if S[i]==1:
#         continue
#     else:
#         count0+=1
#     if count0 == K:
#         last0_ind = i
#     elif count0 == K+1:
#         max_len = max(max_len,now_len-1)
#         now_len = 
    
# max_len = max(max_len,now_len)

ind_0 = []
max_len = 0
left = 0
right = 0
flag = False
left_flag = 1
if S.count('0')==0 or S.count('0')==N:
    max_len = N
    flag = True



for i in range(1,len(S)):
    if(flag==True): 
        max_len = N
        break
    if S[i]=='1':
            continue
    if i == len(S)-1:
        if S[i]=='1':
            continue
        else:
            right = i
            if (left_flag==0):
                ind_0.append([right,right])
            else:
                ind_0.append([left,right])

            continue
    if S[i]=='0' and S[i-1]!='0' and S[i+1]!='0':
        ind_0.append([i,i])
    elif S[i-1]!='0' and S[i+1]=='0':
        left = i
        left_flag = 1
    elif S[i-1]=='0' and S[i+1]!='0':
        right = i
        ind_0.append([left,right])
        left_flag = 0
    else:
        continue


# print(ind_0)



#Kがlenより大きい時の例外

# if 
for i in range(len(ind_0)):
    # print(i,max_len)
    if(flag==True): break
    if K>len(ind_0)-1:
        max_len = N
        break
    elif i+K>len(ind_0)-1:
        # print('1',max((N-ind_0[i-1][1]-1),max_len))
        max_len =max((N-ind_0[i-1][1]-1),max_len)
        break
    elif i == 0:
        # print('2',ind_0[K][0])
        max_len = ind_0[K][0]
    else:
        # print('3',abs(ind_0[i-1][1]+1-ind_0[i+K][0]))
        max_len = max(ind_0[i+K][0]+1-ind_0[i-1][1],max_len)

print(max_len)

Submission status: Wrong Answer

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

from itertools import groupby, accumulate

def gen(s):
  if s[:1] == '0':
  	yield 0
  for ke, g in groupby(s):
    yield len(list(g))
  if s[-1:] == '0':
    yield 0


mc = 0
acc = list(accumulate(gen(s)))
r = 2 * k + 1
if len(acc) < r:
  mc = n
else:
  for i in range(1, len(acc)-r, 2):
    mc = max(mc, acc[i+r] - acc[i])
print(mc)

Submission status: Wrong Answer

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

c = [0]
import itertools
for _, g in itertools.groupby(s):
  c.append(c[-1] + len(list(g)))
  
c = c[1:]
if s[0] == "0":
  c = [0] + c
else:
  c = [0,0] + c
if s[-1] == "1":
  c = c + [0]
else:
  c = c + [0,0]

ans = 1
for i in range(0,len(c), 2):
  start = max(0, i-1)
  end = min(i+2*k, len(c)-1)
  ans = max(ans, c[end]-c[start])

print(ans)

Submission status: Wrong Answer

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

c = [0]
import itertools
for _, g in itertools.groupby(s):
  c.append(c[-1] + len(list(g)))
  
c = c[1:]
if s[0] == "0":
  c = [0] + c
if s[-1] == "1":
  c = c + [0]

ans = 1
for i in range(0,len(c), 2):
  start = max(0, i-1)
  end = min(i+2*k, len(c)-1)
  ans = max(ans, c[end]-c[start])

print(ans)

Submission status: Wrong Answer

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

from collections import deque
from itertools import groupby

def gen(s):
  if s[:1] == '0':
  	yield 0
  for ke, g in groupby(s):
    yield len(list(g))
  if s[-1:] == '0':
    yield 0


mc = 0
ddd = deque()
r = 2 * k + 1
for p in gen(s):
  ddd.append(p)
  cd = len(ddd)
  if cd % 2 and cd > r:
    ddd.popleft()
    ddd.popleft()
  mc = max(mc, sum(ddd))
print(mc)

Submission status: Wrong Answer

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

c = [0]
import itertools
for _, g in itertools.groupby(s):
  ci = len(list(g))
  c.append(c[-1] + ci)

if s[0] == "0":
  c = [0] + c
if s[-1] == "1":
  c = c + [0]

ans = 0

for i in range(0,len(c), 2):
  end = min(i+2*k+1, len(c)-1)
  ans = max(ans, c[end]-c[i])

print(ans)

Submission status: Wrong Answer

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

from collections import deque
from itertools import groupby

def gen(s):
  if s[:1] == '0':
  	yield 0
  for ke, g in groupby(s):
    yield len(list(g))
  if s[-1:] == '0':
    yield 0


mc = 0
ddd = deque()
r = 2 * k + 1
for p in gen(s):
  ddd.append(p)
  cd = len(ddd)
  if cd % 2 and cd > r:
    ddd.popleft()
    ddd.popleft()
    mc = max(mc, sum(ddd))
print(mc)

Submission status: Wrong Answer

Solution code:
import re

input_str=input()
N=int(input_str.split()[0])
K=int(input_str.split()[1])
str_teams = str(input())
sub_str_teams = str_teams

def next_check_val(check_val):
  if check_val == '1':
    return '0'
  else:
    return '1'

cur_check_val = str_teams[0]
teams_group = []
teams_state = ''

## 直立/逆立ちのグループに分ける処理
for i in range(N):
  person = str_teams[i]
  if str_teams[i] == cur_check_val:
    teams_state = teams_state + str_teams[i]
  else:
    teams_group.append(teams_state)
    teams_state = str_teams[i]
    cur_check_val = next_check_val(cur_check_val)

teams_group.append(teams_state)
#print(teams_group)

## 0グループの個数がK以下の場合
## 人数を回答にしてexitする
count_0 = 0
for i in range(len(teams_group)):
  if teams_group[i].startswith('0'):
    count_0 += 1
if count_0 <= K:
  print(N)
  exit()

sum_handstand = [] ## 累積和を格納するリスト
handstands = 0     ## 各累積和を一時的に格納する変数


## 最初の累積和を計算する処理
## 先頭が0か1かによって処理を分割している
if ('0' in teams_group[0]):
  for k in range(K*2):
    handstands += len(teams_group[k])
else:
  for k in range(K*2+1):
    handstands += len(teams_group[k])

sum_handstand.append(handstands)

## 途中の累積和を計算する処理
for i in range(1,len(teams_group)-(K*2)):
  ## 先頭が0か1かによって処理を分割している
  if ('0' in teams_group[i]):
    handstands = sum_handstand[i-1]
  else:
    handstands = sum_handstand[i-1] - (len(teams_group[i-1])+len(teams_group[i-2])) + (len(teams_group[i+2*K-1])+len(teams_group[i+2*K]))
  sum_handstand.append(handstands)

handstands = 0
## 最後の累積和を計算する処理
if ('0' in teams_group[-1]):
  for k in range(0,K*2):
    try:
      handstands += len(teams_group[len(teams_group)-2*K+k])
    except IndexError:
      pass
  sum_handstand.append(handstands)


#print(sum_handstand)
print(max(sum_handstand))

Submission status: Wrong Answer

Solution code:
import re

input_str=input()
N=int(input_str.split()[0])
K=int(input_str.split()[1])
str_teams = str(input())
sub_str_teams = str_teams

def next_check_val(check_val):
  if check_val == '1':
    return '0'
  else:
    return '1'

cur_check_val = str_teams[0]
teams_group = []
teams_state = ''

## 直立/逆立ちのグループに分ける処理
for i in range(N):
  person = str_teams[i]
  if str_teams[i] == cur_check_val:
    teams_state = teams_state + str_teams[i]
  else:
    teams_group.append(teams_state)
    teams_state = str_teams[i]
    cur_check_val = next_check_val(cur_check_val)

teams_group.append(teams_state)
#print(teams_group)

## 0グループの個数がK以下の場合
## 人数を回答にしてexitする
count_0 = 0
for i in range(len(teams_group)):
  if teams_group[i].startswith('0'):
    count_0 += 1
if count_0 <= K:
  print(N)
  exit()

sum_handstand = [] ## 累積和を格納するリスト
handstands = 0     ## 各累積和を一時的に格納する変数


## 最初の累積和を計算する処理
## 先頭が0か1かによって処理を分割している
if ('0' in teams_group[0]):
  for k in range(K*2):
    handstands += len(teams_group[k])
else:
  for k in range(K*2+1):
    handstands += len(teams_group[k])

sum_handstand.append(handstands)

## 途中の累積和を計算する処理
for i in range(1,len(teams_group)-(K*2)):
  ## 先頭が0か1かによって処理を分割している
  if ('0' in teams_group[i]):
    handstands = sum_handstand[i-1]
  else:
    handstands = sum_handstand[i-1] - (len(teams_group[i-1])+len(teams_group[i-2])) + (len(teams_group[i+K+1])+len(teams_group[i+K+2]))
  sum_handstand.append(handstands)

handstands = 0
## 最後の累積和を計算する処理
if ('0' in teams_group[-1]):
  for k in range(0,K*2):
    try:
      handstands += len(teams_group[len(teams_group)-2*K+k])
    except IndexError:
      pass
  sum_handstand.append(handstands)


print(max(sum_handstand))

Submission status: Wrong Answer

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

from itertools import groupby

def gen(s):
  if s[:1] == '0':
  	yield 0
  for ke, g in groupby(s):
    yield len(list(g))
  if s[-1:] == '0':
    yield 0


mc = 0
aaa = list(gen(s))
r = 2 * k + 1
if len(aaa) <= r:
  mc = sum(aaa)
else:
  for i in range(len(aaa) - r + 1):
    mc = max(mc, sum(aaa[i:i+r]))
print(mc)

Submission status: Wrong Answer

Solution code:
import re

input_str=input()
N=int(input_str.split()[0])
K=int(input_str.split()[1])
str_teams = str(input())
sub_str_teams = str_teams

def next_check_val(check_val):
  if check_val == '1':
    return '0'
  else:
    return '1'

cur_check_val = str_teams[0]
teams_group = []
teams_state = ''

## 直立/逆立ちのグループに分ける処理
for i in range(N):
  person = str_teams[i]
  if str_teams[i] == cur_check_val:
    teams_state = teams_state + str_teams[i]
  else:
    teams_group.append(teams_state)
    teams_state = str_teams[i]
    cur_check_val = next_check_val(cur_check_val)

teams_group.append(teams_state)
#print(teams_group)

## 0グループの個数がK以下の場合
## 人数を回答にしてexitする
count_0 = 0
for i in range(len(teams_group)):
  if teams_group[i].startswith('0'):
    count_0 += 1
if count_0 <= K:
  print(N)
  exit()

sum_handstand = [] ## 累積和を格納するリスト
handstands = 0     ## 各累積和を一時的に格納する変数


## 最初の累積和を計算する処理
## 先頭が0か1かによって処理を分割している
if ('0' in teams_group[0]):
  for k in range(K*2):
    handstands += len(teams_group[k])
else:
  for k in range(K*2+1):
    handstands += len(teams_group[k])

sum_handstand.append(handstands)

## 途中の累積和を計算する処理
for i in range(1,len(teams_group)-(K*2)):
  ## 先頭が0か1かによって処理を分割している
  if ('0' in teams_group[i]):
    #print(sum_handstand[i-1] , teams_group[i-1])
    handstands = sum_handstand[i-1] - len(teams_group[i-1])
  else:
    #print(sum_handstand[i-1] , teams_group[i-1] , teams_group[i+K+1] , teams_group[i+K+2])
    handstands = sum_handstand[i-1] - len(teams_group[i-1]) + (len(teams_group[i+K+1])+len(teams_group[i+K+2]))
  sum_handstand.append(handstands)

handstands = 0
## 最後の累積和を計算する処理
if ('0' in teams_group[-1]):
  for k in range(0,K*2):
    try:
      handstands += len(teams_group[len(teams_group)-2*K+k])
    except IndexError:
      pass
  sum_handstand.append(handstands)


print(max(sum_handstand))

Submission status: Wrong Answer

Solution code:
import re

input_str=input()
N=int(input_str.split()[0])
K=int(input_str.split()[1])
str_teams = str(input())
sub_str_teams = str_teams

def next_check_val(check_val):
  if check_val == '1':
    return '0'
  else:
    return '1'

cur_check_val = str_teams[0]
teams_group = []
teams_state = ''

## 直立/逆立ちのグループに分ける処理
for i in range(N):
  person = str_teams[i]
  if str_teams[i] == cur_check_val:
    teams_state = teams_state + str_teams[i]
  else:
    teams_group.append(teams_state)
    teams_state = str_teams[i]
    cur_check_val = next_check_val(cur_check_val)

teams_group.append(teams_state)
#print(teams_group)

## 0グループの個数がK以下の場合
## 人数を回答にしてexitする
count_0 = 0
for i in range(len(teams_group)):
  if teams_group[i].startswith('0'):
    count_0 += 1
if count_0 <= K:
  print(N)
  exit()

sum_handstand = [] ## 累積和を格納するリスト
handstands = 0     ## 各累積和を一時的に格納する変数

## 最初の累積和を計算する処理
## 先頭が0か1かによって処理を分割している
if ('0' in teams_group[0]):
  for k in range(K*2):
    handstands += len(teams_group[k])
else:
  for k in range(K*2+1):
    handstands += len(teams_group[k])

sum_handstand.append(handstands)

## 途中の累積和を計算する処理
for i in range(1,len(teams_group)-(K*2)):
  ## 先頭が0か1かによって処理を分割している
  if ('0' in teams_group[i]):
    #print(sum_handstand[i-1] , teams_group[i-1])
    handstands = sum_handstand[i-1] - len(teams_group[i-1])
  else:
    #print(sum_handstand[i-1] , teams_group[i-1] , teams_group[i+K+1] , teams_group[i+K+2])
    handstands = sum_handstand[i-1] - len(teams_group[i-1]) + (len(teams_group[i+K+1])+len(teams_group[i+K+2]))
  sum_handstand.append(handstands)

handstands = 0
## 最後の累積和を計算する処理
if ('0' in teams_group[-1]):
  for k in range(0,K*2):
    handstands += len(teams_group[len(teams_group)-2*K+k])
  sum_handstand.append(handstands)


print(max(sum_handstand))