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 = list(input())
if s[0] == "1":
    cnt_list = []
else:
    cnt_list = [0]
cnt = 1
for i in range(n-1):
    if s[i] == s[i+1]:
        cnt += 1
    else:
        cnt_list.append(cnt)
        cnt = 1
cnt_list.append(cnt)
if s[-1] == '0':
    cnt_list.append(0)
#print(cnt_list)
sum_list = [0]
for i in range(len(cnt_list)):
    sum_list.append(sum_list[i] + cnt_list[i])
left = 0
right = max(len(sum_list)-2*k, 0)
ans = 1
for i in range(0,right,2):
    ans = max(ans,sum_list[i+2*k+1]-sum_list[i])
print(ans)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
s = list(input())
if s[0] == "1":
    cnt_list = []
else:
    cnt_list = [0]
cnt = 1
for i in range(n-1):
    if s[i] == s[i+1]:
        cnt += 1
    else:
        cnt_list.append(cnt)
        cnt = 1
cnt_list.append(cnt)
if s[-1] == '0':
    cnt_list.append(0)
#print(cnt_list)
sum_list = [0]
for i in range(len(cnt_list)):
    sum_list.append(sum_list[i] + cnt_list[i])
left = 0
right = max(len(cnt_list)-2*k, 1)
ans = 0
for i in range(1,right,2):
    ans = max(ans,sum_list[i+2*k+1]-sum_list[i])
print(ans)

Submission status: Wrong Answer

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

tmp = 0
if k*2 <= len(a):
    tmp = k
else:
    if len(a)%2 == 0:
        tmp = len(a)/2
    else:
        tmp = len(a)//2 

ans = 0
i = 0
while i+tmp*2+1<=len(a):
    tmpans = sum(a[i:i+tmp*2+1])
    ans = max(ans,tmpans)
    i+=2
print(ans)

Submission status: Wrong Answer

Solution code:
#-*- coding: utf-8 -*-
"""
oj dl https://atcoder.jp/contests/abc116/tasks/abc116_d -d test-d
oj test -d test-d -c "python abc116d.py"
oj test -d test-d -c "python abc116d.py" test-d/sample-3.in
"""

from collections import defaultdict
import sys
import time
import logging



def deco(func):
    def wrapper(*args, **kwargs):
        s = time.time()
        #print('--start--')
        ret = func(*args, **kwargs)
        logging.debug('--end in %f --' % (time.time() - s))
        return ret

    return wrapper


def sol(n, k, S):
    logging.debug("n:%s, k:%s, S:%s" % (n, k, S))
    longest = 0
    k_tmp = 0
    C = [] # checkpoint
    def get_length(idx, C, k_tmp, k):
        c_idx = k_tmp - (k+1)
        try:
            c_value = C[c_idx]
        except:
            c_value = 0
        return idx - c_value + 1

    for i in range(len(S)):
        cur_length = get_length(i, C, k_tmp, k)
        logging.debug("i:%s, S[%s]:%s, k_tmp:%s, cur:%s, max:%s, C:%s" % (
            i, i, S[i], k_tmp, cur_length, longest, C)
        )
        longest = max(longest, cur_length)
        if (i > 0 and S[i-1] == '1' and S[i] == '0') or (i == 0 and S[i] == '0'):
            k_tmp += 1
        if i > 0 and S[i] == '1' and S[i-1] == '0':
            C.append(i)
    return longest





do_submit = True
#do_submit = False
if do_submit:
    logging.basicConfig(level=logging.ERROR, format="%(message)s")
else:
    logging.basicConfig(level=logging.DEBUG, format="%(message)s")

def input_parse(input_str):
    lines = [x.strip() for x in input_str.split("\n") if x.strip()]
    parsed_lines = [[x.strip() for x in line.split()] for line in lines]
    n, k = parsed_lines[0]
    n = int(n)
    k = int(k)
    S = parsed_lines[1][0]
    return (n, k, S)


if not do_submit:
    n, k, S = input_parse("""
    14 2
    11101010110011
    """)
    print(sol(n, k, S))

    n, k, S = input_parse("""
    5 1
    00010
    """)
    print(sol(n, k, S))

    n, k, S = input_parse("""
    1 1
    1
    """)
    print(sol(n, k, S))


else:
    str = ""
    str += input().strip() + "\n"# n, k
    str += input().strip() # S
    n, k, S = input_parse(str)
    print (sol(n, k, S))

Submission status: Wrong Answer

Solution code:
import sys

N, K = map(int, input().split())
S = input()
"""
Sに対して操作を行い1を何個連続で並べられるかという問題
SはN桁の2進数で表すことができる
0を潰していくほうが効率がいいので0を潰す
連続している数字の数を配列にする
3
1 00 1 0000 1111 0 1111 000 1 0 11
[1, 2, 1, 4, 4, 1, 4, 3, 1, 1, 2]
000 1 000 111 0000 1 0 1 000 11 0 1
[3, 1, 3, 3, 4, 1, 1, 1, 3, 2, 1, 1]

数え始める先頭(S_i)が1の場合連続で並ぶ1の数は得られた配列のS_i+2K  までの合計で与えられる
数え始める先頭(S_i)が0の場合連続で並ぶ1の数は得られた配列のS_i+2K-1までの合計で与えられる
"""
S_1 = S[0] # Sの先頭の数字

S_arr = []
checker = S_1 # 現在の並んでいる数字
num = 0
for i in range(N):
    if checker == S[i]:
        num += 1
    
    else:
        S_arr.append(num)
        num = 1
        checker = S[i]
S_arr.append(num)

S_1 = int(S_1)

l = len(S_arr)
if S_1 == 1 and l <= K * 2 + 1:
    print(N)
    sys.exit()

if S_1 == 0 and l <= K * 2:
    print(N)
    sys.exit()
"""
sum(S_arr[i:i+K*2+1]) # 数え始めが1
sum(S_arr[i:i+K*2])   # 数え始めが0
"""
S_ini = S_1
def f1(i): # i はS_arrのindex
    ini = S_1 ^ (i % 2)
   
    if i+K*2 + 1 * ini > l:
        return 0
    return sum(S_arr[i:i+K*2 + 1 * ini])
kazunoko = [0] * l
for i in range(l):
    kazunoko[i] = f1(i)
# print(S_arr)
# print(kazunoko)
print(max(kazunoko))

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3


def main():
    _, k = map(int, input().split())
    s = input()

    # 配列を下記で変換
    # '11000' = [(2, 1), (3, 0)]
    groups = list()
    s = s.replace('01', '0,1').replace('10', '1,0')
    splited = s.split(',')
    for group in splited:
        groups.append((len(group), int(group[0])))

    # 0 groupを最大でk個含む部分配列を生成
    max_len = 0
    for i in range(len(groups)):
        if i > len(groups) - 2 * k + 2:
            break
        if groups[i][1] == 0 and i != 0:
            continue

        if groups[i][1] == 0:
            subgroups = groups[i:i + 2 * k]
        else:
            subgroups = groups[i:i + 2 * k + 1]
        handstand_len = sum([g[0] for g in subgroups])
        if handstand_len > max_len:
            max_len = handstand_len

    print(max_len)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
l =[int(i) for i in list(str(input()))]
ans = 0
if len(l) - len([i for i in l if i ==1]) <=k:
    ans = len(l)
else:
    for i in l: 
        if i==0: 
            k = k-1
            ans = ans +1
            if k ==0:
                break
            else:
                ans = ans +1 
print(ans)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
s = input() + "#"

pre, cons, comp = '#', 0, [(0, -1)]
for i in range(len(s)):
    if pre != '#' and pre != s[i]:
        if cons > 0:
            print(cons, s[i-1])
            comp.append((cons, s[i-1]))
            cons = 1
    else:
        cons += 1

    pre = s[i]

acc = [0 for _ in range(len(comp))]
for i in range(1, len(comp)):
    acc[i] = acc[i-1] + comp[i][0]

ans = 0
for i in range(len(comp) - 1):
    lim = len(comp) - 1
    if comp[i+1][1] == '1':
        lim = min(lim, i + 2*K + 1)
    else:
        lim = min(lim, i + 2*K)
    # print("i = {}, lim = {}".format(i, lim))
    ans = max(ans, acc[lim] - acc[i])

print(ans)

Submission status: Wrong Answer

Solution code:

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

res_val=0
k=0
while(k<K and res_val<len(S)):
    #Sを0から順番に見ていきS[i]==0のiをrとする
    isRend=False
    i=0
    while(isRend!=True and i<len(S)):
        if S[i]=='0':
            isRend=True
        i=i+1
    
    #Sをiから順番に見ていきS[j]==1のjをlとする
    isLend=False
    j=i
    while(isLend!=True and j<len(S)):
        if S[j]=='1':
            isLend=True
        j=j+1

    #rから(rは含む)lまで(lは含まない)のSを1とする
    if i==len(S):
        #操作完了
        res_val=len(S)
    else:
        #iからjまで1にする
        for a in range(i,j):
            S=S[:a-1]+'1'+S[a:]
    k=k+1

#res_valを求める
s=0
while(s<len(S) and S[s]=='1'):
    res_val=s+1
    s=s+1

print(s)

Submission status: Wrong Answer

Solution code:
import sys

def count(a, p, q): 
    x = 0
    for i in range(q):
        x += len(a[p+i])
    return x

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

num = [] 
k = 0

for i in range(N-1):
    if S[i] != S[i+1]:
        num.append(S[k:i+1])
        k = i+1

num.append(S[k:])

if len(num) <= 2*K: 
    print(N)
    sys.exit()

ans = [] 

if S[0] == '0':
    start = 0
    ans.append(count(num, start, 2*K))
    start += 1
    while True:
        if start + 2*K > len(num)-1: 
            break
        else: 
            ans.append(count(num, start, 2*K+1))
        start += 2
        
if S[0] == '1': 
    start = 0
    while True:
        if start + 2*K > len(num)-1: 
            break
        else: 
            ans.append(count(num, start, 2*K+1))
        start += 2

print(max(ans))

Submission status: Wrong Answer

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

now = 1
cnt = 0
x = []

for i in range(n):
    if sss[i] == str(now):
        cnt += 1
    else:
        x.append(cnt)
        now = 1 - now
        cnt = 1
if cnt != 0:
    x.append(cnt)
if sss[-1] == 0:
    x.append(0)

Add = 2 * k + 1
ans = 0

y = [0]
for i in range(len(x)):
    y.append(y[i] + x[i])
print(y)

for i in range(len(x))[::2]:
    left = i
    right = min(i + Add, len(x))
    tmp = y[right] - y[left]
    
    ans = max(tmp, ans)

print(ans)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
S = input()
tmp = S[0]
count = 0
num_count = []
f = int(S[0])
for s in S:
    if tmp == s:
        count += 1
    else:
        num_count.append(count)
        count = 1
    tmp = s
num_count.append(count)
if S[0] == '0':
    num_count.insert(0, 0)
if S[-1] == '0':
    num_count.insert(-1, 0)
sum = [0]
for i in num_count:
    sum.append(sum[-1] + i)
a = 0
for i, num in enumerate(sum[::2]):
    left = i
    right = min(i + 2 * k + 1, len(num_count))
    t = sum[right] - sum[left]
    a = max(a, t)
print(a)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
S = input()
tmp = S[0]
count = 0
num_count = []
f = int(S[0])
for s in S:
    if tmp == s:
        count += 1
    else:
        num_count.append(count)
        count = 1
    tmp = s
num_count.append(count)
if S[0] == '0':
    num_count.insert(0, 0)
if S[-1] == '0':
    num_count.insert(-1, 0)
sum = [0]
for i in num_count:
    sum.append(sum[-1] + i)
a = 0
for i, num in enumerate(sum):
    left = i
    right = min(i + 2 * k + 1, len(num_count))
    t = sum[right] - sum[left]
    a = max(a, t)
print(a)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
S = input()
tmp = S[0]
count = 0
num_count = []
f = int(S[0])
for s in S:
    if tmp == s:
        count += 1
    else:
        num_count.append(count)
        count = 1
    tmp = s
num_count.append(count)
if S[0] == '0':
    num_count.insert(0, 0)
if S[-1] == '0':
    num_count.insert(-1, 0)
sum = [0]
for i in num_count:
    sum.append(sum[-1] + i)
a = 0
for i, num in enumerate(sum[:-1]):
    left = i
    right = min(i + 2 * k + 1, len(num_count))
    t = sum[right] - sum[left]
    a = max(a, t)
print(a)

Submission status: Wrong Answer

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

cnt = 0
state = sss[0]
x = []
if state == "0":
    x.append(0)
for s in sss:
    if s == state:
        cnt += 1
    else:
        state = s
        x.append(cnt)
        cnt = 1
x.append(cnt)
#11101010110011
#1:3 1 1 2 2
#0: 1 1 1 2

#1
#1

y = [0]
for i in range(len(x)):
    y.append(y[i] + x[i])
#:0,1,2,3,4,5,6 7, 8 ,9
#:0,3,4,5,6,7,8,10,12,14

#0,1

z = []
for i in range(k*2+1,len(y))[::2]:
    z.append(y[i] - y[i - (k*2+1)])
#print(x)
#print(y)
#print(z)
if len(x) <= k*2:
    print(sum(x))
else:
    print(max(z))

Submission status: Wrong Answer

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

cnt = 0
state = sss[0]
x = []
if state == "0":
    x.append(0)
for s in sss:
    if s == state:
        cnt += 1
    else:
        state = s
        x.append(cnt)
        cnt = 1
x.append(cnt)
#11101010110011
#1:3 1 1 2 2
#0: 1 1 1 2

#1
#1

y = [0]
for i in range(len(x)):
    y.append(y[i] + x[i])
#:0,1,2,3,4,5,6 7, 8 ,9
#:0,3,4,5,6,7,8,10,12,14

#0,1

z = []
for i in range(k*2+1,len(y))[::2]:
    z.append(y[i] - y[i - (k*2+1)])
#print(x)
#print(y)
#print(z)
if len(x) <= k*2+1:
    print(sum(x))
else:
    print(max(z))

Submission status: Wrong Answer

Solution code:
def solve(n, k, s):
    v = []
    if s[0] == "0":
        v.append(0)
    i = 0
    while i < n:
        num = 1
        while (i+1 < n) and (s[i+1] == s[i]):
            num += 1
            i += 1
        v.append(num)
        i += 1
    if s[n-1] == "0":
        v.append(0)
    m = len(v)
    w = [0] * m
    w[0] = v[0]
    for i in range(m-1):
        w[i+1] = w[i] + v[i+1]
    best = w[0]
    for i in range(m):
        left = i
        right = min(i + 2*k + 1, m-1)
        cur = w[right] - w[left]
        best = max(best, cur)
    return best

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

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
S = list(map(int,input().split()))
tmp = 1
tbl = []
cont = 0
for s in S:
    if s == tmp:
        cont += 1
    else:
        tbl.append(cont)
        cont = 1
        tmp = abs(tmp-1)
tbl.append(cont)
if len(tbl) % 2 == 0:
    tbl.append(0)
max = 0
for i in range(0,len(tbl),2):
    if sum(tbl[i:i+K*2+1]) > max:
        max = sum(tbl[i:i+K*2+1])
print(max)

Submission status: Wrong Answer

Solution code:
N, k = map(int, input().split())
S = input()
ans = []
state = S[0]
temp = 1
for i in range(1, len(S)):
  if S[i] != state:
    ans.append(temp)
    state = S[i]
    temp = 1
  else:
    temp +=1
ans.append(temp)
first = S[0]
if S[0] == '0':
  answ = sum(ans[:2*k])
  for i in range(1, len(ans)):
    if i%2 == 1:
      temp = sum(ans[i:i+2*k])
    else:
      temp = sum(ans[i:i+2*k-1])
    if temp > answ:
      answ = temp
else:
  answ = sum(ans[:2*k+1])
  for i in range(1, len(ans)):
    if i%2 == 0:
      temp = sum(ans[i:i+2*k])
    else:
      temp = sum(ans[i:i+2*k-1])
    if temp > answ:
      answ = temp
print(answ)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
s = input()
times = [1]
list_ans = []
for i in range(1,n-1):
        if s[i] != s[i+1]:
                times.append(i+2)
len_times = len(times)
for i in range(2*k+1):
        times.append(n+1)
for i in range(len_times):
        if s[times[i]-1] == "0":
                list_ans.append(times[i+2*k] - times[i])
        else:
                list_ans.append(times[i+2*k+1] - times[i])
print(max(list_ans))

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
s = input()+"f"
times = []
list_ans = []
ans = 0
x = 0
for i in range(len(s)-1):
        if s[i] != s[i+1]:
                times.append(i+1)
len_times = len(times)
if s[0] == "1":
        for i in range(0,len(times)+1,2):
                if 2*k+i<len_times:
                        ans = max(ans,times[2*k+i]-times[i-1])
else:
        for i in range(0,len(times)+1,2):
                if i == 0 and 2*k-1<len_times:
                        ans = max(ans,times[2*k-1])
                else :
                        if 2*k+i<len_times:
                                ans = max(ans,times[2*k+i]-times[i-1])
print(ans)

Submission status: Wrong Answer

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

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

cnts = []
# denote whether the first block is 0 or 1
tmp, cnt = s[0], 0
flag = int(s[0])

for i in range(n):
    if s[i] == tmp:
        cnt += 1
    else: # here tmp should be cleared
        cnts.append(cnt)
        cnt = 1
    tmp = s[i]
cnts.append(cnt)
print(cnts)

n_zero = (len(cnts) + 1) // 2 if flag == 0 else len(cnts) // 2
# print("# of zeros block: ", n_zero)
if n_zero <= k:
     ans = sum(cnts)
else:
    ans = 0
    for inx in range(flag, len(cnts), 2):
        start = max(0, inx - 1)
        end = min(inx + 2 * k, len(cnts))
        tmp = sum(cnts[start: end])
        ans = max(tmp, ans)

print(ans)


# n, k = map(int, input().split())
# s = [int(x) for x in list(input())]
#
# state = 1
# cnt = 0
# l = []
# for e in s:
#     if e != state:
#         l.append(cnt)
#         cnt = 1
#         state = e
#     else:
#         cnt += 1
#
# if cnt != 0:
#     l.append(cnt)
#
#
# if len(l) % 2 == 1:
#     l.append(0)
#
# Add = 2 * k + 1
# ruiseki = [0]
#
# for i, e in enumerate(l):
#     ruiseki.append(ruiseki[i] + l[i])
#
# answer = 0
# for i in range(0, len(l), 2):
#     left = i
#     right = min(i + Add, len(l))
#     tmp = ruiseki[right] - ruiseki[left]
#     answer = max(tmp, answer)
#
# print(answer)

Submission status: Wrong Answer

Solution code:
n, k = [int(e) for e in input().strip().split(' ')]
s = input().strip()

cnts = []
# denote whether the first block is 0 or 1
tmp, cnt = s[0], 0
flag = int(s[0])

for i in range(n):
    if s[i] == tmp:
        cnt += 1
    else: # here tmp should be cleared
        cnts.append(cnt)
        cnt = 1
    tmp = s[i]
cnts.append(cnt)
print(cnts)

n_zero = (len(cnts) + 1) // 2 if flag == 0 else len(cnts) // 2
# print("# of zeros block: ", n_zero)
if n_zero <= k:
     ans = sum(cnts)
else:
    ans = 0
    for inx in range(flag, len(cnts), 2):
        start = max(0, inx - 1)
        end = min(inx + 2 * k, len(cnts))
        tmp = sum(cnts[start: end])
        ans = max(tmp, ans)

print(ans)

Submission status: Wrong Answer

Solution code:
import math
import os
import random
import re
import sys
from collections import defaultdict

# sys.stdin = open('input.in', 'r')
# sys.stdout = open('output.out', 'w') 

def Fzero(l, k, n):
	max = 0
	if k*2 < len(l):
		max = l[k*2] - l[0]
	else:
		return n+1
	for i in range(1, len(l), 2):
		if i+k*2+1 < len(l):
			temp = l[i + k*2+1] - l[i]
		else:
			temp = n - l[i]
		if temp > max:
			max = temp

	return max

def Fone(l, k, n):
	max = 0
	for i in range(0, len(l), 2):
		# print(max)
		if i+k*2+1 < len(l):
			temp = l[i+k*2+1] - l[i]
		else:
			temp = n - l[i]
		if temp > max:
			max = temp

	return max

def findmax(n, k, N):
	if n == 1:
		return 1
	sum = 0
	prev = N[0]
	length = []
	length.append(N[0])
	for i in range(n):
		if N[i]!=prev:
			length.append(i)
		prev = N[i]
	# print(length)
	if N[0] == 0:
		return Fzero(length, k, n)
	else:
		return Fone(length, k, n)



if __name__ == '__main__':
	n, k = map(int, input().rstrip().split())
	N = [int(d) for d in input()]
	print(findmax(n, k, N))

Submission status: Wrong Answer

Solution code:
import math
import os
import random
import re
import sys
from collections import defaultdict

# sys.stdin = open('input.in', 'r')
# sys.stdout = open('output.out', 'w') 

def Fzero(l, k, n):
	max = 0
	if k*2 < len(l):
		max = l[k*2] - l[0]
	else:
		return n
	for i in range(1, len(l), 2):
		if i+k*2+1 < len(l):
			temp = l[i + k*2+1] - l[i]
		else:
			temp = n - l[i]
		if temp > max:
			max = temp

	return max

def Fone(l, k, n):
	max = 0
	for i in range(0, len(l), 2):
		# print(max)
		if i+k*2+1 < len(l):
			temp = l[i+k*2+1] - l[i]
		else:
			temp = n - l[i]
		if temp > max:
			max = temp

	return max

def findmax(n, k, N):
	if n == 1:
		return 1
	sum = 0
	prev = N[0]
	length = []
	length.append(N[0])
	for i in range(n):
		if N[i]!=prev:
			length.append(i)
		prev = N[i]
	# print(length)
	if N[0] == 0:
		return Fzero(length, k, n)
	else:
		return Fone(length, k, n)



if __name__ == '__main__':
	n, k = map(int, input().rstrip().split())
	N = [int(d) for d in input()]
	print(findmax(n, k, N))

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
s = [int(x) for x in list(input())]

state = 1
cnt = 0
l = []
for e in s:
    if e != state:
        l.append(cnt)
        cnt = 1
        state = e
        print(e, l)
    else:
        cnt += 1

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

if len(l) % 2 == 1:
    l.append(0)

Add = 2 * k + 1
ruiseki = [0]

for i, e in enumerate(l):
    ruiseki.append(ruiseki[i] + l[i])

answer = 0
for i in range(0, len(l), 2):
    left = i
    right = min(i + Add, len(l))
    tmp = ruiseki[right] - ruiseki[left]
    answer = max(tmp, answer)
 
print(answer)

Submission status: Wrong Answer

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

n, k = [int(e) for e in input().strip().split(' ')]
s = input().strip()

cnts = []
ans = 0
# denote whether the first block is 0 or 1
flag = s[0]
reverse = 0

tmp = s[0]
cnt = 0
for i in range(n):
    if s[i] == tmp:
        cnt += 1
    else: # here tmp should be cleared
        cnts.append(cnt)
        cnt = 1
    tmp = s[i]
cnts.append(cnt)

if flag == 0:
    n_zero = (len(cnts) + 1) // 2
else:
    n_zero = len(cnts) // 2

if n_zero <= k:
     ans = sum(cnts)
else:
    if flag == 0:
        ans = 0
        for inx in range(0, len(cnts), 2):
            end = min(inx + 2 * k, len(cnts))
            tmp = sum(cnts[inx: end])
            if tmp > ans:
                ans = tmp
    else:
        ans = 0
        for inx in range(1, len(cnts), 2):
            end = min(inx + 2 * k, len(cnts))
            tmp = sum(cnts[inx-1:end])
            if tmp >= ans:
                ans = tmp

print(ans)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
s = [int(x) for x in list(input())]

state = 1
cnt = 0
l = []
for i in s:
    if i == state:
        cnt += 1
    else:
        l.append(cnt)
        cnt = 1
        state = i
l = [x for x in l if x >0]
if len(l) % 2 == 1:
    l.append(0)

Add = 2 * k + 1
ruiseki = [0]

for i, e in enumerate(l):
    ruiseki.append(ruiseki[i] + l[i])

answer = 0
for i in range(0, len(l), 2):
    left = i
    right = min(i + Add, len(l))
    tmp = ruiseki[right] - ruiseki[left]
    answer = max(tmp, answer)

print(answer)

Submission status: Wrong Answer

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

state = "1"
cnt = 0
l = []
for i in s:
    if i == state:
        cnt += 1
    else:
        l.append(cnt)
        cnt = 1
        state = i
l = [x for x in l if x >0]
if len(l) % 2 == 1:
    l.append(0)

Add = 2 * k + 1
ruiseki = [0]
for i, e in enumerate(l):
    ruiseki.append(ruiseki[i] + l[i])

answer = 0
for i in range(0, len(l), 2):
    left = i
    right = min(i + Add, len(l))
    temp = ruiseki[right] - ruiseki[left]
    answer = max(temp, answer)
print(answer)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
s = input()+"f"
times = []
list_ans = []
ans = 0
x = 0
for i in range(len(s)-1):
        if s[i] != s[i+1]:
                times.append(i+1-x)
                x = i+1
len_times = len(times)
if s[0] == "1":
        for i in range(0,len(times)//2+1,2):
                ans = max(ans,sum(times[i:2*k+i+1]))
else:
        for i in range(0,len(times)//2+1,2):
                if i == 0:
                        ans = max(ans,sum(times[0:2*k]))
                else:
                        ans = max(ans,sum(times[i:2*k+1+i]))
print(ans)

Submission status: Wrong Answer

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

#numsの偶数番目の要素が連続した1の個数,奇数番目の要素が連続した0の個数
#	data[1001110001] -> nums[1,2,3,3,1]
nums = []
if data[0]==0:
	nums.append(0)
i,j = 0,0
for i in range(len(data)):
	i,count = j,1
	for j in range(i+1,len(data)):
		if data[i]==data[j]:
			count+=1
		if data[i]!=data[j] or j==len(data)-1:
			nums.append(count)
			break

#累積和 nums[1,2,3,3,1] -> sums[0,1,3,6,9,10]
sums = [0]
for i in range(len(nums)):
	sums.append(sums[i] + nums[i])

#連続した0を1にする操作をK回行うと、2K+1個連続した1の要素が並ぶ 
ans = 0
for i in  range(0,len(sums),2):
	j = i + K*2+1
	if j>=len(sums):
		j = len(sums)-1
	ans = max(ans, sums[j]-sums[i])

print(ans)

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
S = input()
if S[0] == '0':
  divide = []
else:
  divide = [0]
tmp = 1
for i in range(1,N):
  if S[i] == S[i-1]:
    tmp += 1
  else:
    divide += [tmp]
    tmp = 1
divide += [tmp]
le = len(divide)
zero = [0]*2*le + divide + [0]*2*le
su = [0]
for i in range(0,5*le):
  su += [su[-1]+zero[i]]
del su[0]
lle = len(su)
mxm = 0
for k in range(K,lle):
  if 2*k+1 >= lle:
    break
  cd = su[2*k+1] - su[2*k-2*K]
  if cd > mxm:
    mxm = cd
print(mxm)

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
S = input()
if S[0] == '0':
  divide = []
else:
  divide = [0]
tmp = 1
for i in range(1,N):
  if S[i] == S[i-1]:
    tmp += 1
  else:
    divide += [tmp]
    tmp = 1
divide += [tmp]
le = len(divide)
zero = [0]*2*le + divide + [0]*2*le
su = [0]
for i in range(0,5*le):
  su += [su[-1]+zero[i]]
del su[0]
lle = len(su)
mxm = 0
for k in range(1,lle):
  if 2*k+1 >= lle:
    break
  cd = su[2*k+1] - su[2*k-2]
  if cd > mxm:
    mxm = cd
print(divide,zero,su,mxm)

Submission status: Wrong Answer

Solution code:
N,K = map(int, input().split())
S = list(str(input()))
l1 = [0]
l0 = [0]
count = 0
flg = int(S[0])#今0or1のどれについて数えているか
if flg == 1:
    l1[0] = 1
elif flg == 0:
    l0[0] = 1
else:
    print ("ERROR-1")
index = 1
#文字列分割が謎
while True:
    if index == len(S):
        #終了条件
        break
        
    if flg == 1:
        #前の文字が1
        if int(S[index]) == flg:
            #l1のケツの数字をインクリメント
            l1[len(l1)-1] += 1
        else:
            l0.append(1)
            flg = 0
            
    else:
        #前の文字が0
        if int(S[index]) == flg:
            #l0のケツをインクリメント
            l0[len(l0)-1] += 1
        else:
            l1.append(1)
            flg = 1
            
    index += 1

#テスト済み　4/19 15.00
#初めの文字でない方は0になる
#これはlist.remove(0)で消せる

#K個のl0の連続する要素とその間のindexのl1を足して保存する
#その最大値を出す

import numpy as np


#そもそもlen(l0) <= Kなら普通に全て足してOK
if len(l0) <= K:
    print(np.array(l0).sum() + np.array(l1).sum())
else:
    index0 = 0
    index1 = 0
    sum_tmp = 0
    tmp = [] #最大値を入れておく
    if l0[0] == 0:
        l0.remove(0)
        #初めが1スタートの時
        #l1からK+1こ、l0からKこ
        while index0 + K -1 < len(l0) and index1+K < len(l1):

            #値計算
            for k in range(K):
                sum_tmp +=  l1[index1 + k] + l0[index0 + k]
            sum_tmp += l1[index1 + K]

            tmp.append(sum_tmp)
            sum_tmp = 0
            index0 += 1
            index1 += 1

    else:
        #そんな変わらないからif文の分岐ここじゃなくていいかも
        #初めが0スタート
        l1.remove(0)
        #初めだけl0からKこ、l1からKこ　0-1-0-1
        #他は上と同様
        for k in range(K):
            sum_tmp +=  l0[index0 + k] + l1[index1 + k]

        tmp.append(sum_tmp)
        sum_tmp = 0
        index0 += 1#初めの0だけ使わない

        while index0 + K-1 < len(l0) and index1 + K < len(l1):

            #値計算
            for k in range(K):
                sum_tmp +=  l1[index1 + k] + l0[index0 + k]
            sum_tmp += l1[index1 + K]

            tmp.append(sum_tmp)
            sum_tmp = 0
            index0 += 1
            index1 += 1
    print(np.array(tmp).max())

Submission status: Wrong Answer

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

S += "!"
cur_char = S[0]
cur_char_count = 1
smart_S = ""
smart_count = []
for c in S[1:]:
    if cur_char == c:
        cur_char_count += 1
    else:
        smart_S += cur_char
        smart_count.append(cur_char_count)
        cur_char = c
        cur_char_count = 1

l = 0
r = 0
cnt_0 = 0
cur_sum = 0

ans_cand = []

# print("smart_S: ", smart_S)

while r == 0 or l < r:
    # print("l: {}, r: {}".format(l, r))
    while cnt_0 < K and r < len(smart_S) - 1:
        cur_sum += smart_count[r]
        if smart_S[r] == '0':
            cnt_0 += 1
        r += 1
        
    # proceed one more 
    if r < len(smart_S) and smart_S[r] == '1':
        cur_sum += smart_count[r]
        cnt_0 += 1
        r += 1

    ans_cand.append(cur_sum)

    if l < len(smart_S) and smart_S[l] == '0':
        cur_sum -= smart_count[l]
        cnt_0 -= 1
        l += 1

    else:
        if l < len(smart_S):
            cur_sum -= smart_count[l]
            cnt_0 -= 1
            l += 1
        if l < len(smart_S):
            cur_sum -= smart_count[l]
            cnt_0 -= 1
            l += 1


#print("smart_count: ", smart_count)
#print("ans_cand: ", ans_cand)
print(max(ans_cand))

Submission status: Wrong Answer

Solution code:
N,k = map(int,input().split())
S = input()
lenth = 0
AllLen =[]
preS0,preS1 = S.split('1'),S.split('0')
S0 = [i for i in preS0 if i != '']
S1 = [i for i in preS1 if i != '']
num0 = k
if len(S0) <= k:
    print(N)
    exit() 
if S[0] == '0':
    J = k
else:J = k + 1
FirstLen = sum([len(S0[i]) for i in range(k)]) + sum([len(S1[j]) for j in range(J)])
AllLen.append(FirstLen)
CheckLen = FirstLen
if S[0] == '1' and S[-1] == '1':
    for i in range(len(S0) - k):
        CheckLen = CheckLen - len(S0[i]) - len(S1[i]) + len(S0[i + k]) + len(S1[i + k + 1])
        AllLen.append(CheckLen)
if S[0] == '0' and S[-1] == '1':
    CheckLen = CheckLen - len(S0[0]) + len(S0[k]) + len(S1[k])
    AllLen.append(CheckLen)
    for i in range(len(S0) - k - 1):
        CheckLen = CheckLen - len(S0[i + 1]) - len(S1[i + 1]) + len(S0[i + k + 1]) + len(S1[i + k + 1])
        AllLen.append(CheckLen)
if S[0] == '1' and S[-1] == '0':
    for i in range(len(S0) - k - 1):
        CheckLen = CheckLen - len(S0[i]) - len(S1[i]) + len(S0[i + k]) + len(S1[i + k + 1])
        AllLen.append(CheckLen)
    CheckLen = CheckLen - len(S0[len(S0) - k - 1]) - len(S1[len(S0) - k - 1]) + len(S0[len(S0) - 1])
    AllLen.append(CheckLen)
if S[0] == '0' and S[-1] == '0':
    if len(S1) == k:
        S00 = []
        S00.append(S0[0])
        S00.append(S0[-1])
        print(len(S) - len(min(S00)))
        exit()
    if len(S0) <= k:
        print(N)
        exit()
    CheckLen = CheckLen - len(S0[0]) + len(S0[k]) + len(S1[k])
    AllLen.append(CheckLen)
    for i in range(len(S0) - k - 2):
        CheckLen = CheckLen - len(S0[i + 1]) - len(S1[i + 1]) + len(S0[i + k + 1]) + len(S1[i + k + 1])
        AllLen.append(CheckLen)
    CheckLen = CheckLen - len(S0[len(S0) - k - 1]) - len(S1[len(S0) - k - 1]) + len(S0[len(S0) - 1])
    AllLen.append(CheckLen)
print(max(AllLen))