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 = [int(i) for i in input().split()]
S = list(map (int, list(input())))
start = S[0]
row = []
state = [0,start]
for i in S:
    if i == start:
        state[0] += 1
    else:
        row.append(state)
        start = i
        state = [1,i]
row.append(state)
print(row)
ans = []
for i in range(max(len(row)-K*2,1)):
    if row[i][1] == 1:
        ans.append(sum([row[i][0] for i in range(i,min(K*2+i+1,len(row)))]))
    else:
        ans.append(sum([row[i][0] for i in range(i,min(K*2+i,len(row)))]))
print(max(ans))

N,K = [int(i) for i in input().split()]
S = list(map (int, list(input())))
start = S[0]
row = []
state = [0,start]
for i in S:
    if i == start:
        state[0] += 1
    else:
        row.append(state)
        start = i
        state = [1,i]
row.append(state)
ans = []
for i in range(max(len(row)-K*2+1,1)):
    if row[i][1] == 1:
        ans.append(sum([row[i][0] for i in range(i,min(K*2+i+1,len(row)))]))
if ans == []:
    for i in range(max(len(row)-K*2+1,1)):
        ans.append(sum([row[i][0] for i in range(i,min(K*2+i,len(row)))]))
print(max(ans))

N,K = map(int,input().split())
S = input()
row = []
count = 1
flag = S[0]
for i in range(1,N):
    if S[i] != flag:
        row.append([count,flag])
        count = 1
        flag = S[i]
    else:
        count += 1
row.append([count,flag])
print(row)
inst = 0
ans,temp = 0,0
left,right = 0,0
for i in range(len(row)):
    if row[i][1] == "1":
        temp += row[i][0]
    else:
        if inst < K:
            temp += row[i][0]
            inst += 1
        else:
            temp -= row[left][0] + row[left+1][0]
            temp += row[i][0]
            left += 2
    ans = max(ans,temp)
if row[0][1] == "0":
    temp += row[left-1][0]
    ans = max(ans,temp)
print(ans)

N,K = [int(i) for i in input().split()]
S = list(map (int, list(input())))
start = S[0]
row = []
state = [0,start]
for i in S:
    if i == start:
        state[0] += 1
    else:
        row.append(state)
        start = i
        state = [1,i]
row.append(state)
ans = []
for i in range(max(len(row)-K*2+1,1)):
    if row[i][1] == 1:
        ans.append(sum([row[i][0] for i in range(i,min(K*2+i+1,len(row)))]))
if ans == []:
    for i in range(max(len(row)-K*2+1,1)):
        if row[i][1] == 0:
          ans.append(sum([row[i][0] for i in range(i,min(K*2+i,len(row)))]))
print(max(ans))

N,K = map(int,input().split())
S = input()
row = []
count = 1
flag = S[0]
for i in range(1,N):
    if S[i] != flag:
        row.append([count,flag])
        count = 1
        flag = S[i]
    else:
        count += 1
row.append([count,flag])
inst = 0
ans,temp = 0,0
left,right = 0,0
for i in range(len(row)):
    if row[i][1] == "1":
        temp += row[i][0]
    else:
        inst += 1
        if inst <= K:
            temp += row[i][0]
        else:
            temp -= row[left][0]
            if row[left][1] == "1":
                temp -=row[left+1][0]
                left += 1
            temp += row[i][0]
            left += 1
    ans = max(ans,temp)
print(ans)

Verdicts: Wrong Answer

Wrong Answer

Wrong Answer

Wrong Answer

Accepted