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

L = S.replace('01', '0,1')
L = L.replace('10', '1,0')
L = L.split(',')
L = [len(l) for l in L]

R = []

if S[0] == '1':
    for i in range(max(1, len(L) // 2 + 1 - K)):
        R.append(sum(L[2*i:2*(i+K)+1]))
else:
    for i in range(max(1, len(L) // 2 + 2 - K)):
        R.append(sum(L[2*i:2*(i+K)]))

print(max(R))

Submission status: Wrong Answer

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

R_1  = [len(str(s)) for s in S.split('0')]
R_1  = [r for r in R_1 if r > 0]
R_0  = [len(str(s)) for s in S.split('1')]
R_0  = [r for r in R_0 if r > 0]

R_1
R_0

R = []
S
L = []

if S[0] == '1':
    R = [R_1[0]]
    for i in range(len(R_0)):
        R.append(R_0[i])
        try:
            R.append(R_1[i+1])
        except:
            pass
    for i in range(len(R) // 2 + 1):
        L.append(sum(R[2*i:2*(i+K)+1]))
else:
    R = [R_0[0]]
    for i in range(len(R_0)-1):
        R.append(R_1[i])
        try:
            R.append(R_0[i+1])
        except:
            pass
    for i in range(len(R) // 2 + 1):
        L.append(sum(R[2*i:2*(i+(K-1))+2]))

print(max(L))

Submission status: Wrong Answer

Solution code:
from itertools import groupby
N, K = map(int, input().split())
S = input()
g_cnt = [len(list(v)) for k, v in groupby(S)]
g_num = [k for k, v in groupby(S)]
ans = 1

if S[0]=="0":
    g_cnt = [0] + g_cnt
if S[-1]=="0":
    g_cnt = g_cnt + [0]
    
for i in range(0, len(g_cnt)-(2*K+1)+1, 2):
    ans = max(ans, sum(g_cnt[i:i+2*K+1]))
print(ans)

Submission status: Wrong Answer

Solution code:
# 累積和
from sys import exit

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

p = '1'
count = 0
cs = []
for c in S:
  if c != p:
    cs.append(count)
    p = c
    count = 1
  else:
    count += 1
cs.append(count)

if len(cs) < 2 * K:
  print(N)
  exit()

for i in range(1, len(cs)):
  cs[i] += cs[i - 1]

result = cs[2 * K]
for i in range(2, len(cs) - 2 * K, 2):
  t = cs[2 * K + i] - cs[i - 1]
  if t > result:
    result = t
print(result)

Submission status: Wrong Answer

Solution code:
N , K = map(int ,input().split())
S = list(input())
if not '0' in S:
    print(N)
    exit()

target_char = S[0]
max_val = 0
cnt = 1
zero_list = []
one_list = []
for i in range(1,N):
    if S[i] == target_char:
        cnt += 1

    else:
        if target_char == "0":
            zero_list.append(cnt)
            cnt = 1
            target_char = "1"
        else:
            one_list.append(cnt)
            cnt = 1
            target_char = "0"
    if i == N - 1:
        if target_char == '0':
            zero_list.append(cnt)
        else:
            one_list.append(cnt)
#print(one_list)
#print(zero_list)
i = 0
max_val = 0

if S[0] == "1":
    first_one = one_list[0]
    one_list.pop(0)

for i in range(len(zero_list) - K + 1):
    sum_val = 0

    if S[0] == "1" and i == 0:
        sum_val += first_one
    if S[0] == "0" and i == 0:
        sum_val += sum(zero_list[i:i + K]) + sum(one_list[i:i + K ])
    else:
        if i == 0:
            sum_val += sum(zero_list[i:i + K]) + sum(one_list[i:i + K])
        else:
            sum_val += sum(zero_list[i:i+K]) + sum(one_list[i - 1:i + K + 1])
    if max_val < sum_val:
        max_val = sum_val
print(max_val)

Submission status: Wrong Answer

Solution code:
def makelist(n, m):
    return [[0 for i in range(m)] for j in range(n)]

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

hoge = N*3

imos = [0]*(hoge)
imosInd = 2
cnt = 1
ind = 1
while ind < N:
    if S[ind] == S[ind-1]:
        cnt += 1
    else:
        imos[imosInd] = cnt
        cnt = 1
        imosInd += 1

    ind += 1
imos[imosInd] = cnt

for i in range(2, hoge):
    imos[i] += imos[i-1]


if S[0] == '1':
    ind = 2
else:
    ind = 1

ans = 0

while ind + K*2 < hoge:
    ans = max(ans, imos[ind + K*2] - imos[ind-1])
    ind += 2

print(ans)

Submission status: Wrong Answer

Solution code:
def makelist(n, m):
    return [[0 for i in range(m)] for j in range(n)]

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

imos = [0]*(N+5)
imosInd = 2
cnt = 1
ind = 1
while ind < N:
    if S[ind] == S[ind-1]:
        cnt += 1
    else:
        imos[imosInd] = cnt
        cnt = 1
        imosInd += 1

    ind += 1
imos[imosInd] = cnt

for i in range(2, N+5):
    imos[i] += imos[i-1]


if S[0] == '1':
    ind = 2
else:
    ind = 1

ans = 0

while ind + K*2 <= N+4:
    ans = max(ans, imos[ind + K*2] - imos[ind-1])
    ind += 2

print(ans)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
s=input()
lists=[]
revlists=[]

for i in range(len(s)):
    lists.append(int(s[i]))
    revlists.append(int(s[n-1-i]))

lists.insert(0,1)
lists.insert(n+1,1)

revlists.insert(0,1)
revlists.insert(n+1,1)

checklists=[]
rechecklists=[]

for i in range(1,n+1):
    if lists[i]==0 and lists[i+1]==1:
        checklists.append(i)
for i in range(1,n+1):
    if revlists[i]==0 and revlists[i+1]==1:
        rechecklists.append(i)

        
        
if len(checklists)<k:
    ans1=n
elif len(checklists)>k:
    ans1=checklists[k-1]+1
elif len(checklists)==k:
    if lists[-1]==0:
        ans1=n
    elif lists[-1]==1:
        ans1=checklists[k-1]+1

        
if len(rechecklists)<k:
    ans2=n
elif len(rechecklists)>k:
    ans2=rechecklists[k-1]+1
elif len(rechecklists)==k:
    if revlists[-1]==0:
        ans2=n
    elif revlists[-1]==1:
        ans2=rechecklists[k-1]+1
print(max(ans1,ans2))

Submission status: Wrong Answer

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

a = []
j = 0
temp = 1
prev = s[0]
for i in s[1:]:
    if i == prev:
        prev = i
        temp += 1
    else:
        a.append(temp)
        prev = i        
        temp = 1
a.append(temp)

b = []
if s[0] == "1":
    b.append(sum(a[:2*k+1]))
    for i in range(len(a) - 2*k + 2):
        if i % 2 == 0:
            try:
                b.append(b[-1] - a[i] - a[i + 1] + a[i+(2*k) -1] + a[i+(2*k)])
            except:
                continue
    if s[-1] == "0":
        b.append(sum(a[-2*k - 2:-1]))
    print(max(b))

else:
    t = a[1:]
    b.append(sum(a[:2*k]))
    b.append(sum(t[:2*k+1]))
    for i in range(len(t) - 2*k + 2):
        if i % 2 == 0:
            try:
                b.append(b[-1] - t[i] - t[i + 1] + t[i+(2*k) -1] + t[i+(2*k)])
            except:
                continue
    if s[-1] == "0":
        b.append(sum(a[-2*k - 2:-1]))
    b.append(sum(a[:2*k]))
    print(max(b))

Submission status: Wrong Answer

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

a = []
j = 0
temp = 1
prev = s[0]
for i in s[1:]:
    if i == prev:
        prev = i
        temp += 1
    else:
        a.append(temp)
        prev = i        
        temp = 1
a.append(temp)

b = []
if s[0] == "1":
    b.append(sum(a[:2*k+1]))
    for i in range(len(a) - 2*k + 2):
        if i % 2 == 0:
            try:
                b.append(b[-1] - a[i] - a[i + 1] + a[i+(2*k) -1] + a[i+(2*k)])
            except:
                continue
    if s[-1] == "0":
        b.append(sum(a[-2*k - 2:-1]))
    print(max(b))

else:
    t = a[1:]
    b.append(sum(t[:2*k - 1]))
    b.append(sum(t[1:2*k + 1]))
    for i in range(len(t) - 2*k + 2):
        if i % 2 == 0:
            try:
                b.append(b[-1] - t[i] - t[i + 1] + t[i+(2*k) -1] + t[i+(2*k)])
            except:
                continue
    if s[-1] == "0":
        b.append(sum(a[-2*k - 2:-1]))
    b.append(sum(a[:2*k]))
    print(max(b))

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
S = list(map(int,input()))
num = 1
T = []
T_append = T.append
if S[0] == 0:
    T.append(0)
tmp = S[0]
for i,s in enumerate(S):
    if i == 0:
        continue
    if tmp != s:
        T_append(num)
        num = 0
    tmp = s
    if i == N-1:
        T_append(num+1)
        continue
    num += 1
if S[N-1] == 0:
    T.append(0)
print(T)
#index 0,2,4...に1の連続数、1,3,5...に0の連続数
if K >= len(T)//2:
    print(N)
else:
    ans = 0
    for i in range(len(T)//2 -K+1):
        ans = max(ans,sum(T[2*i:2*i+2*K+1]))
    print(ans)

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
S = input() + "#"
A = []
cnt = 1
for i in range(N) :
    if S[i] == S[i+1] :
        cnt += 1
    else :
        A.append(cnt)
        cnt = 1

if S[0] == "1" :
    ans = sum(A[:2*K+1])
    calc = ans
    for i in range(2*K+2, len(A), 2) :
        calc += (A[i-1] + A[i])
        calc -= (A[i-2-2*K] + A[i-1-2*K])
        ans = max(ans, calc)
else :
    calc = sum(A[:2*K+2])-A[0]
    ans = max(sum(A[:2*K]), calc)
    for i in range(2*K+3, len(A), 2) :
        calc += (A[i-1] + A[i])
        calc -= (A[i-2-2*K] + A[i-1-2*K])
        ans = max(ans, calc)

print(ans)

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
S = input() + "#"
A = []
cnt = 1
for i in range(N) :
    if S[i] == S[i+1] :
        cnt += 1
    else :
        A.append(cnt)
        cnt = 1

if S[0] == "1" :
    ans = sum(A[:2*K+1])
    calc = ans
    for i in range(2*K+2, len(A), 2) :
        calc += (A[i-1] + A[i])
        calc -= (A[i-2-2*K] + A[i-1-2*K])
        ans = max(ans, calc)
else :
    calc = sum(A[:2*K+2])-A[0]
    ans = max(sum(A[:2*K]), calc)
    for i in range(2*K+2, len(A), 2) :
        calc += (A[i-1] + A[i])
        calc -= (A[i-2-2*K] + A[i-1-2*K])
        ans = max(ans, calc)

print(ans)

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
S = input() + "#"
A = []
cnt = 1
for i in range(N) :
    if S[i] == S[i+1] :
        cnt += 1
    else :
        A.append(cnt)
        cnt = 1

if S[0] == "1" :
    ans = sum(A[:2*K+1])
    calc = ans
    for i in range(2*K+2, len(A), 2) :
        calc += (A[i-1] + A[i])
        calc -= (A[i-2-2*K] + A[i-1-2*K])
        ans = max(ans, calc)
else :
    calc = sum(A[:2*K+2])-A[0]
    ans = max(sum(A[:2*K]), calc)
    for i in range(2*K+1, len(A), 2) :
        calc += (A[i-1] + A[i])
        calc -= (A[i-2-2*K] + A[i-1-2*K])
        ans = max(ans, calc)

print(ans)

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
S = input() + "#"
A = []
cnt = 1
for i in range(N) :
    if S[i] == S[i+1] :
        cnt += 1
    else :
        A.append(cnt)
        cnt = 1

if S[0] == "1" :
    ans = sum(A[:2*K+1])
    calc = ans
    for i in range(2*K+2, len(A), 2) :
        calc += (A[i-1] + A[i])
        calc -= (A[i-2-2*K] + A[i-1-2*K])
        ans = max(ans, calc)
else :
    calc = sum(A[:2*K+2])-A[0]
    ans = max(sum(A[:2*K]), calc)
    for i in range(2*K+1, len(A), 2) :
        calc += (A[i-1] + A[i])
        calc -= (A[i-1-2*K] + A[i-2*K])
        ans = max(ans, calc)

print(ans)

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
S = input() + "#"
A = []
cnt = 1
for i in range(N) :
    if S[i] == S[i+1] :
        cnt += 1
    else :
        A.append(cnt)
        cnt = 1

if S[0] == "1" :
    ans = sum(A[:2*K+1])
    calc = ans
    for i in range(2*K+2, len(A), 2) :
        calc += (A[i-1] + A[i])
        calc -= (A[i-2-2*K] + A[i-1-2*K])
        ans = max(ans, calc)
else :
    ans = sum(A[:2*K])
    calc = ans
    for i in range(2*K+1, len(A), 2) :
        calc += (A[i-1] + A[i])
        calc -= (A[i-1-2*K] + A[i-2*K])
        ans = max(ans, calc)

print(ans)

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
S = input() + "#"
A = []
cnt = 1
for i in range(N) :
    if S[i] == S[i+1] :
        cnt += 1
    else :
        A.append(cnt)
        cnt = 1

if S[0] == "1" :
    ans = sum(A[:2*K+1])
    calc = ans
    for i in range(2*K+2, len(A), 2) :
        calc += (A[i-1] + A[i])
        calc -= (A[i-2-2*K] + A[i-1-2*K])
        ans = max(ans, calc)
else :
    ans = sum(A[:2*K])
    calc = ans
    for i in range(2*K+2, len(A), 2) :
        calc += (A[i-1] + A[i])
        calc -= (A[i-1-2*K] + A[i-2*K])
        ans = max(ans, calc)

print(ans)

Submission status: Wrong Answer

Solution code:
from itertools import groupby

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

grp_0s = []
idx = 0
last_elem = None
len_prv_1 = 0
for key, value in groupby(input()):
  len_value = len(list(value))
  if key == '0':
    grp_0s.append([idx,len_value-1, len_prv_1])
  else:
    len_prv_1 = len_value
  idx += len_value
  last_elem = [key, len_value]

len_grp_0s = len(grp_0s)
if len_grp_0s <= K:
  print(N)
  exit()
  
if last_elem[0] == '1':
  grp_0s[-1] = [grp_0s[-1][0],grp_0s[-1][1]+last_elem[1],grp_0s[-1][2]]

start_i = 0
end_i = start_i + K - 1
ans = -float('inf')
while end_i < len_grp_0s:
  if K == 1:
    val1 = grp_0s[start_i+1][2] if start_i+1 <= len_grp_0s - 1 else 0
    ans = max(ans,  grp_0s[start_i][1]+1+grp_0s[start_i][2]+val1)
  else: ans = max(ans, grp_0s[end_i][0]+grp_0s[end_i][1] - (grp_0s[start_i][0]-grp_0s[start_i][2]-1))
  start_i += 1
  end_i = start_i + K - 1
  #print(ans, start_i, end_i)
print(ans)
#print(grp_0s)

Submission status: Wrong Answer

Solution code:
from itertools import groupby

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

grp_0s = []
idx = 0
last_elem = None
len_prv_1 = 0
for key, value in groupby(input()):
  len_value = len(list(value))
  if key == '0':
    grp_0s.append([idx,len_value-1, len_prv_1])
  else:
    len_prv_1 = len_value
  idx += len_value
  last_elem = [key, len_value]

len_grp_0s = len(grp_0s)
if len_grp_0s <= K:
  print(N)
  exit()
  
if last_elem[0] == '1': grp_0s[-1] = [grp_0s[-1][0],grp_0s[-1][1]+last_elem[1],grp_0s[-1][2]]

start_i = 0
end_i = start_i + K - 1
ans = -float('inf')
while end_i < len_grp_0s:
  if K == 1:
    val1 = grp_0s[start_i+1][1]+1 if start_i+1 <= len_grp_0s - 1 else 0
    ans = max(ans,  grp_0s[start_i][1]+1+grp_0s[start_i][2]+val1)
  else: ans = max(ans, grp_0s[end_i][0]+grp_0s[end_i][1] - (grp_0s[start_i][0]-grp_0s[start_i][2]-1))
  start_i += 1
  end_i = start_i + K - 1
  #print(ans, start_i, end_i)
print(ans)
#print(grp_0s)

Submission status: Wrong Answer

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

L0 = []
L1 = []
temp = S[0]
cnt = 1
for i in range(1, N):
    if S[i] == "0":
        if temp == "0":
            cnt += 1
        else:
            L1.append(cnt)
            temp = "0"
            cnt = 1
    else:
        if temp == "1":
            cnt += 1
        else:
            L0.append(cnt)
            temp = "1"
            cnt = 1
if S[-1] == "0":
    L0.append(cnt)
else:
    L1.append(cnt)

ans = 0
if S[0] == "0":
    if K >= len(L0):
        ans += N
    else:
        for i in range(K):
            ans += L0[i]+L1[i]
        i = K
        temp = ans
        while i < len(L0):
            if i-K == 0:
                temp -= L0[i-K]
            else:
                temp -= L0[i-K]+L1[i-K]
            if i == len(L1):
                temp += L0[i]
            else:
                temp += L0[i]+L1[i]
            if ans < temp:
                ans = temp
            i += 1
else:
    if K >= len(L0):
        ans += N
    else:
        for i in range(K):
            ans += L0[i]+L1[i]
        ans += L1[K]
        i = K
        temp = ans
        while i < len(L0):
            temp -= L0[i-K]+L1[i-K]
            if S[-1] == "0" and i == len(L1)-1:
                temp += L0[i]
            else:
                temp += L0[i]+L1[i+1]
            if ans < temp:
                ans = temp
            i += 1
            
print(ans)

Submission status: Wrong Answer

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

L0 = []
L1 = []
temp = S[0]
cnt = 1
for i in range(1, N):
    if S[i] == "0":
        if temp == "0":
            cnt += 1
        else:
            L1.append(cnt)
            temp = "0"
            cnt = 1
    else:
        if temp == "1":
            cnt += 1
        else:
            L0.append(cnt)
            temp = "1"
            cnt = 1
if S[-1] == "0":
    L0.append(cnt)
else:
    L1.append(cnt)

ans = 0
if K >= len(L0):
    ans += N
else:
    if S[0] == "0":
        for i in range(K):
            ans += L0[i]
            ans += L1[i]
    else:
        for i in range(K):
            ans += L1[i]
            ans += L0[i]
        if K < len(L1):
            ans += L1[K]
    temp = ans
    i = K
    while i < len(L0) and i+1 < len(L1):
        temp += L0[i]+L1[i+1]
        temp -= L0[i-K]+L1[i-K]
        if ans < temp:
            ans = temp
        i += 1

print(ans)

Submission status: Wrong Answer

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

l0 = []
l1 = []

temp = s[0]
if temp == '0':
  l0.append(1)
else:
  l1.append(1)

for i in range(1, n):
  if temp == '0' and s[i] == '0':
    l0[-1] += 1
  elif temp == '0' and s[i] == '1':
    l1.append(1)
    temp = '1'
  elif temp == '1' and s[i] == '0':
    l0.append(1)
    temp = '0'
  else:
    l1[-1] += 1
#print(l1)
#print(l0)

if len(l0) <= k:
  print(n)
  exit()

from itertools import accumulate
l0 = [0] + l0
l1 = [0] + l1
cum_l0 = list(accumulate(l0))
cum_l1 = list(accumulate(l1))

#print(cum_l0)
#print(cum_l1)

ms = []
if s[0] == '1':
  for i in range(len(l0)-k):
    if i != len(l0)-k-1:
      ms.append(cum_l0[i+k] - cum_l0[i] +  cum_l1[i+k+1] - cum_l1[i])
    else:
      if len(cum_l0) < len(cum_l1):
        ms.append(cum_l0[i+k] - cum_l0[i] +  cum_l1[i+k+1] - cum_l1[i])
      else:
        ms.append(cum_l0[i+k] - cum_l0[i] +  cum_l1[-1] - cum_l1[i])
  print(max(ms))
  exit()
        
if s[0] == '0':
  for i in range(len(l0)-k):
    if i != len(l0)-k-1:
      ms.append(cum_l1[i+k] - cum_l1[i] +  cum_l0[i+k] - cum_l0[i])
    else:
      if len(cum_l0) <= len(cum_l1):
        ms.append(cum_l1[i+k] - cum_l1[i] +  cum_l0[i+k] - cum_l0[i])
      else:
        ms.append(cum_l1[-1] - cum_l1[i] +  cum_l0[i+k] - cum_l0[i])
  print(max(ms))
  exit()

Submission status: Wrong Answer

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

l0 = []
l1 = []

temp = s[0]
if temp == '0':
  l0.append(1)
else:
  l1.append(1)

for i in range(1, n):
  if temp == '0' and s[i] == '0':
    l0[-1] += 1
  elif temp == '0' and s[i] == '1':
    l1.append(1)
    temp = '1'
  elif temp == '1' and s[i] == '0':
    l0.append(1)
    temp = '0'
  else:
    l1[-1] += 1
#print(l1)
#print(l0)

if len(l0) <= k:
  print(n)
  exit()

from itertools import accumulate
l0 = [0] + l0
l1 = [0] + l1
cum_l0 = list(accumulate(l0))
cum_l1 = list(accumulate(l1))

#print(cum_l0)
#print(cum_l1)

ms = []
if s[0] == '1':
  for i in range(len(l0)-k):
    if i != len(l0)-k-1:
      ms.append(cum_l0[i+k] - cum_l0[i] +  cum_l1[i+k+1] - cum_l1[i])
    else:
      if len(cum_l0) < len(cum_l1):
        ms.append(cum_l0[i+k] - cum_l0[i] +  cum_l1[i+k+1] - cum_l1[i])
      else:
        ms.append(cum_l0[i+k] - cum_l0[i] +  cum_l1[-1] - cum_l1[i])
  print(max(ms))
  exit()
        
if s[0] == '0':
  for i in range(len(l0)-k):
    if i != len(l0)-k-1:
      ms.append(cum_l1[i+k] - cum_l1[i] +  cum_l0[i+k] - cum_l0[i])
    else:
      if len(cum_l0) < len(cum_l1):
        ms.append(cum_l1[i+k] - cum_l1[i] +  cum_l0[i+k] - cum_l0[i])
      else:
        ms.append(cum_l1[-1] - cum_l1[i] +  cum_l0[i+k] - cum_l0[i])
  print(max(ms))
  exit()

Submission status: Wrong Answer

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

l0 = []
l1 = []

temp = s[0]
if temp == '0':
  l0.append(1)
else:
  l1.append(1)

for i in range(1, n):
  if temp == '0' and s[i] == '0':
    l0[-1] += 1
  elif temp == '0' and s[i] == '1':
    l1.append(1)
    temp = '1'
  elif temp == '1' and s[i] == '0':
    l0.append(1)
    temp = '0'
  else:
    l1[-1] += 1
#print(l1)
#print(l0)

if len(l0) <= k:
  print(n)
  exit()

from itertools import accumulate
l0 = [0] + l0
l1 = [0] + l1
cum_l0 = list(accumulate(l0))
cum_l1 = list(accumulate(l1))

#print(cum_l0)
#print(cum_l1)

ms = []
if s[0] == '1':
  for i in range(len(l0)-k):
    if i != len(l0)-k-1:
      ms.append(cum_l0[i+k] - cum_l0[i] +  cum_l1[i+k+1] - cum_l1[i])
    else:
      if len(cum_l0) < len(cum_l1):
        ms.append(cum_l0[i+k] - cum_l0[i] +  cum_l1[i+k+1] - cum_l1[i])
      else:
        ms.append(cum_l0[i+k] - cum_l0[i] +  cum_l1[-1] - cum_l1[-1-k])
  print(max(ms))
  exit()
        
if s[0] == '0':
  for i in range(len(l0)-k):
    if i != len(l0)-k-1:
      ms.append(cum_l1[i+k] - cum_l1[i] +  cum_l0[i+k] - cum_l0[i])
    else:
      if len(cum_l0) < len(cum_l1):
        ms.append(cum_l1[i+k] - cum_l1[i] +  cum_l0[i+k] - cum_l0[i])
      else:
        ms.append(cum_l1[-1] - cum_l1[-1-k] +  cum_l0[i+k] - cum_l0[i])
  print(max(ms))
  exit()

Submission status: Wrong Answer

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

l0 = []
l1 = []

temp = s[0]
if temp == '0':
  l0.append(1)
else:
  l1.append(1)

for i in range(1, n):
  if temp == '0' and s[i] == '0':
    l0[-1] += 1
  elif temp == '0' and s[i] == '1':
    l1.append(1)
    temp = '1'
  elif temp == '1' and s[i] == '0':
    l0.append(1)
    temp = '0'
  else:
    l1[-1] += 1
#print(l1)
#print(l0)

if len(l0) <= k:
  print(n)
  exit()

from itertools import accumulate
l0 = [0] + l0
l1 = [0] + l1
cum_l0 = list(accumulate(l0))
cum_l1 = list(accumulate(l1))

#print(cum_l0)
#print(cum_l1)

ms = []
if s[0] == '1':
  for i in range(len(l0)-k):
    if i != len(l0)-k-1:
      ms.append(cum_l0[i+k] - cum_l0[i] +  cum_l1[i+k+1] - cum_l1[i])
    else:
      if len(cum_l0) < len(cum_l1):
        ms.append(cum_l0[i+k] - cum_l0[i] +  cum_l1[i+k+1] - cum_l1[i])
      else:
        ms.append(cum_l0[i+k] - cum_l0[i] +  cum_l1[i+k] - cum_l1[i])
  print(max(ms))
  exit()
        
if s[0] == '0':
  for i in range(len(l0)-k):
    if i != len(l0)-k-1:
      ms.append(cum_l1[i+k] - cum_l1[i] +  cum_l0[i+k] - cum_l0[i])
    else:
      if len(cum_l0) < len(cum_l1):
        ms.append(cum_l1[i+k] - cum_l1[i] +  cum_l0[i+k] - cum_l0[i])
      else:
        ms.append(cum_l1[-1] - cum_l1[-2] +  cum_l0[i+k] - cum_l0[i])
  print(max(ms))
  exit()

Submission status: Wrong Answer

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

l0 = []
l1 = []

temp = s[0]
if temp == '0':
  l0.append(1)
else:
  l1.append(1)

for i in range(1, n):
  if temp == '0' and s[i] == '0':
    l0[-1] += 1
  elif temp == '0' and s[i] == '1':
    l1.append(1)
    temp = '1'
  elif temp == '1' and s[i] == '0':
    l0.append(1)
    temp = '0'
  else:
    l1[-1] += 1
#print(l1)
#print(l0)

if len(l0) <= k:
  print(n)
  exit()

from itertools import accumulate
l0 = [0] + l0
l1 = [0] + l1
cum_l0 = list(accumulate(l0))
cum_l1 = list(accumulate(l1))

#print(cum_l0)
#print(cum_l1)

ms = []
if s[0] == '1':
  for i in range(len(l0)-k):
    if i != len(l0)-k-1:
      ms.append(cum_l0[i+k] - cum_l0[i] +  cum_l1[i+k+1] - cum_l1[i])
    else:
      if len(cum_l0) < len(cum_l1):
        ms.append(cum_l0[i+k] - cum_l0[i] +  cum_l1[i+k+1] - cum_l1[i])
      else:
        ms.append(cum_l0[i+k] - cum_l0[i] +  cum_l1[i+k] - cum_l1[i])
  print(max(ms))
  exit()
        
if s[0] == '0':
  for i in range(len(l0)-k):
    if i != len(l0)-k-1:
      ms.append(cum_l1[i+k] - cum_l1[i] +  cum_l0[i+k] - cum_l0[i])
    else:
      if len(cum_l0) < len(cum_l1):
        ms.append(cum_l1[i+k] - cum_l1[i] +  cum_l0[i+k] - cum_l0[i])
      else:
        ms.append(cum_l1[i+k-1] - cum_l1[i-1] +  cum_l0[i+k] - cum_l0[i])
  print(ms)
  exit()

Submission status: Wrong Answer

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

l0 = []
l1 = []

temp = s[0]
if temp == '0':
  l0.append(1)
else:
  l1.append(1)

for i in range(1, n):
  if temp == '0' and s[i] == '0':
    l0[-1] += 1
  elif temp == '0' and s[i] == '1':
    l1.append(1)
    temp = '1'
  elif temp == '1' and s[i] == '0':
    l0.append(1)
    temp = '0'
  else:
    l1[-1] += 1
#print(l1)
#print(l0)

if len(l0) <= k:
  print(n)
  exit()

from itertools import accumulate
l0 = [0] + l0
l1 = [0] + l1
cum_l0 = list(accumulate(l0))
cum_l1 = list(accumulate(l1))

#print(cum_l0)
#print(cum_l1)

ms = []
if s[0] == '1':
  for i in range(len(l0)-k):
    if i != len(l0)-k-1:
      ms.append(cum_l0[i+k] - cum_l0[i] +  cum_l1[i+k+1] - cum_l1[i])
    else:
      if len(cum_l0) < len(cum_l1):
        ms.append(cum_l0[i+k] - cum_l0[i] +  cum_l1[i+k+1] - cum_l1[i])
      else:
        ms.append(cum_l0[i+k] - cum_l0[i] +  cum_l1[i+k] - cum_l1[i])
  print(max(ms))
  exit()
        
if s[0] == '0':
  for i in range(len(l0)-k):
    if i != len(l0)-k-1:
      ms.append(cum_l1[i+k] - cum_l1[i] +  cum_l0[i+k] - cum_l0[i])
    else:
      if len(cum_l0) < len(cum_l1):
        ms.append(cum_l1[i+k] - cum_l1[i] +  cum_l0[i+k] - cum_l0[i])
      else:
        ms.append(cum_l1[i+k-1] - cum_l1[i] +  cum_l0[i+k] - cum_l0[i])
  print(max(ms))
  exit()

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
s = list(map(int,input()))
s_list = list()
num = s[0]
temp_streak = 0
temp_max = 0
[s_list.append(0) for i in range(1+s[0])]

for i in s:
    if i == num:
        temp_streak += 1
    else:
        s_list.append(temp_streak)
        temp_streak = 1
        num = i
s_list.append(temp_streak)
[s_list.append(0) for i in range(1+s[-1])]
# odd = len(s_list)%2
num_i = len(s_list) // 2 
print(s_list)

for i in range(max(num_i+1-k,1)):
    temp_max = max(temp_max,sum(s_list[i*2:i*2+k*2+1]))
print(temp_max)




#print(s_list)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
s = list(map(int,input()))
s_list = list()
num = s[0]
temp_streak = 0
temp_max = 0
[s_list.append(0) for i in range(1+s[0])]

for i in s:
    if i == num:
        temp_streak += 1
    else:
        s_list.append(temp_streak)
        temp_streak = 1
        num = i
s_list.append(temp_streak)
[s_list.append(0) for i in range(1+s[-1])]
# odd = len(s_list)%2
num_i = len(s_list) // 2 
#print(s_list)
for i in range(num_i+1-k):
    temp_max = max(temp_max,sum(s_list[i*2:i*2+k*2+1]))
print(temp_max)




#print(s_list)

Submission status: Wrong Answer

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

if S.count('1') == N or S.count('0') == N:
  print(N)
  exit()

S += '2'
s = S[0]
L0 = []
if s == '0':
  L1 = [0]
else:
  L1 = []
c = 1
for i in range(1,N+1):
  if S[i] != s:
    if s == '0':
      L0.append(c)
    else:
      L1.append(c)
    s = S[i]
    c = 1
  else:
    c += 1
L1 += [0]
#print(L0)
#print(L1)

ans = 0
for i in range(len(L0)-K+1):
    ans = max(ans,sum(L0[i:i+K])+sum(L1[i:i+K+1]))
print(ans)

Submission status: Wrong Answer

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

if S.count('1') == N:
  print(N)
  exit()

S += '2'
s = S[0]
L0 = []
if s == '0':
  L1 = [0]
else:
  L1 = []
c = 1
for i in range(1,N+1):
  if S[i] != s:
    if s == '0':
      L0.append(c)
    else:
      L1.append(c)
    s = S[i]
    c = 1
  else:
    c += 1
L1 += [0]
#print(L0)
#print(L1)

ans = 0
for i in range(len(L0)-K+1):
    ans = max(ans,sum(L0[i:i+K])+sum(L1[i:i+K+1]))
print(ans)

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
s = input()
cnt = 0
d = s[0]
ss = []
for i in range(N):
  if s[i] == d:
    cnt += 1
  else:
    ss.append(cnt)
    cnt = 1
    d = s[i]
ss.append(cnt)

cs = [0]
if s[0]=="0":
  cs.append(0)
for x in ss:
  cs.append(cs[-1]+x)

ans = 0
for i in range(1,len(cs)-2*K,2):
  ans = max(ans,cs[2*K+i]-cs[i-1])
if 2*K+1>=len(cs):
  ans = cs[-1]
print(ans)

Submission status: Wrong Answer

Solution code:
N, K = map(int,input().split(' '))
S = input()
S2=[]
key=S[0]
tmpcnt=1
if key=='0':
    S2.append(0)
S2.append(0)
S2.append(0)
for i in range(1,len(S)):
    if key!=S[i]:
        if key == '0':
            S2.append(-1*tmpcnt)
        else:
            S2.append(tmpcnt)
        key=S[i]
        tmpcnt=1
    else:
        tmpcnt+=1
if key == '0':
    S2.append(-1*tmpcnt)
    S2.append(0)
    S2.append(0)
else:
    S2.append(tmpcnt)
    S2.append(0)
    S2.append(0)
print(S2)
max=0
tmp=S2[0]
maxj=len(S2)-1
for j in range(1,K*2+1):
    tmp+=abs(S2[min(0+j,maxj)])
    #print(S2[i],S2[min(i+j,maxj)])
if tmp >= max:
    max=tmp
    #print(tmp)

for i in range(2,len(S2),2):
    tmp-=abs(S2[i-1])
    tmp-=abs(S2[i-2])
    tmp+=abs(S2[min(i+j-1,maxj)])
    tmp+=abs(S2[min(i+j,maxj)])
    #print(i-1,i-2,i+j-1,i+j)
    #print(tmp)
    if tmp>= max:
        max=tmp
print(max)

Submission status: Wrong Answer

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


human = []
humancount = []
for key, group in itertools.groupby(S):
    human.append(key)
    humancount.append(len(list(group)))

humancount
human[0]
human

humancount_ac = [0] + list(itertools.accumulate(humancount))

ans = 0
j = 0
temp = 0
count = 0

for i in range(N):
    while j <= len(human)-1:
        print(ans, i, j)
        if human[j] == "0" and count < K:
            count += 1
            j += 1
            ans = max(ans, humancount_ac[j] - humancount_ac[i])
        elif human[j] == "1":
            j += 1
            ans = max(ans, humancount_ac[j] - humancount_ac[i])
        else:
            if human[i] == "0":
                count -= 1
            break
print(ans)

Submission status: Wrong Answer

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

a = []
j = 0
temp = 1
prev = s[0]
for i in s[1:]:
    if i == prev:
        prev = i
        temp += 1
    else:
        a.append(temp)
        prev = i        
        temp = 1
a.append(temp)

b = []
if s[0] == "1":
    b.append(sum(a[:2*k+1]))
    for i in range(len(a) - 2*k + 2):
        if i % 2 == 0:
            try:
                b.append(b[-1] - a[i] - a[i + 1] + a[i+(2*k) -1] + a[i+(2*k)])
            except:
                continue
    if s[-1] == "0":
        b.append(sum(a[-2*k - 2:-1]))
    print(max(b))

else:
    t = a[1:]
    b.append(sum(t[:2*k+1]))
    for i in range(len(t) - 2*k + 2):
        if i % 2 == 0:
            try:
                b.append(b[-1] - t[i] - t[i + 1] + t[i+(2*k) -1] + t[i+(2*k)])
            except:
                continue
    if s[-1] == "0":
        b.append(sum(a[-2*k - 2:-1]))
    b.append(sum(a[:2*k]))
    print(max(b))