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 sys, bisect, math, itertools, string, queue, copy
# import numpy as np
# import scipy
from collections import Counter,defaultdict,deque
from itertools import permutations, combinations
from heapq import heappop, heappush
input = sys.stdin.readline
sys.setrecursionlimit(10**8)
mod = 10**9+7
def inp(): return int(input()) # n=1
def inpm(): return map(int,input().split()) # x=1,y=2
def inpl(): return list(map(int, input().split())) # a=[1,2,3,4,5,...,n]
def inpls(): return list(input().split())  # a=['1','2','3',...,'n']
def inplm(n): return list(int(input()) for _ in range(n)) # x=[] 複数行
def inplL(n): return [list(input()) for _ in range(n)]
def inplT(n): return [tuple(input()) for _ in range(n)]
def inpll(n): return [list(map(int, input().split())) for _ in range(n)] # [[1,1,1,1],[2,2,2,2],[3,3,3,3]] 
def inplls(n): return sorted([list(map(int, input().split())) for _ in range(n)]) # [[1,1,1,1],[2,2,2,2],[3,3,3,3]] 
def graph():
    n=inp()
    g=[[] for _ in range(n)]
    for i in range(n):
        a = inp()
        a -= 1
        g[i].append(a)
        g[a].append(i)
    return n,g

def main():
    n,k = inpm()
    s = list(input())
    t = [0]
    for i in range(n-1):
        if s[i] != s[i+1]:
            t.append(i+1)
    a = len(t)
    if a <= 2*k - 1:
        print(n)
        return
    for i in range(k*2+1):
        t.append(n)
    ans = 0
    if s[0]:
        for i in range(a//2):
            ans = max(ans,t[2*k+2*i]-t[2*i])
    else:
        for i in range(a//2):
            ans = max(ans,t[2*k+2*i+2]-t[2*i+1])
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import sys, bisect, math, itertools, string, queue, copy
# import numpy as np
# import scipy
from collections import Counter,defaultdict,deque
from itertools import permutations, combinations
from heapq import heappop, heappush
input = sys.stdin.readline
sys.setrecursionlimit(10**8)
mod = 10**9+7
def inp(): return int(input()) # n=1
def inpm(): return map(int,input().split()) # x=1,y=2
def inpl(): return list(map(int, input().split())) # a=[1,2,3,4,5,...,n]
def inpls(): return list(input().split())  # a=['1','2','3',...,'n']
def inplm(n): return list(int(input()) for _ in range(n)) # x=[] 複数行
def inplL(n): return [list(input()) for _ in range(n)]
def inplT(n): return [tuple(input()) for _ in range(n)]
def inpll(n): return [list(map(int, input().split())) for _ in range(n)] # [[1,1,1,1],[2,2,2,2],[3,3,3,3]] 
def inplls(n): return sorted([list(map(int, input().split())) for _ in range(n)]) # [[1,1,1,1],[2,2,2,2],[3,3,3,3]] 
def graph():
    n=inp()
    g=[[] for _ in range(n)]
    for i in range(n):
        a = inp()
        a -= 1
        g[i].append(a)
        g[a].append(i)
    return n,g

def main():
    n,k = inpm()
    s = list(input())
    t = [0]
    for i in range(n-1):
        if s[i] != s[i+1]:
            t.append(i+1)
    a = len(t)
    if a <= 2*k - 1:
        print(n)
        return
    for i in range(k*2+1):
        t.append(n)
    ans = 0
    if s[0]:
        for i in range(a//2):
            ans = max(ans,t[2*k+2*i+1]-t[2*i])
    else:
        for i in range(a//2):
            ans = max(ans,t[2*k+2*i+2]-t[2*i+1])
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import sys, bisect, math, itertools, string, queue, copy
# import numpy as np
# import scipy
from collections import Counter,defaultdict,deque
from itertools import permutations, combinations
from heapq import heappop, heappush
input = sys.stdin.readline
sys.setrecursionlimit(10**8)
mod = 10**9+7
def inp(): return int(input()) # n=1
def inpm(): return map(int,input().split()) # x=1,y=2
def inpl(): return list(map(int, input().split())) # a=[1,2,3,4,5,...,n]
def inpls(): return list(input().split())  # a=['1','2','3',...,'n']
def inplm(n): return list(int(input()) for _ in range(n)) # x=[] 複数行
def inplL(n): return [list(input()) for _ in range(n)]
def inplT(n): return [tuple(input()) for _ in range(n)]
def inpll(n): return [list(map(int, input().split())) for _ in range(n)] # [[1,1,1,1],[2,2,2,2],[3,3,3,3]] 
def inplls(n): return sorted([list(map(int, input().split())) for _ in range(n)]) # [[1,1,1,1],[2,2,2,2],[3,3,3,3]] 
def graph():
    n=inp()
    g=[[] for _ in range(n)]
    for i in range(n):
        a = inp()
        a -= 1
        g[i].append(a)
        g[a].append(i)
    return n,g

def main():
    n,k = inpm()
    s = list(input())
    t = [0]
    for i in range(n-1):
        if s[i] != s[i+1]:
            t.append(i+1)
    a = len(t)
    if a <= 2*k - 1:
        print(n)
        return
    print(t)
    for i in range(k*2+1):
        t.append(n)
    print(t)
    ans = 0
    if s[0]:
        for i in range(a//2):
            ans = max(ans,t[2*k+2*i+1]-t[2*i])
    else:
        for i in range(a//2):
            ans = max(ans,t[2*k+2*i+2]-t[2*i+1])
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
s=input()
if '0' not in s:
    print(len(s))
else:
    s2 = [0]
    count = 0
    for i in range(len(s)-1):
        count += 1
        if s[i] != s[i+1]:
            s2.append(count)
    s2.append(count+1)
    if s[0] == '0':
        s2.insert(0,0)
    ans = 0
    for i in range(0,len(s2)-2*K,2):
        ans = max(ans,s2[min(len(s2)-1,i+2*K+1)] - s2[i])
    print(ans)

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
s=input()
if '0' not in s:
    print(len(s))
else:
    s2 = [0]
    count = 0
    for i in range(len(s)-1):
        count += 1
        if s[i] != s[i+1]:
            s2.append(count)
    s2.append(count+1)
    if s[0] == '0':
        s2.insert(0,0)
    ans = 0
    for i in range(0,len(s2)-2*K-1,2):
        ans = max(ans,s2[i+2*K+1] - s2[i])
    print(ans)

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
s=input()
if '0' not in s:
    print(len(s))
else:
    s2 = []
    count = 1
    for i in range(1,len(s)):
        if s[i-1] == s[i]:
            count += 1
        if s[i-1] != s[i]:
            s2.append(count)
            count = 1
    s2.append(count)
    if s[0] != '1':
        s2.insert(0,0)
    if s[-1] != '1':
        s2.append(0)
    ans2 = 0
    K2 = 2*K+1
    for i in range(max(1,len(s2) - K2 +1)):
        ans2 = max(ans2,sum(s2[i:i+K2]))
    print(ans2)

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
s=input()
if '0' not in s:
    print(len(s))
else:
    ans = ''
    count = 1
    for i in range(1,len(s)):
        if s[i-1] == s[i]:
            count += 1
        if s[i-1] != s[i]:
            ans += s[i-1] + str(count)
            count = 1
    s2 = ans+s[-1]+str(count)
    if s2[0] != '1':
        s2 = '10'+s2
    if s2[-2] != '1':
        s2 += '10'
    s3 = [int(s2[i]) for i in range(len(s2)) if i%2 == 1]
    ans2 = 0
    K2 = 2*K+1
    for i in range(len(s3) - K2 +1):
        ans2 = max(ans2,sum(s3[i:i+K2]))
    print(ans2)

Submission status: Wrong Answer

Solution code:
import sys, bisect, math, itertools, string, queue, copy
# import numpy as np
# import scipy
from collections import Counter,defaultdict,deque
from itertools import permutations, combinations
from heapq import heappop, heappush
# input = sys.stdin.readline
sys.setrecursionlimit(10**8)
mod = 10**9+7

def inp(): return int(input()) # n=1
def inpm(): return map(int,input().split()) # x=1,y=2
def inpl(): return list(map(int, input().split())) # a=[1,2,3,4,5,...,n]
def inpls():return list(input().split())  # a=['1','2','3',...,'n']
def inpim(n): return list(int(input()) for _ in range(n)) # x=[] 複数行
def inplm(n): return [list(input()) for _ in range(n)]
def inptm(n): return [tuple(input()) for _ in range(n)]
def inpll(n): return [list(map(int, input().split())) for _ in range(n)] # [[1,1,1,1],[2,2,2,2],[3,3,3,3]] 
def inplls(n): return sorted([list(map(int, input().split())) for _ in range(n)]) # [[1,1,1,1],[2,2,2,2],[3,3,3,3]] 
def graph():
    n=inp()
    g=[[] for _ in range(n)]
    for i in range(n):
        a = inp()
        a -= 1
        g[i].append(a)
        g[a].append(i)
    return n,g


def main():
    n,k = inpm()
    s = list(input())
    for i in range(n):
        s[i] = int(s[i])
    r = 0
    cnt = 0
    key = 1
    que = []
    conter = 0
    while r < n:
        if s[r]:
            if key == 0:
                que.append(conter)
                conter = 0
            conter += 1
            key = 1
            r += 1
        elif (not s[r]) and key:
            if conter != 0:
                que.append(conter)
            conter = 1
            key = 0
            cnt += 1
            r += 1
        else:
            conter += 1
            r += 1
    que.append(conter)
    m = len(que)
    index_l = 0
    index_r = 0
    cnt = 0
    if s[0]:
        index_r = 2*k
    else:
        index_r = 2*k - 1
    if index_r >= m:
        print(n)
        return
    for i in range(index_r+1):
        cnt += que[i]
    ans = cnt
    while index_r < m:
        cnt -= que[index_l]
        cnt -= que[index_l+1]
        index_r += 1
        index_l += 2
        if index_r == m:
            print(max(ans,cnt))
            return
        cnt += que[index_r]
        index_r += 1
        if index_r == m:
            print(max(ans,cnt))
            return
        cnt += que[index_r]
        ans = max(ans,cnt)
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import sys, bisect, math, itertools, string, queue, copy
# import numpy as np
# import scipy
from collections import Counter,defaultdict,deque
from itertools import permutations, combinations
from heapq import heappop, heappush
# input = sys.stdin.readline
sys.setrecursionlimit(10**8)
mod = 10**9+7

def inp(): return int(input()) # n=1
def inpm(): return map(int,input().split()) # x=1,y=2
def inpl(): return list(map(int, input().split())) # a=[1,2,3,4,5,...,n]
def inpls():return list(input().split())  # a=['1','2','3',...,'n']
def inpim(n): return list(int(input()) for _ in range(n)) # x=[] 複数行
def inplm(n): return [list(input()) for _ in range(n)]
def inptm(n): return [tuple(input()) for _ in range(n)]
def inpll(n): return [list(map(int, input().split())) for _ in range(n)] # [[1,1,1,1],[2,2,2,2],[3,3,3,3]] 
def inplls(n): return sorted([list(map(int, input().split())) for _ in range(n)]) # [[1,1,1,1],[2,2,2,2],[3,3,3,3]] 
def graph():
    n=inp()
    g=[[] for _ in range(n)]
    for i in range(n):
        a = inp()
        a -= 1
        g[i].append(a)
        g[a].append(i)
    return n,g


def main():
    n,k = inpm()
    s = list(input())
    for i in range(n):
        s[i] = int(s[i])
    l = 0
    r = 0
    cnt = 0
    key = 1
    que = deque()
    conter = 0
    while cnt <= k:
        if s[r]:
            if key == 0:
                que.append(conter)
                conter = 0
            conter += 1
            key = 1
            r += 1
        elif (not s[r]) and key:
            if cnt == k:
                conter = 0
                break
            if conter != 0:
                que.append(conter)
            conter = 1
            key = 0
            cnt += 1
            if cnt > k:
                break
            r += 1
        else:
            conter += 1
            r += 1
        if r == n:
            print(n)
            return
    ans = r - l
    road = ans
    a,b = 0,0
    while r < n:
        if s[r]:
            if key == 0:
                a = conter
                que.append(conter)
                conter = 0
            conter += 1
            key = 1
            r += 1
        elif (not s[r]) and key:
            if conter != 0:
                x = que.popleft()
                y = que.popleft()
                b = conter
                road -= x + y
                road += a + b
                a,b = 0,0
                ans = max(ans,road)
                que.append(conter)
            conter = 1
            key = 0
            r += 1
        else:
            conter += 1
            r += 1
    x = 0
    if que:
        x = que.popleft()
    y = 0
    if que:
        y = que.popleft()
    ans = max(ans,road+a+conter-x-y)
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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


count0 = 0
count1 = 0
t = []
for i in range(n):
    if s[i] == '0':
        count0 += 1
        if s[i-1] == '1':
            t.append(count1)
        count1 = 0
    else:
        count1 += 1
        if s[i-1] == '0':
            t.append(count0)
        count0 = 0
    if i == n-1:
        if count1 > 0:
            t.append(count1)
        else:
            t.append(count0)

ans = 0
# 累積和
sums = [sum(t[:i + 1]) for i in range(len(t))]
# 左端に0個の1があるとして、0を付け足しておく
sums = [0] + sums
for left in range(0, len(sums), 2):
    if left == 0:
        right = left+2*k
    else:
        right = left+2*k+1
    if right >= len(sums):
        right = len(sums) - 1
    ans = max(ans, sums[right] - sums[left])
print(ans)

Submission status: Wrong Answer

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

ans = 0
for i in range(n):
    if i != 0 and s[i-1] == '1':
        continue
    if i != 0 and s[i] == '0' and s[i-1] == '0':
        continue
    inv_count = 0
    continuous_count = 0
    if i == 0 and s[0] == '0':
        inv_count += 1
    for j in range(i, n):
        if s[j-1] != '0' and s[j] == '0':
            inv_count += 1
        if inv_count > k:
            break
        continuous_count += 1
    ans = max(ans, continuous_count)
print(ans)

Submission status: Wrong Answer

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

ans = 0
for i in range(n):
    if i != 0 and s[i-1] == '1':
        continue
    if i != 0 and s[i] == '0' and s[i-1] == '0':
        continue
    inv_count = 0
    continuous_count = 0
    for j in range(i, n):
        if i == 0 and s[0] == '0':
            inv_count += 1
        elif s[j-1] != '0' and s[j] == '0':
            inv_count += 1
        if inv_count > k:
            break
        continuous_count += 1
    ans = max(ans, continuous_count)
print(ans)

Submission status: Wrong Answer

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

ans = 0
for i in range(n):
    if i != 0 and s[i-1] == '1':
        continue
    inv_count = 0
    continuous_count = 0
    for j in range(i, n):
        if i == 0 and s[0] == '0':
            inv_count += 1
        elif s[j-1] != '0' and s[j] == '0':
            inv_count += 1
        if inv_count > k:
            break
        continuous_count += 1
    ans = max(ans, continuous_count)
print(ans)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
s = list(input())
s_0_1 = [1]*(k+int(s[0]))
s_1_0 = []
for i in range(n-1):
    if s[i] == "0" and s[i+1] == "1":
        s_0_1.append(i+2)
    elif s[i] == "1" and s[i+1] == "0":
        s_1_0.append(i+1)
s_1_0.extend([n]*(k+int(s[-1])))
print(s_0_1)
print(s_1_0)

ans = 0
for i in range(len(s_0_1)):
    ans = max(ans, s_1_0[i]-s_0_1[i]+1)
print(ans)

Submission status: Wrong Answer

Solution code:
# ABC124 D - Handstand
from math import ceil
n,k=map(int,input().split())
s=input()

l=[]
tmp=''
t_cnt=0
for i,ss in enumerate(s):
    if i==0:
        tmp=ss
        t_cnt+=1
        if len(s)==1:
            l.append(t_cnt)
            break
        continue
    if i==n-1:
        if ss==tmp:
            t_cnt+=1
            l.append(t_cnt)
        else:
            t_cnt=1
            l.append(t_cnt)
        continue
    if ss==tmp:
        t_cnt+=1
    else:
        l.append(t_cnt)
        t_cnt=1
        tmp=ss

if len(l)==1:
    print(1)
    exit()

l+=[0]+l
ans=0
if s[0]=='0':
    for i in range(0,min(2*k,(len(l)-2*k-1)//2),2):
        if i==0:
            ans=sum(l[:2*k])
            continue
        ans=max(ans,ans+l[i+1]-l[i-1])
else:
    for i in range(1,min(2*k+1,(len(l)-2*k-1)//2)+1,2):
        if i==1:
            ans=sum(l[:2*k+1])
            continue
        ans=max(ans,ans+l[i+1]-l[i-1])
    
print(ans)

Submission status: Wrong Answer

Solution code:
# ABC124 D - Handstand
from math import ceil
n,k=map(int,input().split())
s=input()

l=[]
tmp=''
t_cnt=0
for i,ss in enumerate(s):
    if i==0:
        tmp=ss
        t_cnt+=1
        if len(s)==1:
            l.append(t_cnt)
            break
        continue
    if i==n-1:
        if ss==tmp:
            t_cnt+=1
            l.append(t_cnt)
        else:
            t_cnt=1
            l.append(t_cnt)
        continue
    if ss==tmp:
        t_cnt+=1
    else:
        l.append(t_cnt)
        t_cnt=1
        tmp=ss

if len(l)==1:
    print(1)
    exit()

l+=[0]
ans=0
if s[0]=='0':
    for i in range(0,len(l)-1,2):
        if i==0:
            ans=sum(l[:2*k])
            continue
        ans=max(ans,ans+l[i+1]-l[i-1])
else:
    for i in range(1,len(l)-1,2):
        if i==1:
            ans=sum(l[:2*k+1])
            continue
        ans=max(ans,ans+l[i+1]-l[i-1])
    
print(ans)

Submission status: Wrong Answer

Solution code:
# ABC124 D - Handstand
from math import ceil
n,k=map(int,input().split())
s=input()

l=[]
tmp=''
t_cnt=0
for i,ss in enumerate(s):
    if i==0:
        tmp=ss
        t_cnt+=1
        if len(s)==1:
            l.append(t_cnt)
            break
        continue
    if i==n-1:
        if ss==tmp:
            t_cnt+=1
            l.append(t_cnt)
        else:
            t_cnt=1
            l.append(t_cnt)
        continue
    if ss==tmp:
        t_cnt+=1
    else:
        l.append(t_cnt)
        t_cnt=1
        tmp=ss

l+=[0]
ans=0
if s[0]=='0':
    for i in range(0,len(l)-1,2):
        if i==0:
            ans=sum(l[:2*k])
            continue
        ans=max(ans,ans+l[i+1]-l[i-1])
else:
    for i in range(1,len(l)-1,2):
        if i==1:
            ans=sum(l[:2*k+1])
            continue
        ans=max(ans,ans+l[i+1]-l[i-1])
    
print(ans)

Submission status: Wrong Answer

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

c0 = S.count('0')
if c0 == 0 or c0 == N:
  ans = N
else:
  A = [[0, 0, 0]]
  c = 0
  now = '1' if S[0]=='0' else '0'
  for s in S:
    if s == '0':
      if now == '1':
        A[-1][2] = c
        A.append([c, 1, 0])
        c = 1
        now = '0'
      else:
        c += 1
    else:
      if now == '1':
        c += 1
      else:
        A[-1][1] = c
        c = 1
        now = '1'
  
  A = A[1:]  
  if s == '0':
    if S[-2] == '0':
      A[-1][1] = c
  else:
    if S[-2] == '0':
      A[-1][2] = 1
    else:
      A[-1][2] = c
  m = len(A)
  ans = 0
  for i, a in enumerate(A):
    ans += a[0]+a[1]
    if i == K-1:
      ans += a[2]
      break

  l = ans
  for i in range(1, m-K+1):
    a1 = A[i-1]
    a2 = A[i+K-1]
    l = l-(a1[0]+a1[1])+(a2[1]+a2[2])
    ans = max(ans, l)


print(ans)

Submission status: Wrong Answer

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

cnt = 0
for i in range(n):
    if i == 0 and s[i] == '0':
        ss.append(0)

    if i > 1 and s[i] != s[i-1]:
        ss.append(cnt)
        cnt = 0

    cnt += 1

ss.append(cnt)

if s[-1] == '0':
    ss.append(0)

#print(ss)
ans = 0
i = 0
while i < n:
    #print(ans, i, sum(ss[i:i+2*k+1]))
    #if i+2*k+1 > n:
    #    break
    ans = max(ans, sum(ss[i:i+2*k+1]))

    i += 2

print(ans)

Submission status: Wrong Answer

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

cnt = 0
for i in range(n):
    if i == 0 and s[i] == '0':
        ss.append(0)

    if i > 1 and s[i] != s[i-1]:
        ss.append(cnt)
        cnt = 0

    cnt += 1

ss.append(cnt)

if s[-1] == '0':
    ss.append(0)

#print(ss)
ans = 0
i = 0
while i+2*k+1 < n:
    #print(ans, i, sum(ss[i:i+2*k+1]))
    ans = max(ans, sum(ss[i:i+2*k+1]))

    i += 2 * k

print(1 if k == 1 else ans)

Submission status: Wrong Answer

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

cnt = 0
for i in range(n):
    if i == 0 and s[i] == '0':
        ss.append(0)

    if i > 1 and s[i] != s[i-1]:
        ss.append(cnt)
        cnt = 0

    cnt += 1

ss.append(cnt)

if s[-1] == '0':
    ss.append(0)

#print(ss)
ans = 0
i = 0
while i < n:
    #print(ans, i, sum(ss[i:i+2*k+1]))
    ans = max(ans, sum(ss[i:i+2*k+1]))

    i += 2 * k

print(ans)

Submission status: Wrong Answer

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

T = []
i = 0
before = '1'
for s in S:
    if before == s:
        i += 1
    else:
        T.append(i)
        i = 1
        before = s
T.append(i)

i = 0
ans = 0
while i <= len(T)//2:
    j = 1
    tmp = 0
    for t in T[i:]:
        if j > 2*K+1:
            break
        tmp += t
        j += 1
    ans = max(ans, tmp)
    i += 2

print(ans)

Submission status: Wrong Answer

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

s=1
temp=0
for i in range(N):
    if s!= int(S[i]):
        A.append(temp)
        temp=1
        s=(s+1)%2
    else:
        temp+=1
        
A.append(temp)
if s==0:
    A.append(0)
        
NA=len(A)
if 2*K+1>=NA:
    ans=N
else:
    temp=sum(A[:2*K+1])
    ans=temp
    for i in range(NA-2*K-3):
        temp=temp-A[i]-A[i+1]+A[i+2*K+1]+A[i+2*K+2]
        ans=max(ans,temp)
        
print(ans)

Submission status: Wrong Answer

Solution code:
import itertools

N, K = map(int, input().split())
S = input()
gnum = 0
g0 = []
g1 = []
for key, elems in itertools.groupby(S):
    gnum += 1
    elems = list(elems)
    if key == '0':
        g0.append(len(elems))
    else:
        g1.append(len(elems))

if N >= 2:
    if (N - 2) // 2 + 1 <= K:
        print(N)
    else:
        remained_gnum = (N - 2) // 2 + 1 - K
        g0.sort()
        g1.sort()

        if remained_gnum % 2 == 0:
            print(N - sum(g0[:remained_gnum // 2]) - sum(g1[:remained_gnum // 2]))
        else:
            if len(g0) % 2 == 0:
                print(N - sum(g0[:remained_gnum // 2]) - sum(g1[:remained_gnum // 2 + 1]))
            else:
                print(N - sum(g0[:remained_gnum // 2 + 1]) - sum(g1[:remained_gnum // 2]))
else:
    print(N)

Submission status: Wrong Answer

Solution code:
import copy
N, K = map(int, input().split())
S = input()
if S.count('0') == 0:
    print(S.count('1'))
    exit()
olst = []
zlst = []
tmp = S[0]
tcnt = 1
for i in range(1, N):
    if tmp != S[i]:
        if tmp == '1':
            olst.append(tcnt)
        else:
            zlst.append(tcnt)
        tmp = S[i]
        tcnt = 1
    else:
        tcnt += 1

if tmp == '1':
    olst.append(tcnt)
else:
    zlst.append(tcnt)

index = 0
flst, slst = [], []
if S[0] == '1':
    index = 1
    flst = copy.deepcopy(olst)
    slst = copy.deepcopy(zlst)
else:
    flst = copy.deepcopy(zlst)
    slst = copy.deepcopy(olst)
total = [0] * (len(flst) + len(slst) + 1)
for i in range(1, len(flst) + len(slst) + 1):
    if i % 2 != 0:
        total[i] += flst[i // 2] + total[i - 1]
    else:
        total[i] += slst[i // 2 - 1] + total[i - 1]
ans = 0
tmp1 = (2 * K) - 1
tmp2 = 2 * K
for i in range(1, len(total) - K):
    tmp = 0
    if index == 0:
        if i % 2 != 0:
            if i + tmp1 < len(total):
                tmp += total[i + tmp1] - total[i - 1]
            else:
                tmp += total[-1] - total[i - 1]
        else:
            if i + tmp2 < len(total):
                tmp += total[i + tmp2] - total[i - 1]
            else:
                tmp += total[-1] - total[i - 1]
    else:
        if i % 2 != 0:
            if i + tmp2 < len(total):
                tmp += total[i + tmp2] - total[i - 1]
            else:
                tmp += total[-1] - total[i - 1]
        else:
            if i + tmp1 < len(total):
                tmp += total[i + tmp1] - total[i - 1]
            else:
                tmp += total[-1] - total[i - 1]
    ans = max(ans, tmp)
print(ans)

Submission status: Wrong Answer

Solution code:
import copy
N, K = map(int, input().split())
S = input()
if S.count('0') == 0:
    print(S.count('1'))
    exit()
olst = []
zlst = []
tmp = S[0]
tcnt = 1
for i in range(1, N):
    if tmp != S[i]:
        if tmp == '1':
            olst.append(tcnt)
        else:
            zlst.append(tcnt)
        tmp = S[i]
        tcnt = 1
    else:
        tcnt += 1

if tmp == '1':
    olst.append(tcnt)
else:
    zlst.append(tcnt)

index = 0
flst, slst = [], []
if S[0] == '1':
    index = 1
    flst = copy.deepcopy(olst)
    slst = copy.deepcopy(zlst)
else:
    flst = copy.deepcopy(zlst)
    slst = copy.deepcopy(olst)
total = [0] * (len(flst) + len(slst) + 1)
for i in range(1, len(flst) + len(slst) + 1):
    if i % 2 != 0:
        total[i] += flst[i // 2] + total[i - 1]
    else:
        total[i] += slst[i // 2 - 1] + total[i - 1]
ans = 0
tmp1 = (2 * K) - 1
tmp2 = 2 * K
for i in range(1, len(total) - K):
    tmp = 0
    if index == 0:
        if i % 2 != 0:
            if i + tmp1 < len(total):
                tmp += total[i + tmp1] - total[i - 1]
        else:
            if i + tmp2 < len(total):
                tmp += total[i + tmp2] - total[i - 1]
    else:
        if i % 2 != 0:
            if i + tmp2 < len(total):
                tmp += total[i + tmp2] - total[i - 1]
        else:
            if i + tmp1 < len(total):
                tmp += total[i + tmp1] - total[i - 1]
    ans = max(ans, tmp)
print(ans)

Submission status: Wrong Answer

Solution code:
import copy
N, K = map(int, input().split())
S = input()
if S.count('0') == 0:
    print(S.count('1'))
    exit()
olst = []
zlst = []
tmp = S[0]
tcnt = 1
for i in range(1, N):
    if tmp != S[i]:
        if tmp == '1':
            olst.append(tcnt)
        else:
            zlst.append(tcnt)
        tmp = S[i]
        tcnt = 1
    else:
        tcnt += 1

if tmp == '1':
    olst.append(tcnt)
else:
    zlst.append(tcnt)

index = 0
flst, slst = [], []
if S[0] == '1':
    index = 1
    flst = copy.deepcopy(olst)
    slst = copy.deepcopy(zlst)
else:
    flst = copy.deepcopy(zlst)
    slst = copy.deepcopy(olst)
totalf = [0] * (len(flst) + 1)
totals = [0] * (len(slst) + 1)
for i in range(1, len(flst) + 1):
    totalf[i] += totalf[i - 1] + flst[i - 1]
for i in range(1, len(slst) + 1):
    totals[i] += totals[i - 1] + slst[i - 1]
ans = 0
for i in range(len(flst) - K):
    tmp = 0
    tmp += totalf[i + K + index] - totalf[i]
    tmp += totals[i + K] - totals[i]
    ans = max(ans, tmp)
print(ans)

Submission status: Wrong Answer

Solution code:
import copy
N, K = map(int, input().split())
S = input()
if S.count('0') == 0:
    print(S.count('1'))
    exit()
olst = []
zlst = []
tmp = S[0]
tcnt = 1
for i in range(1, N):
    if tmp != S[i]:
        if tmp == '1':
            olst.append(tcnt)
        else:
            zlst.append(tcnt)
        tmp = S[i]
        tcnt = 1
    else:
        tcnt += 1

if tmp == '1':
    olst.append(tcnt)
else:
    zlst.append(tcnt)

index = 0
flst, slst = [], []
if S[0] == '1':
    index = 1
    flst = copy.deepcopy(olst)
    slst = copy.deepcopy(zlst)
else:
    flst = copy.deepcopy(zlst)
    slst = copy.deepcopy(olst)
totalf = [0] * (len(flst) + 1)
totals = [0] * (len(slst) + 1)
for i in range(1, len(flst) + 1):
    totalf[i] += totalf[i - 1] + flst[i - 1]
for i in range(1, len(slst) + 1):
    totals[i] += totals[i - 1] + slst[i - 1]
ans = 0
for i in range(len(flst)):
    tmp = 0
    if i + K + index < len(flst):
        tmp += totalf[i + K + index] - totalf[i]
    else:
        tmp += totalf[-1] - totalf[i]
    if i + K < len(slst):
        tmp += totals[i + K] - totals[i]
    else:
        tmp += totals[-1] - totals[i]
    ans = max(ans, tmp)
print(ans)

Submission status: Wrong Answer

Solution code:
import copy
N, K = map(int, input().split())
S = input()
if S.count('0') == 0:
    print(S.count('1'))
    exit()
olst = []
zlst = []
tmp = S[0]
tcnt = 1
for i in range(1, N):
    if tmp != S[i]:
        if tmp == '1':
            olst.append(tcnt)
        else:
            zlst.append(tcnt)
        tmp = S[i]
        tcnt = 1
    else:
        tcnt += 1

if tmp == '1':
    olst.append(tcnt)
else:
    zlst.append(tcnt)

index = 0
flst, slst = [], []
if S[0] == '1':
    index = 1
    flst = copy.deepcopy(olst)
    slst = copy.deepcopy(zlst)
else:
    flst = copy.deepcopy(zlst)
    slst = copy.deepcopy(olst)

ans = 0
for i in range(len(flst)):
    tmp = sum(flst[i: i + K + index])
    if i < len(slst):
        tmp += sum(slst[i:i + K])
    ans = max(ans, tmp)
print(ans)

Submission status: Wrong Answer

Solution code:
import copy
N, K = map(int, input().split())
S = input()
if S.count('0') == 0:
    print(S.count('1'))
    exit()
olst = []
zlst = []
tmp = S[0]
tcnt = 1
for i in range(1, N):
    if tmp != S[i]:
        if tmp == '1':
            olst.append(tcnt)
        else:
            zlst.append(tcnt)
        tmp = S[i]
        tcnt = 1
    else:
        tcnt += 1

if tmp == '1':
    olst.append(tcnt)
else:
    zlst.append(tcnt)

index = 0
flst, slst = [], []
if S[0] == '1':
    index = 1
    flst = copy.deepcopy(olst)
    slst = copy.deepcopy(zlst)
else:
    flst = copy.deepcopy(zlst)
    slst = copy.deepcopy(olst)

ans = 0
for i in range(len(flst)):
    tmp = sum(flst[i: i + K + index])
    if i < len(slst):
        tmp += sum(slst[i:i + K])
    ans = max(ans, tmp)
print(ans)

Submission status: Wrong Answer

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

s.append("")

current = 0
l = []
for i in range(n+1):
    if s[i] != s[current]:
        l.append(i-current)
        current = i

if s[0] == "0":
    l.insert(0, 0)
if s[-2] == "0":
    l.append(0)

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

print(ans)

Submission status: Wrong Answer

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

c = collections.Counter(s)

s.append("1")

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

l.sort(reverse=True)
print(c["1"]+sum(l[:k]))

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
l=[0]
s=input()
cnt=0
for c in s:
  if (len(l)%2) ^ (c=='0'):
    cnt+=1
  else:
    l.append(cnt)
    cnt+=1
l.append(cnt)
if len(l)%2:
  l.append(0)
#print(l)
k = min(k,(len(l)-1)//2)
ans = 0
for i in range(0,len(l)-2*k-1,2):
  ans = max(ans,l[i+2*k+1]-l[i])
print(ans)