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()
numbers = [len(c) for c in s.replace('0',' ').split()]
zero = [len(c) for c in s.replace('1',' ').split()]
ans = 0
if len(zero) == 0:
    ans = numbers[0]
elif len(zero) <= k:
    ans = sum(numbers) + sum(zero)
else:
    for i in range(len(zero)-k+1):
        total = sum(zero[i:i+k]) + sum(numbers[i:i+k+1])
        ans = max(ans,total)
print(ans)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
s = input()
numbers = [len(c) for c in s.replace('0',' ').split()]
zero = [len(c) for c in s.replace('1',' ').split()]
ans = 0
if len(zero) == 0:
    ans = numbers[0]
else:
    if len(zero) <= k:
        ans = sum(numbers) + sum(zero)
    else:
        for i in range(len(zero)-k+1):
            total = sum(zero[i:i+k]) + sum(numbers[i:i+k+1])
            ans = max(ans,total)
print(ans)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
s = input()
numbers = [len(c) for c in s.replace('0',' ').split()]
zero = [len(c) for c in s.replace('1',' ').split()]
ans = 0
if len(numbers) == 1:
    ans = numbers[0]
else:
    if len(zero) <= k:
        ans = sum(numbers) + sum(zero)
    else:
        for i in range(len(zero)-k+1):
            total = sum(zero[i:i+k]) + sum(numbers[i:i+k+1])
            ans = max(ans,total)
print(ans)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
s = input()
numbers = [len(c) for c in s.replace('0',' ').split()]
zero = [len(c) for c in s.replace('1',' ').split()]
ans = 0
if len(numbers) == 1:
    ans = numbers[0]
else:
    for i in range(len(zero)-k+1):
        total = sum(zero[i:i+k]) + sum(numbers[i:i+k+1])
        ans = max(ans,total)
print(ans)

Submission status: Wrong Answer

Solution code:
def main():
    N,K = map(int, input().split())
    S = input()
    flag = S[0]
    count = 0
    A = [1]
    count0 = 0
    if S[0] == "0":
        count0 += 1
    
    for i in range(1,len(S)):
        if S[i] == flag:
            A[count] += 1
        else:
            A.append(1)
            count += 1
            flag = S[i]
            if S[i] == "0":
                count0 += 1
    
    if count0 <= K:
        answer = N
        print(answer)
    else:
        head = 0
        tail = 0
        if S[0] == "0":
            head = sum(A[:2*K])
            if A != []:
                A.pop(0)
        if S[-1] == "0":
            tail = sum(A[::-1][:2*K])
            if A != []:
                A.pop(-1)
        g = min(len(A),2*K+1)
        tmp = sum(A[:g])
        MAX = sum(A[:g])
        s = 0
        while g  < len(A):
            tmp += - A[s] - A[s+1] + A[g] + A[g+1]
            if MAX < tmp:
                MAX = tmp
            s += 2
            g += 2
        print(max(tmp,head,tail))
    
main()

Submission status: Wrong Answer

Solution code:
def main():
    N,K = map(int, input().split())
    S = input()
    flag = S[0]
    count = 0
    A = [1]
    count0 = 0
    if S[0] == "0":
        count0 += 1
    
    for i in range(1,len(S)):
        if S[i] == flag:
            A[count] += 1
        else:
            A.append(1)
            count += 1
            flag = S[i]
            if S[i] == "0":
                count0 += 1
    
    if count0 <= K:
        answer = N
        print(answer)
    else:
        head = 0
        tail = 0
        if S[0] == "0":
            head = sum(A[:2*K])
            A.pop(0)
        if S[-1] == "0":
            tail = sum(A[::-1][:2*K])
            A.pop()
        g = min(len(S),2*K+1)
        tmp = sum(A[:g])
        MAX = sum(A[:g])
        s = 0
        while g  < len(A):
            if MAX < tmp - A[s] - A[s+1] + A[g] + A[g+1]:
                MAX = tmp - A[s] - A[s+1] + A[g] + A[g+1]
            tmp = tmp - A[s] - A[s+1] + A[g] + A[g+1]
            s += 2
            g += 2
        print(max(tmp,head,tail))
    
main()

Submission status: Wrong Answer

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

A = []
S0 = S[0]
temp = 1

for i in range(1, N):
    if S[i] == S0:
        temp += 1
    elif S[i] != S0:
        A.append(temp)
        S0 = S[i]
        temp = 1
A.append(temp)

if len(A) <= 2 * K:
    print(len(S))
    exit()
mx = 0
if S[0] == "0":
    for i in range(len(A) - 2 * K):
        if i % 2 == 0:
            sum1 = sum(A[i:i + 2 * K])  # Aを偶数個足す
        else:  # i%2==1
            sum1 = sum(A[i:i + 2 * K + 1])
        if sum1 > mx:
            mx = sum1

if S[0] == "1":
    for i in range(len(A) - 2 * K):
        if i % 2 == 1:
            sum1 = sum(A[i:i + 2 * K])
        else:
            sum1 = sum(A[i:i + 2 * K + 1])
        if sum1 > mx:
            mx = sum1
print(mx)

Submission status: Wrong Answer

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

A = []
S0 = S[0]
temp = 1

for i in range(1, N):
    if S[i] == S0:
        temp += 1
    elif S[i] != S0:
        A.append(temp)
        S0 = S[i]
        temp = 1
A.append(temp)
mx = 0
if S[0] == "0":
    for i in range(len(A) - 2 * K):
        if i % 2 == 0:
            sum1 = sum(A[i:i + 2 * K])  # Aを偶数個足す
        else:  # i%2==1
            sum1 = sum(A[i:i + 2 * K + 1])
        if sum1 > mx:
            mx = sum1

if S[0] == "1":
    for i in range(len(A) - 2 * K):
        if i % 2 == 1:
            sum1 = sum(A[i:i + 2 * K])
        else:
            sum1 = sum(A[i:i + 2 * K + 1])
        if sum1 > mx:
            mx = sum1
print(mx)

Submission status: Wrong Answer

Solution code:
n,k = (int(i) for i in input().split())
S = input()
if S[0] == '0': jud,sign = '0',1#-1
else: jud,sign = '1',1
li = []
cnt = 0
for s in S:
    if s == jud:
        cnt += 1
    else:
        li.append(cnt*sign)
        cnt = 1
        sign *= 1
        if jud == '0': jud = '1'
        else: jud = '0'

li.append(cnt*sign)
ans = 1
if S[0] == '0':
    for i in range(len(li)-k):
        ans = max(ans,sum(li[i:k*2+i]))
    print(ans)
else:
    for i in range(len(li)-k):
        ans = max(ans,sum(li[i:k*2+i+1]))
    print(ans)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
ss=input()
s=ss.replace('10','1 0').replace('01','0 1').split()
t=[0]*len(s)
for i in range(len(s)):
  t[i]=list(map(int, list(s[i])))
f=0
if t[0][0] == 0:
  e=2*k
else:
  e=2*k+1

ans=0
while e<=len(t):
  x=0
  for i in t[f:e]:
    x+=len(i)
  ans=max(x,ans)
  f+=1
  e+=1

if len(t)<=2:
  ans=len(ss)
print(ans)

Submission status: Wrong Answer

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

secs = []
i = 0
flg1 = True
while i < N:
    tmp = 0
    if flg1:
        while i < N and S[i] == 1:
            tmp += 1
            i += 1
    else:
        while i < N and S[i] == 0:
            tmp += 1
            i += 1
    flg1 = not flg1
    secs.append(tmp)
if S[-1] == 0: secs.append(0)

sums = [0] * (len(secs)+1)
for i in range(len(secs)): sums[i+1] = sums[i] + secs[i]

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

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
s = list(str(input()))
s.append(abs(1-int(s[n-1])))
arr0,arr1,arr = [],[],[]

cnt = 1
for i in range(n):
  if s[i] != s[i+1]:
    if s[i] == "0":
      arr0.append(cnt)
    else:
      arr1.append(cnt)
    arr.append(cnt)
    cnt = 1
  else:
    cnt += 1
arr1 = arr1 + [0]

if k >= len(arr0):
  print(n)
  exit()
flag = 0

if s[0] == "1":
  flag = 1
else:
  arr1 = [0] + arr1
M = 0

for i in range(len(arr0)-k+1):
  l0 = arr0[i:k+i]
  l1 = arr1[i:k+i+1]
  M = max(M,sum(l0)+sum(l1))

Submission status: Wrong Answer

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

secs = []
i = 0
flg1 = True
while i < N:
    tmp = 0
    if flg1:
        while i < N and S[i] == 1:
            tmp += 1
            i += 1
    else:
        while i < N and S[i] == 0:
            tmp += 1
            i += 1
    flg1 = not flg1
    secs.append(tmp)
# print(secs)

ans = 1
i = 0
while i + 2*K + 1 <= len(secs):
    tmp = sum(secs[i:i+2*K+1])
    if tmp > ans: ans = tmp
    i += 2
print(ans)

Submission status: Wrong Answer

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

secs = []
i = 0
flg1 = True
while i < N:
    tmp = 0
    if flg1:
        while i < N and S[i] == 1:
            tmp += 1
            i += 1
    else:
        while i < N and S[i] == 0:
            tmp += 1
            i += 1
    flg1 = not flg1
    secs.append(tmp)
# print(secs)

ans = 0
i = 0
while i + 2*K + 1 < N :
    tmp = sum(secs[i:i+2*K+1])
    if tmp > ans: ans = tmp
    i += 2
print(ans)

Submission status: Wrong Answer

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

Data = []
i = 0
while i < N:
    c = S[i]
    j = i
    while j < N and c == S[j]:
        j += 1
    # iから(j-1)番目まで条件をみたす  (j-i)個が連続
    Data.append([c,(j-i)]) 
    i = j

#1 0 1 0 1 ... 1 のパターンにする
if Data[0][0] == '0':
    Data.insert(0,['1',0])
if Data[-1][0] == '0':
    Data.append(['1',0])
#print(Data)

maxsize = 0
while K > 0:
    if len(Data) == 1:
        maxsize = Data[0][1]
        break

    max1 = 0
    maxidx = -1
    length = (len(Data)-1)//2 
    for i in range(0,len(Data)-1,2):
        c = Data[i][1]+Data[i+1][1]+Data[i+2][1]
        if max1 < c:
            max1 = c
            maxidx = i
        if maxsize < max1:
            maxsize = max1

    Data[maxidx][1]= max1
    del Data[maxidx+1:maxidx+3]
    #print(Data)
    K -= 1

print(maxsize)

Submission status: Wrong Answer

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

  from itertools import groupby

  tmp = []
  num = []
  for ke, v in groupby(s):
    num.append(ke)
    tmp.append(len(list(v)))
  
    num += [0]
    tmp += [0]

  nt = len(tmp)
  wa = [0] * (nt+1)
  for i in range(nt):
    wa[i+1] = wa[i] + tmp[i]

  ans = 0
  for i in range(len(wa)):
    x = k*2
    if num[i] == "1":
      x += 1
    try:
      ans = max(ans, wa[i+x] - wa[i])
    except:
      break
  if not ans:
    ans = len(s)
  print(ans)

main()

Submission status: Wrong Answer

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

  from itertools import groupby

  tmp = []
  num = []
  for ke, v in groupby(s):
    num.append(ke)
    tmp.append(len(list(v)))
  
  num += [0]
  tmp += [0]

  nt = len(tmp)
  wa = [0] * (nt+1)
  for i in range(nt):
    wa[i+1] = wa[i] + tmp[i]
  print(wa, num, tmp)
  ans = 0
  for i in range(len(wa)):
    x = k*2
    if num[i] == "1":
      x += 1
    try:
      ans = max(ans, wa[i+x] - wa[i])
    except:
      break
  if not ans:
    ans = len(s)
  print(ans)

main()

Submission status: Wrong Answer

Solution code:
#!usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

#A
def A():
    n,m = LI()
    a = LIR(n)
    ans = 0
    for i in range(2):
        for j in range(2):
            for k in range(2):
                a.sort(key = lambda x:(-1)**i*x[0]+(-1)**j*x[1]+(-1)**k*x[2])
                p = [0,0,0]
                for l in range(m):
                    p[0] += a[l][0]
                    p[1] += a[l][1]
                    p[2] += a[l][2]
                p = abs(p[0])+abs(p[1])+abs(p[2])
                if p > ans:
                    ans = p
    print(ans)
    return

#B
def B():
    def root(x):
        if x == par[x]:
            return x
        par[x] = root(par[x])
        return par[x]

    def unite(x,y,su):
        x = root(x)
        y = root(y)
        su += comb_2(s[x])+comb_2(s[y])
        if rank[x] < rank[y]:
            s[y] += s[x]
            su -= comb_2(s[y])
            par[x] = y
        else:
            s[x] += s[y]
            su -= comb_2(s[x])
            par[y] = x
            if rank[x] == rank[y]:
                rank[x] += 1
        return su

    def comb_2(a):
        return a*(a-1)//2

    n,m = LI()
    v = LIR(m)
    par = [i for i in range(n)]
    rank = [0]*n
    s = [1]*n
    su = comb_2(n)
    ans = [su]
    for a,b in v[:0:-1]:
        a -= 1
        b -= 1
        if root(a) != root(b):
            su = unite(a,b,su)
        ans.append(su)
    for i in ans[::-1]:
        print(i)
    return

#C
def C():
    def root(x):
        if par[x] == x:
            return par[x]
        r = root(par[x])
        d[x] += d[par[x]]
        par[x] = r
        return par[x]

    def weight(x):
        root(x)
        return d[x]

    def unite(x,y,w):
        w += weight(x)
        w -= weight(y)
        x = root(x)
        y = root(y)
        if rank[x] < rank[y]:
            par[x] = y
            d[x] = -w
        else:
            par[y] = x
            d[y] = w
            if rank[x] == rank[y]:
                rank[x] += 1
    n,m = LI()
    par = [i for i in range(n)]
    rank = [0]*n
    d = [0]*n
    for i in range(m):
        l,r,w = LI()
        l -= 1
        r -= 1
        if root(l) != root(r):
            unite(l,r,w)
        else:
            if weight(r)-weight(l) != w:
                print("No")
                return
    print("Yes")
    return

#D
def D():
    h,w = LI()
    n = I()
    a = LI()
    ans = [0]*h*w
    p = 0
    for i in range(n):
        for j in range(a[i]):
            ans[p+j] = i+1
        p += a[i]
    for i in range(h):
        s = ans[i*w:(i+1)*w]
        if i%2:
            print(*s[::-1])
        else:
            print(*s)
    return

#E
def E():
    n,k = LI()
    s = [int(x) for x in input()]
    f = [0]*n
    j = 0
    su = 1
    p = s[0]
    for i in range(1,n):
        if s[i] != p:
            f[j] = su
            j += 1
            su = 1
            p = s[i]
        else:
            su += 1
    f[j] = su
    key = s[0]
    m = sum(f[:2*k+1])
    ans = m if key else 0
    for i in range(2*k+1,n):
        key ^= 1
        m -= f[i-2*k-1]
        m += f[i]
        if key:
            ans = max(m,ans)
    m = sum(f[-2*k:])
    if key:
        ans = max(m,ans)
    for i in range(n-2*k)[::-1]:
        key ^= 1
        m -= f[i+2*k]
        m += f[i]
        if key:
            ans = max(m,ans)
    print(ans)
    return

#F
def F():

    return

#G
def G():

    return

#H
def H():

    return

#I
def I_():

    return

#Solve
if __name__ == "__main__":
    E()

Submission status: Wrong Answer

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

  from itertools import groupby

  tmp = []
  num = []
  for ke, v in groupby(s):
    num.append(ke)
    tmp.append(len(list(v)))
  
  nt = len(tmp)
  wa = [0] * (nt+1)
  for i in range(nt):
    wa[i+1] = wa[i] + tmp[i]

  ans = 0
  for i in range(len(wa)):
    x = k*2
    if num[i] == "1":
      x += 1
    try:
      ans = max(ans, wa[i+x] - wa[i])
    except:
      break
  if not ans:
    ans = len(s)
  print(ans)

main()

Submission status: Wrong Answer

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

  from itertools import groupby

  tmp = []
  num = []
  for ke, v in groupby(s):
    num.append(ke)
    tmp.append(len(list(v)))
  
  nt = len(tmp)
  wa = [0] * (nt+1)
  for i in range(nt):
    wa[i+1] = wa[i] + tmp[i]

  ans = 1
  for i in range(len(wa)):
    x = k*2
    if num[i] == "1":
      x += 1
    try:
      ans = max(ans, wa[i+x] - wa[i])
    except:
      break

  print(ans)

main()

Submission status: Wrong Answer

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

  from itertools import groupby

  tmp = []
  for ke, v in groupby(s):
    tmp.append(len(list(v)))
  
  nt = len(tmp)
  wa = [0] * (nt+1)
  for i in range(nt):
    wa[i+1] = wa[i] + tmp[i]

  ans = 1
  for i in range(len(wa)):
    x = k*2
    if s[0] == "1":
      x += 1
    try:
      ans = max(ans, wa[i+x] - wa[i])
    except:
      break

  print(ans)

main()

Submission status: Wrong Answer

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

  from itertools import groupby

  one = []
  zero = []
  for ke, v in groupby(s):
    if ke == '1':
      one.append(len(list(v)))
    else:
      zero.append(len(list(v)))
    
  ans = 0
  for i in range(len(one) - k + 1):
    if s[0] == "1":
      ans = max(ans, sum(one[i:i+k+1]) + sum(zero[i:i+k]))
    else:
      ans = max(ans, sum(one[i:i+k]) + sum(zero[i:i+k]))
  print(ans)

main()

Submission status: Wrong Answer

Solution code:
from collections import deque,defaultdict,Counter
from itertools import accumulate
import bisect
from heapq import heappop,heappush
from fractions import gcd
from copy import deepcopy
import math
import queue
#import numpy as np
Mod = 1000000007




def main(): #startline--------------------------------------------
    N,K = map(int,input().split())
    S = input()
    keep = "start"
    p = []
    cnt = 0
    for i in S:
        if keep == "start":
            keep = i
        elif keep == i:
            keep = i
        else:
            p.append(cnt)
            keep = i
        cnt += 1
    p.append(cnt)
    if S[-1] == "0":
        p.append(0)
    if len(p) > 2*K+1:
        if S[0] == "0":
            p.insert(0,0)
            ans = 0
            for i in range(0,len(p)-2*K):
                ans = max(ans,p[i+2*K]-p[i])
            print(ans)
        else:
            ans = 0

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

    else:
        print(N)

if __name__ == "__main__":
    main() #endline===============================================





def sieve_of_eratosthenes(n):
    if not isinstance(n,int):
        raise TypeError("n is not int")
    if n<2:
        raise ValueError("n < 2 is not effective")
    prime = [1]*(n+1)
    for i in range(2,int(math.sqrt(n))):
        if prime[i] == 1:
            for j in range(2*i,n):
                if j%i == 0:
                    prime[j] = 0
    res = []
    for i in range(2,n):
        if prime[i] == 1:
            res.append(i)
    return res

class UnionFind:
    def __init__(self,n):
        self.parent = [i for i in range(n+1)]
        self.rank = [0 for i in range(n+1)]
    
    def findroot(self,x):
        if x == self.parent[x]:
            return x
        else:
            y = self.parent[x]
            y = self.findroot(self.parent[x])
            return y
    
    def union(self,x,y):
        px = self.findroot(x)
        py = self.findroot(y)
        if px < py:
            self.parent[py] = px
        else:
            self.parent[px] = py

    def same_group_or_no(self,x,y):
        return self.findroot(x) == self.findroot(y)

Submission status: Wrong Answer

Solution code:
from collections import deque,defaultdict,Counter
from itertools import accumulate
import bisect
from heapq import heappop,heappush
from fractions import gcd
from copy import deepcopy
import math
import queue
#import numpy as np
Mod = 1000000007




def main(): #startline--------------------------------------------
    N,K = map(int,input().split())
    S = input()
    keep = "start"
    p = []
    cnt = 0
    for i in S:
        if keep == "start":
            keep = i
        elif keep == i:
            keep = i
        else:
            p.append(cnt)
            keep = i
        cnt += 1
    p.append(cnt)
    if S[-1] == "0":
        p.append(0)
    if len(p) > 2*K+1:
        if S[0] == "0":
            p.insert(0,0)
            ans = 0
            for i in range(0,len(p)-2*K,2):
                ans = max(ans,p[i+2*K]-p[i])
            print(ans)
        else:
            ans = 0

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

    else:
        print(N)

if __name__ == "__main__":
    main() #endline===============================================





def sieve_of_eratosthenes(n):
    if not isinstance(n,int):
        raise TypeError("n is not int")
    if n<2:
        raise ValueError("n < 2 is not effective")
    prime = [1]*(n+1)
    for i in range(2,int(math.sqrt(n))):
        if prime[i] == 1:
            for j in range(2*i,n):
                if j%i == 0:
                    prime[j] = 0
    res = []
    for i in range(2,n):
        if prime[i] == 1:
            res.append(i)
    return res

class UnionFind:
    def __init__(self,n):
        self.parent = [i for i in range(n+1)]
        self.rank = [0 for i in range(n+1)]
    
    def findroot(self,x):
        if x == self.parent[x]:
            return x
        else:
            y = self.parent[x]
            y = self.findroot(self.parent[x])
            return y
    
    def union(self,x,y):
        px = self.findroot(x)
        py = self.findroot(y)
        if px < py:
            self.parent[py] = px
        else:
            self.parent[px] = py

    def same_group_or_no(self,x,y):
        return self.findroot(x) == self.findroot(y)

Submission status: Wrong Answer

Solution code:
from collections import deque,defaultdict,Counter
from itertools import accumulate
import bisect
from heapq import heappop,heappush
from fractions import gcd
from copy import deepcopy
import math
import queue
#import numpy as np
Mod = 1000000007




def main(): #startline--------------------------------------------
    N,K = map(int,input().split())
    S = input()
    keep = "start"
    p = []
    cnt = 0
    for i in S:
        if keep == "start":
            keep = i
        elif keep == i:
            keep = i
        else:
            p.append(cnt)
            keep = i
        cnt += 1
    p.append(cnt)
    if S[-1] == "0":
        p.append(0)
    if len(p) > 2*K+1:
        if S[0] == "0":
            p.insert(0,0)
            ans = 0
            for i in range(0,len(p)-2*K,2):
                ans = max(ans,p[i+2*K]-p[i])
            print(ans)
        else:
            ans = 0

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

    else:
        print(N)

if __name__ == "__main__":
    main() #endline===============================================





def sieve_of_eratosthenes(n):
    if not isinstance(n,int):
        raise TypeError("n is not int")
    if n<2:
        raise ValueError("n < 2 is not effective")
    prime = [1]*(n+1)
    for i in range(2,int(math.sqrt(n))):
        if prime[i] == 1:
            for j in range(2*i,n):
                if j%i == 0:
                    prime[j] = 0
    res = []
    for i in range(2,n):
        if prime[i] == 1:
            res.append(i)
    return res

class UnionFind:
    def __init__(self,n):
        self.parent = [i for i in range(n+1)]
        self.rank = [0 for i in range(n+1)]
    
    def findroot(self,x):
        if x == self.parent[x]:
            return x
        else:
            y = self.parent[x]
            y = self.findroot(self.parent[x])
            return y
    
    def union(self,x,y):
        px = self.findroot(x)
        py = self.findroot(y)
        if px < py:
            self.parent[py] = px
        else:
            self.parent[px] = py

    def same_group_or_no(self,x,y):
        return self.findroot(x) == self.findroot(y)

Submission status: Wrong Answer

Solution code:
# ABC124D - Handstand
def main():
    N, K = tuple(map(int, input().split()))
    S = input()
    A = [i for i in range(1, N) if S[i] != S[i - 1]]
    # change to start and end with length of "1"
    A = [0] + A if S[0] == "1" else [0, 0] + A
    A += [N] if S[-1] == "1" else [N, 0]
    
    l, x = len(A), K * 2 + 1
    if l <= x:  # possible to change all to "1"
        print(N)
    else:
        # check all sections of the length x
        ans = max(A[i + x] - A[i] for i in range(0, l - x + 1, 2))
        print(ans)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
from collections import deque,defaultdict,Counter
from itertools import accumulate
import bisect
from heapq import heappop,heappush
from fractions import gcd
from copy import deepcopy
import math
import queue
#import numpy as np
Mod = 1000000007




def main(): #startline--------------------------------------------
    N,K = map(int,input().split())
    S = input()
    flag = False
    anslist = []
    if S.count("0") == 0:
        print(N)
        exit()
    if S[0] == "0":
        S = "1"+S
        flag = True
    if S[-1] == "0":
        S += "1" 
        flag = True
    S += "A"
    cnt = 0
    keep = "start"
    for i in S:
        if keep == "start":
            keep = i
        elif keep == i:
            keep = i
        else:
            keep = i
            anslist.append(cnt)
        cnt += 1
    p = len(anslist)
    ans = 0  
    if flag:
        for i in range(p-2*K):
            ans = max(ans,anslist[i+2*K]-anslist[i])
    else:
        for i in range(p-2*K-1):
            ans = max(ans,anslist[i+2*K+1]-anslist[i])
    print(ans)



if __name__ == "__main__":
    main() #endline===============================================





def sieve_of_eratosthenes(n):
    if not isinstance(n,int):
        raise TypeError("n is not int")
    if n<2:
        raise ValueError("n < 2 is not effective")
    prime = [1]*(n+1)
    for i in range(2,int(math.sqrt(n))):
        if prime[i] == 1:
            for j in range(2*i,n):
                if j%i == 0:
                    prime[j] = 0
    res = []
    for i in range(2,n):
        if prime[i] == 1:
            res.append(i)
    return res

class UnionFind:
    def __init__(self,n):
        self.parent = [i for i in range(n+1)]
        self.rank = [0 for i in range(n+1)]
    
    def findroot(self,x):
        if x == self.parent[x]:
            return x
        else:
            y = self.parent[x]
            y = self.findroot(self.parent[x])
            return y
    
    def union(self,x,y):
        px = self.findroot(x)
        py = self.findroot(y)
        if px < py:
            self.parent[py] = px
        else:
            self.parent[px] = py

    def same_group_or_no(self,x,y):
        return self.findroot(x) == self.findroot(y)

Submission status: Wrong Answer

Solution code:
from collections import deque,defaultdict,Counter
from itertools import accumulate
import bisect
from heapq import heappop,heappush
from fractions import gcd
from copy import deepcopy
import math
import queue
#import numpy as np
Mod = 1000000007




def main(): #startline--------------------------------------------
    N,K = map(int,input().split())
    S = input()
    anslist = []
    if S.count("0") == 0:
        print(len(S))
        exit()
    S += "A"
    cnt = 0
    keep = "start"
    for i in S:
        if keep == "start":
            keep = i
        elif keep == i:
            keep = i
        else:
            keep = i
            anslist.append(cnt)
        cnt += 1
    p = len(anslist)
    ans = 0
    if S[0] == "1":    
        for i in range(p-2*K-1):
            ans = max(anslist[i+2*K+1]-anslist[i],ans)
    else:
        anslist.insert(0,1)
        for i in range(p-2*K):
            ans = max(anslist[i+2*K+1]-anslist[i],ans)
    print(ans)



if __name__ == "__main__":
    main() #endline===============================================





def sieve_of_eratosthenes(n):
    if not isinstance(n,int):
        raise TypeError("n is not int")
    if n<2:
        raise ValueError("n < 2 is not effective")
    prime = [1]*(n+1)
    for i in range(2,int(math.sqrt(n))):
        if prime[i] == 1:
            for j in range(2*i,n):
                if j%i == 0:
                    prime[j] = 0
    res = []
    for i in range(2,n):
        if prime[i] == 1:
            res.append(i)
    return res

class UnionFind:
    def __init__(self,n):
        self.parent = [i for i in range(n+1)]
        self.rank = [0 for i in range(n+1)]
    
    def findroot(self,x):
        if x == self.parent[x]:
            return x
        else:
            y = self.parent[x]
            y = self.findroot(self.parent[x])
            return y
    
    def union(self,x,y):
        px = self.findroot(x)
        py = self.findroot(y)
        if px < py:
            self.parent[py] = px
        else:
            self.parent[px] = py

    def same_group_or_no(self,x,y):
        return self.findroot(x) == self.findroot(y)