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:
x = list(map(int,input().split()))
a = input()
a = list(a)
for t in range(len(a)):
    a[t] = int(a[t])
k = 1
loup = 0
rengi = 0
goukei = []
renzoku = []
if a[0] == 0:
    for i in range(x[0]-1):
        if a[i] == a[i+1]:
            k += 1
            if i == (x[0] - 2):
                renzoku.append(k)
        else :
            renzoku.append(k)
            k = 1
elif a[0] == 1:
    for j in range(x[0]-1):
        if a[j] == a[j+1]:
            k += 1
            if j == (x[0] - 2):
                renzoku.append(k)
        else :
            renzoku.append(k)
            k = 1
if int(len(renzoku)/2) <= x[1]:
    print(sum(renzoku))
else:
    rengi = 2*x[1]
    loup = len(renzoku) - rengi
    for k in range(loup-1):
        f = sum(renzoku[k:(k+rengi+a[k]+1)])
        goukei.append(f)
    print(max(goukei))

Submission status: Wrong Answer

Solution code:
N , K = map(int,input().split())
S = str(input())
T =[] #０と１の個数を並べていったリスト
m = 1
for i in range(N-1):
    if S[i] == S[i + 1]:
        m = m + 1
    else:
        T.append(m)
        m = 1
T.append(m)
n = len(T)

left_1 = []
left_0 = []
if S[0] == str(1):
    if K >= (len(T) // 2):
        ans = N
    else:
        if len(T) % 2 == 0:
            i = 0
            while 2*i + 2*K + 1 <= n:
                left_1.append(sum(T[2*i:2*i+2*K+2]))
            left_1.append(sum(T[n - 2*K :n]))
            ans = max(left_1)
        else:
            i = 0
            while 2*i + 2*K + 1 <= n:
                left_1.append(sum(T[2*i:2*i+2*K+2]))
            ans = max(left_1)

else: # if S[0] == 0:
    if n % 2 == 0: 
        if K >= (len(T) / 2):
            ans = N
        else:
            left_0.append(sum(T[0:2*K]))
            i = 0
            while 2*i + 2*K + 2 <= n:
                left_1.append(sum(T[2*i + 1:2*i + 2*K + 3]))
            ans = max(left_0)
    
    else:
        if K >= (len(T)//2 + 1):
            ans = N
        else:
            left_0.append(sum(T[0:2*K]))
            left_0.append(sum(T[n-2*K:n]))
            while 2*i + 2*K + 2 <= n:
                left_1.append(sum(T[2*i + 1:2*i + 2*K + 3]))
            ans = max(left_0)

print (ans)

Submission status: Wrong Answer

Solution code:
N , K = map(int,input().split())
S = str(input())
T =[] #０と１の個数を並べていったリスト
m = 1
for i in range(N-1):
    if S[i] == S[i + 1]:
        m = m + 1
    else:
        T.append(m)
        m = 1
T.append(m)
n = len(T)

left_1 = []
left_0 = []
if S[0] == str(1):
    if K >= (len(T) // 2):
        ans = N
    else:
        if len(T) % 2 == 0:
            i = 0
            while 2*i + 2*K + 1 <= n:
                left_1.append(sum(T[2*i:2*i+2*K+2]))
            left_1.append(sum(T[n - 2*K :n]))
            ans = max(left_1)
        else:
            i = 0
            while 2*i + 2*K + 1 <= n:
                left_1.append(sum(T[2*i:2*i+2*K+2]))
            ans = max(left_1)

else: # if S[0] == 0:
    if n % 2 == 0: 
        if K >= (len(T) / 2):
            ans = N
        else:
            left_0.append(sum(T[0:2*K]))
            i = 0
            while 2*i + 2*K + 2 <= n:
                left_1.append(sum(T[2*i + 1:2*i + 2*K + 3]))
            ans = max(left_0)
    
    else:
        if K >= (len(T)//2 + 1):
            ans = N
        else:
            left_0.append(sum(T[0:2*K]))
            left_0.append(sum(T[n-2*K:n]))
            while 2*i + 2*K + 2 <= n:
                left_1.append(sum(T[2*i + 1:2*i + 2*K + 3]))
            ans = max(left_0)

print (left_1)
print (ans)

Submission status: Wrong Answer

Solution code:
import re


n, k = map(int, input().split())
s = input()
s_re = re.split(r'(0+)', s)
srl = len(s_re)
m = [0] * srl
if (srl - 1 // 2) <= k:
    ans = [n]
else:
    for i, w in enumerate(s_re):
        m[i] = len(w)
    ans = [1]
    for i in range(0, srl, 2):
        ie = i + k * 2 + 1
        if ie <= srl:
            ans += [sum(m[i:ie])]
        else:
            break
print(max(ans))

Submission status: Wrong Answer

Solution code:
import re


n, k = map(int, input().split())
s = input()
s_re = re.split(r'(0+)', s)
srl = len(s_re)
m = [0] * srl
if (srl - 1 // 2) <= k:
    print(n)
for i, w in enumerate(s_re):
    m[i] = len(w)
ans = [1]
for i in range(0, srl, 2):
    ie = i + k * 2 + 1
    if ie <= srl:
        ans += [sum(m[i:ie])]
    else:
        break
print(max(ans))

Submission status: Wrong Answer

Solution code:
import re


n, k = map(int, input().split())
s = input()
s_re = re.split(r'(0+)', s)
srl = len(s_re)
m = [0] * srl
if (srl - 1 // 2) <= k:
    print(n)
for i, w in enumerate(s_re):
    m[i] = len(w)
ans = [1]
for i in range(srl):
    ie = i + k + 3
    if ie <= srl:
        ans += [sum(m[i:ie])]
    else:
        break
print(max(ans))

Submission status: Wrong Answer

Solution code:
'''input
5 1
00010
'''


N, K=map(int,input().split())
Sstr=input()
# print(Sstr)
S=[]

for i in range(N):
	S.append(int(Sstr[i]))

# print(S)
# flip=0
# for i in range(N-1):
# 	flip+=abs(S[i+1]-S[i])
# print(flip)


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

if cnt!=0:
	Nums.append(cnt)

if(len(Nums)%2==0):
	Nums.append(0)

Add=2*K+1
ans=0

left=0
right=0
tmp=0

for i in range(0, len(Nums), 2):
	# tmp=0
	Nextleft=i
	Nextright=min(i+Add, len(Nums))
	# for j in range(left, right):
	# 	tmp+=Nums[j]
	while(Nextleft>left):
		tmp-=Nums[left]
		left+=1
	while(Nextright>right):
		tmp+=Nums[right]
		right+=1
	
	ans=max(tmp,ans)

print(ans)

Submission status: Wrong Answer

Solution code:
'''input
5 1
00010
'''


N, K=map(int,input().split())
Sstr=input()
# print(Sstr)
S=[]

for i in range(N):
	S.append(int(Sstr[i]))

# print(S)
# flip=0
# for i in range(N-1):
# 	flip+=abs(S[i+1]-S[i])
# print(flip)


now=1
cnt=0
Nums=[]
for i in range(N):
	if S[i]==now:
		cnt+=1
	else:
		Nums.append(cnt)
		now+=1
		cnt=1

if cnt!=0:
	Nums.append(cnt)

if(len(Nums)%2==0):
	Nums.append(0)

Add=2*K+1
ans=0

left=0
right=0
tmp=0

for i in range(0, len(Nums), 2):
	# tmp=0
	Nextleft=i
	Nextright=min(i+Add, len(Nums))
	# for j in range(left, right):
	# 	tmp+=Nums[j]
	while(Nextleft>left):
		tmp-=Nums[left]
		left+=1
	while(Nextright>right):
		tmp+=Nums[right]
		right+=1
	
	ans=max(tmp,ans)

print(ans)

Submission status: Wrong Answer

Solution code:
'''input
5 1
00010
'''


N, K=map(int,input().split())
Sstr=input()
# print(Sstr)
S=[]

for i in range(N):
	S.append(int(Sstr[i]))

# print(S)
# flip=0
# for i in range(N-1):
# 	flip+=abs(S[i+1]-S[i])
# print(flip)


now=1
cnt=0
Nums=[]
for i in range(N):
	if S[i]==1:
		cnt+=1
	else:
		Nums.append(cnt)
		now+=1
		cnt=1

if cnt!=0:
	Nums.append(cnt)

if(len(Nums)%2==0):
	Nums.append(0)

Add=2*K+1
ans=0

left=0
right=0
tmp=0

for i in range(0, len(Nums), 2):
	# tmp=0
	Nextleft=i
	Nextright=min(i+Add, len(Nums))
	# for j in range(left, right):
	# 	tmp+=Nums[j]
	while(Nextleft>left):
		tmp-=Nums[left]
		left+=1
	while(Nextright>right):
		tmp+=Nums[right]
		right+=1
	
	ans=max(tmp,ans)

print(ans)

Submission status: Wrong Answer

Solution code:
'''input
5 1
00010
'''


N, K=map(int,input().split())
Sstr=input()
# print(Sstr)
S=[]

for i in range(N):
	S.append(int(Sstr[i]))

# print(S)
# flip=0
# for i in range(N-1):
# 	flip+=abs(S[i+1]-S[i])
# print(flip)


now=True
cnt=0
Nums=[]
for i in range(N):
	if S[i]:
		cnt+=1
	else:
		Nums.append(cnt)
		now+=1
		cnt=1

if cnt:
	Nums.append(cnt)

if(len(Nums)%2==0):
	Nums.append(0)

Add=2*K+1
ans=0

left=0
right=0
tmp=0

for i in range(0, len(Nums), 2):
	# tmp=0
	Nextleft=i
	Nextright=min(i+Add, len(Nums))
	# for j in range(left, right):
	# 	tmp+=Nums[j]
	while(Nextleft>left):
		tmp-=Nums[left]
		left+=1
	while(Nextright>right):
		tmp+=Nums[right]
		right+=1
	
	ans=max(tmp,ans)

print(ans)

Submission status: Wrong Answer

Solution code:
import math

n,k=list(map(int,input().split()))
s=input()
li=[0]
c=s[0]
for i in range(1,n):
  if c!=s[i]:
    li.append(i)
    c=s[i]

ans=0
if c[0]=="0":
  #0の塊の数
  count=math.ceil(len(li)/2)
  if count<=k:
    ans=len(s)
  else:
    ans=0
    for i in range(count-k+1):
      #i個目の0塊からi+k-1個目の0塊までをひっくり返したときの1の連続する数
      #li[2(i+k)] - li[2i-1]
      if 2*(i+k)<len(li):
        ans=max(ans,li[2*(i+k)] - li[max(0,2*i-1)])
      else:
        ans=max(ans,n-li[max(0,2*i-1)]-1)
else:
  count=math.floor(len(li)/2)
  if count<=k:
    ans=len(s)
  else:
    ans=0
    for i in range(count-k+1):
      #i個目の0塊からi+k-1個目の0塊までをひっくり返したときの1の連続する数
      #li[2(i+k)+2]-li[2i]
      if 2*(i+k+1)<len(li):
        ans=max(ans,li[2*(i+k)+2] - li[2*i])
      else:
        ans=max(ans,n - li[2*i]-1)
print(ans)

Submission status: Wrong Answer

Solution code:
import math

n,k=list(map(int,input().split()))
s=input()
li=[0]
c=s[0]
for i in range(1,n):
  if c!=s[i]:
    li.append(i)
    c=s[i]

ans=0
if c[0]=="0":
  #0の塊の数
  count=math.ceil(len(li)/2)
  if count<=k:
    ans=len(s)
  else:
    ans=0
    for i in range(count-k+1):
      #i個目の0塊からi+k-1個目の0塊までをひっくり返したときの1の連続する数
      #li[2(i+k)] - li[2i-1]
      #ただし、0,len(li)-1をはみ出るときは0,len(li)-1
      ans=max(ans,li[min(len(li)-1,2*(i+k))] - li[max(0,2*i-1)])
else:
  count=math.floor(len(li)/2)
  if count<=k:
    ans=len(s)
  else:
    ans=0
    for i in range(count-k+1):
      #i個目の0塊からi+k-1個目の0塊までをひっくり返したときの1の連続する数
      #li[2(i+k)+2]-li[2i]
      #ただし、0,len(li)-1をはみ出るときは0,len(li)-1
      ans=max(ans,li[min(len(li)-1,2*(i+k)+2)] - li[2*i])
print(ans)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
"""
D - Handstand
https://atcoder.jp/contests/abc124/tasks/abc124_d
WA
"""
import sys


from itertools import groupby
from collections import deque

def solve(N, K, S):
    seq = [[k, len(list(g))]  for k, g in groupby(S)]
    if len(seq) <= 2*K:
        return len(S)
    if seq[0][0] == '0':
        seq = [['1', 0]] + seq
    if len(seq) % 2 == 0:
        seq.append(['1', 0])

    seq = deque(seq)
    rem = deque()
    ans = 0
    for i in range(2*K+1):
        _, v = seq.popleft()
        ans += v
        rem.append(v)

    max_ans = ans
    while seq:
        try:
            ans -= rem.popleft()
            _, v = seq.popleft()
            ans += v
            ans -= rem.popleft()
            _, v = seq.popleft()
            ans += v
            max_ans = max(max_ans, ans)
        except:
            max_ans = max(max_ans, ans)
            break
    return max_ans


def main(args):
    N, K = map(int, input().split())
    S = input()
    ans = solve(N, K, S)
    print(ans)


if __name__ == '__main__':
    main(sys.argv[1:])

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
"""
D - Handstand
https://atcoder.jp/contests/abc124/tasks/abc124_d

"""
import sys


from itertools import groupby
from collections import deque

def solve(N, K, S):
    seq = [[k, len(list(g))]  for k, g in groupby(S)]
    if len(seq) <= 2*K:
        return len(S)
    if seq[0][0] == '0':
        seq = [['1', 0]] + seq
    if len(seq) % 2 == 0:
        seq.append(['1', 0])

    seq = deque(seq)
    rem = deque()
    ans = 0
    for i in range(2*K+1):
        _, v = seq.popleft()
        ans += v
        rem.append(v)

    max_ans = ans
    while seq:
        try:
            ans -= rem.popleft()
            _, v = seq.popleft()
            ans += v
            ans -= rem.popleft()
            _, v = seq.popleft()
            ans += v
            max_ans = max(max_ans, ans)
        except:
            break
    return max_ans


def main(args):
    N, K = map(int, input().split())
    S = input()
    ans = solve(N, K, S)
    print(ans)


if __name__ == '__main__':
    main(sys.argv[1:])

Submission status: Wrong Answer

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

l += [0,0] if (s[0] == '0') else [0]

for i in range(1, n):
    if s[i-1] != s[i]:
        l += [c+l[-1]]
        c = 1
    else: 
        c +=1
l += [c+l[-1]]

if s[-1] == '0':
    l += [l[-1]]

ll = len(l)
pl = 2 * k + 1

print(l)
print(ll, pl)

if ll <= pl:
    print(n)
else:
   m = 0
   for i in range(0, ll-pl+1, 2):
       t = l[pl+i]-l[i]
       m = t if (t > m) else m
   print(m)

Submission status: Wrong Answer

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

l += [0]
for i in range(1, n):
    if s[i-1] != s[i]:
        l += [c+l[-1]]
        c = 1
    else: 
        c +=1
l += [c+l[-1]]

if s[-1] == '0':
    l += [l[-1]+1]

ll = len(l)
pl = 2 * k + (s[0] != '0')

if ll <= pl:
    print(n)
else:
   m = 0
   for i in range(0, ll-pl, 2):
       t = l[pl+i]-l[i]
       m = t if (t > m) else m
   print(m)

Submission status: Wrong Answer

Solution code:
def solve():
    N, K = [int(_) for _ in raw_input().split()]
    S = raw_input()
    elms = [S[0]]
    cnts = []
    cnt = 1
    for i in range(1,N):
        if S[i] == elms[-1]:
            cnt += 1
        else:
            cnts.append(cnt)
            elms.append(S[i])
            cnt = 1
    cnts.append(cnt)
    num_elm = len(elms)
    max_cont1 = 0
    tmp = 0
    for i in range(num_elm):
        if i == 0:
            if elms[i] == '0':
                tmp = sum(cnts[i:(i+2*K)])
                max_cont1 = max(max_cont1, tmp)
            if elms[i] == '1':
                tmp = sum(cnts[i:(i+2*K+1)])
                max_cont1 = max(max_cont1, tmp)
        elif i == 1:
            if elms[i] == '1':
                tmp -= cnts[i-1]
                if i + 2*K - 1 < num_elm:
                    tmp += cnts[i + 2*K - 1]
                if i + 2*K < num_elm:
                    tmp += cnts[i + 2*K]
                max_cont1 = max(max_cont1, tmp)
        else:
            tmp -= cnts[i - 2]
            if i + 2*K < num_elm:
                tmp += cnts[i + 2*K]
            max_cont1 = max(max_cont1, tmp)
    print str(max_cont1)

if __name__ == '__main__':
    solve()

Submission status: Wrong Answer

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

h = (0, S[0])
t = (0, S[0])
k = 1 if S[0] == '0' else 0
max_handstands = 1
current_handstands = 1
while True:
    if h[0] == N-1 and t[0] == N-1:
        break
    # kを維持したままtを進める
    elif t[0] < N-1 and not (t[1] == '1' and S[t[0]+1] == '0'):
        t = (t[0]+1, S[t[0]+1])
        current_handstands += 1
    # kを増やしてtを進める
    elif t[0] < N-1 and k < K and S[t[0]+1] != t[1]:
        k += 1
        t = (t[0]+1, S[t[0]+1])
        current_handstands += 1
    # kを維持したままhを進める
    elif not (h[1] == '0' and S[h[0]+1] == '1') and h[0] < t[0]:
        max_handstands = max(max_handstands, current_handstands)
        h = (h[0]+1, S[h[0]+1])
        current_handstands -= 1
    # kを減らしてtを進める
    elif S[h[0]+1] != h[1] and h[0] < t[0]:
        k -= 1
        h = (h[0]+1, S[h[0]+1])
        current_handstands -= 1
    else:
        raise

print(max_handstands)

Submission status: Wrong Answer

Solution code:
N,K = map(int, input().split())
s = input()+"8"
F = ["1","0"]
d = {"0":[],"1":[]}
f = {"0":"1","1":"0"}
k = []
kk = [0]
S = s[0]
t = 1
for i in s[1:]:
    if S == i:t+=1
    else:
        k.append(t)
        d[S].append(t)
        t = 1
        S = f[S]
kk[0]=k[0]
for i ,n in enumerate(k[1:]):
    kk.append(kk[i]+n)
print(d)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
n,k = map(int, input().split())
s = input()

il = [0]

for i in range(1,n):
    if s[i]!=s[i-1]:
        il.append(i)

res = -1
for i in il:
    if s[i]=="0":
        ri = i+2*k
        li = i
    else:
        ri = i+2*k+1
        li = i

    if ri>=len(il):
        r = n
    else:
        r = il[ri]

    if li>=len(il):
        l = n
    else:
        l = il[li]

    res = max(res, r-l)

print(res)

Submission status: Wrong Answer

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

def f(n,k,s):
    acc = [0]*(n+1)
    for i in range(n):
        if i==0:
            if s[i]=="0":
                acc[i+1] = 1
        else:
            if s[i]=="0" and s[i-1]=="1":
                acc[i+1] = acc[i]+1
            else:
                acc[i+1] = acc[i]

    left = 0
    right = 0
    res = -1
    while left<n:
        while right<n and acc[right+1]-acc[left]<=k:
            right += 1

        res = max(res, right-left)

        if left==right:
            right += 1

        left += 1

    return res

if __name__ == '__main__':
    n,k = map(int, input().split())
    s = input()
    print(f(n,k,s))

Submission status: Wrong Answer

Solution code:
N,K = [ int(it) for it in input().split() ]
S = [ int(it) for it in input() ]

li = []

v = 1
c = 0
for it in S:
  if (v != it ):
    li.append(c)
    c=1
  else:
    c+=1
  v = it
li.append(c)
s = 0
for i in range(N):
  ds = sum( li[i:i+2*K+1] )
  s = max(s,ds)
print (s)

Submission status: Wrong Answer

Solution code:
N,K = [ int(it) for it in input().split() ]
S = [ int(it) for it in input() ]

li = []

v = 1
c = 0
for it in S:
  if (v != it ):
    li.append(c)
    c=1
  else:
    c+=1
  v = it
li.append(c)
print (li)
s = 0
for i in range(N):
  ds = sum( li[i:i+2*K+1] )
  s = max(s,ds)
print (s)

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
slist = [1 if s == "1" else -1 for s in input()]
last_ch = slist[0]

s_enc_list = []
seq = 0
for s in slist:
  if s == last_ch:
    seq += s
  else:
    s_enc_list.append(seq)
    seq = s
    last_ch = s
s_enc_list.append(seq)

k_seq = 0
if s_enc_list[0] > 0:
  k_seq = min(2*K+1,len(s_enc_list))
else:
  k_seq = min(2*K,len(s_enc_list))
max_sum = 0
for i in range(k_seq):
  max_sum += abs(s_enc_list[i])
  
sum = max_sum
for i in range(1,len(s_enc_list)-k_seq+1):
  sum += abs(s_enc_list[i+k_seq-1])
  sum -= abs(s_enc_list[i-1])
  if sum > max_sum:
    max_sum = sum
    
print(max_sum)

Submission status: Wrong Answer

Solution code:
def possible(m):
    l = 0
    r = 1
    zeroes = 0
    ans = N
    while r != N:
        if r - l >= m:
            l += 1
            if s[l] != s[l - 1] and s[l] == '1':
                zeroes -= 1
            ans = min(ans, zeroes)
        if s[r] != s[r - 1] and s[r] == '0':
            zeroes += 1  
        r += 1
    return ans <= K
            
N, K = map(int, input().split())
s = input()
l = 0
r = N + 1
while r - l > 1:
    m = (l + r) // 2
    if possible(m):
        l = m
    else:
        r = m
print(l)

Submission status: Wrong Answer

Solution code:

# coding: utf-8

# In[5]:

import sys
#input = sys.stdin.readline
N, K = map(int, input().split())
S = input()


# In[6]:

#print(N, K)
#print(len(S), S)


# In[21]:

tmp = S[0]
sums = [0]*(N+2)
counter = 1
gc = N
nb = 0
for i in range(1, N):
    if S[i] != tmp:
        sums[nb+1] = i
        nb += 1
        counter = 0
        tmp = S[i]
    else:
        gc -= 1
    counter += 1
    if i == N-1:
        sums[nb+1] = i+1

#print(N, gc)
sums = sums[:gc+2]
#print(S)
#print(sums)
#print(gc)
#print(gc-K*2)
pl = [0]*(gc+2)
if gc < K*2:
    print(N)
else:
    if S[0] == "0":
        for i in range(-1, gc-K*2, 2):
            j = 0 if i < 0 else i
            pl[i] = sums[i+1+K*2]-sums[j]
    else:
        if S[-1] == "0":
            for i in range(0, gc-K*2+1, 2):
                #print(i, i+1+K*2, gc)
                j=i
                l = gc if i+1+K*2>gc else i+1+K*2
                #print(j, l)
                pl[i] = sums[l]-sums[j]
        else:
            for i in range(0, gc-K*2, 2):
                j = i
                pl[i] = sums[i+1+K*2]-sums[j]
    #print(pl)
    print(max(pl))


# In[ ]:

Submission status: Wrong Answer

Solution code:

# coding: utf-8

# In[1]:

import sys
#input = sys.stdin.readline
N, K = map(int, input().split())
S = input()


# In[2]:

#print(N, K)
#print(len(S), S)


# In[4]:

tmp = S[0]
sums = [0]*(N+1)
counter = 1
gc = N
nb = 0
for i in range(1, N):
    if S[i] != tmp:
        sums[nb+1] = i
        nb += 1
        counter = 0
        tmp = S[i]
    else:
        gc -= 1
    counter += 1
    if i == N-1:
        sums[nb+1] = i+1

#print(N, gc)
sums = sums[:gc+1]
pl = [0]*gc
if gc < K*2:
    print(N)
else:
    if S[0] == "0":
        for i in range(-1, gc-K*2, 2):
            j = 0 if i < 0 else i
            pl[i] = sums[i+1+K*2]-sums[j]
    else:
        for i in range(0, gc-K*2, 2):
            j = i
            pl[i] = sums[i+1+K*2]-sums[j]
    print(max(pl))


# In[ ]:

Submission status: Wrong Answer

Solution code:

# coding: utf-8

# In[1]:

import sys
#input = sys.stdin.readline
N, K = map(int, input().split())
S = input()


# In[2]:

#print(N, K)
#print(len(S), S)


# In[4]:

tmp = S[0]
sums = [0]*(N+1)
counter = 1
gc = N
nb = 0
for i in range(1, N):
    if S[i] != tmp:
        sums[nb+1] = i
        nb += 1
        counter = 0
        tmp = S[i]
    else:
        gc -= 1
    counter += 1
    if i == N-1:
        sums[nb+1] = i+1

#print(N, gc)
sums = sums[:gc+1]
pl = [0]*gc
if gc <= K*2:
    print(N)
else:
    if S[0] == "0":
        for i in range(-1, gc-K*2, 2):
            j = 0 if i < 0 else i
            pl[i] = sums[i+1+K*2]-sums[j]
    else:
        for i in range(0, gc-K*2, 2):
            j = i
            pl[i] = sums[i+1+K*2]-sums[j]
    print(max(pl))


# In[ ]:

Submission status: Wrong Answer

Solution code:

# coding: utf-8

# In[1]:

import sys
#input = sys.stdin.readline
N, K = map(int, input().split())
S = input()


# In[2]:

#print(N, K)
#print(len(S), S)


# In[4]:

tmp = S[0]
sums = [0]*(N+1)
counter = 1
gc = N
nb = 0
for i in range(1, N):
    if S[i] != tmp:
        sums[nb+1] = i
        nb += 1
        counter = 0
        tmp = S[i]
    else:
        gc -= 1
    counter += 1
    if i == N-1:
        sums[nb+1] = i+1

#print(N, gc)
sums = sums[:gc+1]
pl = [0]*gc
if gc <= K*2:
    print(N)
    print(N)
else:
    if S[0] == "0":
        for i in range(-1, gc-K*2, 2):
            j = 0 if i < 0 else i
            pl[i] = sums[i+1+K*2]-sums[j]
    else:
        for i in range(0, gc-K*2, 2):
            j = i
            pl[i] = sums[i+1+K*2]-sums[j]
    print(max(pl))


# In[ ]:

Submission status: Wrong Answer

Solution code:

# coding: utf-8

# In[51]:

import sys
#input = sys.stdin.readline
N, K = map(int, input().split())
S = input()


# In[86]:

#print(N, K)
#print(S)


# In[90]:

tmp = S[0]
f = S[0]
#c_list = [0]*N
sums = [0]*(N+1)
counter = 1
gc = N
nb = 0
for i in range(1, N):
    #counter += 1
    #print(S[i], i)
    if S[i] != tmp:
        #c_list[nb] = counter
        sums[nb+1] = i
        nb += 1
        counter = 0
        tmp = S[i]
    else:
        gc -= 1
    counter += 1
    if i == N-1:
        #c_list[nb] = counter
        sums[nb+1] = i+1
    #print(S[i], i,counter, c_list)

#c_list = c_list[:gc]
#print(sums)
sums = sums[:gc+1]
#print(sums)
#print(c_list)
pl = [0]*gc
if gc <= K*2:
    #print("a")
    print(N)
else:
    #sums = [0]*(gc+1)
    #for i in range(gc):
    #    sums[i+1] = sum(c_list[0:i+1])
    #print(sums)
    #print("b")
    if f == "0":
        #print("c")
        for i in range(-1, gc-K*2, 2):
            #print(i, i+K*2+1)
            j = 0 if i < 0 else i
            #pl[i] = sum(c_list[j:i+K*2+1])
            pl[i] = sums[i+1+K*2]-sums[j]
    else:
        #print("d")
        for i in range(0, gc-K*2, 2):
            #print(i, i+K*2+1)
            #print(sums[i], sums[i+K*2+1])
            j = i
            #pl[i] = sum(c_list[j:i+K*2+1])
            pl[i] = sums[i+1+K*2]-sums[j]
    #print(pl)
    print(max(pl))


# In[ ]:

Submission status: Wrong Answer

Solution code:

# coding: utf-8

# In[51]:

import sys
#input = sys.stdin.readline
N, K = map(int, input().split())
S = input()


# In[75]:

#print(N, K)
#print(S)


# In[78]:

tmp = S[0]
f = S[0]
c_list = [0]*N
counter = 1
gc = N
nb = 0
for i in range(1, N):
    #counter += 1
    #print(S[i], i)
    if S[i] != tmp:
        c_list[nb] = counter
        nb += 1
        counter = 0
        tmp = S[i]
    else:
        gc -= 1
    counter += 1
    if i == N-1:
        c_list[nb] = counter
    #print(S[i], i,counter, c_list)

c_list = c_list[:gc]
#print(c_list)
pl = [0]*gc
if gc <= K*2:
    #print("a")
    print(N)
else:
    sums = [0]*(gc+1)
    for i in range(gc):
        sums[i+1] = sum(c_list[0:i+1])
    #print(sums)
    #print("b")
    if f == "0":
        #print("c")
        for i in range(-1, len(c_list)-K*2, 2):
            #print(i, i+K*2+1)
            j = 0 if i < 0 else i
            #pl[i] = sum(c_list[j:i+K*2+1])
            pl[i] = sums[i+1+K*2]-sums[j]
    else:
        #print("d")
        for i in range(0, len(c_list)-K*2, 2):
            #print(i, i+K*2+1)
            j = i
            #pl[i] = sum(c_list[j:i+K*2+1])
            pl[i] = sums[i+1+K*2]-sums[j]
    #print(pl)
    print(max(pl))


# In[ ]:

Submission status: Wrong Answer

Solution code:

# coding: utf-8

# In[24]:

import sys
#input = sys.stdin.readline
N, K = map(int, input().split())
S = input()


# In[26]:

tmp = S[0]
c_list = []
counter = 1
for i in range(1, len(S)):
    #counter += 1
    #print(S[i], i)
    if S[i] != tmp:
        c_list.append(counter)
        counter = 0
        tmp = S[i]
    counter += 1
    if i == len(S)-1:
        c_list.append(counter)
    #print(S[i], i,counter, c_list)

#print(c_list)
pl = [0]*len(c_list)
if len(c_list) <= K*2:
    print(len(S))
else:
    for i in range(len(c_list)-K*2):
        pl[i] = sum(c_list[i:i+K*2+1])
    #print(pl)
    print(max(pl))


# In[ ]:

Submission status: Wrong Answer

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

def findLongest(N, K, S):
    longest = 0
    currentL = 1
    for m in range(N):
        k = 0
        for n in range(m, N-1):
            if S[m]!= S[n] and S[n] != S[n+1]:
                k += 1
                if S[m] == '1' and k > K:
                    currentL = n-m+1
                    break
                elif S[m] == '0' and k == K:
                    currentL = n-m+1
                    break
        if currentL > longest:
            longest = currentL
    print(longest)
    
findLongest(N, K, S)

Submission status: Wrong Answer

Solution code:
n,k=map(int, input().split())
s=[int(i) for i in input()]
s_new=[]
f=0
i=0
cnt=0
while cnt<=len(s)-2:
    if s[cnt]!=0 and s[cnt+1]!=0:
        f+=1
        cnt+=1
    elif s[cnt]!=0 and s[cnt+1]==0:
        f+=1
        s_new.append(f)
        f=0
        cnt+=1
    elif s[cnt]==0 and s[cnt+1]==0:
        f-=1
        cnt+=1
    elif s[cnt]==0 and s[cnt+1]!=0:
        f-=1
        s_new.append(f)
        f=0
        cnt+=1
if len(s)==1 and s[0]==1:
    s_new.append(1)
elif len(s)==1 and s[0]==0:
    s_new.append(-1)
elif f<0 and s[-1]==0:
    s_new.append(f-1)
elif f>0 and s[-1]==1:
    s_new.append(f+1)
elif s_new[-1]<0 and s[-1]==1:
    s_new.append(1)
elif s_new[-1]>0 and s[-1]==0:
    s_new.append(-1)
s_new.insert(0,0)
s_new.append(0)
def make_newlist(l):
    max_sum=0
    for i in range(len(l)-2):
        if l[i]>=0:
            temp_sum=l[i]+abs(l[i+1])+l[i+2]
            if temp_sum>max_sum:
                max_sum=temp_sum
                max_idx=i
    l.insert(max_idx, max_sum)
    del l[max_idx+1:max_idx+4]
    if l[0]!=0:
        l.insert(0, 0)
    if l[-1]!=0:
        l.append(0)
    return l
  
for i in range(k):
    s_new=make_newlist(s_new)
print(max(s_new))

Submission status: Wrong Answer

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

num_list = []
old = S[0]
count = 0

for i in range(1, N + 1):
    
    if(S[i] == old):
        count += 1
    else:
        num_list.append(count + 1)
        count = 0
        old = S[i]

if(S[0] == '0'):
    flag = 0
else:
    flag = 1

max = 0
for i in range(len(num_list)):

    sum = 0

    for j in range(2 * K + flag):
        if(i + j == len(num_list)):
            break
        sum += num_list[i + j]

    if(max < sum):
        max = sum

    i += 1

#print(num_list)
print(max)

Submission status: Wrong Answer

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

a = []
cnt = 0
for i in range(n-1):
    cnt += 1
    if s[i] != s[i+1]:
        a.append(cnt)
        cnt = 0 # reset
a.append(cnt+1)
# print(a) # 連続する0,1の数の配列を作った
### 前処理 両端が1の数を表す数列を作ることが出来た
if s[0] == "0": # 左端
    a.insert(0, 0)
if s[-1] == "0": # 右端
    a.append(0)
print(a)
# print((len(a)-2*k+1)//2)
if (len(a)-2*k+1)/2 < 1:
    k = int((len(a)-1)/2)
# print(k)
ans = 0
for i in range((len(a)-2*k+1)//2):
    # b = 0
    b = sum(a[2*i:2*i+2*k+1])
    #for j in range(2*k+1):
    #    b += a[2*i+j]
    # print(i, b)
    ans = max(ans, b) # 最大値を更新していく
print(ans)

Submission status: Wrong Answer

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

a = []
cnt = 0
for i in range(n-1):
    cnt += 1
    if s[i] != s[i+1]:
        a.append(cnt)
        cnt = 0 # reset
a.append(cnt+1)
# print(a) # 連続する0,1の数の配列を作った
### 前処理 両端が1の数を表す数列を作ることが出来た
if s[0] == "0": # 左端
    a.insert(0, 0)
if s[-1] == "0": # 右端
    a.append(0)
# print(a)
# print((len(a)-2*k+1)//2)
if (len(a)-2*k+1)/2 < 1:
    k = int((len(a)-1)/2)
# print(k)
ans = 0
for i in range((len(a)-2*k+1)//2):
    # b = 0
    b = sum(a[2*i:2*i+2*k])
    #for j in range(2*k+1):
    #    b += a[2*i+j]
    # print(i, b)
    ans = max(ans, b) # 最大値を更新していく
print(ans)

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
S=input()
First=S[0]
Now=First
cnt=0
A=[0]
for s in S:
    if Now=='1' and s=='0':
        A+=[cnt]
        Now='0'
    elif Now=='0' and s=='1':
        A+=[cnt]
        Now='1'
    cnt+=1
A+=[N]
dp=2*K+1
ans=[]

if First=="0":
    p=1
    ans.append(A[min(dp-1,len(A)-1)])
else:
    p=0
    ans.append(A[min(dp,len(A)-1)])

while p+dp<len(A):
	ans.append(A[p+dp]-A[p])
	p+=2
ans.append(N-A[p])
#print(A)
print(ans)

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
S=input()
First=S[0]
Now=First
cnt=0
A=[0]
for s in S:
    if Now=='1' and s=='0':
        A+=[cnt]
        Now='0'
    elif Now=='0' and s=='1':
        A+=[cnt]
        Now='1'
    cnt+=1
A+=[N]
dp=2*K+1
ans=[]

if First=="0":
    p=1
    ans.append(A[min(dp-1,len(A)-1)])
else:
    p=0
    ans.append(A[min(dp,len(A)-1)])

while p+dp<len(A):
	ans.append(A[p+dp]-A[p])
	p+=2

#print(A)
print(max(ans))