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()

sl = []
c = 1
for i in range(n):
    if (i == 0):
        if (s[i] == "1"):
            sl.append(0)
    if (i != 0):
        if (s[i] == s[i-1]):
            c += 1
        else:
            sl.append(c)
            c = 1
sl.append(c)

cl = []
for i in range(n):
    if(k % 2 == 1):
        if (i % 2 == 0):
            cl.append(sum(sl[i - k:i + k + 1]))
    else:
        if (i % 2 == 1):
            cl.append(sum(sl[i - k:i + k + 1]))

print(max(cl))

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

sl = []
c = 1
for i in range(n):
    if (i == 0):
        if (s[i] == "1"):
            sl.append(0)
    if (i != 0):
        if (s[i] == s[i-1]):
            c += 1
        else:
            sl.append(c)
            c = 1
sl.append(c)

print(sl)
cl = []
for i in range(n):
    st = i - k
    ed = i + k + 1
    if (st < 0):
        st = 0
    if (ed > len(sl)-1):
        ed = len(sl)-1
    if (k % 2 == 1):  
        if (i % 2 == 0):
            print(st, i, ed, sum(sl[st:ed]))
            cl.append(sum(sl[st:ed]))
    else:  
        if (i % 2 == 1):
            print(st, i, ed, sum(sl[st:ed]))
            cl.append(sum(sl[st:ed]))

print(max(cl))

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

if(n != 1):
    sl = []
    c = 1
    if (s[0] == "1"):
        sl.append(0)
    for i in range(n):
        if (i != 0):
            if (s[i] == s[i-1]):
                c += 1
            else:
                sl.append(c)
                c = 1
    sl.append(c)

    print(sl)
    cl = []
    c = 0
    for i in range(n):
        st = i-k
        ed = i + k+1
        if (st < 0):
            st = 0
        if (k % 2 == 1 and i % 2 == 0 or k % 2 == 0 and i % 2 == 1):  
            if(c == 0):
                cl.append(sum(sl[st:ed]))
            else:
                t = cl[c - 1]
                if(len(sl) >= ed):
                    t += sum(sl[ed - 2:ed])
                elif (len(sl) == ed + 1):
                    t += sum(sl[ed - 2:ed-1])
                if (st > 0):
                    t -= sum(sl[st-2:st])
                cl.append(t)
            c += 1

    print(max(cl))
else:
    print(1)

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

if(n != 1):
    sl = []
    c = 1
    for i in range(n):
        if (i == 0):
            if (s[i] == "1"):
                sl.append(0)
        if (i != 0):
            if (s[i] == s[i-1]):
                c += 1
            else:
                sl.append(c)
                c = 1
    sl.append(c)

    print(sl)
    cl = []
    for i in range(n):
        st = i - k
        ed = i + k+1
        if (st < 0):
            st = 0
        if (ed > len(sl)):
            ed = len(sl)
        if (k % 2 == 1):  
            if (i % 2 == 0):
                print(st, ed, sl[st:ed])
                print(i, sl[st:ed], sum(sl[st:ed]))
                cl.append(sum(sl[st:ed]))
        else:  
            if (i % 2 == 1):
                print(st, ed, sl[st:ed])
                print(i, sl[st:ed], sum(sl[st:ed]))
                cl.append(sum(sl[st:ed]))

    print(max(cl))
else:
    print(1)

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

sl = []
c = 1
for i in range(n):
    if (i == 0):
        if (s[i] == "1"):
            sl.append(0)
    if (i != 0):
        if (s[i] == s[i-1]):
            c += 1
        else:
            sl.append(c)
            c = 1
sl.append(c)

print(sl)
cl = []
for i in range(n):
    st = i - k
    ed = i + k + 1
    if (st < 0):
        st = 0
    if (ed > len(sl)):
        ed = len(sl)
    if (k % 2 == 1):
        if (i % 2 == 0):
            print(st, i, ed, sum(sl[st:ed]))
            cl.append(sum(sl[st:ed]))
    else:
        if (i % 2 == 1):
            print(st, i, ed, sum(sl[st:ed]))
            cl.append(sum(sl[st:ed]))

print(max(cl))

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

sl = []
c = 1
if (s[0] == "1"):
    sl.append(0)
for i in range(n):
    if (i != 0):
        if (s[i] == s[i-1]):
            c += 1
        else:
            sl.append(c)
            c = 1
sl.append(c)

ruisekiwa = [0]
sums = []
ans = 0
w = 2*k+1

if (w > len(sl)):
    print(n)
    exit()
for i in range(len(sl)):
    ruisekiwa.append(ruisekiwa[i] + sl[i])
# print(sl)
# print(ruisekiwa)
for i in range(len(sl)):
    if(k % 2 == 1 and i % 2 == 0 or k % 2 == 0 and i % 2 == 1):
        left = i-k
        if (left < 0):
            left = 0
        right = i + k+1
        if (right >= len(ruisekiwa)):
            right = len(ruisekiwa)-1
        
        
        ans = max(ans, ruisekiwa[right]-ruisekiwa[left])
print(ans)

Verdicts: Wrong Answer

Wrong Answer

Wrong Answer

Wrong Answer

Wrong Answer

Accepted