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.

Solution (Python):
from collections import defaultdict,deque
import sys,heapq,bisect,math,itertools,string,queue,datetime,random
sys.setrecursionlimit(10**8)
INF = float('inf')
mod = 10**9+7
eps = 10**-7
def inpl(): return list(map(int, input().split()))
def inpls(): return list(input().split())

N,K = inpl()
S = list(map(int,list(str(input()))))
ss = []

cnt = 0
flag1 = (S[0] == 1)
if S[0] == 0:
    ss.append(0)

for s in S:
    if flag1 and s == 0:
        ss.append(cnt)
        cnt = 1
        flag1 = False
    elif not flag1 and s==1:
        ss.append(cnt)
        cnt = 1
        flag1 = True
    else:
        cnt += 1

ss.append(cnt)
if S[-1] == 0:
    ss.append(0)



l = 0
r = 2*K
tmp = sum(ss[l:r+1])
ans = tmp
while True:
    l += 2
    r += 2
    if r >= len(ss):
        break
    tmp -= ss[l-2] + ss[l-1]
    tmp += ss[r-1] + ss[r]
    ans = max(ans,tmp)

print(ans)

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

o = []
l = [0]

prev = '1'
for s in S:
	if s == '1':
		if prev == '0':
			l.append(1)
		else:
			l[-1] += 1
	else:
		if prev == '1':
			o.append(1)
		else:
			o[-1] += 1
	prev = s
l.append(0)

agg = sum(l[0:K+1]) + sum(o[0:K])
max_agg = agg
for i in range(1, len(o)-K+1):
	agg -= l[i-1] + o[i-1]
	agg += l[i+K] + o[i+K-1]
	max_agg = max(max_agg, agg)
print(max_agg)

Solution (Python):
N, K = map(int, raw_input().split())
S = raw_input()

n = [0]
if S[0] == '0':
    n.append(0)

for i in xrange(1, N):
    if S[i - 1] != S[i]:
        n.append(i)

n.append(N)
if S[N - 1] == '0':
    n.append(N)

if len(n) <= K * 2 + 1:
    print N
else:
    m = 0
    for i in xrange(K * 2 + 1, len(n), 2):
        m = max(m, n[i] - n[i - K * 2 - 1])
    print m

Solution (Python):
def main():
    N, K = map(int, input().split())
    S = input()
    seq = []
    current = "1"
    cnt = 0
    for c in S:
        if c == current:
            cnt += 1
        else:
            seq.append(cnt)
            current = c
            cnt = 1
    seq.append(cnt)
    if len(seq) % 2 == 0:
        seq.append(0)
    if len(seq) <= K*2+1:
        print(sum(seq))
        return
    ans = 0
    total = 0
    j = 0
    for i in range(len(seq)-K*2):
        while j < i+K*2+1:
            total += seq[j]
            j += 1
        if i % 2 == 0:
            ans = max(ans, total)
        total -= seq[i]
    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
N, K = map(int, raw_input().split())
S = raw_input()

T = []
if S[0]=="0":
  T = [0]
p = 0
for i in range(1, N+1):
  if i==N or S[i]!=S[i-1]:
    T += [i-p]
    p = i
if S[-1]=="0":
  T += [0]

if len(S)<=2*K+1:
  ans = sum(T)
else:
  t = sum(T[:2*K+1])
  ans = t
  for i in range(2*K+1, len(T)):
    t -= T[i-2*K-1]
    t += T[i]
    if i%2==0:
      ans = max(ans, t)
print ans

Solution (Python):
N,K=map(int,input().split())
S=input()
L=[0]


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

M=len(L)
if M<=2*K+1:
    print(N)
    exit()
X=[0 for i in range(M+1)]
for i in range(M):
    X[i+1]=L[i]+X[i]

ans=0
for i in range(0,M-2*K,2):
    
    ans=max(ans,X[i+2*K+1]-X[i])
print(ans)

Solution (Python):
N, K = map(int, input().split())
S = input()
p = 1
l = 0
a = []
for i in S:
    if int(i) != p:
        a.append(l)
        p = 1 - p
        l = 1
    else:
        l += 1
a.append(l)
a.append(0)
s = sum(a[:2 * K + 1])
ans = s
for i in range(0, len(a) - 2 * K - 1, 2):
    if i + 2 * K + 3 > len(a):
        break
    s = s - a[i] - a[i + 1] + a[i + 2 * K + 1] + a[i + 2 * K + 2]
    ans = max(ans, s)
print(ans)

Solution (Python):
from itertools import accumulate

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


def solve():
    l = []
    prev = '1'
    cnt = 0
    for c in s:
        if prev == c:
            cnt += 1
        else:
            l.append(cnt)
            cnt = 1
        prev = c
    l.append(cnt)
    if prev == '0':
        l.append(0)

    w = 2 * k + 1

    if len(l) <= w:
        return n

    acc = [0] + list(accumulate(l))
    ans = 0
    for i in range(0, len(l) - w + 1, 2):
        ans = max(ans, acc[i + w] - acc[i])
    return ans


print(solve())

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

s += 'X'

i = 0
j = 0
zero = 1 if s[0] == '0' else 0
ans = 0
while j < n:
    if zero <= k:
        ans = max(ans, j - i + 1)
        if s[j] == '1' and s[j + 1] == '0':
            zero += 1
        j += 1
    else:
        if s[i] == '0' and s[i + 1] == '1':
            zero -= 1
        i += 1

print(ans)

Solution (Python):


from collections import deque
import itertools as it
import sys
import math

sys.setrecursionlimit(1000000)

INF = 10 ** 18
MOD = 10 ** 9 + 7

N, K = map(int, raw_input().split())
S = raw_input()
ls = []
cc = '1'
num = 0
for c in S:
    if cc == c:
        num += 1
    else:
        ls += [num]
        num = 1
        cc = c
ls += [num]
if cc == '0':
    ls += [0]
S = 0
for i in range(min(len(ls), 2 * K + 1)):
    S += ls[i]
ans = S
for i in range(2 * K + 1, len(ls), 2):
    S += ls[i]
    S += ls[i + 1]
    S -= ls[i - (2 * K + 1)]
    S -= ls[i - (2 * K)]
    ans = max(ans, S)
print ans

Solution (Python):
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,copy,functools
import random

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
mod = 10**9+7
dd = [(-1,0),(0,1),(1,0),(0,-1)]
ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(): return [tuple(map(int, l.split())) for l in sys.stdin]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)

def main():
    n,k = LI()
    s = [int(c) for c in S()]
    a = []
    if s[0] == 0:
        a.append(0)
    t = s[0]
    c = 1
    for b in s[1:]:
        if t == b:
            c += 1
        else:
            a.append(c)
            c = 1
            t = b
    a.append(c)
    l = len(a)
    if l // 2 <= k:
        return sum(a)

    r = t = sum(a[:k*2+1])
    if l % 2 == 0:
        a.append(0)
        l += 1
    g = k * 2 + 1
    for i in range(k*2+1,l-1,2):
        t -= a[i-g] + a[i-g+1]
        t += a[i] + a[i+1]
        if r < t:
            r = t

    return r


print(main())

Solution (Python):
from collections import deque
N, K = map(int, input().split())
*S, = map(int, input())

C = []
prv = 1; cnt = 0
for c in S:
    if prv != c:
        C.append(cnt)
        cnt = 1
    else:
        cnt += 1
    prv = c
if cnt > 0:
    C.append(cnt)
if prv == 0:
    C.append(0)

M = len(C)//2
que = deque([C[0]])
res = ans = C[0]
for i in range(M):
    if len(que) == 2*K+1:
        res -= que.popleft() + que.popleft()
    que.append(C[2*i+1])
    que.append(C[2*i+2])
    res += C[2*i+1] + C[2*i+2]
    ans = max(res, ans)
print(ans)