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):
n, k = map(int, input().split())
s = input()

a = []
j = 0
temp = 1
prev = s[0]
for i in s[1:]:
    if i == prev:
        prev = i
        temp += 1
    else:
        a.append(temp)
        prev = i        
        temp = 1
a.append(temp)

b = []
if s[0] == "1":
    b.append(sum(a[:2*k+1]))
    for i in range(len(a) - 2*k):
        if i % 2 == 0:
            try:
                b.append(b[-1] - a[i] + a[2*k+i+1])
            except:
                continue
    print(max(b))

else:
    t = a[1:]
    b.append(sum(t[:2*k+1]))
    for i in range(len(a) - 2*k):
        if i % 2 == 0:
            try:
                b.append(b[-1] - t[i] + t[2*k+i+1])
            except:
                continue
        b.append(sum(a[:2*k]))
    print(max(b))

n, k = map(int, input().split())
s = input()
hand_list = []
for i in range(n):
    if i == 0:
        hand_list.append(i)
    else:
        if s[i] != s[i - 1]:
            hand_list.append(i)
hand_list.append(n)

ans_list = []
if len(hand_list) > 2 * k:
    for i in range(len(hand_list) - 2 * k):
        if s[i] == '1':
            if i + 2 * k + 1 < len(hand_list):
                ans = hand_list[i + 2 * k + 1] - hand_list[i]
        else:
            ans = hand_list[i + 2 * k] - hand_list[i]
            print(ans)
        ans_list.append(ans)
else:
    ans_list.append(n)
print(max(ans_list))

n, k = map(int, input().split())
s = input()
hand_list = []
for i in range(n):
    if i == 0:
        hand_list.append(i)
    else:
        if s[i] != s[i - 1]:
            hand_list.append(i)
hand_list.append(n)

ans_list = []
if len(hand_list) > 2 * k:
    for i in range(len(hand_list) - 2 * k):
        if s[i] == '1':
            if i + 2 * k + 1 < len(hand_list):
                ans = hand_list[i + 2 * k + 1] - hand_list[i]
        else:
            if i == 0:
                ans = hand_list[i + 2 * k] - hand_list[i]
            else:
                ans = hand_list[i + 2 * k] - hand_list[i - 1]
        ans_list.append(ans)
else:
    ans_list.append(n)
print(max(ans_list))

n, k = map(int, input().split())
s = input()
temp = ""
ans = []
c = 0
for s_ in s:
    if temp != s_:
        if s_ == "0":
            ans.append(c)
            temp = "0"
            c = -1
        else:
            ans.append(c)
            temp = "1"
            c = 1
    else:
        if s_ == "0":
            c -= 1
        else:
            c += 1
ans.append(c)
ans = ans[1:]
c = []
t = [i for i in range(len(ans)) if ans[i] < 0]
aaa = []
for v in itertools.combinations(t, k):
    anst = ans.copy()
    for v_ in v:
        anst[v_] *= -1
    c_ = 0
    for s_ in anst:
        if s_ > 0:
            c_ += s_
        else:
            aaa.append(c_)
            c_ = 0
    aaa.append(c_)
if len(aaa) > 0:
    print(max(aaa))
else:
    print(len(s))

n, k = map(int, input().split())
s = input()
hand_list = []
for i in range(n):
    if i == 0:
        hand_list.append(i)
    else:
        if s[i] != s[i - 1]:
            hand_list.append(i)
hand_list.append(n)

ans_list = []
if len(hand_list) > 2 * k:
    for i in range(len(hand_list) - 2 * k):
        if s[hand_list[i]] == '1':
            ans = hand_list[min(i + 2 * k + 1, len(hand_list)-1)] - hand_list[i]
        else:
            ans = hand_list[i + 2 * k] - hand_list[i]
        ans_list.append(ans)
else:
    ans_list.append(n)
print(max(ans_list))

Verdicts: Wrong Answer

Wrong Answer

Wrong Answer

Runtime Error

Accepted