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

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]):
#  print('start is 0')
  handstands=len(teams_group[0])
  k=1
  for k in range((K-1)*2+1):
    handstands += len(teams_group[k])
else:
#  print('start is 1')
  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+1):
#  print(sum_handstand[i-1] , len(teams_group[i-1]) , len(teams_group[i+K-1]))
  handstands = sum_handstand[i-1] - len(teams_group[i-1]) + len(teams_group[i+K-1])
  sum_handstand.append(handstands)

print(sum_handstand)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
s=input()
s+="2"
A=[]
B=[0]
C=[0]
a=0
b=0
ans=0

for i in range(n):
    if s[i]!=s[i+1]:
        b+=i-a+1
        A.append(i-a+1)
        B.append(b)
        a=i+1

if (len(A)-1)/2<k:
    ans=n
elif k%2==1:
    for i in range(len(A)-2*k):
        c=B[i+2*k]-B[i]
        if (i%2==1 and s[0]=="0") or (i%2==0 and s[0]=="1"):
            C.append(c)
        else:
            C.append(-c)
    ans=max(C)
elif k%2==0:
    for i in range(len(A)-2*k):
        c=B[i+2*k+1]-B[i]
        if (i%2==1 and s[0]=="0") or (i%2==0 and s[0]=="1"):
            C.append(c)
        else:
            C.append(-c)
        
    ans=max(C)

"""print(s)
print(A)
print(B)"""
print(ans)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
s=input()
s+="2"
A=[]
B=[0]
C=[0]
a=0
b=0
ans=0

for i in range(n):
    if s[i]!=s[i+1]:
        b+=i-a+1
        A.append(i-a+1)
        B.append(b)
        a=i+1

if (len(A)-1)/2<k:
    ans=n
elif k%2==1:
    for i in range(len(A)-2*k):
        c=B[i+2*k]-B[i]
        if (i%2==0 and s[0]=="0") or (i%2==1 and s[0]=="1"):
            C.append(c)
        else:
            C.append(-c)
    ans=max(C)
elif k%2==0:
    for i in range(len(A)-2*k):
        c=B[i+2*k+1]-B[i]
        if (i%2==0 and s[0]=="0") or (i%2==1 and s[0]=="1"):
            C.append(c)
        else:
            C.append(-c)
        
    ans=max(C)

"""print(s)
print(A)
print(B)"""
print(ans)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
s=input()
s+="2"
A=[]
B=[0]
C=[0]
a=0
b=0
ans=0

for i in range(n):
    if s[i]!=s[i+1]:
        b+=i-a+1
        A.append(i-a+1)
        B.append(b)
        a=i+1

if (len(A)-1)/2<k:
    ans=n
elif k%2==1:
    for i in range(len(A)-2*k):
        c=B[i+2*k]-B[i]
        C.append(c)
    ans=max(C)
elif k%2==0:
    for i in range(len(A)-2*k):
        c=B[i+2*k+1]-B[i]
        C.append(c)
    ans=max(C)

"""print(s)
print(A)
print(B)"""
print(ans)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
s=input()
s+="2"
A=[]
B=[0]
C=[0]
a=0
b=0
ans=0

for i in range(n):
    if s[i]!=s[i+1]:
        b+=i-a+1
        A.append(i-a+1)
        B.append(b)
        a=i+1

if (len(A)-1)/2<k:
    ans=n
elif k%2==1:
    for i in range(len(A)-2*k+1):
        c=B[i+2*k]-B[i]
        C.append(c)
    ans=max(C)
elif k%2==0:
    for i in range(len(A)-2*k):
        c=B[i+2*k+1]-B[i]
        C.append(c)
    ans=max(C)

"""print(s)
print(A)
print(B)"""
print(ans)

Submission status: Wrong Answer

Solution code:

_, k = map(int, input().split())
s = input()

a = []
cnt_one = 0
cnt_zero = 0

if s[0] == "0":
    a.append(0)

for i in s:
    if i == "1":
        cnt_one += + 1
        if cnt_zero != 0:
            a.append(cnt_zero)
            cnt_zero = 0
    else:
        cnt_zero += 1
        if cnt_one != 0:
            a.append(cnt_one)
            cnt_one = 0

a.append(max(cnt_zero, cnt_one))

if s[-1] == "0":
    a.append(0)

n = len(a)
right = 2 * k + 1
up = sum(a[:right])
max_up = up

for i in range((n - right) // 2):
    new_up = up + a[right + i * 2] + a[right + i * 2 + 1] - a[i * 2] - a[i * 2 + 1]
    max_up = max(up, new_up)
    up = new_up

print(max_up)

Submission status: Wrong Answer

Solution code:

_, k = map(int, input().split())
s = input()

a = []
cnt_one = 0
cnt_zero = 0

if s[0] == "0":
    a.append(0)

for i in s:
    if i == "1":
        cnt_one += + 1
        if cnt_zero != 0:
            a.append(cnt_zero)
            cnt_zero = 0
    else:
        cnt_zero += 1
        if cnt_one != 0:
            a.append(cnt_one)
            cnt_one = 0

a.append(max(cnt_zero, cnt_one))

if s[-1] == "0":
    a.append(0)

n = len(a)
right = 2 * k + 1
up = sum(a[:right])
max_up = up

for i in range((n - right) // 2):
    new_up = up + a[right + i * 2] + a[right + i * 2 + 1] - a[i * 2] - a[i * 2 + 1]
    max_up = max(up, new_up)
    print(new_up)
    up = new_up

print(max_up)

Submission status: Wrong Answer

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

l = [0]
ans = 0

for i in range(1, n):
  if s[i-1]=="1" and s[i]=="0":
    l.append(i)
  if s[i-1]=="0" and s[i]=="1":
    l.append(i)

for i, e in enumerate(l):
  if s[i]=="0":
    if i+2*k>=len(l):
      ans = max(ans, n-e)
    else:
      ans = max(ans, l[i+2*k]-e)
  else:
    if i+2*k+1>=len(l):
      ans = max(ans, n-e)
    else:
      ans = max(ans, l[i+2*k+1]-e)
print(ans)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
s = input()
a = ''
tmp = s[0]
count = 0
for i in s:
  if tmp == i:
    count += 1
  else:
    a += str(count)
    tmp = i
    count = 1
a += str(count)
ans = [0] * (len(a) - k - 2)
if k + 3 >= len(a):
  print(n)
else:
  for i in range(len(a) - k - 2):
    sum = 0
    for j in range(k + 3):
      sum += int(a[i + j])
    ans[i] = sum
  print(max(ans))

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
s = input()
S = [int(x) for x in list(str(s))]
S.append(2)

l = []
tmp = 1
p = S[0]

for i in range(1, N + 1):
    if p == S[i]:
        tmp += 1
    else:
        l.append(tmp)
        p = S[i]
        tmp = 1

i = 0
check = False

if 2 * K > len(l):
    print(N)
    exit()

if S[0] == 0:
    r = 2 * K - 1
    check = True
else:
    r = 2 * K

ans = sum(l[i:r + 1])
tmp = ans

while r < len(l) - 2:
    tmp += l[r + 1] + l[r + 2]
    r += 2
    if check:
        tmp -= l[i]
        i += 1
        check = False
    else:
        tmp -= l[i] + l[i + 1]
        i += 2
    ans = max(tmp, ans)

ans = max(ans, tmp)
print(ans)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
s = input()
S = [int(x) for x in list(str(s))]
S.append(2)

l = []
tmp = 1
p = S[0]

for i in range(1, N + 1):
    if p == S[i]:
        tmp += 1
    else:
        l.append(tmp)
        p = S[i]
        tmp = 1

i = 0
check = False

if S[0] == 0:
    r = 2 * K - 1
    check = True
else:
    r = 2 * K

ans = sum(l[i:r + 1])
tmp = ans

while r < len(l) - 2:
    tmp += l[r + 1] + l[r + 2]
    r += 2
    if check:
        tmp -= l[i]
        i += 1
        check = False
    else:
        tmp -= l[i] + l[i + 1]
        i += 2
    ans = max(tmp, ans)

ans = max(ans, tmp)
print(ans)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
s = input()
S = [int(x) for x in list(str(s))]
S.append(2)

l = []
tmp = 1
p = S[0]

for i in range(1, N + 1):
    if p == S[i]:
        tmp += 1
    else:
        l.append(tmp)
        p = S[i]
        tmp = 1

i = 0
check = False

if S[0] == 0:
    r = 2 * K - 1
    check = True
else:
    r = 2 * K

ans = sum(l[i:r + 1])
tmp = ans

while r < len(l) - 2:
    tmp += l[r + 1] + l[r + 2]
    r += 2
    if check:
        tmp -= l[i]
        i += 1
    else:
        tmp -= l[i] + l[i + 1]
        i += 2
    ans = max(tmp, ans)

ans = max(ans, tmp)
print(ans)

Submission status: Wrong Answer

Solution code:
def main():
    input = sys.stdin.readline
    try:
        N,K = map(int,input().split())
        S = input().rstrip()
        arr = []
        cnt = 0
        
        if S[0] == "0": arr.append(0)
        for i in range(N):
            cnt += 1
            if i == N - 1 or S[i] != S[i+1]:
                arr.append(cnt)
                cnt = 0
        if S[-1] == "0": arr.append(0)
        
        tmp = sum(arr[:K*2+1])
        ans = tmp
        for i in range(len(arr)//2-1):
            print(tmp)
            tmp += sum(arr[i+K*2+1 : i+K*2+3])
            tmp -= sum(arr[i : i+2])
            ans = max(ans, tmp)
        print(ans)
        
    finally:
        input = None

import sys; sys.setrecursionlimit(50000)
if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
def main():
    input = sys.stdin.readline
    try:
        N,K = map(int,input().split())
        S = input().rstrip()
        arr = []
        cnt = 0
        
        if S[0] == "0": arr.append(0)
        for i in range(N):
            cnt += 1
            if i == N - 1 or S[i] != S[i+1]:
                arr.append(cnt)
                cnt = 0
        if S[-1] == "0": arr.append(0)
        
        tmp = sum(arr[:K*2+1])
        ans = tmp
        for i in range(0, len(arr)//2-1, K):
            tmp += sum(arr[i+K*2+1 : i+K*2+1+K])
            tmp -= sum(arr[i : i+2])
            ans = max(ans, tmp)
        print(ans)
        
    finally:
        input = None

import sys; sys.setrecursionlimit(50000)
if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
[n,k] = "5 1".split(" ")
s = "00010"
n = int(n)
k = int(k)
cnt = 0
init = (s[0]=='1')
h=[1]
h_cnt = 0

for i in range(1,n):
    if(s[i]!=s[i-1]):
        h_cnt+=1
        h.append(1)
    else:
        h[h_cnt]+=1

h_cnt+=1

zero_num = 0
ans = 0

if(init): #start with 1
    zero_num = h_cnt//2;
    if(zero_num<=k):
        ans = n
    else:
        sum_0 = sum(h[0:k*2+1])
        ans = sum_0
        h = h +[0]
        for j in range(0,zero_num-k): #j is 0's order
            sum_0 = sum_0 - h[j*2] - h[j*2+1] + h[(j+k)*2+1] + h[(j+k)*2+2]
            if(ans<=sum_0):
                ans = sum_0

else: #start with 0
    zero_num = h_cnt//2 +1;
    if(zero_num<=k):
        ans = n
    else:
        sum_0 = sum(h[0:k*2])
        ans = sum_0
        h = [0] + h +[0]
        for j in range(0,zero_num-k):#j is 0's order
            sum_0 = sum_0 - h[j*2] - h[j*2+1] + h[(j+k)*2+1] + h[(j+k)*2+2]
            if(ans<=sum_0):
                ans = sum_0

print(ans)

Submission status: Wrong Answer

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

c = 1
s = S[0]
C = []
for i in range(1, len(S)):
    if S[i] == S[i-1]:
        c += 1
    else:
        C.append((c, s))
        c = 1
        s = '1' if s == '0' else '0'
C.append((c, s))

ans = 0
for i in range(len(C)):
    if i == 0 and C[i][1] == '0':
        ans = max(ans, sum(map(lambda x: x[0], C[i: i + 2 * K])))
    else:
        ans = max(ans, sum(map(lambda x: x[0], C[i: i + 2 * K + 1])))
print(ans)

Submission status: Wrong Answer

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

nums = []
now = 1
count = 0
for i in range(len(S)):
    if S[i] is str(now):
        count += 1
    else:
        nums.append(count)
        now = 1 - now
        count = 1
if count != 0:
    nums.append(count)
if len(nums) % 2 == 0:
    nums.append(0)  # ? 1を足すのでは？？

add = 2 * K + 1

sum = [0] * (len(nums)+1)
for i in range(len(nums)):
    sum[i+1] = sum[i] + nums[i]
ans = 0
print(list(range(0,len(nums), 2)))
for i in range(0,len(nums), 2):
    left = i
    right = min(i + add, len(nums))
    tmp = sum[right] - sum[left]
    ans = max(tmp, ans)

print(ans)

Submission status: Wrong Answer

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

nums = []
now = 1
count = 0
for i in range(len(S)):
    if S[i] is str(now):
        count += 1
    else:
        nums.append(count)
        now = 1 - now
        count = 1
if count != 0:
    nums.append(count)
if len(nums) % 2 == 0:
    nums.append(0)  # ? 1を足すのでは？？

add = 2 * K + 1

sum = [0] * (len(nums)+1)
for i in range(len(nums)):
    sum[i+1] = sum[i] + nums[i]
ans = 0

for i in range(0,len(nums), 2):
    left = i
    right = min(i + add, len(nums))
    tmp = sum[right] - sum[left]
    ans = max(tmp, ans)

print(ans)

Submission status: Wrong Answer

Solution code:
n,k=map(int, input().split())
s=input()
t=[]
now=s[0]
count=0
for i in s:
    if now==i:
        count+=1
    else:
        t.append(count)
        now=i
        count=1
t.append(count)
if k>len(t)//2+10000:print(sum(t))
else:
    ans=0
    if s[0]=='0':
        ans=max(ans,sum(t[:2*k]))
        t=t[1:]
    if s[-1]=='0':
        ans=max(ans,sum(t[-2*k:]))
        t=t[:-2]
    ans2=sum(t[:2*k+1])
    ans=max(ans,ans2)
    for i in range(len(t)//2-k+2):
        ans2=ans2-sum(t[2*i:2*i+2])+sum(t[2*i+2*k+1: 2*i+2*k+1+2])
        ans=max(ans,ans2)
    print(ans)

Submission status: Wrong Answer

Solution code:
from itertools import groupby, accumulate

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

# 連続する0または1の数に関する累積和
A = list(accumulate(len(list(seq)) for _, seq in groupby(S))) + [N]
m = (
    1 if S[0] == '0' else
    0
)
# 累積和を用いて解を求める
ans = (
    max(
        (
            A[2 * K - 1] if 2 * K - 1 < len(A) else
            0
        ),
        (
            A[-1] - A[-(2 * K)] if 2 * K <= len(A) else
            0
        ),
        max(
            (
                a - b
                for a, b in zip(A[(2 * K + m)::2], ([0] + A)[m::2])
            ),
            default=0
        )
    ) if m < len(A) else
    A[-1]
)

# 出力
print(ans)

Submission status: Wrong Answer

Solution code:
n,k=map(int, input().split())
s=input()
t=[]
now=s[0]
count=0
for i in s:
    if now==i:
        count+=1
    else:
        t.append(count)
        now=i
        count=1
t.append(count)
if k>len(t)//2+10000:print(sum(t))
else:
    ans=0
    if s[0]=='0':
        ans=max(ans,sum(t[:2*k]))
        t=t[1:]
    if s[-1]=='0':
        ans=max(ans,sum(t[-2*k:]))
        t=t[:-2]
    ans2=sum(t[:2*k+1])
    ans=max(ans,ans2)
    for i in range(len(t)//2-k+1):
        ans2=ans2-sum(t[2*i:2*i+2])+sum(t[2*i+2*k+1: 2*i+2*k+1+2])
        ans=max(ans,ans2)
    print(ans)

Submission status: Wrong Answer

Solution code:
n,k=map(int, input().split())
s=input()
t=[]
now=s[0]
count=0
for i in s:
    if now==i:
        count+=1
    else:
        t.append(count)
        now=i
        count=1
t.append(count)
if k>len(t)//2:print(sum(t))
else:
    ans=0
    if s[0]=='0':
        ans=max(ans,sum(t[:2*k]))
        t=t[1:]
    if s[-1]=='0':
        ans=max(ans,sum(t[-2*k:]))
        t=t[:-2]
    ans2=sum(t[:2*k+1])
    ans=max(ans,ans2)
    for i in range(len(t)//2-k+1):
        ans2=ans2-sum(t[2*i:2*i+2])+sum(t[2*i+2*k+1: 2*i+2*k+1+2])
        ans=max(ans,ans2)
    print(ans)

Submission status: Wrong Answer

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

nums = []
now = 1
count = 0
for i in range(len(S)):
    if S[i] == str(now):
        count += 1
    else:
        nums.append(count)
        now = 1 - now
        count = 1
if count != 0:
    nums.append(count)
if len(nums) % 2 == 0:
    nums.append(0)  # ? 1を足すのでは？？

add = 2 * K + 1

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

ans = 0

for i in range(len(nums),2):
    left = i
    right = min(i + add, len(nums))
    tmp = sum[right] - sum[left] 
    ans = max(tmp, ans)

print(ans)

Submission status: Wrong Answer

Solution code:
n,k=map(int, input().split())
s=input()
t=[]
now=s[0]
count=0
for i in s:
    if now==i:
        count+=1
    else:
        t.append(count)
        now=i
        count=1
t.append(count)
if k>len(t)//2:print(sum(t))
else:
    ans=0
    if s[0]=='0':
        ans=max(ans,sum(t[:2*k]))
        t=t[1:]
    if s[-1]=='0':
        ans=max(ans,sum(t[-2*k:-1]))
        t=t[:-2]
    ans2=sum(t[:2*k+1])
    ans=max(ans,ans2)
    for i in range(len(t)//2-k):
        ans2=ans2-sum(t[2*i:2*i+2])+sum(t[2*i+2*k+1: 2*i+2*k+1+2])
        ans=max(ans,ans2)
    print(ans)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
s = input()
n = len(s)
if n == 1:
    print(1)
    exit(0)
l = []
if s[0] == "0":
    l.append(0)

temp = 1
for i in range(1, n):
    if s[i] == s[i - 1]:
        temp += 1
        if i == n - 1:
            l.append(temp)
    else:
        l.append(temp)
        temp = 1

if s[-1] == "0":
    l.append(0)

ans = 0
for i in range((len(l) + 1) // 2):
    if i * 2 + 2 * k + 1 > len(l):
        break
    ans = max(ans, sum(l[i * 2 : min(i * 2 + 2 * k + 1, len(l)) : 1]))
print(ans)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
s = input()
n = len(s)
if n == 1:
    print(1)
    exit(0)
l = []
if s[0] == "0":
    l.append(0)

temp = 1
for i in range(1, n):
    if s[i] == s[i - 1]:
        temp += 1
        if i == n - 1:
            l.append(temp)
    else:
        l.append(temp)
        temp = 1

if s[-1] == "0":
    l.append(0)

ans = 0
for i in range(len(l)):
    if i + 2 * k + 1 > len(l):
        break
    ans = max(ans, sum(l[i : min(i + 2 * k + 1, len(l))]))
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(len(s))
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()
n = len(s)
if n == 1:
    print(1)
    exit(0)
l = []
if s[0] == "0":
    l.append(0)

temp = 1
for i in range(1, n):
    if s[i] == s[i - 1]:
        temp += 1
        if i == n - 1:
            l.append(temp)
    else:
        l.append(temp)
        temp = 1

if s[-1] == "0":
    l.append(0)

ans=0
for i in range(len(l)):
    ans = max(ans, sum(l[i:i + 2 * k+1]))
print(ans)

Submission status: Wrong Answer

Solution code:
def main():
    input = sys.stdin.readline
    try:
        N,K = map(int,input().split())
        S = input()
        arr = []
        before = S[0]
        cnt = 0
        for s in S:
            if s == before:
                cnt += 1
            else:
                arr.append(cnt)
                cnt = 1
                before = s
        tmp = sum(arr[:K**2+1])
        result = [tmp]
        for i in range(0, len(arr) - K ** 2 - 1, K):
            tmp += sum(arr[i+(K**2)+1 : i+(K**2)+1+K])
            tmp -= sum(arr[i : i+K])
            result.append(tmp)
        print(max(result))
    finally:
        input = None

import sys; sys.setrecursionlimit(50000)
if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

Nums = []
now = int(s[0])#今見てる数
cnt = 1#nowがいくつ並んでるか

for i in range(1,n):
    if s[i] == str(now):
        cnt += 1
    else:
        Nums.append(cnt)
        now = 1 - now#０と１の切り替え
        cnt = 1#新しいのをカウントし始める
if cnt != 0:Nums.append(cnt)

#長さが２の倍数であるならば任意の数を最後に加える
#if len(Nums) % 2 == 0:Nums.append(0)

add = 2 * k + 1 #ひっくり返せる範囲を指定
ans = 0
for i in range(0,len(Nums),2):
    tmp = 0
    left = i#(左端)
    right = min(i + add,len(Nums))#みる長さの最大値を調節（右端）
    #print(i)
    for j in range(left,right):
        tmp += Nums[j]
        #print(tmp,ans)
    ans = max(tmp,ans)

print(ans)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**7)
input = sys.stdin.readline
INF = float('inf')
MOD = 10**9+7
dy = [0,1,0,-1]
dx = [1,0,-1,0]
ddy = [0,1,1,1,0,-1,-1,-1]
ddx = [1,1,0,-1,-1,-1,0,1]

n,k = map(int,input().split())
S = input()+'!'
if S[0] == '1':
    S = '0' + S
A = []
tmp = 1
for i in range(1,len(S)):
    if S[i] == S[i-1]:
        tmp += 1
    else:
        A.append(tmp)
        tmp = 1
for i in range(1,len(A)):
    A[i] += A[i-1]
A = [0] + A
#print(A)
res = 0
for i in range(len(A)):
    if i%2 == 0:
        ans = A[min(i+2*k,len(A)-1)]-A[i]
    else:
        ans = A[min(i+2*k+1,len(A)-1)]-A[i]
    if ans >= res:
        res = ans
print(res)

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:
  u=0
  r=len_li-2*k-1
  for m in range (0,r):
    d=lis[m+2*k+1]-lis[m]
    if u<=d:
      u=d
  print(d)

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)

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

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

if final < M:
    while(final < M):
        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])

Submission status: Wrong Answer

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

if S[N-1] == '1':
    S += '0'
else:
    S += '1'

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

print(memo)
max = 0

if S[0] == '1':
    for i in range(len(memo)):
        if i%2 == 0:
            s = sum(memo[i:i+2*K+1])
        else:
            s = sum(memo[i:i+2*K])


        if s > max:
            max = s
else:
    for i in range(len(memo)):
        if i%2 == 1:
            s = sum(memo[i:i+2*K+1])
        else:
            s = sum(memo[i:i+2*K])


        if s > max:
            max = s

print(max)

Submission status: Wrong Answer

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

if S[N-1] == '1':
    S += '0'
else:
    S += '1'

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

max = 0
for i in range(N):
    if S[0] == '1':
        if i%2 == 0:
            s = sum(memo[i:i+2*K+1])
            if s > max:
                max = s
    else:
        if i%2 == 1:
            s = sum(memo[i:i+2*K+1])
            if s > max:
                max = s

print(max)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
S = input()
new_S = []
count_S = []

cur_c = S[0]
cur_count = 0

new_S.append(cur_c)
for c in S:
    if c == cur_c:
        cur_count += 1
    else:
        new_S.append(c)
        count_S.append(cur_count)
        cur_count = 1
        cur_c = c
count_S.append(cur_count)        

new_count = []
st_ix = 0
if S[0] == '0':
    cur_sum = sum(count_S[:2*K])
    st_ix = 1
    new_count.append(cur_sum)
else:
    cur_sum = sum(count_S[:2*K+1])
    new_count.append(cur_sum)

en_ix = st_ix + 2*K + 1
cur_sum = sum(count_S[st_ix:en_ix])
new_count.append(cur_sum)

while en_ix < len(count_S):
    # shift st_ix
    st_ix = st_ix + 2
    en_ix = en_ix + 2
    cur_sum -= (count_S[st_ix-2] + count_S[st_ix-1])
    cur_sum += (count_S[min(en_ix-2, len(count_S)-1)]
                        + count_S[min(en_ix-1, len(count_S)-1)])
    new_count.append(cur_sum)

print(max(new_count))

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
S = input()
new_S = []
count_S = []

cur_c = S[0]
cur_count = 0

new_S.append(cur_c)
for c in S:
    if c == cur_c:
        cur_count += 1
    else:
        new_S.append(c)
        count_S.append(cur_count)
        cur_count = 1
        cur_c = c
count_S.append(cur_count)        

new_count = []
st_ix = 0
if S[0] == '0':
    cur_sum = sum(count_S[:2*K])
    st_ix = 1
    new_count.append(cur_sum)
else:
    cur_sum = sum(count_S[:2*K+1])
    new_count.append(cur_sum)

en_ix = st_ix + 2*K + 1
cur_sum = sum(count_S[st_ix:en_ix])
new_count.append(cur_sum)

while en_ix + 2< len(count_S):
    # shift st_ix
    st_ix = st_ix + 2
    en_ix = en_ix + 2
    cur_sum -= (count_S[st_ix-2] + count_S[st_ix-1])
    cur_sum += (count_S[en_ix-2] + count_S[en_ix-1])
    new_count.append(cur_sum)

print(max(new_count))

Submission status: Wrong Answer

Solution code:
from itertools import accumulate

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

nums = []
tmp = "1" #はじめに0が入ったら、0からスタートというのがわかる。
count = 0
for s in S:
    if tmp == s:
        count += 1
    else:
        nums.append(count)
        count = 1
        tmp = s
nums.append(count)

# print(nums)
# 累積和表示にする(listって足し算できるのね)
# これによって、各文字最初の値が明らかになる
acc = [0]+list(accumulate(nums))
# print(acc)
n = len(nums)
w = 2*K + 1

if len(nums) <= w:
    print(N)
else:
    ans = 0
    for i in range(0,n-w+1,2): #2個飛ばし
        ans = max(ans,acc[i+w]-acc[i])

    print(ans)

Submission status: Wrong Answer

Solution code:
from itertools import accumulate

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

nums = []
tmp = "1" #はじめに0が入ったら、0からスタートというのがわかる。
count = 0
for s in S:
    if tmp == s:
        count += 1
    else:
        nums.append(count)
        count = 1
        tmp = s
nums.append(count)

# print(nums)
# 累積和表示にする(listって足し算できるのね)
# これによって、各文字最初の値が明らかになる
acc = [0]+list(accumulate(nums))
# print(acc)
n = len(nums)
w = 2*K + 1

ans = 0
for i in range(0,n-w+1,2): #2個飛ばし
    ans = max(ans,acc[i+w]-acc[i])
print(ans)

Submission status: Wrong Answer

Solution code:
from itertools import accumulate

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

nums = []
tmp = "1" #はじめに0が入ったら、0からスタートというのがわかる。
count = 0
for s in S:
    if tmp == s:
        count += 1
    else:
        nums.append(count)
        count = 1
        tmp = s
nums.append(count)

# print(nums)
# 累積和表示にする(listって足し算できるのね)
# これによって、各文字最初の値が明らかになる
acc = [0]+list(accumulate(nums))
# print(acc)
n = len(nums)
w = min(2*K + 1, N//2)

ans = 0
for i in range(0,n-w+1,2): #2個飛ばし
    ans = max(ans,acc[i+w]-acc[i])
print(ans)