Problem:


Score : 400 points

Problem Statement

N people are arranged in a row from left to right.

You are given a string S of length N consisting of 0 and 1, and a positive integer K.

The i-th person from the left is standing on feet if the i-th character of S is 0, and standing on hands if that character is 1.

You will give the following direction at most K times (possibly zero):

Direction: Choose integers l and r satisfying 1 \leq l \leq r \leq N, and flip the l-th, (l+1)-th, ..., and r-th persons. That is, for each i = l, l+1, ..., r, the i-th person from the left now stands on hands if he/she was standing on feet, and stands on feet if he/she was standing on hands.

Find the maximum possible number of consecutive people standing on hands after at most K directions.

Constraints

N is an integer satisfying 1 \leq N \leq 10^5.

K is an integer satisfying 1 \leq K \leq 10^5.

The length of the string S is N.

Each character of the string S is 0 or 1.

Input

Input is given from Standard Input in the following format:

N K
S

Output

Print the maximum possible number of consecutive people standing on hands after at most K directions.

Sample Input 1

5 1
00010

Sample Output 1

4

We can have four consecutive people standing on hands, which is the maximum result, by giving the following direction:

Give the direction with l = 1, r = 3, which flips the first, second and third persons from the left.

Sample Input 2

14 2
11101010110011

Sample Output 2

8

Sample Input 3

1 1
1

Sample Output 3

1

No directions are necessary.



Sample input:
5 1
00010


Sample output:
4

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
s = input()
l=list()
s = s.replace("10","1,0")
s = s.replace("01","0,1")

l = [len(i) for i in s.split(",")]
r = 0
ans = 0

if k%2 == 0:
    r = int(s[0] == 1)
else:
    r = int(s[0] == 0)

t = l[r]
ans = t
for j in range(1, k+1):
    if r-j-2 >= 0:
        t -= l[r-j-2]
    if r+j < len(l):
        t += l[r+j]

for i in range(r+2, len(l) ,2):
    for j in range(1, 3):
        if i-j-2 >= 0:
            t -= l[i-j-2]
        if i+j < len(l):
            t += l[i+j]
    ans = max(ans, t)
print(ans)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
s = input()
l=list()
s = s.replace("10","1,0")
s = s.replace("01","0,1")

l = [len(i) for i in s.split(",")]
r = 0
ans = 0

if k%2 == 0:
    r = int(s[0] == 1)
else:
    r = int(s[0] == 0)

t = l[r]
for j in range(1, k+1):
    if r-j-2 >= 0:
        t -= l[r-j-2]
    if r+j < len(l):
        t += l[r+j]

for i in range(r+2, len(l) ,2):
    for j in range(1, 3):
        if i-j-2 >= 0:
            t -= l[i-j-2]
        if i+j < len(l):
            t += l[i+j]
    ans = max(ans, t)
print(ans)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
s = input()
l=list()
s = s.replace("10","1,0")
s = s.replace("01","0,1")

l = [len(i) for i in s.split(",")]
r = 0
ans = 0

if k%2 == 0:
    r = int(s[0] == 1)
else:
    r = int(s[0] == 0)
t = l[r]
for i in range(r, len(l) ,2):
    for j in range(1, k+1):
        if i-j-2 >= 0:
            t -= l[i-j-2]
        if i+j < len(l):
            t += l[i+j]
    ans = max(ans, t)
print(ans)

Submission status: Wrong Answer

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

s = s[0] + s + str((int(s[-1])+1)%2)
b = []
l = 1
for i in range(1, n+2):
    if s[i-1] == s[i]:
        continue
    b.append(i-l)
    l = i
if s[0] == '0':
    b = b + [0]
if s[-2] == '0':
    b = [0] + b

c = [0]
for i in range(len(b)):
    c.append(c[-1]+b[i])

lb = len(b)
ans = 0
for left in range(0, lb, 2):
    right = min(left + 2*k+1, lb)
    ans = max(ans, c[right]-c[left])
print(ans)

Submission status: Wrong Answer

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

s = s[0] + s + str((int(s[-1])+1)%2)
b = []
l = 1
for i in range(1, n+2):
    if s[i-1] == s[i]:
        continue
    b.append(i-l)
    l = i
if s[0] == '0':
    b = b + [0]
if s[-2] == '0':
    b = [0] + b

c = [0]
for i in range(len(b)):
    c.append(c[-1]+b[i])

lc = len(c)
ans = 0
for left in range(0, lc, 2):
    right = left + 2*k+1
    if right >= lc:
        right = lc - 1
    ans = max(ans, c[right]-c[left])
print(ans)

Submission status: Wrong Answer

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

s = s[0] + s + str((int(s[-1])+1)%2)
b = []
l = 1
for i in range(1, n+2):
    if s[i-1] == s[i]:
        continue
    b.append(i-l)
    l = i
if s[0] == '0':
    b = b + [0]
if s[-2] == '0':
    b = [0] + b

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

Submission status: Wrong Answer

Solution code:
import sys


# \n
def input():
    return sys.stdin.readline().rstrip()


def main():
    N, K = map(int, input().split())
    S = list(input())

    l = []
    down = 0  # "0"
    up = 0  # "1"
    flag = int(S[0])
    if not flag:
        l.append(0)
    for i in range(N):

        if int(S[i]) == 1:
            up += 1
            if down:
                l.append(down)
                down = 0
        if int(S[i]) == 0:
            down += 1
            if up:
                l.append(up)
                up = 0
    if up:
        l.append(up)
    else:
        l.append(down)

    if len(l)<=2*K+1:
        print(N)
        exit()
    total = sum(l[:2*K+1])
    ans =total
    flag =1
    for i in range(1,len(l) - 2 * K +1):
        total =total - l[i-1] +l[min(i+2*K,len(l)-1)]

        if flag ==0:
            ans =max(total,ans)
        flag =(flag +1)%2
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import sys


# \n
def input():
    return sys.stdin.readline().rstrip()


def main():
    N, K = map(int, input().split())
    S = list(input())

    l = []
    down = 0  # "0"
    up = 0  # "1"
    flag = int(S[0])
    if not flag:
        l.append(0)
    for i in range(N):

        if int(S[i]) == 1:
            up += 1
            if down:
                l.append(down)
                down = 0
        if int(S[i]) == 0:
            down += 1
            if up:
                l.append(up)
                up = 0
    if up:
        l.append(up)
    else:
        l.append(down)

    if len(l)<=2*K+1:
        print(N)
        exit()
    total = sum(l[:2*K+1])
    ans =total
    flag =1
    for i in range(1,len(l) - 2 * K ):
        total =total - l[i-1] +l[i+2*K]

        if flag ==0:
            ans =max(total,ans)
        flag =(flag +1)%2
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import sys


# \n
def input():
    return sys.stdin.readline().rstrip()


def main():
    N, K = map(int, input().split())
    S = list(input())

    l = []
    down = 0  # "0"
    up = 0  # "1"
    flag = int(S[0])
    if not flag:
        l.append(0)
    for i in range(N):

        if int(S[i]) == 1:
            up += 1
            if down:
                l.append(down)
                down = 0
        if int(S[i]) == 0:
            down += 1
            if up:
                l.append(up)
                up = 0
    if up:
        l.append(up)
    else:
        l.append(down)

    if len(l)<2*K+1:
        print(N)
        exit()
    total = sum(l[:2*K+1])
    ans =total
    flag =1
    for i in range(1,len(l) - 2 * K ):
        total =total - l[i-1] +l[i+2*K]

        if flag ==0:
            ans =max(total,ans)
        flag =(flag +1)%2
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

# make seq_cnt_list
seq_cnt_list = []
seq_cnt = 0
last_man = ""
for i in range(N):
    cur_man = S[i]
    if cur_man != last_man:
        #print("S[%d]=%s is change" % (i, S[i]))
        if seq_cnt > 0:
            if last_man == "0":
                seq_cnt *= -1
            seq_cnt_list.append(seq_cnt)
        seq_cnt = 1
    else:
        #print("S[%d]=%s is same" % (i, S[i]))
        seq_cnt += 1
    last_man = cur_man
if seq_cnt > 0:
    if last_man == "0":
        seq_cnt *= -1
    seq_cnt_list.append(seq_cnt)

#print(seq_cnt_list)
'''
for k in range(K):
    seq_cnt_list_size = len(seq_cnt_list)
    if (seq_cnt_list_size < 2) or (seq_cnt_list_size == 3 and seq_cnt_list[0] > 0):
        seq_cnt_list = [sum(map(abs, seq_cnt_list))]
        break
    else:
        max_pos = 0
        max_val = 0
        for i in range(seq_cnt_list_size):
            if seq_cnt_list[i] < 0:
                val = seq_cnt_list[i] * -1
                if i > 0:
                    val += seq_cnt_list[i - 1]
                if i < seq_cnt_list_size - 1:
                    val += seq_cnt_list[i + 1]
                if val > max_val:
                    max_val = val
                    max_pos = i
        start_pos = max_pos - 1 if max_pos > 0 else max_pos
        end_pos = max_pos + 1 if max_pos < seq_cnt_list_size - 1 else max_pos
        seq_cnt_list[start_pos: end_pos + 1] = [max_val]
'''
print(max(seq_cnt_list))

Submission status: Wrong Answer

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

# make seq_cnt_list
seq_cnt_list = []
seq_cnt = 0
last_man = ""
for i in range(N):
    cur_man = S[i]
    if cur_man != last_man:
        #print("S[%d]=%s is change" % (i, S[i]))
        if seq_cnt > 0:
            if last_man == "0":
                seq_cnt *= -1
            seq_cnt_list.append(seq_cnt)
        seq_cnt = 1
    else:
        #print("S[%d]=%s is same" % (i, S[i]))
        seq_cnt += 1
    last_man = cur_man
if seq_cnt > 0:
    if last_man == "0":
        seq_cnt *= -1
    seq_cnt_list.append(seq_cnt)

#print(seq_cnt_list)

for k in range(K):
    seq_cnt_list_size = len(seq_cnt_list)
    if (seq_cnt_list_size < 2) or (seq_cnt_list_size == 3 and seq_cnt_list[0] > 0):
        seq_cnt_list = [sum(map(abs, seq_cnt_list))]
        break
    else:
        max_pos = 0
        max_val = 0
        for i in range(seq_cnt_list_size):
            if seq_cnt_list[i] < 0:
                val = seq_cnt_list[i] * -1
                if i > 0:
                    val += seq_cnt_list[i - 1]
                if i < seq_cnt_list_size - 1:
                    val += seq_cnt_list[i + 1]
                if val > max_val:
                    max_val = val
                    max_pos = i
        start_pos = max_pos - 1 if max_pos > 0 else max_pos
        end_pos = max_pos + 1 if max_pos < seq_cnt_list_size - 1 else max_pos
        seq_cnt_list[start_pos: end_pos + 1] = [max_val]

print(max(seq_cnt_list))

Submission status: Wrong Answer

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

print(N)
print(K)

# make seq_cnt_list
seq_cnt_list = []
seq_cnt = 0
last_man = ""
for i in range(N):
    cur_man = S[i]
    if cur_man != last_man:
        #print("S[%d]=%s is change" % (i, S[i]))
        if seq_cnt > 0:
            if last_man == "0":
                seq_cnt *= -1
            seq_cnt_list.append(seq_cnt)
        seq_cnt = 1
    else:
        #print("S[%d]=%s is same" % (i, S[i]))
        seq_cnt += 1
    last_man = cur_man
if seq_cnt > 0:
    if last_man == "0":
        seq_cnt *= -1
    seq_cnt_list.append(seq_cnt)

print(seq_cnt_list)

for k in range(K):
    seq_cnt_list_size = len(seq_cnt_list)
    if (seq_cnt_list_size < 2) or (seq_cnt_list_size == 3 and seq_cnt_list[0] > 0):
        seq_cnt_list = [sum(map(abs, seq_cnt_list))]
        break
    else:
        max_pos = 0
        max_val = 0
        for i in range(seq_cnt_list_size):
            if seq_cnt_list[i] < 0:
                val = seq_cnt_list[i] * -1
                if i > 0:
                    val += seq_cnt_list[i - 1]
                if i < seq_cnt_list_size - 1:
                    val += seq_cnt_list[i + 1]
                if val > max_val:
                    max_val = val
                    max_pos = i
        start_pos = max_pos - 1 if max_pos > 0 else max_pos
        end_pos = max_pos + 1 if max_pos < seq_cnt_list_size - 1 else max_pos
        seq_cnt_list[start_pos: end_pos + 1] = [max_val]

print(max(seq_cnt_list))

Submission status: Wrong Answer

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

l = []

a = ""
t = 0
for i in range(N):
    if a != S[i]:
        l.append(t)
        a = S[i]
        t = 1
    else:
        t += 1
l = l + [t, 0]

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

Submission status: Wrong Answer

Solution code:
def main():
	N, K = [int(x) for x in input().split(" ")]
	S = list(input())
	cnt = [0] # idx = 0, 2, 4 ... -> handstand
	a = 0
	for i in range(len(S)):
		if S[i] == "0":
			if a % 2 == 0:
				a += 1
				cnt.append(1)
			else:
				cnt[-1] += 1
		elif S[i] == "1":
			if a % 2 == 0:
				cnt[-1] += 1
			else:
				a += 1
				cnt.append(1)
	if S[-1] == "0":
		cnt.append(0)

	if K >= (len(cnt) - 1) / 2:
		print(N)
		return 0

	T = [sum(cnt[:(2 * K + 1)])]
	for i in range(0, len(cnt) - 2 * K - 2, 2):
		T.append(T[-1] - cnt[i] - cnt[i + 1] + cnt[i + 2 * K + 1] + cnt[i + 2 * K + 2])
	print(T)

main()

Submission status: Wrong Answer

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

if "0" not in S:
    print(n)
    exit()

# 1の個数、0の個数、1の個数、、、
cnt = []

# 先頭が0なら1が0個とみなす
if S[0] == "0":
    cnt.append(0)

# 個数勘定
i = 0
while i < n:
    j = i
    while j < n and S[i] == S[j]:
        j += 1
    cnt.append(j-i)
    i = j

# 最後尾が0なら1が0個とみなす
if S[-1] == "0":
    cnt.append(0)

# cnt の累積和
cumCnt = [0]
for c in cnt:
    cumCnt.append(cumCnt[-1] + c)
# cumCnt.pop(0)
# print(cnt)
# print(cumCnt)

lenCnt = len(cnt)
lenCumCnt = len(cumCnt)
ans = 0

for left in range(0, lenCumCnt, 2):
    if lenCumCnt <= left + 2*k+1:
        break
    ans = max(ans, cumCnt[left + 2*k+1] - cumCnt[left])
print(ans)

Submission status: Wrong Answer

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

# 1の個数、0の個数、1の個数、、、
cnt = []

# 先頭が0なら1が0個とみなす
if S[0] == "0":
    cnt.append(0)

# 個数勘定
i = 0
while i < n:
    j = i
    while j < n and S[i] == S[j]:
        j += 1
    cnt.append(j-i)
    i = j

# 最後尾が0なら1が0個とみなす
if S[-1] == "0":
    cnt.append(0)

# cnt の累積和
cumCnt = [0]
for c in cnt:
    cumCnt.append(cumCnt[-1] + c)
cumCnt.pop(0)
# print(cnt)
# print(cumCnt)

lenCnt = len(cnt)
ans = 0

for i in range(lenCnt - (2*k+1 - 1)):
    ans = max(ans, cumCnt[i+(2*k+1 - 1)] - cumCnt[i])
print(ans)

Submission status: Wrong Answer

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

# 1の個数、0の個数、1の個数、、、
cnt = []

# 先頭が0なら1が0個とみなす
if S[0] == "0":
    cnt.append(0)

# 個数勘定
i = 0
while i < n:
    j = i
    while j < n and S[i] == S[j]:
        j += 1
    cnt.append(j-i)
    i = j

# 最後尾が0なら1が0個とみなす
if S[-1] == "0":
    cnt.append(0)

# cnt の累積和
cumCnt = [0]
for c in cnt:
    cumCnt.append(cumCnt[-1] + c)
cumCnt.pop(0)
print(cnt)
print(cumCnt)

lenCnt = len(cnt)
ans = 0

for i in range(lenCnt - (2*k+1 - 1)):
    ans = max(ans, cumCnt[i+(2*k+1 - 1)] - cumCnt[i])
print(ans)

Submission status: Wrong Answer

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

i=0
if s[0]=='1':
    lst=[]
else:
    lst=[0]

while i<n:
    j=i
    cnt=0
    while j<n and s[i]==s[j]:
        cnt+=1
        j+=1
    
    lst.append(cnt)
    i=j

if 2*k+1>=len(lst):
    print(n)
    exit(0)

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

print(ans)

Submission status: Wrong Answer

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

a = []

i = 0
while i < n:
    j = 1
    while i + j < n and s[i] == s[i + j]:
        j += 1
    a.append((s[i], j))
    i += j
if a[0][0] == '0':
    a = [('1', 0)] + a
m = len(a)

if m == 1:
    print(a[0][1])
    exit()
if m // 2 <= k:
    print(n)
    exit()

s = [0] * -~m
for i in range(m):
    s[i + 1] = s[i] + a[i][1]
ans = 0
for i in range(1, m, 2):
    if i + k * 2 > m:
        break
    ans = max(ans, s[i + k * 2] - s[i - 1])
print(ans)

Submission status: Wrong Answer

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

a = []

i = 0
while i < n:
    j = 1
    while i + j < n and s[i] == s[i + j]:
        j += 1
    a.append((s[i], j))
    i += j
if a[0][0] == '0':
    a = [('1', 0)] + a
m = len(a)

if m == 1:
    print(a[0][1])
    exit()

s = [0] * -~m
for i in range(m):
    s[i + 1] = s[i] + a[i][1]
ans = 0
for i in range(1, m, 2):
    if i + k * 2 > m:
        break
    ans = max(ans, s[i + k * 2] - s[i - 1])
print(ans)

Submission status: Wrong Answer

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

if s[0] == "0":
  zero=True
  iti=False
else:
  iti=True
  zero=False

count=0
for i in range(0,len(s)):
  if zero and s[i]=="0":
    count+=1
  elif zero and s[i]=="1":
    d.append(count)
    iti=True
    zero=False
    count=1
  elif iti and s[i]=="0":
    d.append(count)
    zero=True
    iti=False
    count=1
  elif iti and s[i]=="1":
    count+=1
d.append(count)

import itertools
acum = list(itertools.accumulate([0]+d))
hantei=0
ans=0
if s[0]=="0":
  ks1=min(k*2,len(d))
  ks2=min(k*2+1,len(d))
else:
  ks1=min(k*2+1,len(d))
  ks2=min(k*2,len(d))
  
for i in range(0,len(d)-ks1+1,2):
  ans=max(acum[i+(ks1)]-acum[i],ans)
for i in range(1,len(d)-ks2+1,2):
  ans=max(acum[i+(ks2)]-acum[i],ans)
print(ans)

Submission status: Wrong Answer

Solution code:
import sys

input = sys.stdin.readline
N, K = map(int, input().split())
S = list(input().strip())

comp = []
pre = S[0]
tmp = 0
for s_i in S:
    if s_i != pre:
        comp.append(tmp)
        tmp = 1
    else:
        tmp += 1
    
    pre = s_i
comp.append(tmp)

ans = 1
if K == 1:
    w = 2
else:
    w = 2*K + 1
ans = 0
tmp = 0
c = 0
for i, value in enumerate(comp):
    tmp += value
    c += 1
    if w < c:
        tmp -= comp[i-w]
    ans = max(ans, tmp)
print(ans)

Submission status: Wrong Answer

Solution code:
import sys

input = sys.stdin.readline
N, K = map(int, input().split())
S = list(input().strip())

acc = [0]
pre = S[0]
tmp = 0
for s_i in S:
    if s_i != pre:
        acc.append(tmp)
        tmp = 1
    else:
        tmp += 1
    
    pre = s_i
acc.append(tmp)
# print(acc)
for i in range(len(acc)-1):
    acc[i+1] += acc[i]
# print(acc)

ans = 1
if K == 1:
    w = 2
else:
    w = 2*K + 1
for i in range(w, len(acc)):
    ans = max(ans, acc[i] - acc[i-w])
print(ans)

Submission status: Wrong Answer

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

i=0
if s[0]=='1':
    lst=[]
else:
    lst=[0]

while i<n:
    j=i
    cnt=0
    while j<n and s[i]==s[j]:
        cnt+=1
        j+=1
    
    lst.append(cnt)
    i=j

if 2*k+1>len(lst):
    print(n)
    exit(0)

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

print(ans)

Submission status: Wrong Answer

Solution code:
from itertools import accumulate


def run_length_compress(S):
    res = [[S[0], 1]]
    for c in S[1:]:
        if c == res[-1][0]:
            res[-1][1] += 1
        else:
            res.append([c, 1])
    return res


N, K = map(int, input().split())
S = input()
if S == "1" * N:
    print(N)
    exit()
comp_S = run_length_compress(S)
if S[0] == "0":
    comp_S = [["1", 0]] + comp_S
if S[-1] == "0":
    comp_S += [["1", 0]]
acc = list(accumulate([c[1] for c in comp_S], initial=0))
ans = 0
for i in range(2 * K + 1, len(acc), 2):
    ans = max(ans, acc[i] - acc[i - 2 * K - 1])
print(ans)

Submission status: Wrong Answer

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

l = []
ans = 0
q = "1"
n = 0

for i in range(len(S)):
    if q != S[i]:
        l.append(n)
        n = 1
    else:
        n += 1
    q = S[i]
l.append(n)
if S[-1] == "0":
    l.append(0)

maxlen = K * 2 + 1
print(l)

if maxlen >= len(l):
    print(N)
else:
    su = 0
    for i in range(maxlen):
        su += l[i]
    ans = su
    for i in range(0,len(l) - maxlen,2):
        su -= l[i] + l[i + 1]
        su += l[maxlen + i] + l[maxlen + i + 1]
        ans = max(ans,su)
    print(ans)

Submission status: Wrong Answer

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

C = [0]
tmp = 0
car = 0
for i in range(N):
  if S[i] != car:
    C.append(tmp)
    tmp = 1
    car = S[i]
  else:
    tmp += 1
C.append(tmp)
if car == 0:
  C.append(0)

#print(C)
if len(C) <= 2*K+1:
  print(N)
else:
  tmp = sum(C[0:2*K+1])
  ans = sum(C[0:2*K+1])
  for i in range(len(C)-(2*K+1),2):
    tmp -= C[i]
    tmp += C[i + 2*K+1]
    ans = max(ans, tmp)
  print(ans)

Submission status: Wrong Answer

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

s1 = 0
s0 = 0

if N < K:
    print(N)
    exit(0)
# Create 01 array
for c in S:
    if c == '1':
        s1 += 1
        if not s0 == 0:
            ts.append((0, s0))
        s0 = 0
    else:
        s0 += 1
        if not s1 == 0:
            ts.append((1,s1))
        s1 = 0

if s1 != 0:
    ts.append((1,s1))
if s0 != 0:
    ts.append((0,s0))

# print("TS:", ts)

# top/bottom
top0 = False
bottom0 = False

if ts[0][0] == 0:
    top0 = True

if ts[-1][0] == 0:
    bottom0 = True

# print(top0, bottom0)
## pre calc
if len(ts) >= 2:
    ans = 1
else:
    ans = ts[0][0]

sp = 0
ep = len(ts)

if top0:
    ans = sum(x[1] for x in ts[:K*2])
    # print("t0:", ans)
    sp = 1

if bottom0:
    ans = max(ans, sum(x[1] for x in ts[-K*2:]))
    # print("b0:", ans)
    ep = len(ts)-1

# Calc
# print(sp, ep)
if ep - sp > 1:
    W = 2 * K + 1
    temp = sum([x[1] for x in ts[sp:sp+W]])
    # print("temp:", temp)
    ans = max(temp, ans)

    for ix in range(sp+2, ep-W+1, 2):
        temp = temp - ts[ix-1][1] - ts[ix-2][1] + ts[ix+W-1][1] + ts[ix+W-2][1]
        # print(ix, temp)
        ans = max(temp, ans)

print(ans)

Submission status: Wrong Answer

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

s1 = 0
s0 = 0

# Create 01 array
for c in S:
    if c == '1':
        s1 += 1
        if not s0 == 0:
            ts.append((0, s0))
        s0 = 0
    else:
        s0 += 1
        if not s1 == 0:
            ts.append((1,s1))
        s1 = 0

if s1 != 0:
    ts.append((1,s1))
if s0 != 0:
    ts.append((0,s0))

# print("TS:", ts)

# top/bottom
top0 = False
bottom0 = False

if ts[0][0] == 0:
    top0 = True

if ts[-1][0] == 0:
    bottom0 = True

# print(top0, bottom0)
## pre calc
if len(ts) >= 2:
    ans = 1
else:
    ans = ts[0][0]

sp = 0
ep = len(ts)

if top0:
    ans = sum(x[1] for x in ts[:K*2])
    # print("t0:", ans)
    sp = 1

if bottom0:
    ans = max(ans, sum(x[1] for x in ts[-K*2:]))
    # print("b0:", ans)
    ep = len(ts)-1

# Calc
# print(sp, ep)
if ep - sp > 1:
    W = 2 * K + 1
    temp = sum([x[1] for x in ts[sp:sp+W]])
    # print("temp:", temp)
    ans = max(temp, ans)

    for ix in range(sp+2, ep-W+1, 2):
        temp = temp - ts[ix-1][1] - ts[ix-2][1] + ts[ix+W-1][1] + ts[ix+W-2][1]
        # print(ix, temp)
        ans = max(temp, ans)

print(ans)

Submission status: Wrong Answer

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

s1 = 0
s0 = 0

# Create 01 array
for c in S:
    if c == '1':
        s1 += 1
        if not s0 == 0:
            ts.append((0, s0))
        s0 = 0
    else:
        s0 += 1
        if not s1 == 0:
            ts.append((1,s1))
        s1 = 0

if s1 != 0:
    ts.append((1,s1))
if s0 != 0:
    ts.append((0,s0))

# print("TS:", ts)

# top/bottom
top0 = False
bottom0 = False

if ts[0][0] == 0:
    top0 = True

if ts[-1][0] == 0:
    bottom0 = True

# print(top0, bottom0)
## pre calc
if len(ts) >= 2:
    ans = 1
else:
    ans = ts[0][0]

sp = 0
ep = len(ts)

if top0:
    ans = sum(x[1] for x in ts[:K*2])
    # print("t0:", ans)
    sp = 1

if bottom0:
    ans = max(ans, sum(x[1] for x in ts[-K*2:]))
    # print("b0:", ans)
    ep = len(ts)-1

# Calc
print(sp, ep)
if ep - sp > 1:
    W = 2 * K + 1
    temp = sum([x[1] for x in ts[sp:sp+W]])
    # print("temp:", temp)
    ans = max(temp, ans)

    for ix in range(sp+2, ep-W+1, 2):
        temp = temp - ts[ix-1][1] - ts[ix-2][1] + ts[ix+W-1][1] + ts[ix+W-2][1]
        # print(ix, temp)
        ans = max(temp, ans)

print(ans)

Submission status: Wrong Answer

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

s1 = 0
s0 = 0

# Create 01 array
for c in S:
    if c == '1':
        s1 += 1
        if not s0 == 0:
            ts.append((0, s0))
        s0 = 0
    else:
        s0 += 1
        if not s1 == 0:
            ts.append((1,s1))
        s1 = 0

if s1 != 0:
    ts.append((1,s1))
if s0 != 0:
    ts.append((0,s0))

# print("TS:", ts)

# top/bottom
top0 = False
bottom0 = False

if ts[0][0] == 0:
    top0 = True

if ts[-1][0] == 0:
    bottom0 = True

# print(top0, bottom0)
## pre calc
if len(ts) >= 2:
    ans = 1
else:
    ans = ts[0][0]

sp = 0
ep = len(ts)

if top0:
    ans = sum(x[1] for x in ts[:K*2])
    # print("t0:", ans)
    sp = 1

if bottom0:
    ans = max(ans, sum(x[1] for x in ts[-K*2:]))
    # print("b0:", ans)
    ep = len(ts)-1

# Calc
# print(sp, ep)
if ep - sp > 1:
    W = 2 * K + 1
    temp = sum([x[1] for x in ts[sp:sp+W]])
    # print(temp)
    ans = max(temp, ans)
    for ix in range(sp+1, ep-W+1):
        temp = temp - ts[ix-1][1] + ts[ix+W-1][1]
        ans = max(temp, ans)


print(ans)

Submission status: Wrong Answer

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

s1 = 0
s0 = 0

# Create 01 array
for c in S:
    if c == '1':
        s1 += 1
        if not s0 == 0:
            ts.append((0, s0))
        s0 = 0
    else:
        s0 += 1
        if not s1 == 0:
            ts.append((1,s1))
        s1 = 0

if s1 != 0:
    ts.append((1,s1))
if s0 != 0:
    ts.append((0,s0))

# print("TS:", ts)

# top/bottom
top0 = False
bottom0 = False

if ts[0][0] == 0:
    top0 = True

if ts[-1][0] == 0:
    bottom0 = True

# print(top0, bottom0)
## pre calc
if len(ts) >= 2:
    ans = 1
else:
    ans = ts[0][0]

sp = 0
ep = len(ts)

if top0:
    ans = sum(x[1] for x in ts[:K*2])
    # print("t0:", ans)
    sp = 1

if bottom0:
    ans = max(ans, sum(x[1] for x in ts[-K*2:]))
    # print("b0:", ans)
    ep = len(ts)-1

# Calc
# print(sp, ep)
if ep - sp > 1:
    W = 2 * K + 1
    temp = sum([x[1] for x in ts[:sp+W]])
    ans = max(temp, ans)
    for ix in range(sp+1, ep-W+1):
        temp = temp - ts[ix-1][1] + ts[ix+W-1][1]
        ans = max(temp, ans)


print(ans)

Submission status: Wrong Answer

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

s1 = 0
s0 = 0

# Create 01 array
for c in S:
    if c == '1':
        s1 += 1
        if not s0 == 0:
            ts.append((0, s0))
        s0 = 0
    else:
        s0 += 1
        if not s1 == 0:
            ts.append((1,s1))
        s1 = 0

if s1 != 0:
    ts.append((1,s1))
if s0 != 0:
    ts.append((0,s0))

# print("TS:", ts)

# top/bottom
top0 = False
bottom0 = False

if ts[0][0] == '0':
    top0 = True

if ts[-1][0] == '0':
    bottom0 = True

# print(top0, bottom0)
## pre calc
ans = 0
sp = 0
ep = len(ts)

if top0:
    ans = sum(x[1] for x in ts[:K*2])
    # print("t0:", ans)
    sp = 1

if bottom0:
    ans = max(ans, sum(x[1] for x in ts[-K*2:]))
    # print("b0:", ans)
    ep = len(ts)-1

# Calc
# print(sp, ep)
if ep - sp > 1:
    W = 2 * K + 1
    temp = sum([x[1] for x in ts[:sp+W]])
    ans = max(temp, ans)
    for ix in range(sp+1, ep-W+1):
        temp = temp - ts[ix-1][1] + ts[ix+W-1][1]
        ans = max(temp, ans)


print(ans)

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
S=input()
seqs = [] #(start, end, 0or1): [start, end)
now_num = S[0]
start = 0
for i in range(1, N):
    if S[i] != now_num:
        seqs.append((start, i, int(now_num)))
        now_num = S[i]
        start = i
        if i == N-1:
            seqs.append((start, N, int(now_num)))
            break
    if i == N-1:
        seqs.append((start, N, int(now_num)))
        break

first = S[0]
zero_ranges = None
if first == "0":
    zero_ranges = [i for i in range(0, len(seqs), 2)]
else:
    zero_ranges = [i for i in range(1, len(seqs), 2)]
n_zero = len(zero_ranges)
if n_zero <= K:
    print(N)
    exit()

cum = [0]*(len(seqs)+1)
cum[0] = 0
for i in range(1, len(seqs)+1):
    s, e, n = seqs[i-1]
    cum[i] = cum[i-1] + e-s
length = len(seqs)
ans = 0
for i in range(int(first), len(seqs), 2):
    j = i + 2*K - 2
    tmp = 0
    if j+2 <= length and i-1 >= 0:
        tmp = cum[j+2] - cum[i-1]
    elif j+2 <= length and i-1 < 0:
        tmp = cum[j+2]
    else:
        continue
    ans = max(ans, tmp)
print(ans)

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
S=input()
seqs = [] #(start, end, 0or1): [start, end)
now_num = S[0]
start = 0
for i in range(1, N):
    if S[i] != now_num:
        seqs.append((start, i, int(now_num)))
        now_num = S[i]
        start = i
        if i == N-1:
            seqs.append((start, N, int(now_num)))
            break
    if i == N-1:
        seqs.append((start, N, int(now_num)))
        break

first = S[0]
zero_ranges = None
if first == "0":
    zero_ranges = [i for i in range(0, len(seqs), 2)]
else:
    zero_ranges = [i for i in range(1, len(seqs), 2)]
n_zero = len(zero_ranges)
if n_zero == 0:
    print(N)
    exit()

cum = [0]*(len(seqs))
cum[0] = seqs[0][1] - seqs[0][0]
for i in range(1, len(seqs)):
    s, e, n = seqs[i]
    cum[i] = cum[i-1] + e-s

ans = 0
cum = [cum[i] for i in range(int(first), len(seqs), 2)] + [0]

for i in range(n_zero-K+1):
    delta = 0
    if i == n_zero - K:
        s, e, n = seqs[-1]
        if n == 1:
            delta += e - s
    elif first == "1":
        j = (i+K-1)*2 + 1
        s, e, n = seqs[j+1]
        delta += e - s
    elif first == "0":
        j = (i+K-1)*2
        s, e, n = seqs[j+1]
        delta += e - s

    ans = max(ans, cum[i+K-1] - cum[i-1] + delta)
print(ans)

Submission status: Wrong Answer

Solution code:
from itertools import accumulate
n,k = map(int,input().split())
s = list(input())
if s[0] == '0':
    d = 2*k
else:
    d = 2*k+1
l = [0]
cnt = 0
for i in range(n):
    cnt += 1
    if i == n-1 or s[i] != s[i+1] :
        l.append(cnt)
        cnt = 0

acc = list(accumulate(l))
# print(acc)
if s[0] =='0':
    reverse_limit = len(acc)//2
else:
    reverse_limit = (len(acc)-1)//2
if k >= reverse_limit:
    print(n)
    exit()

maxi = 1
for start in range(0,len(acc)-d,2):
    maxi = max(maxi,acc[start+d]-acc[start])
print(maxi)

Submission status: Wrong Answer

Solution code:
from itertools import accumulate
n,k = map(int,input().split())
s = list(input())
if s[0] == '0':
    d = 2*k
else:
    d = 2*k+1
l = [0]
cnt = 0
for i in range(n):
    cnt += 1
    if i == n-1 or s[i] != s[i+1] :
        l.append(cnt)
        cnt = 0
acc = list(accumulate(l))
# print(acc)
if s[0] =='0':
    reverse_limit = len(acc)//2
else:
    reverse_limit = (len(acc)-1)//2
if k >= reverse_limit:
    print(n)
    exit()

maxi = 1
for start in range(len(acc)-d):
    maxi = max(maxi,acc[start+d]-acc[start])
print(maxi)

Submission status: Wrong Answer

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

ans=[0]
le=[]
ch=0

for i in range(n):
  if s[i]=='0':
    ch+=1
    if i==n-1:
      ans.append(i+1)
      le.append(ch)
  else:
    if ch>0:
      ans.append(i+1)
      le.append(ch)
      ch=0

s=-10000000


if len(ans)-1<=k:
  print(n)
  

else:
  for j in range(len(ans)):
    if j+k<=len(ans)-1:
      
      if j+k==len(ans)-1:
        t=ans[j+k]-ans[j]+(n-1)-ans[j+k]+1
      else:
        t=-ans[j]+ans[j+k+1]-le[j+k]-1
      s=max(s,t)
  print(s)