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.



Sample input:
5 1
00010


Sample output:
4

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
a=input()
l=len(a)
src=[int(a[i:i+1]) for i in range(l)]
from itertools import groupby
y =[sum(1 for e in it) for _, it in groupby(src, key=lambda x: x==1)]
m=len(y)
if src[0]==1 and src[l-1]==1:
  if (m-1)/2<=k:
    print(int(n))
  else:
    c=0
    for i in range(m-2*k+1):
      if i%2==0:
        d=y[i:i+2*k+1]
        f=sum(d)
        if f>=c:
          c=f
    print(c)
if src[0]==1 and src[l-1]==0:
  if m/2<=k:
    print(int(n))
  else:
    c=0
    for i in range(m-2*k+1):
      if i%2==0:
        d=y[i:i+2*k+1]
        f=sum(d)
        if f>=c:
          c=f
    print(c)
if src[0]==0 and src[l-1]==1:
  if m/2<=k:
    print(int(n))
  else:
    c=0
    for i in range(m-2*k+1):
      if i%2==1:
        d=y[i:i+2*k+1]
        f=sum(d)
        if f>=c:
          c=f
    print(c)
if src[0]==0 and src[l-1]==0:
  if (m+1)/2<=k:
    print(int(n))
  else:
    c=0
    for i in range(m-2*k+1):
      if i%2==1:
        d=y[i:i+2*k+1]
        f=sum(d)
        if f>=c:
          c=f
    print(c)

Submission status: Wrong Answer

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

s2 = s.replace("01","0,1").replace("10","1,0").split(",")

if s[0]== "1":
  patterns = list(map(len, s2))
else:
  patterns = [0] + list(map(len, s2))

max_len = 1
#偶数番目+が１の人たち
lengths = [0] * (len(patterns)+1)
outs = [0] * (len(patterns)+1)
width = 2*k + 1
if width > len(patterns):
  print(len(s))
else:
  for i in range(len(lengths)):
    try:
      #print(i, len(patterns), len(lengths), len(outs))
      lengths[i+1] = lengths[i] + patterns[i]
    except Exception as e:
      pass
    if width <= i and i % 2 == 1:
       outs[i] = lengths[i] - lengths[i - width]
  print(patterns)
  print(lengths)
  print(outs)
  print(max(outs))

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
a=input()
l=len(a)
src=[int(a[i:i+1]) for i in range(l)]
from itertools import groupby
y =[sum(1 for e in it) for _, it in groupby(src, key=lambda x: x==1)]
m=len(y)
if src[0]==1 and src[l-1]==1:
  if (m-1)/2<=k:
    print(int(n))
  else:
    c=0
    for i in range(m-2*k+1):
      if i%2==0:
        d=y[i:i+2*k+1]
        f=sum(d)
        if f>=c:
          c=f
          print(c)
if src[0]==1 and src[l-1]==0:
  if m/2<=k:
    print(int(n))
  else:
    c=0
    for i in range(m-2*k+1):
      if i%2==0:
        d=y[i:i+2*k+1]
        f=sum(d)
        if f>=c:
          c=f
          print(c)
if src[0]==0 and src[l-1]==1:
  if m/2<=k:
    print(int(n))
  else:
    c=0
    for i in range(m-2*k+1):
      if i%2==1:
        d=y[i:i+2*k+1]
        f=sum(d)
        if f>=c:
          c=f
          print(c)
if src[0]==0 and src[l-1]==0:
  if (m-1)/2<=k:
    print(int(n))
  else:
    c=0
    for i in range(m-2*k+1):
      if i%2==1:
        d=y[i:i+2*k+1]
        f=sum(d)
        if f>=c:
          c=f
          print(c)

Submission status: Wrong Answer

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

n_sum = []

ct = 1
for i in range(N-1):
    if S[i+1] == S[i]:
        ct += 1
    else:
        n_sum.append(ct)
        ct += 1

n_sum.append(ct)

if len(n_sum) == 1:
    if S[0] == '0':
        for i in range(2*K+1):
            n_sum.append(ct)
            n_sum.insert(0, 0)
    else:
        for i in range(2*K):
            n_sum.append(0)
            n_sum.insert(0, 0)

else:
    if S[-1] == '0':
        n_sum.append(ct)
    if S[0] == '0':
        n_sum.insert(0, 0)

Max = -1
i = 0
while i+2*K < len(n_sum):
    if i == 0:
        tmp = n_sum[i+2*K]
    else:
        tmp = n_sum[i+2*K] - n_sum[i-1]
    i += 2
    Max = max(Max, tmp)

print(Max)

Submission status: Wrong Answer

Solution code:
n,k = (int(i) for i in input().split())
s = input()
change = []
leng = len(s)
flag = ''
ans = 0

for i in range(leng):
  if s[i] != flag:
    flag = s[i]
    change.append(i)

change.append(leng)
leng = len(change)

if leng <= 2*k+1:
  print(n)
  exit()

for i in range(leng):
  if s[change[i]] == '0' and i+2*k < leng:
    ans = max(ans,change[i+2*k]-change[i])    
  elif s[change[i]] == '1' and i+2*k+1 < leng:
    ans = max(ans,change[i+2*k+1]-change[i]) 
  elif i+2*k >= leng:
    break
print(ans)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
a=input()
l=len(a)
src=[int(a[i:i+1]) for i in range(l)]
from itertools import groupby
y =[sum(1 for e in it) for _, it in groupby(src, key=lambda x: x==1)]
m=len(y)
if src[0]==1 and src[l-1]==1:
  if (m-1)/2<=k:
    print(int(n))
  else:
    c=0
    for i in range(m-2*k+1):
      if i%2==0:
        d=y[i:i+2*k+1]
        f=sum(d)
        if f>=c:
          c=f
          print(c)
if src[0]==1 and src[l-1]==0:
  if m/2<=k:
    print(int(n))
  else:
    c=0
    for i in range(m-2*k+1):
      if i%2==0:
        d=y[i:i+2*k+1]
        f=sum(d)
        if f>=c:
          c=f
          print(c)
if src[0]==0 and src[l-1]==1:
  if m/2<=k:
    print(int(n))
  else:
    c=0
    for i in range(m-2*k+1):
      if i%2==1:
        d=y[i:i+2*k+1]
        f=sum(d)
        if f>=c:
          c=f
          print(c)

Submission status: Wrong Answer

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

s2 = s.replace("01","0,1").replace("10","1,0").split(",")

if s[0]== "1":
  patterns = list(map(len, s2))
else:
  patterns = [0] + list(map(len, s2))

max_len = 1
#偶数番目+が１の人たち
lengths = [0, patterns[0]] + [0] * (len(patterns) - 1)
outs = [0] * (len(patterns)+1)
width = 2*k + 1
if width > len(patterns):
  print(len(s))
else:
  for i in range(1,len(lengths)-1):
    #print(i, len(patterns), len(lengths), len(outs))
    lengths[i+1] = lengths[i] + patterns[i]
    if width <= i + 1 and i % 2 == 0:
       outs[i+1] = lengths[i+1] - lengths[i - width+1]
#  print(patterns)
#  print(lengths)
#  print(outs)
  print(max(outs))

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
a=input()
l=len(a)
src=[int(a[i:i+1]) for i in range(l)]
from itertools import groupby
y =[sum(1 for e in it) for _, it in groupby(src, key=lambda x: x==1)]
m=len(y)
if src[0]==1 and src[l-1]==1:
  if (m-1)/2<=k:
    print(int(n))
  else:
    c=0
    for i in range(m-2*k+1):
      if i%2==0:
        d=y[i:i+2*k+1]
        f=sum(d)
        if f>=c:
          c=f
          print(c)
if src[0]==1 and src[l-1]==0:
  if m/2<=k:
    print(int(n))
  else:
    c=0
    for i in range(m-2*k+1):
      if i%2==0:
        d=y[i:i+2*k+1]
        f=sum(d)
        if f>=c:
          c=f
          print(c)

Submission status: Wrong Answer

Solution code:
n,k = (int(i) for i in input().split())
s = input()
change = []
leng = len(s)
flag = ''
ans = 0

for i in range(leng):
  if s[i] != flag:
    flag = s[i]
    change.append(i)

if leng <= 2*k:
  print(n)
  exit()

change.append(leng)
leng = len(change)

for i in range(leng):
  if s[change[i]] == '0' and i+2*k < leng:
    ans = max(ans,change[i+2*k]-change[i])    
  elif s[change[i]] == '1' and i+2*k+1 < leng:
    ans = max(ans,change[i+2*k+1]-change[i]) 
  elif i+2*k >= leng:
    break
print(ans)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
a=input()
l=len(a)
src=[int(a[i:i+1]) for i in range(l)]
from itertools import groupby
y =[sum(1 for e in it) for _, it in groupby(src, key=lambda x: x==1)]
m=len(y)
if src[0]==1 and src[l-1]==1:
  if (m-1)/2<=k:
    print(int(n))
  else:
    c=0
    for i in range(m-2*k+1):
      if i%2==0:
        d=y[i:i+2*k+1]
        f=sum(d)
        if f>=c:
          c=f
          print(c)
if src[0]==0 and src[l-1]==1:
  if m/2<=k:
    print(int(n))
  else:
    c=0
    for i in range(m-2*k+1):
      if i%2==1:
        d=y[i:i+2*k+1]
        f=sum(d)
        if f>=c:
          c=f
          print(c)

Submission status: Wrong Answer

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

def press(array):
    pressed = []
    cnts = []
    remark = array[0]
    cnt = 0
    for i in range(len(array)):
        cnt += 1
        if i == N-1:
            pressed.append(remark)
            cnts.append(cnt)
        elif S[i] != S[i+1]:
            pressed.append(remark)
            cnts.append(cnt)
            remark = S[i+1]
            cnt = 0
    return pressed,cnts

pressed, cnts = press(list(S))
if pressed[0] == "0":
    pressed = ["1"] + pressed
    cnts = [0] + cnts
if pressed[-1] == "0":
    pressed = pressed + ["1"]
    cnts = cnts + [0]

acc = [0]*(len(cnts)+1)
for i in range(len(cnts)):
    acc[i+1] = acc[i] + cnts[i]

ans = -1
for i in range(0,len(cnts),2):
    ans = max(ans,cnts[i])

for l in range(0,len(cnts),2):
    r = l + 2*K
    if r+1 > len(acc)-1:
        break
    res = acc[r+1] - acc[l]
    ans = max(ans,res)

print(ans)

Submission status: Wrong Answer

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

n_sum = []
if S[0] == '0':
    n_sum.append(0)
ct = 1
for i in range(N-1):
    if S[i+1] == S[i]:
        ct += 1
    else:
        n_sum.append(ct)
        ct += 1


n_sum.append(ct)

if S[-1] == '0':
    n_sum.append(ct)

if len(n_sum) == 1:
    n_sum.append(0)
    n_sum.append(0)
    n_sum.insert(0, 0)
    n_sum.insert(0, 0)

Max = -1
i = 0
while i+2*K < len(n_sum):
    if i == 0:
        tmp = n_sum[i+2*K]
    else:
        tmp = n_sum[i+2*K] - n_sum[i-1]
    i += 2
    Max = max(Max, tmp)

print(Max)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
a=input()
l=len(a)
src=[int(a[i:i+1]) for i in range(l)]
from itertools import groupby
y =[sum(1 for e in it) for _, it in groupby(src, key=lambda x: x==1)]
m=len(y)
if src[0]==1 and src[l-1]==1:
  if (m-1)/2<=k:
    print(int(n))
  else:
    c=0
    for i in range(m-2*k+1):
      if i%2==0:
        d=y[i:i+2*k+1]
        f=sum(d)
        if f>=c:
          c=f
          print(c)

Submission status: Wrong Answer

Solution code:
n,k = (int(i) for i in input().split())
s = input()
change = []
leng = len(s)
flag = ''
ans = 0

for i in range(leng):
  if s[i] != flag:
    flag = s[i]
    change.append(i)

change.append(leng)
leng = len(change)

if leng < 2*k+1:
  print(n)
  exit()

for i in range(leng):
  if s[change[i]] == '0' and i+2*k < leng:
    ans = max(ans,change[i+2*k]-change[i])    
  elif s[change[i]] == '1' and i+2*k+1 < leng:
    ans = max(ans,change[i+2*k+1]-change[i]) 
  elif i+2*k >= leng:
    break
print(ans)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
s=list(input())
s=list(map(int,s))
stack=[]
tmp=1
flag=True
for i in range(1,len(s)):
  if s[i]!=s[i-1]:
    stack.append(tmp)
    tmp=1
  else:
    tmp+=1
stack.append(tmp)
left=[0]
right=[0]
tmp=0
if s[0]==0:
  flag=False
  if len(stack)%2==1:
    stack.append(0)
  if len(stack)//2<=k:
    print(len(s))
    exit()
else:
  flag=True
  if len(stack)%2==0:
    stack.append(0)
  if len(stack)//2<=k:
    print(len(s))
    exit()
for i in range(len(stack)):
  tmp+=stack[i]
  if i%2==0:
    if flag:
      right.append(tmp)
    else:
      left.append(tmp)
  else:
    if flag:
      left.append(tmp)
    else:
      right.append(tmp)
ans=0
for i in range(len(right)-k):
  ans=max(ans,right[i+k]-left[i])
print(ans)

Submission status: Wrong Answer

Solution code:
def main():
    N, K = map(int, input().split())
    S = input()
    # １で始まるグループでK=1を想定する
    # [1，0，1]だとすると、K=1で2K+1で1グループは表せる
    c_num = "1"  # 必ず1でスタートする形とする。0で始まれば、0個の1を頭に入れる
    cnt = 0
    seq = []

    for i in S:
        if i == c_num:
            cnt += 1
        else:
            c_num = i
            seq.append(cnt)
            cnt = 1
    seq.append(cnt)
    # print(seq)

    leng_seq = len(seq)
    # print(leng_seq)
    if leng_seq % 2 == 0:  # "1"から始まる順番で、配列が偶数ということは0で終わってる=>ケツに0個の1をブチコム
        seq.append(0)
    # 全部１になるケース
    if leng_seq <= 2*K+1:  # 当該の要素含め前後消えるから2K+1
        print(sum(seq))
        return

    answer = 0
    total = 0
    stpr = 0

    for j in range(leng_seq-2*K):
        while stpr < j+2*K+1:
            total += seq[stpr]
            stpr += 1
        if j % 2 == 0:  # 1からスタートするものだけ2k+1分を抽出
            answer = max(answer, total)
        total -= seq[j]

    print(answer)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
def main():
    N, K = map(int, input().split())
    S = input()
    # １で始まるグループでK=1を想定する
    # [1，0，1]だとすると、K=1で2K+1で1グループは表せる
    c_num = "1"  # 必ず1でスタートする形とする。0で始まれば、0個の1を頭に入れる
    cnt = 0
    seq = []

    for i in S:
        if i == c_num:
            cnt += 1
        else:
            c_num = i
            seq.append(cnt)
            cnt = 1
    seq.append(cnt)
    # print(seq)

    leng_seq = len(seq)
    # print(leng_seq)
    if leng_seq % 2 == 0:  # "1"から始まる順番で、配列が偶数ということは0で終わってる=>ケツに0個の1をブチコム
        seq.append(0)
    # 全部１になるケース
    if leng_seq <= 2*K+1:  # 当該の要素含め前後消えるから2K+1
        print(sum(seq))
        exit()

    answer = 0
    total = 0
    stpr = 0

    for j in range(leng_seq-2*K):
        while stpr < j+2*K+1:
            total += seq[stpr]
            stpr += 1
        if j % 2 == 0:  # 1からスタートするものだけ2k+1分を抽出
            answer = max(answer, total)
        total -= seq[j]

    print(answer)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
S = input()
# １で始まるグループでK=1を想定する
# [1，0，1]だとすると、K=1で2K+1で1グループは表せる
c_num = "1"  # 必ず1でスタートする形とする。0で始まれば、0個の1を頭に入れる
cnt = 0
seq = []

for i in S:
    if i == c_num:
        cnt += 1
    else:
        c_num = i
        seq.append(cnt)
        cnt = 1
seq.append(cnt)
# print(seq)

leng_seq = len(seq)
# print(leng_seq)
if leng_seq % 2 == 0:  # "1"から始まる順番で、配列が偶数ということは0で終わってる=>ケツに0個の1をブチコム
    seq.append(0)
# 全部１になるケース
if leng_seq <= 2*K+1:  # 当該の要素含め前後消えるから2K+1
    print(sum(seq))
    exit()

answer = 0
total = 0
stpr = 0

for j in range(leng_seq-2*K):
    while stpr < j+2*K+1:
        total += seq[stpr]
        stpr += 1
    if j % 2 == 0:  # 1からスタートするものだけ2k+1分を抽出
        answer = max(answer, total)
    total -= seq[j]

print(answer)

Submission status: Wrong Answer

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

ct = 1
num = []
if S[0] == '0':
    num.append(0)
for i in range(1, N):
    if S[i] == S[i-1]:
        ct += 1
    else:
        num.append(ct)
        ct = 1

num.append(ct)

if len(num) % 2 == 0:
    num.append(0)

if len(num) != 1:
    Max = -1
    for i in range(0, len(num)-2*K, 2):
        if i == 0:
            tmp = sum(num[:2*K+1])
        else:
            tmp -= sum(num[i-2:i])
            tmp += sum(num[i-2+2*K+1:i+2*K+1])
        Max = max(Max, tmp)
else:
    Max = num[0]

print(Max)

Submission status: Wrong Answer

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

c_num = "1" #必ず1でスタートする形とする。0で始まれば、0個の1を頭に入れる
cnt = 0
seq = []

for i in S:
    if i == c_num:
        cnt += 1
    else:
        c_num = i
        seq.append(cnt)
        cnt = 1
seq.append(cnt)
# print(seq)

leng_seq = len(seq)
# print(leng_seq)
if leng_seq%2 == 0: # "1"から始まる順番で、配列が偶数ということは0で終わってる=>ケツに0個の1をブチコム
    seq.append(0)

if leng_seq <= 2*K+1:  # 当該の要素含め前後消えるから2K+1
    print(N)
    exit()

answer = 0
total = 0
stpr = 0

for j in range(leng_seq-2*K):
    while stpr < j+2*K+1:
        total += seq[stpr]
        stpr += 1
    if j%2 == 0: #1からスタートするものだけ2k+1分を抽出
        answer = max(answer, total)
    total -= seq[j]

print(answer)

Submission status: Wrong Answer

Solution code:
def solve():
    N,K=map(int,input().split())
    S=input()
    s2=[]
    cp='_'
    cn=0
    count={'0':0, '1':0}
    for c in S:
        if cp != c:
            if cp != '_':
                s2.append(cn)
                count[cp]+=1
            cp = c
            cn = 0
        cn += 1
    s2.append(cn)
    count[cp]+=1

    csum=[0,0,0,0,0]
    csumval=0
    for v in s2:
        csumval+=v
        csum.append(csumval)

    csum+=[csumval,csumval,csumval,csumval,csumval]
    s2_len = len(s2)
    #print(s2,s2_len)
    #print(csum)
    csumlen=len(csum)

    if count['0'] <= K:
        return csum[-1]

    #print('csumlen =',csumlen)
    ans=0
    if S[0]=='1':
        for i in range(0,s2_len//2+3,2):
            if 5+i+K*2>=csumlen:
                ans1=csum[csumlen-1]-csum[5+i-1]
            else:
                ans1=csum[5+i+K*2]-csum[5+i-1]
            #print(ans1,i,i+K*2)
            ans=max(ans,ans1)
    else:
        for i in range(-1,s2_len//2+3,2):
            if 5+i+K*2>=csumlen:
                ans1=csum[csumlen-1]-csum[5+i-1]
            else:
                ans1=csum[5+i+K*2]-csum[5+i-1]
            #print(ans1,i,i+K*2)
            ans=max(ans,ans1)
    return ans

print(solve())

Submission status: Wrong Answer

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

if "0" not in S:
	print len(S)
	quit()
L=[]
if S[0]=="0": L.append(0)
for i,x in enumerate(S):
	if i==0:
		cnt=1
	else:
		if prev==x:
			cnt+=1
		else:
			L.append(cnt)
			cnt=1
	prev=x
else:
	L.append(cnt)

if S[-1]=="0": L.append(0)

LSUM=[0]
for x in L:
	LSUM.append(LSUM[-1]+x)

ans=0
len_lsum=len(LSUM)
for l in range(0,len_lsum,2):
	r=l+2*K+1
	if r < len_lsum:
		ans=max(ans , LSUM[r]-LSUM[l])

print ans

Submission status: Wrong Answer

Solution code:
import itertools

n,k = map(int, input().split())
s_str = input()
s = []
for i in s_str:
    s.append(int(i))

s_encoded = []
now = 0
tmp = 0
for i in range(len(s)):
    if i == 0:
        if s[0] == 1:
            s_encoded.append(0)
            now = 1
        else:
            pass
    
    if s[i]==now:
        tmp=tmp+1
    else:
        s_encoded.append(tmp)
        tmp=1
        now = 1 - now
                
    if i==len(s)-1:
        s_encoded.append(tmp)
        
sum_before = []
sum_tmp=0
for i in range(len(s_encoded)):
    sum_tmp = sum_tmp+s_encoded[i]
    sum_before.append(sum_tmp)
max_line=s_encoded[0]
left = 0
if left%2==0:
    right = min(left + 2*(k-1) +1, len(sum_before)-1)
else:
    right = min(left + 2*k, len(sum_before)-1)
right = min(left + 2*(k-1) +1, len(sum_before)-1)
while left < len(sum_before):
    line = sum_before[right] - sum_before[left-1]
    if max_line<line:
        max_line=line
    left = left + 1
    if left%2==0:
        right = min(left + 2*(k-1) +1, len(sum_before)-1)
    else:
        right = min(left + 2*k, len(sum_before)-1)
print(max_line)

Submission status: Wrong Answer

Solution code:
import itertools

n,k = map(int, input().split())
s_str = input()
s = []
for i in s_str:
    s.append(int(i))

s_encoded = []
now = 0
tmp = 0
for i in range(len(s)):
    if i == 0:
        if s[0] == 1:
            s_encoded.append(0)
            now = 1
        else:
            pass
    
    if s[i]==now:
        tmp=tmp+1
    else:
        s_encoded.append(tmp)
        tmp=1
        now = 1 - now
                
    if i==len(s)-1:
        s_encoded.append(tmp)
        
sum_before = []
sum_tmp=0
for i in range(len(s_encoded)):
    sum_tmp = sum_tmp+s_encoded[i]
    sum_before.append(sum_tmp)
max_line=s_encoded[0]
left = 1
right = min(left + 2*k, len(sum_before)-1)
while left < len(sum_before):
    line = sum_before[right] - sum_before[left-1]
    if max_line<line:
        max_line=line
    left = left + 2
    right = min(left + 2*k, len(sum_before)-1)
print(max_line)

Submission status: Wrong Answer

Solution code:
import itertools

n,k = map(int, input().split())
s_str = input()
s = []
for i in s_str:
    s.append(int(i))

s_encoded = []
now = 0
tmp = 0
for i in range(len(s)):
    if i == 0:
        if s[0] == 1:
            s_encoded.append(0)
            now = 1
        else:
            pass
    
    if s[i]==now:
        tmp=tmp+1
    else:
        s_encoded.append(tmp)
        tmp=1
        now = 1 - now
                
    if i==len(s)-1:
        s_encoded.append(tmp)
        
sum_before = []
sum_tmp=0
for i in range(len(s_encoded)):
    sum_tmp = sum_tmp+s_encoded[i]
    sum_before.append(sum_tmp)
max_line=s_encoded[0]
left = 0
if left%2==0:
    right = left + 2*(k-1)+1
else:
    right = left + 2*k
while right<=len(sum_before)-1:
    line = sum_before[right] - sum_before[left-1]
    if max_line<line:
        max_line=line
    left = left + 1
    if left%2==0:
        right = left + 2*(k-1)+1
    else:
        right = left + 2*k
print(max_line)

Submission status: Wrong Answer

Solution code:
import itertools

n,k = map(int, input().split())
s_str = input()
s = []
for i in s_str:
    s.append(int(i))

s_encoded = []
now = 0
tmp = 0
for i in range(len(s)):
    if i == 0:
        if s[0] == 1:
            s_encoded.append(0)
            now = 1
        else:
            pass
    
    if s[i]==now:
        tmp=tmp+1
    else:
        s_encoded.append(tmp)
        tmp=1
        now = 1 - now
                
    if i==len(s)-1:
        s_encoded.append(tmp)
        
sum_before = []
sum_tmp=0
for i in range(len(s_encoded)):
    sum_tmp = sum_tmp+s_encoded[i]
    sum_before.append(sum_tmp)
max_line=1
left = 0
if left%2==0:
    right = left + 2*(k-1)+1
else:
    right = left + 2*k
while right<=len(sum_before)-1:
    line = sum_before[right] - sum_before[left-1]
    if max_line<line:
        max_line=line
    left = left + 1
    if left%2==0:
        right = left + 2*(k-1)+1
    else:
        right = left + 2*k
print(max_line)

Submission status: Wrong Answer

Solution code:
import itertools

n,k = map(int, input().split())
s_str = input()
s = []
for i in s_str:
    s.append(int(i))

s_encoded = []
now = 0
tmp = 0
for i in range(len(s)):
    if i == 0:
        if s[0] == 1:
            s_encoded.append(0)
            now = 1
        else:
            pass
    
    if s[i]==now:
        tmp=tmp+1
    else:
        s_encoded.append(tmp)
        tmp=1
        now = 1 - now
                
    if i==len(s)-1:
        s_encoded.append(tmp)
        
sum_before = []
sum_tmp=0
for i in range(len(s_encoded)):
    sum_tmp = sum_tmp+s_encoded[i]
    sum_before.append(sum_tmp)
print(s_encoded)
print(sum_before)
max_line=1
left = 0
if left%2==0:
    right = left + 2*(k-1)+1
else:
    right = left + 2*k
while right<=len(sum_before)-1:
    line = sum_before[right] - sum_before[left-1]
    if max_line<line:
        max_line=line
    left = left + 1
    if left%2==0:
        right = left + 2*(k-1)+1
    else:
        right = left + 2*k
print(max_line)

Submission status: Wrong Answer

Solution code:

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

ones = []
t = 0
for i in range(N):

    if i == 0:
        t += 1
        pass
    elif (S[i - 1] != S[i]):
        ones.append(t)
        t = 1
    else:
        t += 1
ones.append(t)

if S[0] == '0':
    t = ones
    ones = [0]
    ones.extend(t)

res = 0

for k in range(2 * K + 1):
    if k >= len(ones):
        break
    res += ones[k]
temp = ones[0]

if k > 1:
    temp += ones[1]

for k in range(2 * K + 1, len(ones), 2):

    temp_k = 0
    if k < len(ones):
        temp_k += ones[k]
    if (k + 1) < len(ones):
        temp_k += ones[k + 1]

    temp_res = res - temp + temp_k
    temp = temp_k
    if res < temp_res:
        res = temp_res

print(res)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
S = input()
S = S + "2"
index = [0]
flag = S[0]
for i in range(N+1):
    if flag != S[i]:
        index.append(i)
        flag = S[i]

ans = 0
diff = int(S[0])
idx = 0
if idx + 2 * K + diff >= len(index):
    ans = max(ans, N)

while idx + 2 * K + diff < len(index):
    ans = max(ans, index[idx + 2 * K + diff] - index[idx])
    idx += 1
    
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
stdin = sys.stdin 
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline()
N,K=na();S=ns()
l=[]
N=len(S)
if(S[0]=="0"):
    l.append(0)
b=S[0]
ct=1
for i in S[1:]:
    if(b!=i):
        l.append(ct)
        ct=1
        b=i
    else:
        ct+=1
l.append(ct)

len_l=len(l)
if(len_l//2 <= K):
    print(sum(l))
    exit()
res = sum(l[:2*K+1])
ans=res
if(len_l%2==0):
    l.append(0)
    len_l+=1
g = K * 2 + 1
for i in range(K*2+1,len_l-1,2):
    ans -= l[i-g] + l[i+1-g]
    ans += l[i] + l[1+i]
    res = max(ans,res)
#print(l)
print(res)

Submission status: Wrong Answer

Solution code:
import sys
stdin = sys.stdin 
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline()
N,K=na();S=na()
l=[]
N=len(S)
if(S[0]==0):
    l.append(0)
b=S[0]
ct=1
for i in S[1:]:
    if(b!=i):
        l.append(ct)
        ct=1
        b=i
    else:
        ct+=1
l.append(ct)

len_l=len(l)
if(len_l//2 <= K):
    print(sum(l))
    exit()

if(len_l%2==0):
    l.append(0)
    len_l+=1
res = sum(l[:2*K+1])
ans=res
for i in range(K*2+1,len_l-1,2):
    ans -= l[i-(2*K+1)] + l[i+1-(2*K+1)]
    ans += l[i] + l[1+i]
    res = max(ans,res)
#print(l)
print(res)

Submission status: Wrong Answer

Solution code:
import sys
stdin = sys.stdin 
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline()
N,K=na();S=na()
l=[]
N=len(S)
if(S[0]=="0"):
    l.append(0)
b=S[0]
ct=1
for i in S[1:]:
    if(b!=i):
        l.append(ct)
        ct=1
        b=i
    else:
        ct+=1
l.append(ct)

len_l=len(l)
if(len_l//2 <= K):
    print(sum(l))
    exit()

if(len_l%2==0):
    l.append(0)
    len_l+=1
res = sum(l[:2*K+1])
ans=res
for i in range(K*2+1,len_l-1,2):
    ans -= l[i-(2*K+1)] + l[i+1-(2*K+1)]
    ans += l[i] + l[1+i]
    res = max(ans,res)
#print(l)
print(res)

Submission status: Wrong Answer

Solution code:
import sys
stdin = sys.stdin 
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline()
N,K=na();S=ns()
l=[]
N=len(S)
if(S[0]=="0"):
    l.append(0)
b=S[0]
ct=1
for i in S[1:]:
    if(b!=i):
        l.append(ct)
        ct=1
        b=i
    else:
        ct+=1
l.append(ct)

len_l=len(l)
if(N//2 < K):
    print(sum(l))
    exit()

if(len_l%2==0):
    l.append(0)
    len_l+=1
res = sum(l[:2*K+1])
ans=res
for i in range(K*2+1,len_l-1,2):
    ans -= l[i-(2*K+1)] + l[i+1-(2*K+1)]
    ans += l[i] + l[1+i]
    res = max(ans,res)
#print(l)
print(res)

Submission status: Wrong Answer

Solution code:
import sys
stdin = sys.stdin 
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline()
N,K=na();S=ns()
l=[]
N=len(S)
if(S[0]=="0"):
    l.append(0)
b=S[0]
ct=1
for i in S[1:]:
    if(b!=i):
        l.append(ct)
        ct=1
        b=i
    else:
        ct+=1

if(ct):
    l.append(ct)
len_l=len(l)
if(N//2 < K):
    print(N)
    exit()

if(len_l%2==0):
    l.append(0)
    len_l+=1
res = sum(l[:2*K+1])
ans=res
for i in range(0,len_l-(2*K+1),2):
    ans -= l[i] + l[i+1]
    ans += l[2*K+1+i] + l[2*K+2+i]
    res = max(ans,res)
#print(l)
print(res)

Submission status: Wrong Answer

Solution code:
N_list= list(map(int, input().split()))
if N_list[0]<=N_list[1] :
    print(N_list[0])
else:
    suji = str(input())
    suji_1 = [int(x) for x in list(suji)]
    suji_2 = []
    for i in range(len(suji_1)):
        suji_2.append( suji_1[len(suji_1)-i-1])

    counter1 =0
    counter2 =0

    while True:
        if suji_1 != []:
            if suji_1[0] ==1:
                del(suji_1[0])
                counter1 +=1
            else:
                break
        else:
            break
    while True:
        if suji_2 != []:
            if suji_2[0] == 1:
                del (suji_2[0])
                counter2 +=1
            else:
                break
        else:
            break


    if not suji_1:
        print(counter1)
    else:
        siji_1 = 0
        while siji_1 < N_list[1] :
            siji_1+=1
            counter_0 = 0

            for _ in range(len(suji_1)):
                if suji_1[_]==0:
                    counter_0 +=1
                else:
                    break
            del(suji_1[:counter_0])
            counter1 += counter_0

            while True:
                if suji_1 != []:
                    if suji_1[0] == 1:
                        del (suji_1[0])
                        counter1 += 1
                    else:
                        break
                else:
                    break
            if not suji_1:
                break

        siji_2 = 0
        while siji_2 < N_list[1]:
            siji_2 += 1
            counter_0 = 0

            for _ in range(len(suji_2)):
                if suji_2[_] == 0:
                    counter_0 += 1
                else:
                    break
            del (suji_2[:counter_0])
            counter2 += counter_0

            while True:
                if suji_2 != []:
                    if suji_2[0] == 1:
                        del (suji_2[0])
                        counter2 += 1
                    else:
                        break
                else:
                    break

            if not suji_2:
                break

        if counter1 >= counter2:
            print(counter1)
        else:
            print(counter2)