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:
k = int(input().split(" ")[1])
s = input()

arr = []
if s[0] is "0": 
  arr.append(0)
arr.append(1)
for i in range(1,len(s)):
  if s[i-1] is s[i]:
    arr[-1] += 1
  else:
    arr.append(1)
if s[-1] is "0": 
  arr.append(0)

t = 2*k+1
if t>=len(s):
  print(len(s))
else:
  m = 0
  for i in range(0,len(arr)-t+1,2):
    m = max(m,sum(arr[i:i+t]))
  print(m)

Submission status: Wrong Answer

Solution code:
k = int(input().split(" ")[1])
s = input()

m = 0

for i in range(len(s)):
  if i>0:
    if s[i] == s[i-1]: continue
  j = i
  count = 0
  while j<len(s) and count <= k:
    if j==i:
      if s[j]=="0": 
        count+=1
    elif s[j-1]=="1" and s[j]=="0": 
      count+=1
    j +=1
  if j == len(s): j+=1
  m = max(m,j-i-1)

print(m)

Submission status: Wrong Answer

Solution code:
k = int(input().split(" ")[1])
s = input()

m = 0

for i in range(len(s)):
  if i>0:
    if s[i] == s[i-1]: continue
  j = i
  count = 0
  while j<len(s) and count <= k:
    if j==i:
      if s[j]=="0": 
        count+=1
    elif s[j-1]=="1" and s[j]=="0": 
      count+=1
    j +=1
  
  m = max(m,j-i)

print(m)

Submission status: Wrong Answer

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


a = [0]
pre = s[0]
for i in range(n):
    if pre != s[i]:
        a.append(i)
    pre = s[i]
a.append(n)
start = s[0] == '1'
right = k * 2 + start
left = 0
cnt = 0
while True:
    if right >= len(a):
        try:
            cnt = max(cnt, a[-1]-a[left])
        except:
            pass
        break
    cnt = max(cnt, a[right]-a[left])
    if not start and left == 0:
        right += 1
        left -= 1
    left += 2
    right += 2

print(cnt)

Submission status: Wrong Answer

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

a = [0]
pre = s[0]
for i in range(n):
    if pre != s[i]:
        a.append(i)
    pre = s[i]
a.append(n)
start = s[0] == '1'
right = k * 2 + start
left = 0
cnt = 0
while True:
    if right >= len(a):
        cnt = max(cnt, a[-1]-a[left])
        break
    cnt = max(cnt, a[right]-a[left])
    if not start and left == 0:
        right += 1
    left += 2
    right += 2

print(cnt)

Submission status: Wrong Answer

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

a = [0]
pre = s[0]
for i in range(n):
    if pre != s[i]:
        a.append(i)
    pre = s[i]
a.append(n)
start = s[0] == '1'
right = k * 2 + start
left = 0
cnt = 0
while True:
    if right >= len(a):
        cnt = max(cnt, a[-1]-a[left])
        break
    cnt = max(cnt, a[right]-a[left])
    if not start and left == 0:
        right += 1
        left -= 1
    left += 2
    right += 2

print(cnt)

Submission status: Wrong Answer

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

a = [0]
pre = s[0]
for i in range(n):
    if pre != s[i]:
        a.append(i)
    pre = s[i]
a.append(n)
start = s[0] == '1'
right = k * 2 + start
left = 0
print(a)
cnt = 0
while True:
    if right >= len(a):
        cnt = max(cnt, a[-1]-a[left])
        break
    cnt = max(cnt, a[right]-a[left])
    if not start and left == 0:
        right += 1
    left += 2
    right += 2

print(cnt)

Submission status: Wrong Answer

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

a = [0]
pre = s[0]
for i in range(n):
    if pre != s[i]:
        a.append(i)
    pre = s[i]
a.append(n)
start = s[0] == '1'
right = k * 2 + start
left = 0
cnt = 0
while True:
    if right >= len(a):
        cnt = max(cnt, a[-1]-a[left])
        break
    cnt = max(cnt, a[right]-a[left])
    if not start and left == 0:
        right += 1
    left += 1
    right += 1

print(cnt)

Submission status: Wrong Answer

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

a = [0]
pre = s[0]
for i in range(n):
    if pre != s[i]:
        a.append(i)
    pre = s[i]
a.append(n)
start = s[0] == '1'
right = k * 2 + start
left = 0
cnt = 0
while True:
    if right >= len(a):
        cnt = max(cnt, a[-1]-a[left])
        break
    cnt = max(cnt, a[right]-a[left])
    left += 1
    right += 1

print(cnt)

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
in_str = input()
st_num = 0
_list = []

l_x = 1
pre_x = in_str[0]

for x in in_str[1:]:
  if pre_x == x:
    l_x += 1
  else:
    _list.append(l_x)
    l_x = 1
    pre_x = x
_list.append(l_x)
print(_list)

L = len(_list)
_list2 = []
for j in range(L//2-M+2):
  if in_str[0] == '1':
    _list2.append(sum(_list[2*j:2*j+2*M+1]))
  else:
    if j == 0:
      _list2.append(sum(_list[0:2*M]))
    else:
      _list2.append(sum(_list[2*j-1:2*j+2*M]))
print(_list2)
if len(_list2) > 0:
  print(max(_list2))
if L == 1:
  print(max(_list))

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
s = list(map(int,input()))
s.append(2)
start = s[0]
choku = [0]
saka = [0]
number = 1
for i in range(N):
    if s[i] == s[i+1]:
        number += 1
    else:
        if s[i] == 0:
            choku.append(number)
            number = 1
        else:
            saka.append(number)
            number = 1
choku.append(0)
saka.append(0)
while(1):
    max_align = 0
    m = len(choku)
    n = len(saka)
    if m <= K:
        print(N)
        break
    if start == "0": #最初が直立の場合
        for i in range(m-K+1):
            if max_align <= sum(choku[i:i+K]) + sum(saka[i-1:i+K]):
                max_align = sum(choku[i:i+K]) + sum(saka[i-1:i+K])
    else:
        for i in range(m-K+1):
            if max_align <= sum(choku[i:i+K]) + sum(saka[i:i+K+1]):
                max_align = sum(choku[i:i+K]) + sum(saka[i:i+K+1])
    print(max_align)
    break

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
in_str = input()
st_num = 0
_list = []

l_x = 1
pre_x = in_str[0]

for x in in_str[1:]:
  if pre_x == x:
    l_x += 1
  else:
    _list.append(l_x)
    l_x = 1
    pre_x = x
_list.append(l_x)

L = len(_list)
_list2 = []
for j in range(L//2-M+1):
  if in_str[0] == '1':
    _list2.append(sum(_list[2*j:2*j+2*M+1]))
  else:
    if j == 0:
      _list2.append(sum(_list[0:2*M]))
    else:
      _list2.append(sum(_list[2*j-1:2*j+2*M]))
if len(_list2) > 0:
  print(max(_list2))
if L == 1:
  print(max(_list))

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
in_str = input()
st_num = 0
_list = []
 
for i, x in enumerate(in_str):
  if i == 0:
    l_x = 1
    pre_x = x
    if x == '1':
      st_num = 1
  else:
    if pre_x == x:
      l_x += 1
    else:
      _list.append(l_x)
      pre_x = x
      l_x = 1
_list.append(l_x)
# print(_list)
# print(st_num)
 
L = len(_list)
 
_list2 = []
# print(L//2,M)
if st_num == 1:
  for j in range(L//2-M+1):
    print(j)
    _list2.append(sum(_list[2*j:2*j+2*M+1]))
if st_num == 0:
  for j in range(L//2-M+1):
    if j == 0:
      _list2.append(sum(_list[0:2*M]))
    else:
      _list2.append(sum(_list[2*j-1:2*j+2*M]))
if not _list2 == []:
  print(max(_list2))
if L == 1:
  print(max(_list))

Submission status: Wrong Answer

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

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

ans = 0
if int(S[0]):
    for i in range(len(state)):
        if i % 2 == 0:
            continue
        else:
            ans = max(ans, sum(state[max(0, i-1):min((len(state)), i+2*K)]))
else:
    for i in range(len(state)):
        if i % 2 == 1:
            continue
        else:
            ans = max(ans, sum(state[max(0, i-1):min((len(state)), i+2*K)]))

if N == 1:
    ans = 1
print(ans)

Submission status: Wrong Answer

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

state = []
for i in range(N):
    if i==0:
        state.append(1)
    else:
        if S[i] == S[i-1]:
            state[-1] += 1
        else:
            state.append(1)
print(state)

ans = 0
if int(S[0]):
    for i in range(len(state)):
        if i % 2 == 0:
            continue
        else:
            ans = max(ans, sum(state[max(0, i-1):min((len(state)), i+2*K)]))
else:
    for i in range(len(state)):
        if i % 2 == 1:
            continue
        else:
            ans = max(ans, sum(state[max(0, i-1):min((len(state)), i+2*K)]))

if N == 1:
    ans = 1
print(ans)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
s=list(map(int,input()))
sp_list=[]
sum=0
sum_0=0
for i in range(n):
  if s[i]==1:
    sum+=1
    if sum_0>0:
      sp_list.append(sum_0)
    sum_0=0
    if i==(n-1):
      sp_list.append(sum)
  else:
    if i==0:
      sp_list.append(0)
    if i>0 and sum>0:
      sp_list.append(sum)
    sum_0+=1
    sum=0
    if i==(n-1):
      sp_list.append(sum_0)
      sp_list.append(0)
sum_list=[0]
sum=0
for i in range(len(sp_list)):
  sum+=sp_list[i]
  sum_list.append(sum)
length=min(k,len(sum_list))
if 2*k+1>=len(sum_list):
  print(sum_list[-1])
else:
  for i in range((len(sum_list)-2*k)//2):
    temp=0
    if temp<(sum_list[2*i+2*k+1]-sum_list[2*i]):
      temp=(sum_list[2*i+2*k+1]-sum_list[i*2])
  print(temp)

Submission status: Wrong Answer

Solution code:
import sys
def main():
  if 'get_ipython' not in globals():  
    # For Subsession
    N,K = map(int, input().split())
    S = sys.stdin.readline()
  else:
    # For Test
    # N = 5
    # K = 1
    # S = "00010"
    N = 14
    K = 2
    S = "11101010110011"
    # N = 1
    # K = 1
    # S = "1"
    
  SC = []
  i = 0
  if S[0] == "0":SC.append(0)
  while i < N :
    if S[i] == "0":
      j = S.find("1",i)
    else:
      j = S.find("0",i)
    if j == -1: j = N 
    cnt = j - i
    i = j
    SC.append(cnt)
    
  if 'get_ipython' in globals(): print(SC)
    
  SH = []
  cnt = 0
  for i in range(len(SC)):
    cnt += SC[i]
    SH.append(cnt)
  
  if 'get_ipython' in globals(): print(SH)
  
  lenSC = len(SC) - 1
  ans = 0
  for i in range(0,lenSC+1,2):
    k = i + K * 2
    if k > lenSC:
      k = lenSC
    if i != 0:
      cnt = SH[k] - SH[i-1]
    else:
      cnt = SH[k] - SH[i]
    if ans < cnt: ans = cnt

  print(ans)

main()

Submission status: Wrong Answer

Solution code:
import sys
def main():
  if 'get_ipython' not in globals():  
    # For Subsession
    N,K = map(int, input().split())
    S = sys.stdin.readline()
  else:
    # For Test
    N = 5
    K = 1
    S = "00010"
    # N = 14
    # K = 2
    # S = "11101010110011"
    # N = 1
    # K = 1
    # S = "1"
    
  SC = []
  i = 0
  if S[0] == "0":SC.append(0)
  while i < N :
    if S[i] == "0":
      j = S.find("1",i)
    else:
      j = S.find("0",i)
    if j == -1: j = N 
    cnt = j - i
    i = j
    SC.append(cnt)
    
  if 'get_ipython' in globals(): print(SC)
    
  SH = []
  cnt = 0
  for i in range(len(SC)):
    cnt += SC[i]
    SH.append(cnt)
  
  if 'get_ipython' in globals(): print(SH)
  
  lenSC = len(SC) - 1
  ans = 0
  for i in range(0,lenSC+1,2):
    k = i + K * 2
    if k > lenSC:
      k = lenSC
    cnt = SH[k] - SH[i]
    if i !=0: cnt += 1
    if ans < cnt: ans = cnt

  print(ans)

main()

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
in_str = input()
st_num = 0
_list = []

for i, x in enumerate(in_str):
  if i == 0:
    l_x = 1
    pre_x = x
    if x == '1':
      st_num = 1
  else:
    if pre_x == x:
      l_x += 1
    else:
      _list.append(l_x)
      pre_x = x
      l_x = 1
_list.append(l_x)
# print(_list)
# print(st_num)

L = len(_list)

_list2 = []
# print(L//2,M)
if st_num == 1:
  for j in range(L//2-M+1):
    print(j)
    _list2.append(sum(_list[2*j:2*j+2*M+1]))
if st_num == 0:
  for j in range(L//2-M+1):
    if j == 0:
      _list2.append(sum(_list[0:2*M]))
    else:
      _list2.append(sum(_list[2*j-1:2*j+2*M]))
if not _list2 == []:
  print(_list2)
if L == 1:
  print(max(_list))

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
s=input()
a=[]
num=1
for i in range(n):
    if i+1<n and s[i]==s[i+1]:
        num+=1
    else:
        a.append(num)
        num=1
start=s[0]
window=2*k
ans=[]
if start=="1":
    window=2*k+1
ans.append(sum(a[0:window]))
if start=="1":
    start="0"
else:
    start="1"
#print(a)
out_flag=False
for i in range(1,len(a)):
    if i+window-1 >len(a)-1:
        window=len(a)-i
        out_flag=True
    next_ans = ans[len(ans)-1]-a[i-1]
    #print(i,window,ans)
    if not out_flag:
        if start=="1":
            next_ans+=a[i+window-1-1]+a[i+window-1]
            start="0"
            window=2*k
        else:
            start="1"
            window=2*k+1
    ans.append(next_ans)
#print(ans)
print(max(ans))

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
s=input()
a=[]
num=1
for i in range(n):
    if i+1<n and s[i]==s[i+1]:
        num+=1
    else:
        a.append(num)
        num=1
start=s[0]
window=2*k
ans=[]
if start=="1":
    window=2*k+1
ans.append(sum(a[0:window]))
if start=="1":
    start="0"
else:
    start="1"
#print(a)
for i in range(1,len(a)):
    if i+window-1 >=len(a)-1:
        window=len(a)-i
    next_ans = ans[len(ans)-1]-a[i-1]
    if window!=len(a)-i:
        if start=="1":
            next_ans+=a[i+window-1-1]+a[i+window-1]
            start="0"
            window=2*k
        else:
            start="1"
            window=2*k+1
    ans.append(next_ans)
#print(ans)
print(max(ans))

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
s=input()
a=[]
num=1
for i in range(n):
    if i+1<n and s[i]==s[i+1]:
        num+=1
    else:
        a.append(num)
        num=1
start=s[0]
window=2*k
ans=[]
if start=="1":
    window=2*k+1
ans.append(sum(a[0:window]))
#print(a)
for i in range(1,len(a)):
    if i+window-1 >=len(a)-1:
        window=len(a)-i
    next_ans = ans[len(ans)-1]-a[i-1]
    if window!=len(a)-i:
        if start=="1":
            next_ans+=a[i+window-1-1]+a[i+window-1]
            start="0"
            window=2*k
        else:
            start="1"
            window=2*k+1
    ans.append(next_ans)
    next_ans = ans[len(ans)-1]
#print(ans)
print(max(ans))

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
s=input()
a=[]
num=1
for i in range(n):
    if i+1<n and s[i]==s[i+1]:
        num+=1
    else:
        a.append(num)
        num=1
start=s[0]
window=2*k
ans=[]
if start=="1":
    window=2*k+1
ans.append(sum(a[0:window]))
#print(a)
for i in range(1,len(a)):
    if i+window-1 >=len(a)-1:
        window=len(a)-i
    next_ans = ans[len(ans)-1]-a[i-1]
    if start=="1":
        next_ans+=a[i+window-1-1]+a[i+window-1]
        start="0"
        window=2*k
    else:
        start="1"
        window=2*k+1
    ans.append(next_ans)
    if window==len(a)-i:
        break
    next_ans = ans[len(ans)-1]
#print(ans)
print(max(ans))

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
s=input()
a=[]
num=1
for i in range(n):
    if i+1<n and s[i]==s[i+1]:
        num+=1
    else:
        a.append(num)
        num=1
start=s[0]
window=2*k
ans=[]
if start=="1":
    window=2*k+1
ans.append(sum(a[0:window]))
#print(a)
for i in range(1,len(a)):
    if i+window-1 >=len(a):
        window=len(a)-i
    next_ans = ans[len(ans)-1]-a[i-1]+a[i+window-1]
    if start=="1":
        next_ans+=a[i+window-1-1]
        start="0"
        window=2*k
    else:
        start="1"
        window=2*k+1
    ans.append(next_ans)
    if window==len(a)-i:
        break
    next_ans = ans[len(ans)-1]
#print(ans)
print(max(ans))

Submission status: Wrong Answer

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

bit_0 = []
bit_1 = []

if S[0] == "0":
	bit_0.append(1)
else:
	bit_1.append(1)
for i in range(1,N):
	if S[i-1] == S[i]:
		if S[i] == "0":
			bit_0[-1] = bit_0[-1] + 1
		else:
			bit_1[-1] = bit_1[-1] + 1
	else:
		if S[i] == "0":
			bit_0.append(1)
		else:
			bit_1.append(1)
max = 0
tmp = 0
for i in range(len(bit_0)):
	if S[0] == "0":
		tmp = sum(bit_0[i:i+K]) + sum(bit_1[i:i+K])
	else:
		tmp = sum(bit_0[i:i+K]) + sum(bit_1[i:i+K+1])
	if tmp > max:
		max = tmp

print(max)

Submission status: Wrong Answer

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

S=[]
F="2"
cnt=0
for i in range(N):
    if F=="0":
        if s[i]=="0":
            cnt-=1
        else:
            S.append(cnt)
            F="1"
            cnt=1
    elif F=="1":
        if s[i]=="1":
            cnt+=1
        else:
            S.append(cnt)
            F="0"
            cnt=-1
    else:
        if s[i]=="0":
            F="0"
            cnt=-1
        else:
            F="1"
            cnt=1
S.append(cnt)
if S[0]<0:
    S=[0]+S+[0]
else:
    S=S+[0]

if S[0]>=0:
    if len(S)<=(2*K+1):
        ans=0
        for i in range(len(S)):
            ans+=abs(S[i])
        print(ans)
        #exit()
    else:
        ans=0
        SUM=0
        for i in range(2*K+1):
            SUM+=abs(S[i])
            ans=SUM
        #print(ans)
        for i in range(1,len(S)):
            if (2*i+2*K)>(len(S)-1):
                #print("A")
                break
            else:
                #print("B")
                SUM=SUM+abs(S[2*i+2*K-1])+abs(S[2*i+2*K])-abs(S[2*i-1])-abs(S[2*i-2])
                if SUM>ans:
                    ans=SUM
        print(ans)
        #exit()
print(S)

Submission status: Wrong Answer

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

S=[]
F="2"
cnt=0
for i in range(N):
    if F=="0":
        if s[i]=="0":
            cnt-=1
        else:
            S.append(cnt)
            F="1"
            cnt=1
    elif F=="1":
        if s[i]=="1":
            cnt+=1
        else:
            S.append(cnt)
            F="0"
            cnt=-1
    else:
        if s[i]=="0":
            F="0"
            cnt=-1
        else:
            F="1"
            cnt=1
S.append(cnt)
if S[0]<0:
    S=[0]+S+[0]

if S[0]>=0:
    if len(S)<=(2*K+1):
        ans=0
        for i in range(len(S)):
            ans+=abs(S[i])
        print(ans)
        #exit()
    else:
        ans=0
        SUM=0
        for i in range(2*K+1):
            SUM+=abs(S[i])
            ans=SUM
        #print(ans)
        for i in range(1,len(S)):
            if (2*i+2*K)>(len(S)-1):
                #print("A")
                break
            else:
                #print("B")
                SUM=SUM+abs(S[2*i+2*K-1])+abs(S[2*i+2*K])-abs(S[2*i-1])-abs(S[2*i-2])
                if SUM>ans:
                    ans=SUM
        print(ans)
        #exit()
#print(S)

Submission status: Wrong Answer

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

S=[]
F="2"
cnt=0
for i in range(N):
    if F=="0":
        if s[i]=="0":
            cnt-=1
        else:
            S.append(cnt)
            F="1"
            cnt=1
    elif F=="1":
        if s[i]=="1":
            cnt+=1
        else:
            S.append(cnt)
            F="0"
            cnt=-1
    else:
        if s[i]=="0":
            F="0"
            cnt=-1
        else:
            F="1"
            cnt=1
S.append(cnt)
if S[0]<0:
    S=[0]+S+[0]

if S[0]>=0:
    if len(S)<=(2*K+1):
        ans=0
        for i in range(len(S)):
            ans+=abs(S[i])
        print(ans)
        #exit()
    else:
        ans=0
        SUM=0
        for i in range(2*K+1):
            SUM+=abs(S[i])
            ans=SUM
        #print(ans)
        for i in range(1,len(S)):
            if (2*i+2*K)>(len(S)-1):
                #print("A")
                break
            else:
                #print("B")
                SUM=SUM+abs(S[2*i+2*K-1])+abs(S[2*i+2*K])-abs(S[2*i-1])-abs(S[2*i-2])
                if SUM>ans:
                    ans=SUM
        print(ans)
        #exit()
print(S)

Submission status: Wrong Answer

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

S=[]
F="2"
cnt=0
for i in range(N):
    if F=="0":
        if s[i]=="0":
            cnt-=1
        else:
            S.append(cnt)
            F="1"
            cnt=1
    elif F=="1":
        if s[i]=="1":
            cnt+=1
        else:
            S.append(cnt)
            F="0"
            cnt=-1
    else:
        if s[i]=="0":
            F="0"
            cnt=-1
        else:
            F="1"
            cnt=1
S.append(cnt)
if S[0]<0:
    S=[0]+S

if S[0]>=0:
    if len(S)<=(2*K+1):
        ans=0
        for i in range(len(S)):
            ans+=abs(S[i])
        print(ans)
        exit()
    else:
        ans=0
        SUM=0
        for i in range(2*K+1):
            SUM+=abs(S[i])
            ans=SUM
        #print(ans)
        for i in range(1,len(S)):
            if (2*i+2*K)>(len(S)-1):
                #print("A")
                break
            else:
                #print("B")
                SUM=SUM+abs(S[2*i+2*K-1])+abs(S[2*i+2*K])-abs(S[2*i-1])-abs(S[2*i-2])
                if SUM>ans:
                    ans=SUM
        print(ans)

Submission status: Wrong Answer

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

S=[]
F="2"
cnt=0
for i in range(N):
    if F=="0":
        if s[i]=="0":
            cnt-=1
        else:
            S.append(cnt)
            F="1"
            cnt=1
    elif F=="1":
        if s[i]=="1":
            cnt+=1
        else:
            S.append(cnt)
            F="0"
            cnt=-1
    else:
        if s[i]=="0":
            F="0"
            cnt=-1
        else:
            F="1"
            cnt=1
S.append(cnt)
if S[0]<0:
    S=[0]+S

if S[0]>=0:
    if len(S)<=(2*K-1):
        ans=0
        for i in range(len(S)):
            ans+=abs(S[i])
        print(ans)
        #exit()
    else:
        ans=0
        for i in range(2*K+1):
            ans+=abs(S[i])
        for i in range(len(S)):
            if (2*i+2*K+2)>(len(S)-1):
                break
            else:
                SUM=ans+S[2*i+2*K+1]+S[2*i+2*K+2]-S[2*i-1]-S[2*i-2]
                if SUM>ans:
                    ans=SUM
        print(ans)

Submission status: Wrong Answer

Solution code:
tmp_lst = input().split()
N, K = int(tmp_lst[0]), int(tmp_lst[1])
S = input()

start_index = 0
stop_index = 1
zerorand_counter = 0
max_len = 0
for i in range(N-1):
    if (S[i] == "0" and S[i+1] == "1") or i + 1 == N - 1:
        zerorand_counter += 1
    if zerorand_counter == K:
        for stop in range(i+1,N-1):
            if stop + 1 == N - 1:
                stop_index = N
            if S[stop] == "1" and S[stop+1] == "0":
                stop_index = stop + 1
                break
        start_index = 0
        max_len = stop_index - start_index
#        print("start_index, stop_index =", start_index, stop_index)
        break

if N == 1:
    if S == "0" and K == 0:
        max_len = 0
    elif S == "1":
        max_len = 1
#print("init_max_len =", max_len)

for start in range(0, N-1):
    if S[start] == "0" and S[start+1] == "1":
        for stop in range(stop_index, N-1):
            if (S[stop] == "1" and S[stop+1] == "0") or stop + 1 == N - 1:
                stop_index = stop + 1
                if stop + 1 == N - 1:
                    stop_index = N
                start_index = start + 1
                length = stop_index - start_index
#                print("start_index, stop_index, length =", start_index, stop_index, length)
                if length > max_len:
                    max_len = length
                break
    if stop_index == N:
        break
print(max_len)

Submission status: Wrong Answer

Solution code:
def listCount(Se):
  slist = []
  cnt0 = 0
  cnt1 = 0
  for i in range(len(Se)):
    if Se[i] == '1':
      cnt1 = cnt1 + 1
      if cnt0 != 0:
        slist.append(cnt0)
        cnt0 = 0
    elif Se[i] == '0':
      cnt0 = cnt0 + 1
      if cnt1 != 0:
        slist.append(cnt1)
      cnt1 = 0
  slist.append(cnt1)
  return slist

def listSum(slist, snum, K):
  
  offset = 2 * K
  for i in range(offset):
    slist.insert(snum, 0)
  for i in range(offset):
    slist.insert(0, 0)

  Ke = 2 * K + 1
  val = 0
  for j in range(offset, offset + Ke):
    val = val + slist[j]
  
  max1val = val
  max1idx = 0
  max2val = 0
  max2idx = 0
  max3val = 0
  max3idx = 0

  vallist = [val]
  for i in range(offset + 2, offset + snum-(Ke)+1, 2):
    for j in range(K):
      val = val - slist[i-j-1] + slist[i+Ke-j-1]
    if val > max1val:
      max3val = max2val
      max3idx = max2idx
      max2val = max1val
      max2idx = max1idx
      max1val = val
      max1idx = i
    vallist.append(val)
    
  for i in range(offset):
    slist.pop(0)
  for i in range(offset):
    slist.pop(snum)

  return vallist, max1idx, max1val, max2val

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

isF0 = False
isE0 = False
Se = S
Ne = N
if Se[Ne-1] == '0':
  Se = Se + '1'
  Ne = Ne + 1
  isE0 = True
if Se[0] == '0':
  Se = '1' + Se
  Ne = Ne + 1
  isF0 = True

slist = listCount(Se)

snum = len(slist)
vallist, max1idx, max1val, max2val = listSum(slist, snum, K)

if max1idx != 0 and max1idx != snum - (2 * K + 1):
  maxval = max1val
elif max1val-1 > max2val:
  maxval = max1val-1
else:
  maxval = max2val

print(maxval)

Submission status: Wrong Answer

Solution code:
def listCount(Se):
  slist = []
  cnt0 = 0
  cnt1 = 0
  for i in range(len(Se)):
    if Se[i] == '1':
      cnt1 = cnt1 + 1
      if cnt0 != 0:
        slist.append(cnt0)
        cnt0 = 0
    elif Se[i] == '0':
      cnt0 = cnt0 + 1
      if cnt1 != 0:
        slist.append(cnt1)
      cnt1 = 0
  slist.append(cnt1)
  return slist

def listSum(slist, snum, K):
  
  offset = 2 * K
  for i in range(offset):
    slist.insert(snum, 0)
  for i in range(offset):
    slist.insert(0, 0)

  Ke = 2 * K + 1
  val = 0
  for j in range(offset, offset + Ke):
    val = val + slist[j]
  
  max1val = val
  max1idx = 0
  max2val = val
  max2idx = 0
  max2val = val
  max2idx = 0

  vallist = [val]
  for i in range(offset + 2, offset + snum-(Ke)+1, 2):
    for j in range(K):
      val = val - slist[i-j-1] + slist[i+Ke-j-1]
    if val > max1val:
      max3val = max2val
      max3idx = max2idx
      max2val = max1val
      max2idx = max1idx
      max1val = val
      max1idx = i
    vallist.append(val)

  for i in range(offset):
    slist.pop(0)
  for i in range(offset):
    slist.pop(snum)

  return vallist, max1idx, max1val, max2val

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

isF0 = False
isE0 = False
Se = S
Ne = N
if Se[Ne-1] == '0':
  Se = Se + '1'
  Ne = Ne + 1
  isE0 = True
if Se[0] == '0':
  Se = '1' + Se
  Ne = Ne + 1
  isF0 = True

slist = listCount(Se)

snum = len(slist)
vallist, max1idx, max1val, max2val = listSum(slist, snum, K)

if max1idx != 0 and max1idx != snum - (2 * K + 1):
  maxval = max1val
elif max1val-1 > max2val:
  maxval = max1val-1
else:
  maxval = max2val

print(maxval)

Submission status: Wrong Answer

Solution code:
lftIdx, rgtIdx, ans = 0, 0, 0
n, k = map(int, input().split())
s = input()


def move(lr, idx):
    # lr: lft := "1", rgt := "0"
    # return: idx
    global s
    if 0 < idx < n-1:
        idx += 1
    contains_zero = False
    while True:
        if idx == n-1:
            return idx
        if s[idx] == "0":
            contains_zero = True
        if contains_zero and s[idx] != s[idx+1] and s[idx+1] == lr:
            return idx
        idx += 1


def solv():
    global lftIdx, rgtIdx, ans, n, k, s
    for i in range(k):
        rgtIdx = move("0", rgtIdx)
    ans = max(ans, rgtIdx + 1)
    while n-1 > rgtIdx:
        rgtIdx = move("0", rgtIdx)
        lftIdx = move("1", lftIdx)
        ans = max(ans, rgtIdx - lftIdx)  # (rgtIdx + 1) - (lftIdx + 1)
    return


solv()
print(ans)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
s = input()
pre = s[0]
max_cnt = 0
stack = []
cnt = 0
s += '2'
if pre == '0':
    k -= 1
    stack = [0, 0]

for a in s:
    if a == pre:
        cnt += 1
    else:
        if a == '1':
            k -= 1
        if k < 0:
            stack.pop(0)
        stack.append(cnt)
        cnt = 1
        pre = a
        max_cnt = max(max_cnt, sum(stack))

print(max_cnt)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
s = input()
print(k)
pre = s[0]
max_cnt = 0
stack = []
cnt = 0
s += '2'
if pre == '0':
    k -= 1
    stack = [0, 0]

for a in s:
    if a == pre:
        cnt += 1
    else:
        if a == '1':
            k -= 1
        if k < 0:
            stack.pop(0)
        stack.append(cnt)
        cnt = 1
        pre = a
        max_cnt = max(max_cnt, sum(stack))

print(max_cnt)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split(' '))
s = input()
s10 = []
cnt = 0
s_prev = '1'
for si in s+'x':
  if si == s_prev:
    cnt += 1
  else:
    s10.append(cnt)
    cnt = 1
  s_prev = si
if s[-1] == '0':
  s10.append(0)

sum_max= 0
for i in range(len(s10)//2-k+1):
  sum_i = sum(s10[2*i:2*i+2*k+1])
  sum_max = max(sum_i, sum_max)
  
print(sum_max)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split(' '))
s = input()
s10 = []
cnt = 0
s_prev = '1'
for si in s+'x':
  if si == s_prev:
    cnt += 1
  else:
    s10.append(cnt)
    cnt = 1
  s_prev = si
if s[-1] == '0':
  s10.append(0)

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

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split(' '))
s = input()
s10 = []
cnt = 0
s_prev = '1'
for si in s+'x':
  if si == s_prev:
    cnt += 1
  else:
    s10.append(cnt)
    cnt = 1
  s_prev = si
if s[-1] == '0':
  s10.append(0)
  
sum_max= 0
for i in range(len(s10)):
  sum_i = sum(s10[i:i+2*k+2])
  sum_max = max(sum_i, sum_max)
  
print(sum_max)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split(' '))
s = input()
s10 = []
cnt = 0
s_prev = '1'
for si in s:
  if si == s_prev:
    cnt += 1
  else:
    s10.append(cnt)
    cnt = 1
  s_prev = si
s10.append(cnt)
if s[-1] == '0':
  s10.append(0)
  
sum_max= 0
for i in range(len(s10)):
  sum_i = sum(s10[i:i+2*k+2])
  sum_max = max(sum_i, sum_max)
  
print(sum_max)