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 = str(input())

S1 = []

tmp = 0
flag = False

if S[0]=="0":
    S1.append((0,0))

if S[-1]=="0":
    for i in range(N):
        if not flag:
            if S[i] =="1":
                flag = True
                tmp = i
            if i ==N-1:
                S1.append((i,i))
    
        if flag:
            if i==N-1:
                S1.append((tmp,i-1))
                S1.append((i,i))

            elif S[i]=="0":
                flag = False
                S1.append((tmp,i-1))



else:
    for i in range(N):
        if not flag:
            if S[i] =="1":
                flag = True
                tmp = i
            elif i ==N-1:
                S1.append((i,i))
    
        if flag:
            if S[i]=="0":
                flag = False
                S1.append((tmp,i-1))
            if i ==N-1:
                S1.append((tmp,N-1))

n = len(S1)

m = [0 for i in range(K+1)]

for k in range(K+1):
    M = 0
    i=0
    while i+k < n:
        M = max(M,S1[i+k][1]-S1[i][0]+1)
        i+=1
    m[k]=M
print(m)
print(max(m))

Submission status: Wrong Answer

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

items = []
current = s[0]
item = 1
for i in range(1, len(s)):
    if s[i] == current:
        item += 1
    else:
        current = s[i]
        items.append(item)
        item = 1
items.append(item)

ret = 0
if len(items) == 1:
    print(1)
else:
    cm = max(items)
    for ki in range(k):
        tmp = []
        for i in range(len(items)):
            if i == 0:
                c = items[i] + items[i + 1]
            elif i == len(items) - 1:
                c = items[i - 1] + items[i]
            else:
                c = items[i-1] + items[i] + items[i + 1]
            tmp.append(c)
        tm = [i for i, x in enumerate(tmp) if x == max(tmp)]

Submission status: Wrong Answer

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

jk_p = []
joutai = S[0]
kaishi = 0
for i in range(len(S)-1):
    if joutai!=S[i+1]:
        w = [joutai, kaishi, i+1-kaishi]
        jk_p.append(w)
        joutai = S[i+1]
        kaishi = i+1
    if i == len(S)-2:
        w = [joutai, kaishi, len(S) - kaishi]
        jk_p.append(w)


max = 0
k = 0
for i in range(len(jk_p)):
    c = 0
    for j in range(K+3):
        if K+3 > len(jk_p)-1:
            break
        if jk_p[i][0] == '0':
            if j == K+1:
                break
        if i+j > len(jk_p)-1:
            break
        c += jk_p[i+j][2]
    if c > max:
        max = c
print(max)

Submission status: Wrong Answer

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

num=0
A=[]
for i in range(n):
    if s[i]=="0":
        num+=1
        if i==n-1:
            A.append(num)
        else:
            if s[i+1]=="1":
                A.append(num)
                num=0
A.sort(reverse=True)

ans=0
ans+=s.count("1")
if len(A)!=0:
    if len(A)>=k:
        for j in range(k):
            ans=A[j]+ans
    else:
        for j in range(len(A)):
            ans=A[j]+ans
print(ans)
#print(type(A[0]))

Submission status: Wrong Answer

Solution code:
import re
N, K = map(int, input().split())
S = input()
a1len = 0
st1, en1 = 0, 0
flag = False
pattern = r"0+"

if '0' in S:
    for k in range(K):
        iterator = re.finditer(pattern ,S)
        a1len = 0
        for match in iterator:
            s, e = match.span()
            lenS = e - s
            if s == 0:
                S1 = '1' * lenS + S[e:]
            else:
                S1 = S[:s] + '1' * lenS + S[e:]
            slis = re.findall(r'1+', S1)
            if max(map(len, slis)) > a1len:
                a1len = max(map(len, slis))
                st1, en1 = s, e
        if st1 == 0:
            S = '1' * lenS + S[en1:]
        else:
            S = S[:st1] + '1' * lenS + S[en1:]

slis = re.findall(r'1+', S)
a1len = max(map(len, slis))
print(a1len)

Submission status: Wrong Answer

Solution code:
import numpy as np

N, trial = input().split()
N, trial = int(N), int(trial)
row = input()
temp = row[0]
row = row[1:]
ppl = np.arange(0)
count = 1
for i in row:
  if i != temp: #切り替えの時
    ppl = np.append(ppl, count)
    count = 1
    temp = i
  else:
    count += 1
ppl = np.append(ppl, count) 

#print(ppl) #pplは人の塊のリスト
if temp == '1':
  temp = 0
  for i in range(len(ppl)):
    comp = sum(ppl[i:i+2*trial+1])
    if temp < comp:
      temp = comp
    else:
      pass
else:
  temp = 0
  for i in range(len(ppl)):
    comp = sum(ppl[i:i+2*trial])
    if temp < comp:
      temp = comp
    else:
      pass
print(temp)

Submission status: Wrong Answer

Solution code:
import numpy
N,K=(int(x) for x in input().split())
S=input()

def Solve(S,K):

    if S.count("0")<=K:
        return len(S)
    if S[0]=="1":
        S="0"+S
    

    LA=[]#立ってる人 = 0
    LB=[]#逆立ちしてる人 = 1
    k=-1
    current="0"
    appendLA=LA.append
    appendLB=LB.append
    for i,s in enumerate(S):
        k+=1
        if s!=current:
            if s=="0":
                appendLA(k)#逆立ちの個数 = 1
                current="0"
                k=0
            else:
                appendLB(k)#立ってる人個数 = 0
                current="1"
                k=0
                
    if current=="1":
        appendLA(k+1)
    else:
        appendLB(k+1)
        appendLA(0)
        
    mx=0

    if K==0:
        print(max(LA))    

    sum_LA = list(numpy.cumsum(LA))
    sum_LB=list(numpy.cumsum(LB))
    

    for i in range(len(LA)-K+1):
        if i==0:
            result=sum_LA[K-1]+sum_LB[K-1]
        elif i==1:
            result=sum_LA[i+K-1]+sum_LB[i+K-1]-sum_LB[i-1]
        else:
            result=sum_LA[i+K-1]-sum_LA[i-2]+sum_LB[i+K-1]-sum_LB[i-1]
        mx=max(mx,result)

    return mx

print(Solve(S,K))

Submission status: Wrong Answer

Solution code:
import math

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

#1
l=len(s)
DIF=[0,0]
main=2
for i in range(l):
  if s[i]!=main:
    DIF.append(i)
    main=s[i]
	
DIF=DIF+[l-1,l-1]
A0=[DIF[i*2] for i in range(len(DIF)//2+ 1 if len(DIF)%2==1 else 0)]
A1=[DIF[i*2+1] for i in range(len(DIF)//2)]

D0=sorted([A0[i]-A0[i-1] for i in range(1,len(A0))])
D1=sorted([A1[i]-A1[i-1] for i in range(1,len(A1))])

a=len(A0)
b=len(A1)
A=0
B=0
op=0
oi=0
for i in range(a-min(k,a)):
  if A<A0[i+min(k,a)]-A0[i]:
    A=A0[i+min(k,a)]-A0[i]
    if A0[i+min(k,a):-1]==[0]*(l-i+min(k,a)):
      op=l-i+min(k,a-1)-1
for i in range(b-min(k,b)):
  if B<A1[i+min(k,b)]-A1[i]:
    A=A1[i+min(k,a)]-A1[i]
    if A1[i+min(k,b):-1]==[0]*(l-i+min(k,a)):
      op=l-i+min(k,a-1)-1
  
print(B+1 if s[0]=="0" else A+1)

Submission status: Wrong Answer

Solution code:
import sys
from itertools import accumulate
N, K  = map(int, input().split())
ans = 0
num = 0
S = input()
a = S.split('1')
b = S.split('0')
ls = []
for x in a:
    if len(x) != 0:
        ls.append(len(x))
c = [0] + list(accumulate(ls))
ls_ = []
for x in b:
    if len(x) != 0:
        ls_.append(len(x))
d = [0] + list(accumulate(ls_))

if len(c)-1 <= K:
    print(N)
    sys.exit()

if len(c) == len(d):
    if S[0] == '0':
        q = c[-1]
        c.append(q)
        r = d[-1]
        d.append(r)
        for x, y, u, t in zip(c, c[K+1:], d, d[K+1:]):
            ans = max(ans, y-x+t-u)
        print(ans)
    elif S[0] == '1':
        q = c[-1]
        c.append(q)
        r = d[-1]
        d.append(r)
        for x, y, u, t in zip(c, c[K+1:], d, d[K+1:]):
            ans = max(ans, y-x+t-u)
        print(ans)
elif len(c) == len(d)+1:
    q = c[-1]
    c.append(q)
    for x, y, u, t in zip(c, c[K:], d, d[K+1:]):
        ans = max(ans, y-x+t-u)
    print(ans)
elif len(c) == len(d)-1:
    r = d[-1]
    d.append(r)
    for x, y, u, t in zip(c, c[K:], d, d[K+1:]):
        ans = max(ans, y-x+t-u)
    print(ans)

Submission status: Wrong Answer

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

ruiseki=[0]
temp=0
for i in range(1,n):
    if s[i]==s[i-1]:
        temp+=1
        ruiseki.append(temp)
    else:
        temp=0
        ruiseki.append(temp)
gorira=[]
ruiseki="".join(map(str,ruiseki))
for i in range(n-1):
    doko=ruiseki.find("0",i,len(ruiseki))
    gorira.append(doko)

gorira=list(set(gorira))

temp2=[]
for i in range(len(gorira)-6):
    temp2.append(int(gorira[i+6])-int(gorira[i]))

if temp2==[]:
    print("1")
    
else:print(max(temp2))

Submission status: Wrong Answer

Solution code:
import numpy as np

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

l = [list(np.repeat(0, K+1)), list(np.repeat(0, K+1))]
ans = []
k = [int(s) for s in S]
v0 = 1 if k[0] == 0 else 0
v1 = 1 if k[0] == 1 else 0
a = sum(l[0][-K:]+l[1][-K-1:])
for i in range(1, N):
    if (k[i]==0) & (k[i-1]==0):
        v0 += 1
    elif (k[i]==1) & (k[i-1]==0):
        l[0].append(v0)
        v0 = 0
        v1 += 1
    elif (k[i]==0) & (k[i-1]==1):
        l[1].append(v1)
        v0 += 1
        
        a -= l[0][-K-1] + l[1][-K-2]
        a += v1 + l[0][-1]
        v1 = 0
        ans.append(a)    
    else:
        v1 += 1
        
for i, v in enumerate([v0, v1]):
    if v > 0:
        l[i].append(v)

if len(l[0])-K-1 <= K:
    print(N)

else:
    if (v1>0) & (v0==0):
        a -= l[0][-K-1] + l[1][-K-2]
        a += v1 + l[0][-1]
        ans.append(a)
    elif (v0 > 0) & (v1==0):
        a -= l[0][-K-1] + l[1][-K-1]
        a += v0
    print(max(ans))

Submission status: Wrong Answer

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

sa = []

last = s[0]
nn = 1
for i in range(1,len(s)):
	if s[i] == last:
		nn += 1
	else:
		sa.append(nn)
		last = s[i]
		nn = 1
sa.append(nn)

max = -1
for saa in sa:
	if saa > max:
		max = saa

max_j = 0

if s[0] == 0:
	amari = 0
else:
	amari = 1

#print(sa)

for i in range(k):
	for j in range(0,len(sa)-2):
		temp = sa[j] + sa[j+1] + sa[j+2]
		if(temp > max):
			max = temp
			max_j = j
	new_sa = sa[0:max_j]
	new_sa.append(max)
	for k in range(max_j+2,len(sa)-1):
		new_sa.append(sa[k])
	#print(new_sa)
	sa = new_sa

print(max)

Submission status: Wrong Answer

Solution code:
list1 = []
str = ''
for i in range(2):
  if (i == 0):
    list1 = list(map(int,input().split()))
  else:
    str = input()
n = list1[0]
k = list1[1]
str_list = [i for i in str]
prev = str_list[0]
max = 0
sum = 0
cnt = 0
for i, char in enumerate(str_list):
  if (char == prev):
    sum += 1
  else:
    cnt += 1
    if (cnt == 1):
      prev = char
      sum += 1
    else:
      cnt = 0
      if (sum > max):
        max = sum
        sum = 0  
        
print(max + 2)

Submission status: Wrong Answer

Solution code:
NK = list(map(int,input().split()))
N = NK[0]
K = NK[1]
S = list(input())
Sc = S[:]
if len(S)>1:
    n=1
    n0=0
    n1=0
    Summary0 = []
    Summary1 = []
    first = S[0]
    last = S[N-1]
    if first == "0":
        n0 += 1
    if first == "1":
        n1 += 1

    while n < N:
        if (S[n]=="0")and(S[n-1]=="0"):
            n0 += 1
            n += 1
        elif (S[n]=="0")and(S[n-1]=="1"):
            n0 += 1
            Summary1.append(n1)
            n1 = 0
            n += 1
        elif (S[n]=="1")and(S[n-1]=="0"):
            n1 += 1
            Summary0.append(n0)
            n0 = 0
            n += 1
        elif (S[n]=="1")and(S[n-1]=="1"):
            n1 += 1
            n += 1

    if last == "0":
        Summary0.append(n0)
    if last == "1":
        Summary1.append(n1)


    cand=0
    maxnow=0
    if first == "0":
        for i in range(len(Summary0)-K):
            cand = sum(Summary0[i:i+K])+sum(Summary1[i:i+K])
            if cand > maxnow:
                maxnow = cand
    elif first == "1":
        for i in range(len(Summary1)-K):
            cand = sum(Summary0[i:i+K])+sum(Summary1[i:i+K+1])
            if cand > maxnow:
                maxnow = cand
if len(S)==1:
    maxnow = int(S[0])
print(maxnow)

Submission status: Wrong Answer

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

flag = False
li = [0]
for i in range(n):
    if bool(int(t[i])) == flag:
        li[-1] += 1
    else:
        flag = not flag
        li.append(1)

zeros = li[0::2]
ones = li[1::2]
print(li, zeros, ones)

ans = []
for i in range(len(zeros)):
    ans.append(sum(zeros[i:i+k]) + sum(ones[i-1:i+k]))
print(max(ans))

Submission status: Wrong Answer

Solution code:
A = list(map(int,input().split()))
LENGTH = A[0]
TIME = A[1]
#print(A)

LINE = list(map(int,input()))
#print(LINE)

def LINE_CHANGER(LINE):
    ZERO_MAX = 1
    ZERO_COUNTER = 1
    POINT = 0
    i = 0
    k = 1
    while i + k < LENGTH:
        if LINE[i] == 0 and LINE[i+k] == 0:
            ZERO_COUNTER += 1
            k += 1
            #print(k,i)
        else:
            if ZERO_COUNTER > ZERO_MAX:
                ZERO_MAX = ZERO_COUNTER
                POINT = i 
            ZERO_COUNTER = 1
            i = i+k
            k = 1
    #print(POINT,ZERO_MAX)        
    for i in range(ZERO_MAX):
        LINE[POINT+i] = 1
    
    return(LINE)


while TIME > 0:
    LINE_CHANGER(LINE)
    #print(LINE)
    TIME -= 1
    
def LINE_ONE(LINE):
    ONE_MAX = 1
    ONE_COUNTER = 1
    POINTER = 0
    i = 0
    k = 1
    while i + k < LENGTH:
        if LINE[i] == 1 and LINE[i+k] == 1:
            ONE_COUNTER += 1
            k += 1
        else:
            if ONE_COUNTER > ONE_MAX:
                ONE_MAX = ONE_COUNTER
                POINT = i 
            ONE_COUNTER = 1
            i = i+k
            k = 1
    return(ONE_MAX)
    

print(LINE_ONE(LINE))

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
s=list(input())
s=list(map(int,s))
stack=[]
tmp=1
flag=True
for i in range(1,len(s)):
  if s[i]!=s[i-1]:
    stack.append(tmp)
    tmp=1
  else:
    tmp+=1
stack.append(tmp)
odd=[]
even=[]
tmp=0
if s[0]==0:
  flag=False
else:
  flag=True
for i in range(len(stack)):
  tmp+=stack[i]
  if i%2==0:
    if flag:
      odd.append(tmp)
    else:
      even.append(tmp)
  else:
    if flag:
      even.append(tmp)
    else:
      odd.append(tmp)
ans=0
for i in range(len(even)-k-1):
  ans=max(ans,even[i+k+1]-even[i])
print(max(ans,1))

Submission status: Wrong Answer

Solution code:
import math                                                                                            

n,k = raw_input().split()
s = list(raw_input())


cnt = 0

prev = ''
tmp = ''
ar = []
for i in s:
    if i == '0' and prev !=  1:
        tmp += '0'
        prev = 0
    elif i == '0' and prev == 1:
        prev = 0
        ar.append(tmp)
        tmp = '0'
    elif i == '1' and prev != 0:
        tmp += '1'
        prev = 1
    elif i == '1' and prev == 0:
        prev = 1
        ar.append(tmp)
        tmp = '1'

ar.append(tmp)
k = int(k)


j = 0
cnt = 0
total = 0
m = 0

for i in ar:
    total += len(i)
    if '0' in i:
        cnt += 1
    if '0' in i and cnt == k + 1:
        total -= len(ar[j-1-2*k]) + len(ar[j-2-2*k])
    m = max(m, total)
    j += 1

print m

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-

import sys
input = sys.stdin.readline

def io_generator():
	return input()

#+++++++++++++++++++

def main(io):
	n,k= map(int, io().split())
	s=[int(i) for i in io()[:n]]
	init=s[0]
	ll=[]
	m=0
	pre=init
	for c in s:
		if c == pre:
			m+=1
		else:
			ll.append(m)
			m=1
			pre=c
	off= 1 if init==1 else 0
	if len(ll)<=2*k + off:
		return n
	pre=sum(ll[:2*k+off])
	rr_max=pre
	for i in range(len(ll)//2 - k):
		cc=pre - ll[i] - ll[i+1] + ll[2*k]+ll[2*k+1]
		rr_max = max(rr_max, cc)
		pre=cc
	return rr_max


#++++++++++++++++++++

if __name__ == "__main__":
	io= lambda : io_generator()
	print (main(io))

Submission status: Wrong Answer

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

li_s = []
count = 1
temp = S[0]
if temp == "0":
    li_s.append(0)
for i in range(1,N):
    if S[i] != temp:
        if i == N-1:
            li_s.append(count)
            li_s.append(1)
        else:
            li_s.append(count)
            temp = S[i]
            count = 1
    elif i == N-1:
        count += 1
        li_s.append(count)
    else:
        count += 1

for i in range(100000):
    li_s.append(0)
max = 0

if K == 1:
    print(1)
    sys.exit()
for i in range(N - K*2+1):
    temp = 0
    if i % 2 == 0:
        for j in range(i,i+K*2+1):
            temp += li_s[j]
    else:
        for j in range(i,i+K*2):
            temp += li_s[j]

    if temp > max:
        max = temp
print(max)

Submission status: Wrong Answer

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

sr = k
tr = k + 1

ss = []
tt = []

if s[0] == '0':
  tt += [0]
  
mm = 0

c = 1
for i in range(1, n):
  if s[i-1:i+1] == '00':
    c += 1
  if s[i-1:i+1] == '10':
    tt += [c]
    if len(tt) > tr:
      tt = tt[1:]
    if len(tt) <= tr:
      m = sum(ss) + sum(tt)
      if mm < m:
        mm = m
    c = 1
  if s[i-1:i+1] == '11':
    c += 1
  if s[i-1:i+1] == '01':
    ss += [c]
    if len(ss) > sr:
      ss = ss[1:]
    c = 1
if s[n-1] == '0':
  ss += [c]
else:
  tt += [c]
  if len(tt) > tr:
    tt = tt[1:]
  if len(tt) <= tr:
    m = sum(ss) + sum(tt)
    if mm < m:
      mm = m
print(mm)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
S = input()
X = []
ch = S[0]
cnt = 1
for i in range(1, len(S)):
    if S[i] != ch:
        X.append([ch, cnt])
        cnt = 1
        ch = S[i]
    elif S[i] == ch:
        cnt += 1
X.append([ch, cnt])

st = None
if X[0][0] == '0':
    st = 0
else:
    st = 1
en = st +  2 * (K - 1)
re = K
for v in X:
    if v[0] == '0':
        re -= 1

if '0' not in S:
    print(N)
elif re > 0:
    print(N)
else:
    max_1 = 0
    for i in range(st, en + 1):
        max_1 += X[i][1]
    if st - 1 >= 0:
        max_1 += X[st - 1][1]
    if en + 1 < len(X):
        max_1 += X[en + 1][1]
    while True:
        if en + 2 >= len(X):
            break
        tmp = max_1
        if st == 0:
            tmp -= X[0][1]
        else:
            tmp -= X[st][1] + X[st - 1][1]
        if en + 2 == len(X) - 1:
            tmp += X[en + 2][1]
        else:
            tmp += X[en + 2][1] + X[en + 1][1]
        st = st + 2
        en = en + 2
        if tmp > max_1:
            max_1 = tmp
    print(max_1)

Submission status: Wrong Answer

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

l = list(map(int,input()))
ll = []
p = l[0]
co = 1
ppp = 0
su = 0
lll = []
for i in range(n-1):
    if p == l[i+1]:
        co += 1
    else:
        if p == 0:
            ppp += 1
        ll.append([p,co])
        p = l[i+1]
        co = 1
if p == 0:
    ppp += 1
ll.append([p,co])
co = 0

if len(ll) == 1:
    print(n)
    exit(0)
if ppp <= k:
    print(n)
    exit(0)

if ll[0][0] == 0:
    if ll[-1][0] == 0:
        for i in range(2*k):
            co += ll[i][1]
        lll.append(co)
        co -= ll[0][1]
        for i in range(ppp-k):
            if ppp-k-1 == i and i == 0:
                co += ll[-1][1]
                lll.append(co)
            elif ppp-k-1 == i:
                co += ll[-1][1] - (ll[i*2+1][1] + ll[i*2+2][1])
                lll.append(co)
            elif i == 0:
                co += ll[i*2+1+2*k][1] + ll[i*2+2*k][1]
                lll.append(co)
            else:
                co += ll[i*2+1+2*k][1] + ll[i*2+2*k][1] - (ll[i*2+1][1] + ll[i*2+2][1])
                lll.append(co)
    else:
        for i in range(2*k):
            co += ll[i][1]
        lll.append(co)
        co -= ll[0][1]
        for i in range(ppp-k):
            if i == 0:
                co += ll[i*2+1+2*k][1] + ll[i*2+2*k][1]
                lll.append(co)
            else:
                co += ll[i*2+1+2*k][1] + ll[i*2+2*k][1] - (ll[i*2+1][1] + ll[i*2+2][1])
                lll.append(co)
else:
    if ll[-1][0] == 1:
        for i in range(2*k+1):
            co += ll[i][1]
        lll.append(co)
        co -= ll[0][1] + ll[1][1]
        for i in range(ppp-k):
            if i == 0:
                co += ll[i*2+1+2*k][1] + ll[i*2+2+2*k][1]
                lll.append(co)
            else:
                co += ll[i*2+1+2*k][1] + ll[i*2+2+2*k][1] - (ll[i*2][1] + ll[i*2+1][1])
                lll.append(co)
    else:
        for i in range(2*k+1):
            co += ll[i][1]
        lll.append(co)
        co -= ll[0][1] + ll[1][1]
        for i in range(ppp-k):
            if ppp-k-1 == i and i == 0:
                co += ll[-1][1]
                lll.append(co)
            elif ppp-k-1 == i:
                co += ll[-1][1] - (ll[i*2][1] + ll[i*2+1][1])
                lll.append(co)
            elif i == 0:
                co += ll[i*2+1+2*k][1] + ll[i*2+2+2*k][1]
                lll.append(co)
            else:
                co += ll[i*2+1+2*k][1] + ll[i*2+2+2*k][1] - (ll[i*2][1] + ll[i*2+1][1])
                lll.append(co)
            print(co)
print(max(lll))

Submission status: Wrong Answer

Solution code:
n, k = list(map(int, input().split()))
s = str(input())
tmp = s + '1'
r = 0
for i in range(n):
  if tmp[i] == '0' and tmp[i+1] == '1':
    r += 1
if k >= r:
  print(n)
else:
  r = [(0, n)] * r
  for i in range(len(r) - 1, 0, -1):
    for j in range(r[i][0], r[i][1]):
      if j == r[i][1] - 1 or (s[j] == '0' and s[j+1] == '1'):
        left = j - r[i][0] + 1
        break
    for j in range(r[i][1] - 1, r[i][0] - 1, -1):
      if j == r[i][0] or (s[j] == '0' and s[j-1] == '1'):
        right = r[i][1] - j
        break
    if left <= right:
      r[i-1] = (r[i][0] + left, r[i][1])
    else:
      r[i-1] = (r[i][0], r[i][1] - right)
  print(r[k-1][1] - r[k-1][0])

Submission status: Wrong Answer

Solution code:
import re
from itertools import combinations
import sys
N = 0
K = 2
S = '11101010110011'
N, K = map(int, input().split())
S = input()


search = re.findall('[0]+|[1]+', S)
# print(search)

# print(''.join(search))

zeros_id = list()
for id, num in enumerate(search):
    if num[0] == '0':
        zeros_id.append(id)
# print(zeros_id)
max_length = 0
if len(zeros_id) == 0:
    max_length = len(search)

comb = combinations(zeros_id, K)
# print(comb)

for c in comb:
    tmp = search[:]
    length = 0
    # print(c)
    for id in c:
        l = len(tmp[id])
        tmp[id] = '1'*l
    # print("ls", tmp)
    back = ''.join(tmp)
    # print(back)
    ones = back.split('0')
    # print(ones)
    length = len(max(ones))
    # print(length)
    if length > max_length:
        max_length = length

print(max_length)

Submission status: Wrong Answer

Solution code:
q = list(map(int, input().split()))
a = [i for i in input()]

for s in range(q[1]):
    target_list = []
    for i, n in enumerate(a):
        cnt = 0
        if i != '0':
            target_list.append(cnt)
            break
        else:
            for j, m in enumerate(a[i + 1:len(a)]):
                init = True
                if m == '0' and init:
                    cnt += 1
                    break
                if m == '1' and init:
                    cnt += 1
                    init = False
            for k, l in enumerate(reversed(a[:i + 1])):
                if l == '1':
                    cnt += 1
    maxval = max(target_list)
    for x, y in enumerate(a[target_list.index(maxval):len(a)]):
        if y != '1':
            a[x] = '1'

target_list2 = []
for i, n in enumerate(a):
    cnt = 0
    if i != '0':
        target_list2.append(cnt)
        break
    else:
        for j, m in enumerate(a[i + 1:len(a)]):
            init = True
            if m == '0' and init:
                cnt += 1
                break
            if m == '1' and init:
                cnt += 1
                init = False
        for k, l in enumerate(reversed(a[:i + 1])):
            if l == '1':
                cnt += 1
print(target_list2)
print(max(target_list2))

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
s = input()
l = r = 0
a = []
while l < n:
    while r < n and s[l] == s[r]:
        r += 1
    a.append(r-l)
    l = r
m = len(a)
asum = [0 for _ in range(m+1)]
ans = 0
for i in range(m):
    asum[i+1] = asum[i] + a[i]
if s[0] == '0':
    if k >= m//2:
        ans = n
    else:
        if s[-1] == '0':
            a.append(0)
            asum.append(asum[-1])
        for i in range(1, m-2*k):
            if i % 2 == 0:
                continue
            ans = max(ans, asum[2*k+i+1] - asum[i])
        ans = max(ans, asum[2*k])
else:
    if k >= m//2:
        ans = n
    else:
        if s[-1] == '0':
            a.append(0)
            asum.append(asum[-1])
        for i in range(0, m-2*k):
            if i % 2 == 1:
                continue
            ans = max(ans, asum[2*k+i+1] - asum[i])
print(ans)

Submission status: Wrong Answer

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

l_blob = [0,0]
c_blob = [0,0]
r_blob = [0,0]
l, r = 0,0

prev = '1'
temp = 0

for i in range(K):
    for i, s in enumerate(G_S):
        if s == '1':
            if prev == '0':
                r_blob = [i,i]
            else:
                r_blob[1] = i
        else:
            if prev == '0':
                c_blob[1] = i
            else:
                x = r_blob[1] - l_blob[0]
                if x > temp and c_blob != [0,0]:
                    l, r = c_blob
                    temp = x

                c_blob = [i,i]
                l_blob = r_blob[:]
            r_blob = [i,i]
        prev = s

    x = r_blob[1] - l_blob[0]
    if x > temp and c_blob != [0,0]:
        l, r = c_blob

    if c_blob == [0,0]:
        break

    for j in range(r-l+1):
        G_S[l+j] = '1' if G_S[l+j] == '0' else '0'
    
ans = 0
temp = 0
for s in G_S:
    if s == '1':
        temp += 1
    else:
        ans = max(ans,temp)
        temp = 0

print(ans)

Submission status: Wrong Answer

Solution code:
N,K = [int(i) for i in input().split()]
S = list(map (int, list(input())))
start = S[0]
row = []
state = [0,start]
for i in S:
    if i == start:
        state[0] += 1
    else:
        row.append(state)
        start = i
        state = [1,i]
row.append(state)
ans = []
for i in range(max(len(row)-K*2+1,1)):
    if row[i][1] == 1:
        ans.append(sum([row[i][0] for i in range(i,min(K*2+i+1,len(row)))]))
if ans == []:
    for i in range(max(len(row)-K*2+1,1)):
        ans.append(sum([row[i][0] for i in range(i,min(K*2+i,len(row)))]))
print(max(ans))

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().rstrip().split())
S = input().rstrip()
index = {}
now = 0
for i in range(N):
    if now > N-1:
        break
    if S[now] == "1":
        count = 1
        for j in range(1,N):
            if now+j > N-1:
                break
            if S[now+j] == "1":
                count += 1  
            else:
                break
        index[now] = count
        now += count
    else:
        now += 1
one = list(index)
one.insert(-1,0)
maximum = 0
for i in range(len(one)//K-1):
    tmp = one[i+K] - one[i] + index[one[i+K]]
    if tmp > maximum:
        maximum = tmp
print(maximum)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
S = list(input())
S = [int(S[i]) for i in range(N)]

def return_ans(S_initial, S):
    ans = 0
    block_size_list = []
    tmp_block_size = 1
    for i in range(1, N):
        if S[i - 1] == S[i]:
            tmp_block_size += 1

        else:
            block_size_list.append(tmp_block_size)
            tmp_block_size = 1
        if i == N - 1:
            block_size_list.append(tmp_block_size)

    times = len(block_size_list) - K - 1

    if K % 2 == 1:
        if K * 2 >= len(block_size_list):
            ans = len(S)
        else:
            for i in range(times):
                ans = max(ans, sum(block_size_list[i:i + K * 2]))
    else:
        block_size_list.append(0)
        if K * 2 >= len(block_size_list):
            ans = len(S)
        else:
            if S_initial == 0:
                ans = max([sum(block_size_list[i:i + K * 2 + i % 2]) for i in range(times)])

            else:
                for i in range(times):
                    ans = max([sum(block_size_list[i:i + K * 2 + (i + 1) % 2]) for i in range(times)])

    return ans

print(return_ans(S[0], S))

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
S = list(input())
l = []
"""
memo = 0
for i in range(len(S)-1):
    if i==len(S)-1-memo:
        break
    if S[i] =="0" and S[i+1]=="0":
        S.pop(i)
        memo += 1
else:
    S=",".join(S)
"""

res= 0
mres = 0
f = 1
for i in range(len(S)):
    res += 1
    if S[i] == "0":
        if f==1:
            K -= 1
            l.append(i)
            f = 0
            if K <= 0:
                mres = max(res,mres)
                res = i-l[-1*(K)]
                continue
        else:
            l[-1] += 1
    else:
        f = 1
    mres = max(mres,res)
else:
    print(mres)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
S = input()
S = "0" + S + "0"
lis = []
for i in range(1, N + 2):
  if S[i - 1] == "0" and S[i] == "1":
    lis.append([i])
  if S[i - 1] == "1" and S[i] == "0":
    lis[-1].append(i - 1)
leng = len(lis)
ans = 0
if S[1] == "1" and S[N] == "1":
  if K >= leng - 1:
    ans = N
  else:
    for i in range(leng):
      if i + K < leng:
        newan = lis[i + K][1] - lis[i][0] + 1
        ans = max(ans, newan)
  print(ans)
elif S[1] == "1" and S[N] == "0":
  if K >= leng:
    ans = N
  else:
    for i in range(leng):
      if i + K < leng:
        newan = lis[i + K][1] - lis[i][0] + 1
        ans = max(ans, newan)
  print(ans)
elif S[1] == "0" and S[N] == "1":
  if K >= leng:
    ans = N
  else:
    for i in range(leng):
      if i + K < leng:
        newan = lis[i + K][1] - lis[i][0] + 1
        ans = max(ans, newan)
    ans = max(ans, lis[K - 1][1])
  print(ans)
elif S[1] == "0" and S[N] == "0":
  if K - 1 >= leng:
    ans = N
  else:
    for i in range(leng):
      if i + K < leng:
        newan = lis[i + K][1] - lis[i][0] + 1
        ans = max(ans, newan)
    ans = max(ans, lis[K - 1][1])
    ans = max(ans, N - lis[leng - K][0] + 1)
  print(ans)