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):
import sys

def i2s():
    return sys.stdin.readline().rstrip()

def ii2ss(n):
    return [sys.stdin.readline() for _ in range(n)]

def sp2nn(sp, sep=' '):
    return [int(s) for s in sp.split(sep)]

def ss2nn(ss):
    return [int(s) for s in list(ss)]

def analyze(S):
    data = []
    i0 = 0
    s0 = S[0]
    sc = 0
    scmax = 0
    for i, s in enumerate(S):
        if s == s0:
            sc += 1
        else:
            data.append((i0, s0, sc))
            if s0 == '1' and sc > scmax:
                scmax = sc
            i0 = i
            s0 = s
            sc = 1
    data.append((i0, s0, sc))
    if s0 == '1' and sc > scmax:
        scmax = sc
    return data, scmax

def rev(S, l, r):
    r += 1
    s0 = S[0:l]
    s1 = S[l:r]
    s2 = S[r:]
    s1r = ''.join(['1' if s == '0' else '0' for s in s1])
    return s0 + s1r + s2

def main(ss):
    N, K = sp2nn(ss[0])
    S = ss[1]
    smax = -1
    
    for _ in range(K):
        data, smax = analyze(S)
        print(data, smax)
        il0 = -1
        ir0 = -1
        S_max = S
        for il in range(len(data)):
            for ir in range(len(data)):
                S_ = rev(S, data[il][0], data[ir][0]+data[ir][2])
                _, sc = analyze(S_)
                #print(S_, sc)
                if smax < sc:
                    il0 = il
                    ir0 = ir
                    smax = sc
                    S_max = S_
        if il0 == -1:
            break
        S = S_max
        
    
    print(smax)

main(ii2ss(2))

import sys

def i2s():
    return sys.stdin.readline().rstrip()

def ii2ss(n):
    return [sys.stdin.readline() for _ in range(n)]

def sp2nn(sp, sep=' '):
    return [int(s) for s in sp.split(sep)]

def ss2nn(ss):
    return [int(s) for s in list(ss)]

def analyze(S):
    data = []
    i0 = 0
    s0 = S[0]
    sc = 0
    scmax = 0
    for i, s in enumerate(S):
        if s == s0:
            sc += 1
        else:
            data.append((i0, s0, sc))
            if s0 == '1' and sc > scmax:
                scmax = sc
            i0 = i
            s0 = s
            sc = 1
    data.append((i0, s0, sc))
    if s0 == '1' and sc > scmax:
        scmax = sc
    return data, scmax

def rev(S, l, r):
    r += 1
    s0 = S[0:l]
    s1 = S[l:r]
    s2 = S[r:]
    s1r = ''.join(['1' if s == '0' else '0' for s in s1])
    return s0 + s1r + s2

def main(ss):
    N, K = sp2nn(ss[0])
    S = ss[1]
    smax = -1
    
    for _ in range(K):
        data, smax = analyze(S)
        #print(data, smax)
        il0 = -1
        ir0 = -1
        S_max = S
        for il in range(len(data)):
            for ir in range(len(data)):
                S_ = rev(S, data[il][0], data[ir][0]+data[ir][2])
                _, sc = analyze(S_)
                #print(S_, sc)
                if smax < sc:
                    il0 = il
                    ir0 = ir
                    smax = sc
                    S_max = S_
        if il0 == -1:
            break
        S = S_max
        
    
    print(smax)

main(ii2ss(2))



import sys

s2nn = lambda s: [int(c) for c in s.split(' ')]
ss2nn = lambda ss: [int(s) for s in list(ss)]
ss2nnn = lambda ss: [s2nn(s) for s in list(ss)]
i2s = lambda: sys.stdin.readline().rstrip()
i2n = lambda: int(i2s())
i2nn = lambda: s2nn(i2s())
ii2ss = lambda n: [sys.stdin.readline() for _ in range(n)]
ii2nnn = lambda n: ss2nnn(ii2ss(n))

def grouping(N, S):
    group = []
    s = S[0]
    c = 1
    for i in range(1, N):
        if S[i] == s:
            c += 1
        else:
            group.append((s, c))
            s = S[i]
            c = 1
    group.append((s, c))
    return group

def main(N, K, S):
    g = grouping(N, S)
    glen = len(g)
    
    cs = [0] * (glen + 1)
    for i in range(glen):
        cs[i+1] = cs[i] + g[i][1]
    nm = 0
    for i in range(glen):
        if g[i][0] == '0':
            n = cs[min(i + 2*K, glen)] - cs[i]
        else:
            n = cs[min(i + 2*K+1, glen)] - cs[i]
        if n > nm:
            nm = n
    print(nm)

N, K = i2nn()
S = i2s()
main(N, K, S)


Verdicts: Wrong Answer

Wrong Answer

Accepted