Problem: 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.

Submission attempts (in order):
#!/usr/bin/env python3.4
# -*- coding: utf-8 -*-

# abc124_d

# input
n, k = [int(s) for s in input().split()]
S = input()

def getp():
    now = S[0]
    for i, c in enumerate(S):
        if now != c:
            now = c
            yield i
pulse = list(getp())
pulse = [0] + pulse + [len(S)]*k*3
def getm():
    for i in range(len(pulse)):
        p = pulse[i]
        if p == len(S): continue
        j = i+k*2 if S[p]==0 else i+k*2+1
        yield pulse[j] - pulse[i]
ans = max(getm())
# output
# print(pulse)
# print(S)
# for i, c in enumerate(S):

#     print(a, end='')
# print()
# print(list(getm()))
print(ans)


#!/usr/bin/env python3.4
# -*- coding: utf-8 -*-

# abc124_d

# input
n, k = [int(s) for s in input().split()]
S = input()

node = [len(s) for s in S.split('0')]
node = [s for i, s in enumerate(node) if not (s==0 and i!=0 and i!=len(node)-1)]
arch = [len(s) for s in S.split('1')]
arch = list(filter(int, arch))
def window(iterable, size):
    
    from itertools import tee
    iters = tee(iterable, size)
    for i in range(1, size):
        for each in iters[i:]:
            next(each, None)
    return zip(*iters)
k = min(k, len(arch))
sls = [(i,i+k) for i in range(len(arch)-k+1)]
m = (sum(arch[i:j]+node[i:j+1]) for i, j in sls)
ans = max(m)
# output
print(node)
print(arch)
print(ans)


#!/usr/bin/env python3.4
# -*- coding: utf-8 -*-

# abc124_d

# input
n, k = [int(s) for s in input().split()]
S = input()
if n<100: exit(0)

node = [len(s) for s in S.split('0')]
node = [s for i, s in enumerate(node) if not (s==0 and i!=0 and i!=len(node)-1)]
arch = [len(s) for s in S.split('1')]
arch = list(filter(int, arch))
k = min(k, len(arch))
sls = [(i,i+k) for i in range(len(arch)-k+1)]
m = (sum(arch[i:j])+sum(node[i:j+1]) for i, j in sls)
ans = max(m)
# output
# print(node)
# print(arch)
print(ans)


#!/usr/bin/env python3.4
# -*- coding: utf-8 -*-

# abc124_d

# input
n, k = [int(s) for s in input().split()]
S = input()
if n<100: exit(0)

node = [len(s) for s in S.split('0')]
node = [s for i, s in enumerate(node) if not (s==0 and i!=0 and i!=len(node)-1)]
arch = [len(s) for s in S.split('1')]
arch = list(filter(int, arch))
def window(iterable, size):
    
    from itertools import tee
    iters = tee(iterable, size)
    for i in range(1, size):
        for each in iters[i:]:
            next(each, None)
    return zip(*iters)
k = min(k, len(arch))
sls = [(i,i+k) for i in range(len(arch)-k+1)]
m = (sum(arch[i:j]+node[i:j+1]) for i, j in sls)
ans = max(m)
# output
# print(node)
# print(arch)
print(ans)


#!/usr/bin/env python3.4
# -*- coding: utf-8 -*-

# abc124_d

# input
n, k = [int(s) for s in input().split()]
S = input()

node = [len(s) for s in S.split('0')]
node = [s for i, s in enumerate(node) if not (s==0 and i!=0 and i!=len(node)-1)]
arch = [len(s) for s in S.split('1')]
arch = list(filter(int, arch))
from itertools import chain
flatten = chain.from_iterable
xs = list(flatten(zip(node, arch))) + node[-1:]
l = len(xs)
sumlist = [None]*(l+2)
sumlist[-1] = 0
for i in range(l):
    sumlist[i] = sumlist[i-1] + xs[i]
def get_sum(f, n):
    f = min(f, l)
    t = f+n
    t = min(t, l)
    return sumlist[t-1] - sumlist[f-1]
def get_ans():
    for i in range(l//2+1):
        yield get_sum(i*2, k*2+1)
ans = max(get_ans())
# output
# print(node)
# print(arch)
# print(xs)
# print(sumlist)
# print(get_sum(0, 2))
# print(list(get_ans()))
print(ans)


Verdicts: Wrong Answer

Wrong Answer

Wrong Answer

Wrong Answer

Accepted