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
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7

N, K = LIST()
S = input()

# stand = []
# reverse = []
# state = None
# count = 0
# tmp = None
# for i in S:
# 	if i == "0":
# 		state = "stand"
# 	else:
# 		state = "reverse"
# 	if tmp == "stand" and state == "reverse":
# 		stand.append(count)
# 		count = 1
# 	elif tmp == "reverse" and state == "stand":
# 		reverse.append(count)
# 		count = 1
# 	else: # 状態が前と同じ
# 		count += 1
# 	tmp = state
# if state == "stand":
# 	stand.append(count)
# else:
# 	reverse.append(count)

# if "0" not in S:
# 	print(len(S))
# else:
# 	if K >= len(stand): # 全員逆立ちにできる
# 		print(len(S))
# 	else:
# 		if len(stand) > len(reverse):
# 			reverse.append(0)
# 			reverse.insert(0, 0)
# 		if len(stand) == len(reverse) and S[0] == "0":
# 			reverse.insert(0, 0)
# 		if len(stand) == len(reverse) and S[0] == "1":
# 			reverse.append(0)
# 		# print(stand)
# 		# print(reverse)
# 		max_ = 0
# 		for i in range(max(1, 1+len(reverse)-(K+1))):
# 			# print(reverse[i:i+K+1],stand[i:i+K])
# 			tmp = sum(reverse[i:i+K+1]+stand[i:i+K])
# 			if tmp > max_:
# 				max_ = tmp
# 		print(max_)
if N == 1:
    print(1)
else:
	indexes = []
	tmp = ""
	for i in range(len(S)):
		if S[i] == "0":
			state = "stand"
		else:
			state = "reverse"
		if tmp != state:
			indexes.append(i)
		else: # 状態が前と同じ
			pass
		tmp = state
	indexes.append(N)
	ans = []
	print(indexes)
	for i in range(len(indexes)):
		# print(i)
		if i == len(indexes)-1:
			break
		if S[indexes[i]] == "0":
			if i+2*K >= len(indexes):
				continue
			ans.append(indexes[i+2*K]-indexes[i])
		else:
			if i+2*K+1 >= len(indexes):
				continue
			ans.append(indexes[i+2*K+1]-indexes[i])
	if ans == []:
		print(len(S))
	else:
		print(max(ans))

Submission status: Wrong Answer

Solution code:
def solve():
  N, K = map(int, input().split())
  s = input()
  print(s)
  S = []
  look = s[0]
  cnt = 0
 
  if s[0] == '0': S.append(0)
  for c in s:
    if look == c: cnt += 1
    else:
      S.append(cnt)
      cnt = 1
      look = c
  S.append(cnt)
  if s[-1] == '0': S.append(0)
 
  num = min(len(S), 2 * K + 1)
  part = sum(S[0:num])
  ans = part
 
  for i in range(0, len(S) - num, 2):
    part += - sum(S[i:i+2]) + sum(S[i + num:i + num + 2])
    ans = max(ans, part)
  return ans
 
print(solve())

Submission status: Wrong Answer

Solution code:
import itertools


def count_maxone(binstr):
    return max([len(l) for l in binstr.split('0')])


n, k = map(int, input().split())
s = input().rstrip()
ones = count_maxone(s)

pos = []
pre = '1'

for i in range(n):
    if s[i] == '0' and pre == '1':
        pos.append(i)
    pre = s[i]

for i in range(1, k + 1):
    combs = itertools.combinations(pos, i)
    for cmb in combs:
        slist = list(s)
        for c in cmb:
            zc = s.find('1', c)
            for t in range(c, zc):
                slist[t] = '1'
        ones = max(ones, count_maxone(''.join(slist)))

print(ones)

Submission status: Wrong Answer

Solution code:
import sys
N, K = map(int, input().split())
s = input()
  
group = []
sign = s[0]
curr = 0
for i in range(N):
  curr += 1
  if s[i] != sign: 
    group += [curr-1]
    curr = 1
    sign = s[i]
else:group += [curr]

if s[0] == '0' :
  if s[N-1] == '0':
    m = (len(group)+1)//2
    if K >= m:
      print(N)
      sys.exit()
    score = [sum(group[:2*K])]
    for i in range(m-K):
      score += [score[-1] - group[2*i] + group[2*i+2*K]]
  else :
    m = (len(group)+1)//2
    if K >= m:
      print(N)
      sys.exit()
    score = [sum(group[:2*K])]
    for i in range(m-K):
      score += [score[-1] - group[2*i] + group[2*i+2*K]]
    score[-1] += group[-1]
else:
  m = len(group)//2
  group += [0]
  if K >= m:
    print(N)
    sys.exit()
  score = [sum(group[:2*K+1])]
  for i in range(m-K):
    score += [score[-1] - group[2*i] - group[2*i+1] + group[2*i+2*K+1]+group[2*i+2*K +2]]

print(max(score))

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
s=input()
s+='e'
arr=[]
arr2=[]
count=1
for i in range(n):
  if s[i]==s[i+1]:
    count+=1
  else:
    arr.append(count)
    arr2.append(s[i])
    count=1
tmp=max([arr[i] for i in range(len(arr))])
for i in range(len(arr)-k*2):
  if arr2[i]=='1':
    if tmp<sum(arr[i:i+k*2+1]):
      tmp=sum(arr[i:i+k*2+1])
  else:
    if tmp<sum(arr[i:i+k*2]):
      tmp=sum(arr[i:i+k*2])
print(tmp)

Submission status: Wrong Answer

Solution code:
from collections import deque
n,k=map(int,input().split())
s=input()
f=int(s[0])
d=[0]
t=s[0]
for i in s:
    if i==t:
        d[-1]+=1
    else:
        d.append(1)
        t=i
p=deque()
q=deque()
ans=0
for a,i in enumerate(d):
    if (a%2==0 and f==0) or (a%2 and f):
        p.append(i)
        if len(p)>k: p.popleft()
    if (a%2 and f==0) or (a%2==0 and f):
        q.append(i)
        if len(q)>k+1: q.popleft()
    ans=max(ans,sum(p)+sum(q))
print(ans)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
s=input()
s+='e'
arr=[]
arr2=[]
count=1
for i in range(n):
  if s[i]==s[i+1]:
    count+=1
  else:
    arr.append(count)
    arr2.append(s[i])
    count=1
print(arr)
print(arr2)
tmp=max([arr[i] for i in range(len(arr))])
for i in range(len(arr)-k*2):
  if arr2[i]=='1':
    if tmp<sum(arr[i:i+k*2+1]):
      tmp=sum(arr[i:i+k*2+1])
  else:
    if tmp<sum(arr[i:i+k*2]):
      tmp=sum(arr[i:i+k*2])
print(tmp)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
S = input()
consecutive = 0
alternately = []
if S[0] == "0":
    first_num = 0
else:
    first_num = 1

current_num = first_num

for i in S:
    if i == str(current_num):
        consecutive += 1
    else:
        alternately.append(consecutive)
        consecutive = 1
        current_num = i
else:
    alternately.append(consecutive)

length = len(alternately)

left = 0
if length == 1:
    right = 1
elif length == 2:
    right = 2
else:
    right = 2 * K + first_num

ans = 0

while right <= length:
    if sum(alternately[left:right]) > ans:
        ans = sum(alternately[left:right])
    if (right - left) % 2 == 0:
        left += 1
    else:
        left += 2
    right += 2

print(ans)

Submission status: Wrong Answer

Solution code:
import sys
N, K = map(int, input().split())
s = input()
  
group = []
sign = s[0]
curr = 0
for i in range(N):
  curr += 1
  if s[i] != sign: 
    group += [curr-1]
    curr = 1
    sign = s[i]
else:group += [curr]

if s[0] == '0':
  m = (len(group)+1)//2
  if K >= m:
    print(N)
    sys.exit()
  score = [sum(group[:2*K])]
  for i in range(m-K):
    score += [score[-1] - group[2*i] + group[2*i+2*K]]
else:
  m = len(group)//2
  group += [0]
  if K >= m:
    print(N)
    sys.exit()
  score = [sum(group[:2*K+1])]
  for i in range(m-K):
    score += [score[-1] - group[2*i] - group[2*i+1] + group[2*i+2*K+1]+group[2*i+2*K +2]]

print(max(score))

Submission status: Wrong Answer

Solution code:
import numpy as np 

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

ls = []
pre = "1"
tmp = 0
for ss in s:
    # 値が変わったら，flush
    if pre!=ss:
        ls.append(tmp)
        tmp = 0
    tmp += 1
    pre = ss

ls.append(tmp)
# print(ls)

if len(ls)%2==0:
    tail = 2*K
else:
    tail = 2*K-1

sumv = 0
for i in range(len(ls)-tail):
    if i%2==1:
        sumv = max(sumv,sum(ls[i-1:i+2*K]))

if len(ls)-tail <= 0:
    print(N)
else:
    print(sumv)

Submission status: Wrong Answer

Solution code:
import math
import functools
import itertools
import numpy as np
import sys
MAX_INT = int(10e10)
MIN_INT = -MAX_INT
mod = 1000000007
sys.setrecursionlimit(1000000)
def IL(): return list(map(int,input().split()))
def SL(): return input().split()
def I(): return int(sys.stdin.readline())
def S(): return input()

N,K = IL()
s = S()

l = []

if s.count("1")==N:
    print(N)
else:
    cnt = 1
    for i in range(N):
        if s[i] == "1":
            cnt += 1
        else:
            if i != N-1:
                if s[i+1]=="0":
                    l.append([cnt,0])
                else:
                    l.append([cnt,-1])
            else:
                l.append([cnt,-1])
            cnt = 1
    else:
        l.append([cnt,-1])
    ans = 0

    ll = [0]
    tmp = 0
    for i in range(len(l)):
        tmp += l[i][0]
        if l[i][1] == -1:
            ll.append(tmp)

    for i in range(len(ll)-K-1):
        num = ll[i+K] - ll[i] + ll[i+K+1] - ll[i+K] -1
        ans = max(ans,num)
    if ans == 0:
        print(len(s))
    else:
        print(ans)

Submission status: Wrong Answer

Solution code:
import sys

def i2s():
    return sys.stdin.readline().rstrip()

def ii2ss(n):
    return [sys.stdin.readline() for _ in range(n)]

def sp2nn(sp, sep=' '):
    return [int(s) for s in sp.split(sep)]

def ss2nn(ss):
    return [int(s) for s in list(ss)]

def analyze(S):
    data = []
    i0 = 0
    s0 = S[0]
    sc = 0
    for i, s in enumerate(S):
        if s == s0:
            sc += 1
        else:
            data.append((i0, s0, sc))
            i0 = i
            s0 = s
            sc = 1
    data.append((i0, s0, sc))
    return data

def main(ss):
    N, K = sp2nn(ss[0])
    S = ss[1]
    data = analyze(S)
    cmax = 0
    
    for dl in range(len(data)):
        c = 0
        dw = K * 2
        if data[dl][1] == '1':
            dw += 1
        if dl + dw < len(data):
            c = data[dl+dw][0] - data[dl][0]
        else:
            c = len(S) - data[dl][0]
        if cmax < c:
            cmax = c
    
    print(cmax)

main(ii2ss(2))

Submission status: Wrong Answer

Solution code:
def cul(lst, m):
    r = 0
    for i in lst:
        r  += i if i < m else m
    return r

n, k = map(int,input().split())
s = input()
val = []

a = 1
for i in range(1, len(s)):
    if s[i-1] == s[i]:
        a += 1
    else:
        val.append(a)
        a = 1
val.append(a)
ans = []
now = int(s[0])
for i in range(len(s)-k):
    lst = val[i:i+k*2+1]
    if now == 1:
        ans.append(cul(lst, n))
    now = 1 if now == 0 else 0

print(max(ans) if not len(ans) == 0 else 1)

Submission status: Wrong Answer

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

pos = 0
i_s = [pos]
continue_s = S[0]
while True:
    pos += 1
    if pos == N :
        break
    s = S[pos]
    if continue_s == s:
        continue
    else:
        continue_s = s
        i_s.append(pos)

i_length = len(i_s) - 1
X = []
rev = K * 2

if i_length+1 <= rev:
    ans = N
else:
    for n,i in enumerate(i_s):
        if n==0 and S[i] == "0":
            if i_length - n == rev:
                X.append(i_s[n+rev]-i)
        if S[i] == "1":
            if i_length - n >= rev-1:
                if i_length - n == rev-1:
                    X.append(N-i)
                elif i_length - n == rev:
                    X.append(N-i)
                else:
                    X.append(i_s[n+rev+1]-i)
    ans = max(X)
print(ans)

Submission status: Wrong Answer

Solution code:
N, K = map(int,input().split())
S = input()
L = [0]
nowL = 0
if S[0] == '0':
    L = [0,0]
for i in range(N-1):
    nowL += 1
    if S[i] != S[i+1]:
        L.append(nowL)
# 終端処理
nowL += 1
L.append(nowL)

ans = 0
for j in range(len(L)):
    if 2*j+2*K+1 >= len(L):
        if ans == 0:
            ans = L[-1]
        break
    ans = max(ans,L[2*j+2*K+1]-L[2*j])

print(ans)

Submission status: Wrong Answer

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

l=[]
count=0
if S[0]=="0":
    now=0
    l+=[0]
    a=0
else:
    now=1
    a=1
for i in range(len(S)):
    if int(S[i])==now:
        count+=1
    else:
        l+=[count]
        count=1
        now=int(S[i])
l+=[count]
l+=[0]
 
ans=sum(l[:2*K+1])
s=sum(l[:2*K+1])
if len(l)-2*K>0:
    for i in range(2*K+2,len(l),2):
        s+=l[i]+l[i-1]-l[i-2*K-1]-l[i-2*K-2]
        ans=max(ans,s)
else:
    ans=sum(l)
 
 
 
if len(l)==1 and a==1:
    print(l[0])
else:
    print(ans)

Submission status: Wrong Answer

Solution code:
#参考　https://atcoder.jp/contests/abc124/submissions/4948803
from itertools import accumulate

n, k = map(int,input().split())
p = list(input())
a = []
pre = '1'
cnt = 0
for i in range(0,n):
     if pre != p[i]:
          a.append(cnt)
          cnt = 1
          pre = p[i]
     else:
          cnt += 1
a.append(cnt)
if pre == '1':
     a.append(0)

w = 2*k+1

if w >= len(a):
     print(n)
else:
     acc = list(accumulate(a))
     ans = 0
     acc = [0] + acc
     for i in range(0,len(a)-w+1,2):
          ans = max(ans,acc[i+w]-acc[i])
     print(ans)

Submission status: Wrong Answer

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

S = input()
para = [0]
for i, s in enumerate(S):
    if i == 0:
        para.append(1)
    else:
        if S[i] == S[i-1]:
            para[-1] += 1
        else:
            para.append(1+para[-1])

if S[-1] == '0':
    para.append(para[-1])


i = 0

tmpmax = 0

if S[0] == '1':
    if i + 2*k + 1 >= len(para) - 1:
        print(n)
    else:
        while i + 2*k + 1 <= len(para) - 1:
            j = i + 2*k + 1
            if para[j] - para[i-1] > tmpmax:
                tmpmax = para[j] - para[i]

            i += 2
        print(tmpmax)

else:
    if i + 2*k >= len(para) - 1:
        print(n)
    else:
        while i + 2*k <= len(para) - 1:
            j = i + 2*k
            if i == 0:
                if para[j] - para[i] > tmpmax:
                    tmpmax = para[j] - para[i]
            else:
                if para[j] - para[i-1] > tmpmax:
                    tmpmax = para[j] - para[i-1]

            i += 2
        print(tmpmax)

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
S = input()
SL = [int(i) for i in S]
keepcount = "A"
count = 0
if S[0] == "0":
    ans = [0]
else:
    ans = []
SL.append("B")
for i in SL:
    if keepcount == "A":
        keepcount = i
        count = 1
    elif keepcount == i:
        count += 1
        keepcount = i
    else:
        ans.append(count)
        count = 1
        keepcount = i



LA = len(ans)
ANS = []
finalans = []
value = 0
for i in range(LA):
    value += ans[i]
    ANS.append(value)
if K == 1:
    for i in range(LA-(2*K)):
       finalans.append(ANS[i+2*K]-ANS[i])
else:
    for i in range(LA-(2*K+1)):
       finalans.append(ANS[i+2*K+1]-ANS[i])

if len(finalans) != 0:
    print(max(finalans))
else:
    print(1)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
s=input()
s+='e'
arr=[]
count=1
for i in range(n):
  if s[i]==s[i+1]:
    count+=1
  else:
    arr.append(count)
    count+=1
tmp=0
for i in range(len(arr)-k):
  if tmp<sum(arr[i:i+k]):
    tmp=sum(arr[i:i+k])
print(tmp)

Submission status: Wrong Answer

Solution code:
#参考　https://atcoder.jp/contests/abc124/submissions/4948803
from itertools import accumulate

n, k = map(int,input().split())
p = list(input())
a = []
pre = '1'
cnt = 0
for i in range(0,n):
     if pre != p[i]:
          a.append(cnt)
          cnt = 1
          pre = p[i]
     else:
          cnt += 1
a.append(cnt)

w = 2*k+1

if w >= len(a):
     print(n)
else:
     acc = list(accumulate(a))
     ans = 0
     acc = [0] + acc
     for i in range(0,len(a)-w+1,2):
          ans = max(ans,acc[i+w]-acc[i])
     print(ans)

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
S = input()
SL = [int(i) for i in S]
keepcount = "A"
count = 0
if S[0] == "0":
    ans = [0]
else:
    ans = []
SL.append("B")
for i in SL:
    if keepcount == "A":
        keepcount = i
        count = 1
    elif keepcount == i:
        count += 1
        keepcount = i
    else:
        ans.append(count)
        count = 1
        keepcount = i



LA = len(ans)
ANS = []
finalans = []
value = 0
for i in range(LA):
    value += ans[i]
    ANS.append(value)
if K == 1:
    for i in range(LA-(2*K)):
       finalans.append(ANS[i+2*K]-ANS[i])
else:
    for i in range(LA-(2*K+1)):
       finalans.append(ANS[i+2*K+1]-ANS[i])

if len(finalans) != 0:
    print(max(finalans))
else:
    print(1 if S[0] == "1" else 0)

Submission status: Wrong Answer

Solution code:
n, k=map(int, input().split())
s=list(map(int, list(input())))
renzoku_list=[]
sum=1
before=s[0]
for i in range(1, n):
    if s[i]==before:
        sum=sum+1
        before=s[i]
    else:
        renzoku_list.append(sum)
        sum=1
        before=s[i]
renzoku_list.append(sum)

ruisekiwa=[0]
for i in range(len(renzoku_list)):
    ruisekiwa.append(renzoku_list[i]+ruisekiwa[i])

result_list=[]
length=len(renzoku_list)

max=0
if s[0]==0 and -(-(length)//2)<=k:
    print(n)
elif s[0]==1 and (length)//2<=k:
    print(n)
elif s[0]==0:
    for i in range(length-(2*k+1)+1):
        if i%2==0:#i=0,2,4,...
            sum=ruisekiwa[i+2*k]-ruisekiwa[i]
            if sum>=max:
                max=sum
        else:
            sum=ruisekiwa[i+2*k+1]-ruisekiwa[i]
            if sum>=max:
                max=sum
    print(max)
elif s[0]==1:
    for i in range(length-(2*k+1)+1):
        if i%2==0:#i=0,2,4,...
            sum=ruisekiwa[i+2*k+1]-ruisekiwa[i]
            if sum>=max:
                max=sum
        else:
            sum=ruisekiwa[i+2*k]-ruisekiwa[i]
            if sum>=max:
                max=sum
    print(max)

Submission status: Wrong Answer

Solution code:
l = list(map(int,input().split()))
k = l[1]
s_str = input()
s = list(map(int,s_str))
t = []
u = 1
ans = 0
for i in range(len(s)):
    if i == 0:
        if s[0] == 1:
            t.append(0)
        else:
            u = 1
    elif i == len(s)-1:
        if s[i] != s[i-1]:
            t.append(u)
            u = 1
        else:
            u = u + 1 
        t.append(u)
    else:
        if s[i] != s[i-1]:
            t.append(u)
            u = 1
        else:
            u = u + 1
if s[len(s)-1] == 1:
    for i in range(0,len(t),2):
        temp_sum = 0
        if i + 2 * k <= len(t):
            if i == 0:
                for j in range(0,i+2*k):
                    temp_sum += t[j]
            else:
                for j in range(i-1,i+2*k):
                    temp_sum += t[j]
        if ans < temp_sum:
            ans = temp_sum
else:
    for i in range(0,len(t),2):
        temp_sum = 0
        if i + 2 * k <= len(t):
            if i == 0:
                for j in range(0,i+2*k):
                    temp_sum += t[j]
            else:
                for j in range(i-1,i+2*k):
                    temp_sum += t[j]
        elif i + 2 * k == len(t) + 1:
            if i == 0:
                for j in range(0,i+2*k-1):
                    temp_sum += t[j]
            else:
                for j in range(i-1,i+2*k-1):
                    temp_sum += t[j]
        if ans < temp_sum:
            ans = temp_sum
if len(t) == 2:
    if t[0] == 0:
        ans = t[1]
if len(t) == 1:
    ans = t[0]
if len(s) == 1:
    ans = 1

print(ans)

Submission status: Wrong Answer

Solution code:
import sys
from pprint import pprint

def solve(N, K, line):

    nums = []
    prev = line[0]
    count = 0
    first = 0
    last = 0
    for i in xrange(len(line)):
       if line[i] == prev:
           count += 1
       else:
           nums.append(count)
           count = 1
       if line[i] == "1":
           if first == 0:
               first = len(nums)
       prev = line[i]

    nums.append(count)
    #pprint(nums)
    if prev == "1":
        #print "prev = 1"
        last = len(nums) - 1
    else:
        last = len(nums) - 2

    #print first, last
    if first == last:
        k = K
    else:
        k = 2*K

    max_n = 0
    #print first - k, last + k
    for i in xrange(first - k, min(len(nums), last + k)):
        l = max(0, i)
        r = min(N, i + k + 1)
        #print l, r
        #print sum(nums[l:r])
        max_n = max(max_n, sum(nums[l:r]))

    return max_n

if __name__ == '__main__':

    N, K = map(int, sys.stdin.readline().strip().split(" "))
    line = sys.stdin.readline().strip()
    ans = solve(N, K, line)
    print ans

Submission status: Wrong Answer

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

S = input()
para = [0]
for i, s in enumerate(S):
    if i == 0:
        para.append(1)
    else:
        if S[i] == S[i-1]:
            para[-1] += 1
        else:
            para.append(1+para[-1])

if S[-1] == '0':
    para.append(para[-1])


i = 0

tmpmax = 0

if S[0] == '1':
    if 2*k + 1 >= len(para) - 1:
        print(n)
    else:
        while i + 2*k + 1 <= len(para) - 1:
            j = i + 2*k + 1
            if para[j] - para[i-1] > tmpmax:
                tmpmax = para[j] - para[i]

            i += 2
        print(tmpmax)

else:
    if 2*k >= len(para) - 1:
        print(n)
    else:
        while i + 2*k <= len(para) - 1:
            j = i + 2*k
            if i == 0:
                if para[j] - para[i] > tmpmax:
                    tmpmax = para[j] - para[i]
            else:
                if para[j] - para[i-1] > tmpmax:
                    tmpmax = para[j] - para[i-1]

            i += 2
        print(tmpmax)

Submission status: Wrong Answer

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

S = input()
para = [0]
for i, s in enumerate(S):
    if i == 0:
        para.append(1)
    else:
        if S[i] == S[i-1]:
            para[-1] += 1
        else:
            para.append(1+para[-1])

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


i = 0

tmpmax = 0

if S[0] == '1':
    if 2*k + 1 >= len(para) - 1:
        print(n)
    else:
        while i + 2*k + 1 <= len(para) - 1:
            j = i + 2*k + 1
            if para[j] - para[i-1] > tmpmax:
                tmpmax = para[j] - para[i]

            i += 2
        print(tmpmax)

else:
    if 2*k >= len(para) - 1:
        print(n)
    else:
        while i + 2*k <= len(para) - 1:
            j = i + 2*k
            if i == 0:
                if para[j] - para[i] > tmpmax:
                    tmpmax = para[j] - para[i]
            else:
                if para[j] - para[i-1] > tmpmax:
                    tmpmax = para[j] - para[i-1]

            i += 2
        print(tmpmax)

Submission status: Wrong Answer

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

S = input()
para = [0]
for i, s in enumerate(S):
    if i == 0:
        para.append(1)
    else:
        if S[i] == S[i-1]:
            para[-1] += 1
        else:
            para.append(1+para[-1])
para.append(0)


i = 0

tmpmax = 0

if S[0] == '1':
    if 2*k + 1 >= len(para) - 1:
        print(n)
    else:
        while i + 2*k + 1 <= len(para) - 1:
            j = i + 2*k + 1
            if para[j] - para[i-1] > tmpmax:
                tmpmax = para[j] - para[i]

            i += 2
        print(tmpmax)

else:
    if 2*k >= len(para) - 1:
        print(n)
    else:
        while i + 2*k <= len(para) - 1:
            j = i + 2*k
            if i == 0:
                if para[j] - para[i] > tmpmax:
                    tmpmax = para[j] - para[i]
            else:
                if para[j] - para[i-1] > tmpmax:
                    tmpmax = para[j] - para[i-1]

            i += 2
        print(tmpmax)

Submission status: Wrong Answer

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

start = [0]
end = []

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

for i, (c0, c1) in enumerate(zip(s, s[1:])):
    print(c0, c1)
    if c0 == "1" and c1 == "0":
        end.append(i + 1)
    elif c0 == "0" and c1 == "1":
        start.append(i + 1)

if s[-1] == "1":
    end.append(n)
if n > 1 and s[-1] == "0":
    end.append(n)
    start.append(n)

print(start, end)

k = min(k, len(start) - 1)

print(max(en - st for st, en in zip(start, end[k:])))

Submission status: Wrong Answer

Solution code:
from collections import deque
n,k=map(int,input().split())
s=input()
f=int(s[0])
d=[0]
t=s[0]
for i in s:
    if i==t:
        d[-1]+=1
    else:
        d.append(1)
        t=i
p=deque([0])
ans=0
for a,i in enumerate(d):
    p.append(i)
    if len(p)>2*k+1:
        p.popleft()
    if f!=a%2 or 2*k>=len(p):
        ans=max(ans,sum(p))


print(ans)

Submission status: Wrong Answer

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

import numpy as np
line = np.zeros([2*n,],dtype=np.int)
counter1 = 0
counter0 = 0
ind = 0
for i in range(n):
    c = int(s[i])
    if ind%2 ==0:
        if c == 1:
            counter1 += 1
        else:
            counter0 += 1
            if ind == 0:
                line[ind] = counter1
            else:
                line[ind] = line[ind-1]+counter1
            counter1 = 0
            ind+=1
    elif ind%2 ==1:
        if c == 0:
            counter0 += 1
        else:
            counter1 += 1
            line[ind] = line[ind-1] + counter0
            counter0 = 0
            ind+=1

if ind%2 ==0:
        if ind == 0:
                line[ind] = counter1
        else:
                line[ind] = line[ind-1]+counter1
        ind+=1
elif ind%2 ==1:
        line[ind] = line[ind-1] + counter0
        ind+=1
        

line[1:ind+1]  = line[:ind]   
line[0] = 0 
line = line[:ind+1]
#print(line)

if (len(line)//2)<=k:
    print(n)
else:
    maxchain = 0
    for i in range(0,ind-(2*k+1)+1,2):
        chain = line[i+2*k+1]-line[i]
        #print(chain)
        if chain>maxchain:
            maxchain=chain
    print(maxchain)

Submission status: Wrong Answer

Solution code:
import sys
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7

N, K = LIST()
S = input()

# stand = []
# reverse = []
# state = None
# count = 0
# tmp = None
# for i in S:
# 	if i == "0":
# 		state = "stand"
# 	else:
# 		state = "reverse"
# 	if tmp == "stand" and state == "reverse":
# 		stand.append(count)
# 		count = 1
# 	elif tmp == "reverse" and state == "stand":
# 		reverse.append(count)
# 		count = 1
# 	else: # 状態が前と同じ
# 		count += 1
# 	tmp = state
# if state == "stand":
# 	stand.append(count)
# else:
# 	reverse.append(count)

# if "0" not in S:
# 	print(len(S))
# else:
# 	if K >= len(stand): # 全員逆立ちにできる
# 		print(len(S))
# 	else:
# 		if len(stand) > len(reverse):
# 			reverse.append(0)
# 			reverse.insert(0, 0)
# 		if len(stand) == len(reverse) and S[0] == "0":
# 			reverse.insert(0, 0)
# 		if len(stand) == len(reverse) and S[0] == "1":
# 			reverse.append(0)
# 		# print(stand)
# 		# print(reverse)
# 		max_ = 0
# 		for i in range(max(1, 1+len(reverse)-(K+1))):
# 			# print(reverse[i:i+K+1],stand[i:i+K])
# 			tmp = sum(reverse[i:i+K+1]+stand[i:i+K])
# 			if tmp > max_:
# 				max_ = tmp
# 		print(max_)

indexes = []
tmp = ""
for i in range(len(S)):
	if S[i] == "0":
		state = "stand"
	else:
		state = "reverse"
	if tmp != state:
		indexes.append(i)
	else: # 状態が前と同じ
		pass
	tmp = state
indexes.append(N)
ans = []
# print(indexes)
for i in range(len(indexes)):
	# print(i)
	if i == len(indexes)-1:
		break
	if S[indexes[i]] == "0":
		if i+2*K >= len(indexes):
			continue
		ans.append(indexes[i+2*K]-indexes[i])
	else:
		if i+2*K+1 >= len(indexes):
			continue
		ans.append(indexes[i+2*K+1]-indexes[i])
if ans == []:
	print(len(S))
else:
	print(max(ans))

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
S=input()
flag = 0
a=[0]*N
if S[0]=='0':
  a[0]=1
  flag = 1

for i in range(1,N):
  if S[i]=='0':
    if not flag:
      a[i]=1
    flag = 1
  else:
    flag = 0
  a[i]+=a[i-1]

r=0
ret=0
count=0
for l in range(N):
  while 1:
    r=max(l,r)
    if r==N:
      break
    if l==0:
      count = a[r]
    else:
      count = a[r]-a[l-1]
    if count <=K:
      ret=max(ret,r-l+1)
      r+=1
    else:
      break
print(ret)

Submission status: Wrong Answer

Solution code:
#input
N, K = map(int, input().split())#0~K-1回目と考える。
s = input() #0~N-1人目と考える。


#output

first_pos = int(s[0:1]) #0人目が○なら1, ×なら0

nowpos = first_pos #今チェックしている立ち方
nowposcount = 0 #1にすると0人目を考えるときにバグる
Accum = [0] * (N+1) #累積和 取り出し方は[まとまり個数目] 要素数はまとまりの個数+1
nowRuisekiCount = 1 #何番目のまとまりを見ているか 0~まとまり個数-1
zeroRenzoku = 1

for i in range(len(s)): #0~N-1人目につき、全てをチェックして累積和のリストを作る
    if int(s[i:i+1]) == nowpos: #今チェックしている立ち方と同じ(左の人と同じ)なら
        nowposcount += 1 #左のまとまりの個数を1増やす
        Accum[nowRuisekiCount] = Accum[nowRuisekiCount-1] + nowposcount #まとまりの数字数を入れる
    else:
        nowpos = abs(nowpos - 1)
        nowposcount = 1 #nowposcountは1に戻る
        nowRuisekiCount += 1 #次のまとまりに移ったことになる
        Accum[nowRuisekiCount] = Accum[nowRuisekiCount-1] + nowposcount #まとまりの文字数を入れる(1を入れる)

matomariCount = nowRuisekiCount #まとまりの数

#累積和を出す
if (first_pos == 1) & (matomariCount <= (K*2+1)): #まとまり数がK*2+1以下なら、全部逆立ちさせられる
    print(len(s))
elif (first_pos == 0) & (matomariCount <= (K*2)): #最初が0だと同様にK*2以下なら
    print(len(s))
else:
    #一番最初の合計文字数
    if first_pos == 1:
        maxaccum = Accum[(K*2+1)]
    else: #最初のまとまりが0なら
        maxaccum = Accum[(K*2)]

    #2個目のまとまりからも同様に見ていく
    for i in range(2, matomariCount+1, 1):
        if i+(K*2+1) > matomariCount+1:
            break
        if maxaccum < Accum[i+(K*2+1)]-Accum[i]:
            if abs((first_pos - ((i+(K*2+1)-1)%2))) == 0: #これが0なら右端が0なので右端を落とす
                maxaccum = max(maxaccum, Accum[i+(K*2+1)-1]-Accum[i])
            else:
                maxaccum = max(maxaccum, Accum[i+(K*2+1)]-Accum[i])
    print(maxaccum)

Submission status: Wrong Answer

Solution code:
def main():
  from itertools import groupby

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

  num = []
  cot = []
  for k, n in groupby(S):
    num.append(k)
    cot.append(len(list(n)))

  X = [0]*(len(cot)+1)
  for i in range(len(cot)):
    X[i+1] = X[i]+cot[i]

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

if __name__ == '__main__':
  main()

Submission status: Wrong Answer

Solution code:
from collections import deque
n,k=map(int,input().split())
s=input()
f=int(s[0])
d=[0]
t=s[0]
for i in s:
    if i==t:
        d[-1]+=1
    else:
        d.append(1)
        t=i
p=deque([0])
ans=0
for a,i in enumerate(d):
    p.append(i)
    if len(p)>2*k+1:
        p.popleft()
    if f!=a%2:
        ans=max(ans,sum(p))


print(ans)