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

S = input()
para = [0]
for i, s in enumerate(S):
    if i == 0:
        para.append(1)
    else:
        if S[i] == S[i-1]:
            para[-1] += 1
        else:
            para.append(1+para[-1])


i = 0

tmpmax = 0

if S[0] == '1':
    if i + 2*k + 1 >= len(para) - 1:
        print(n)
    else:
        while i + 2*k + 1 <= len(para) - 1:
            j = i + 2*k + 1
            if para[j] - para[i-1] > tmpmax:
                tmpmax = para[j] - para[i]

            i += 2
        print(tmpmax)
else:
    if i + 2*k >= len(para) - 1:
        print(n)
    else:
        while i + 2*k <= len(para) - 1:
            j = i + 2*k
            if i == 0:
                if para[j] - para[i] > tmpmax:
                    tmpmax = para[j] - para[i]
            else:
                if para[j] - para[i-1] > tmpmax:
                    tmpmax = para[j] - para[i-1]

            i += 2
        print(tmpmax)

Submission status: Wrong Answer

Solution code:
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(il0, ir0, S_max)
    
    print(smax)

main(ii2ss(2))

Submission status: Wrong Answer

Solution code:
import numpy as np 

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

ls1 = []
ls2 = []

tmp1 = 0
tmp2 = 0
cm1 = False
cm2 = True
for ss in s:
    if ss=="0":
        cm1 = True
        if cm2:
            ls2.append(tmp2)
            tmp2 = 0
            cm2 = False
        tmp2 += 1
    else:
        cm2 = True
        if cm1:
            ls1.append(tmp1)
            tmp1 = 0
            cm1 = False
        tmp1 += 1
ls1.append(tmp1)
ls2.append(tmp2)

# print(ls1)
# print(ls2)

ls1l = len(ls1)
suml = []
for i in range(ls1l-K):
    # print("ls1", ls1[i:i+K+1])
    # print("ls2", ls2[i+1:i+K+1])
    suml.append(sum(ls1[i:i+K+1]) + sum(ls2[i+1:i+K+1]))

if suml == []:
    print(len(s))
else:
    print(max(suml))

Submission status: Wrong Answer

Solution code:
print '-1'

Submission status: Wrong Answer

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

S=input()

#0の位置がK回きたら次の1がきた時点まででカウント終了
#そこからまたカウントスタートするけど前回の終了いちを忘れるな

# max_len = 0
# now_len = 0
# count0 = 0
# last0_ind = -1
# for i in range(N):
#     now_len += 1
#     if S[i]==1:
#         continue
#     else:
#         count0+=1
#     if count0 == K:
#         last0_ind = i
#     elif count0 == K+1:
#         max_len = max(max_len,now_len-1)
#         now_len = 
    
# max_len = max(max_len,now_len)

ind_0 = []
max_len = 0
left = 0
right = 0
flag = False
left_flag = 0
if S.count('0')==0:
    max_len = N
    flag = True

for i in range(1,len(S)):
    if(flag==True): 
        max_len = N
        break
    if S[i]=='1':
            continue
    if i == len(S)-1:
        if S[i]=='1':
            continue
        else:
            right = i
            if (left_flag==0):
                ind_0.append([right,right])
            else:
                ind_0.append([left,right])

            continue
    if S[i]=='0' and S[i-1]!='0' and S[i+1]!='0':
        ind_0.append([i,i])
    elif S[i-1]!='0' and S[i+1]=='0':
        left = i
        left_flag = 1
    elif S[i-1]=='0' and S[i+1]!='0':
        right = i
        ind_0.append([left,right])
        left_flag = 0
    else:
        continue


# print(ind_0)



#Kがlenより大きい時の例外

# if 
for i in range(len(ind_0)):
    if(flag==True): break
    if i+K>len(ind_0)-1:
        # print('1',max(N-ind_0[i-1][1]-1,max_len))
        max_len =max(N-ind_0[i-1][1]-1,max_len)
    elif i == 0:
        # print('2',ind_0[K][0])
        max_len = ind_0[K][0]
    else:
        # print('3',abs(ind_0[i-1][1]+1-ind_0[i+K][0]))
        max_len = max(ind_0[i-1][1]+1-ind_0[i+K][0]-1+1,max_len)

print(max_len)

Submission status: Wrong Answer

Solution code:
import numpy as np

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

l = [list(np.repeat(0, K+1)), list(np.repeat(0, K+1))]
ans = []
k = [int(s) for s in S]
v0 = 1 if k[0] == 0 else 0
v1 = 1 if k[0] == 1 else 0
a = sum(l[0][-K:]+l[1][-K-1:])
for i in range(1, N):
    if (k[i]==0) & (k[i-1]==0):
        v0 += 1
    elif (k[i]==1) & (k[i-1]==0):
        l[0].append(v0)
        v0 = 0
        v1 += 1
    elif (k[i]==0) & (k[i-1]==1):
        l[1].append(v1)
        v0 += 1
        
        a -= l[0][-K-1] + l[1][-K-2]
        a += v1 + l[0][-1]
        v1 = 0
        ans.append(a)    
    else:
        v1 += 1
        
for i, v in enumerate([v0, v1]):
    if v > 0:
        l[i].append(v)

if len(l[0])-K-1 <= K:
    print(N)

else:
    if (v1>0) & (v0==0):
        a -= l[0][-K-1] + l[1][-K-2]
        a += v1 + l[0][-1]
        ans.append(a)
    else:
        a -= l[0][-K-1] + l[1][-K-2]
        a += v0
    print(max(ans))

Submission status: Wrong Answer

Solution code:
import sys
from itertools import accumulate
N, K  = map(int, input().split())
ans = 0
num = 0
S = input()
a = S.split('1')
b = S.split('0')
ls = []
for x in a:
    if len(x) != 0:
        ls.append(len(x))
c = [0] + list(accumulate(ls))
ls_ = []
for x in b:
    if len(x) != 0:
        ls_.append(len(x))
d = [0] + list(accumulate(ls_))

if len(c)-1 <= K:
    print(N)
    sys.exit()

if len(c) == len(d):
    if S[0] == '0':
        q = c[-1]
        c.append(q)
        r = d[-1]
        d.append(r)
        for x, y, u, t in zip(c, c[K+1:], d, d[K+1:]):
            ans = max(ans, y-x+t-u)
        print(ans)
    elif S[0] == '1':
        q = c[-1]
        c.append(q)
        r = d[-1]
        d.append(r)
        for x, y, u, t in zip(c, c[K+1:], d, d[K+1:]):
            ans = max(ans, y-x+t-u)
        print(ans)
elif len(c) == len(d)+1:
    r = d[-1]
    d.append(r)
    q = c[-1]
    c.append(q)
    for x, y, u, t in zip(c, c[K+1:], d, d[K:]):
        ans = max(ans, y-x+t-u)
    print(ans)
elif len(c) == len(d)-1:
    r = d[-1]
    d.append(r)
    q = c[-1]
    c.append(q)
    for x, y, u, t in zip(c, c[K:], d, d[K+1:]):
        ans = max(ans, y-x+t-u)
    print(ans)

Submission status: Wrong Answer

Solution code:
def gcd_core(a, b):
    if b == 0:
        return a
    else:
        return gcd_core(b, a % b)


def gcd(arr):
    g = gcd_core(arr[0], arr[1])
    for i in range(2, len(arr)):
        g = gcd_core(g, arr[i])
    return g

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

s = input()

bef =""
temp = 0
arr = []
s_from1 = False
e_from1 = False
p_flag = True
for i in range(n):
    if i == 0:
        bef = s[i]
        temp = 1
        if s[i] == "1":
            s_from1 = True
    else:
        if bef == s[i]:
            temp += 1
        else:
            bef = s[i]
            arr.append(temp)
            temp = 1
arr.append(temp)
#print(arr)

ran = k
ma = 0

if s[-1] == "1":
    e_from1 = True

if (not s_from1) and (not e_from1):
    ct0 = len(arr)//2+1
else:
    ct0 = len(arr)//2

if k >= ct0:
    print(sum(arr))
    p_flag = False
else:
    sp1 = 0
    sp2 = 0
    if not s_from1:
        sp1 = sum(arr[0:2*k])
        arr = arr[1:]
    if not e_from1:
        sp2 = sum(arr[-2*k:])
        arr = arr[:-1]
    ma = 0
    if sp1 > sp2:
        ma = sp1
    i = 0
    while i < len(arr)-2*k:
        temp = sum(arr[i:i+2*k+1])
        if temp > ma:
            ma = temp
        i += 2
    print(ma)

Submission status: Wrong Answer

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

zeros = []
num_zeros = 0
start = 0
cont = False
for i in range(N):
    s = int(S[i])
    if s == 0 and not cont:
        start = i
        cont = True
        num_zeros += 1
        zeros.append([start,1])
        continue

    elif s == 0 and cont:
        zeros[num_zeros-1][1] += 1
        continue

    elif s == 1:
        cont = False

print(zeros)

max_length = 0
search_num = len(zeros) - K + 1
if search_num == 0:
    if S[0] == "1":
        max_length = 1
else:
    for i in range(search_num):
        S_ = S
        # for k in range(K):
        #     zero = zeros[i+k]
        #     S_ = S_[:zero[0]] + '1' * zero[1] + S_[zero[0]+zero[1]:]

        sub_max_length = 0
        sub_sub_max_length = 0
        for j in range(len(S_)):
            s = int(S_[j])
            if s == 1:
                sub_sub_max_length += 1
            elif s == 0:
                if sub_max_length < sub_sub_max_length:
                    sub_max_length = sub_sub_max_length
                sub_sub_max_length = 0
            if j == len(S_)-1:
                if sub_max_length < sub_sub_max_length:
                    sub_max_length = sub_sub_max_length

        if max_length < sub_max_length:
            max_length = sub_max_length
print(max_length)

Submission status: Wrong Answer

Solution code:
import re
import numpy as np
N, K = [int(item) for item in re.split(r"\s", input())]
S = [int(item) for item in list(input())]

if sum(S) == len(S) or sum(S) == 0:
    print(len(S))
else:
    S = S + [2]

    index = [-1]
    for i in range(N):
        if S[i] != S[i + 1]:
            index.append(i)

    count = np.diff(np.array(index)).tolist()

    if S[0] == 0:
        count = [0] + count

    # print(index)
    # print(count)

    cumsum = [0] + np.cumsum(count).tolist()
    c_len = len(cumsum)
    win = min(2 * K + 1, c_len - 1)
    print(max([cumsum[i + win] - cumsum[i] for i in range(0, c_len - win, 2)]))

Submission status: Wrong Answer

Solution code:
N,K = [int(i) for i in input().split()]
S = input()
l = []
ln = 0
mae = "0"
i,ren = 0,0

if S[N-1]=="0" : S += "1"
else : S += "0"

while i<=N:
  if S[i] != mae :
    l.append(ren)
    mae = S[i]
    ren = 1
    ln += 1
  else :
    ren += 1
  i += 1

i=0
max = 0
while i<=ln-K*2-1:
  sm = sum(l[i:i+K*2+1])+1
  if max < sm : max = sm
  i+=2  

if max==0 : print(N)
else : print(max)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
S = input().split('1')
while '' in S:
    S.remove('')
if len(S) <= K:
    print(N)
else:
    ans = N
    S1 = S[0]
    del S[0]
    S2 = S[-1]
    del S[-1]
    for i in range(len(S)-K):
        if len(S1) <= len(S2):
            ans = ans - len(S1)
            S1 = S[0]
            del S[0]
        else:
            ans = ans - len(S2)
            S2 = S[-1]
            del S[-1]
    print(N)

Submission status: Wrong Answer

Solution code:
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))

Submission status: Wrong Answer

Solution code:
n,k=list(map(int,input().split()))
s=input()
a=[0]
b=[0]
if s[0]=="0":
    a.append(1)
for i in range(1,n):
    if s[i]=="0" and s[i-1]=="1":
        a.append(i+1)
a.append(n+1)
if len(a)-2>k:
    q=len(a)-k-1
    max=0
    for i in range(q):
        if max<a[k+1+i]-a[i]-1:
            max=a[k+1+i]-a[i]-1
else:max=n
if s[0]=="1":
    b.append(1)
for i in range(1,n):
    if s[i]=="1" and s[i-1]=="0":
        b.append(i+1)
b.append(n+1)
if len(b)-2>k:
    q=len(b)-k-1
    min=0
    for i in range(q):
        if min<b[k+1+i]-b[i]-1:
            min=b[k+1+i]-b[i]-1
else:min=n
if max<min:
    print(min)
else:
    print(max)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
s = input().strip()
if s[0] == '0':
    s = '1'+s
i = 0
dp = []
while i < len(s):
    l = i
    while i < len(s) and s[i] == '1':
        i += 1
    if l < i:
        dp.append([l, i-1])
    while i < len(s) and s[i] == '0':
        i += 1


if len(dp) <= K:
    print(N)
else:
    ans = 0
    for i in range(0, len(dp)-K):
        t = dp[i+K][1]-dp[i][0]+1
        if t > ans:
            ans = t
            if len(s) != N and i == 0:
                ans -= 1
    print(ans)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
li = [int(i) for i in list(input())]
kukanidx = []
idx = 0
for i in li:
    if i != li[idx-1]:
        kukanidx.append(idx)
    idx += 1
idx = 1
kukanidx.append(n)

kukan = kukanidx.copy()
for i in kukanidx[1:]:
    kukan[idx] = kukanidx[idx]-kukanidx[idx-1]
    idx += 1

anskouho = []
idx = 0
if li[0] == 1:
    for i in kukan:
        anskouho.append(sum(kukan[idx:idx+(k*2+1)]))
        idx += 1
        if idx > len(kukan)-(k*2+1):
            break
else:
    for i in kukan:
        anskouho.append(sum(kukan[idx:idx+(k*2)]))
        idx += 1
        if idx > len(kukan)-(k*2):
            break

print(max(anskouho))

Submission status: Wrong Answer

Solution code:
def kakaka():
    a = list(map(int, input().split()))
    b = map(int, input().split())
    c = int(input())

def start_process():
    return
def main():
    all_num, inst_num = map(int, input().split())
    data = list(input())

    data_la = []
    ans_0, ans_1 = 0, 0
    for i in range(all_num):
        if data[i] == '1':
            ans_1 += 1
            if ans_0 > 0:
                data_la.append(ans_0)
                ans_0 = 0
        else:
            ans_0 += 1
            if ans_1 > 0:
                data_la.append(ans_1)
                ans_1 = 0
    if ans_1 > 0:
        data_la.append(ans_1)
    elif ans_0 > 0:
        data_la.append(ans_0)

    ans = 0
    ans_data = 0
    count_0 = 0
    if data[0] == '0':
        flg = 0
        flg_hajime = 0
    else:
        flg= 1
        flg_hajime = 1
    # print(data_la)

    if len(data_la) <= 2:
        print(sum(data_la))
    else:
        pre_data1, pre_data2 = data_la[0], data_la[1]
        pre_data1_ind, pre_data2_ind = 0, 1
        # print(data_la)
        for i in range(len(data_la)):
            if i % 2 == flg:
                count_0 += 1
            if count_0 == inst_num + 1:
                if flg_hajime:
                    ans = ans - pre_data1 - pre_data2
                    pre_data1_ind += 2
                    pre_data2_ind += 2
                    if pre_data1_ind >= len(data_la):
                        pre_data1 = 0
                    else:
                        pre_data1 = data_la[pre_data1_ind]
                    if pre_data2_ind >= len(data_la):
                        pre_data2 = 0
                    else:
                        pre_data2 = data_la[pre_data1_ind]
                else:
                    ans = ans - pre_data1
                    pre_data1_ind += 1
                    pre_data2_ind += 1
                    if pre_data1_ind >= len(data_la):
                        pre_data1 = 0
                    else:
                        pre_data1 = data_la[pre_data1_ind]
                    if pre_data2_ind >= len(data_la):
                        pre_data2 = 0
                    else:
                        pre_data2 = data_la[pre_data1_ind]
                    flg_hajime = 1


                count_0 -= 1
            ans += data_la[i]
            # print(ans)
            if ans_data < ans:
                ans_data = ans






        print(ans_data)






if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
s=str(input())
s=s+'e'
arr=[[] for _ in range(3)]
count=1
for i in range(n):
  if s[i]==s[i+1]:
    count+=1
  else:
    if s[i]=='1':
      arr[0].append(count)
      count=1
    else:
      arr[1].append(count)
      count=1
if len(arr[1])<=k:
  print(n)
else:
  if len(arr[0])==len(arr[1]):
    arr[0].append(0)
  elif len(arr[0])<len(arr[1]):
    arr[0].append(0)
    arr[0].append(0)
  print(arr[0])
  print(arr[1])
  tmp=0
  sum=0
  count=0
  max=0
  flag=False
  for i in range(len(arr[1])):
    sum+=arr[0][i]
    sum+=arr[1][i]
    count+=1
    if flag:      
      sum-=arr[0][i-k]
      sum-=arr[1][i-k]
    if count==k:
      tmp=sum
      tmp+=arr[0][i+1]
      if tmp>max:
        max=tmp
      count-=1
      flag=True
  print(max)

Submission status: Wrong Answer

Solution code:
import sys
from itertools import accumulate
N, K  = map(int, input().split())
ans = 0
num = 0
S = input()
a = S.split('1')
b = S.split('0')
ls = []
for x in a:
    if len(x) != 0:
        ls.append(len(x))
c = [0] + list(accumulate(ls))
ls_ = []
for x in b:
    if len(x) != 0:
        ls_.append(len(x))
d = [0] + list(accumulate(ls_))

if len(c)-1 <= K:
    print(N)
    sys.exit()

if len(c) == len(d):
    if S[0] == '0':
        q = c[-1]
        c.append(q)
        r = d[-1]
        d.append(r)
        for x, y, u, t in zip(c, c[K+1:], d, d[K+1:]):
            ans = max(ans, y-x+t-u)
        print(ans)
    elif S[0] == '1':
        q = c[-1]
        c.append(q)
        r = d[-1]
        d.append(r)
        for x, y, u, t in zip(c, c[K+1:], d, d[K+1:]):
            ans = max(ans, y-x+t-u)
        print(ans)
elif len(c) == len(d)+1:
    r = d[-1]
    d.append(r)
    q = c[-1]
    c.append(q)
    for x, y, u, t in zip(c, c[K:], d, d[K:]):
        ans = max(ans, y-x+t-u)
    print(ans)
elif len(c) == len(d)-1:
    r = d[-1]
    d.append(r)
    q = c[-1]
    c.append(q)
    for x, y, u, t in zip(c, c[K:], d, d[K+1:]):
        ans = max(ans, y-x+t-u)
    print(ans)

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
S=input()
if S[-1]=="0":
    S+="1"
else:
    S+="0"
List=[]
ini=S[0]
now=0
zeronum=0
for i in range(N+1):
    if S[i]==ini:
        now+=1
        continue
    else:
        if ini=="1":
            List.append(now)
        else:
            List.append(now*(-1))
            zeronum+=1
        ini=S[i]
        now=1
if List[0]<0:
    List.insert(0,0)
N=len(List)
sum=0
SUM=0

if zeronum<=K:
    print(N)
    quit()
N=len(List)
for i in range(2*K+1):
    if i%2==1:
        sum+=List[i]*(-1)
    else:
        sum+=List[i]
SUM=max(sum,SUM)

for i in range(6):
    List.append(0)
ind=0
for i in range(K+1,N//2+3):

    sum+=-List[ind]+List[ind+1]-List[2*i-1]+List[2*i]
    ind+=2
    SUM=max(sum,SUM)
print(SUM)

Submission status: Wrong Answer

Solution code:
n,k = [int(i) for i in input().split()]
s = input()
nums = []
coin = []

c = ""
n = 0
for i in range(len(s)):
	if s[i] == c:
		n += 1
	else:
		nums.append(n)
		coin.append(c)
		n = 1
		c = s[i]
nums = nums[1:]
coin = coin[1:]

result = 0

for i in range(len(nums)):
	count = 0
	r = 0
	for j in range(i,len(nums)):
		r += nums[j]
		if coin[j] == "0":
			if count == k:
				break
			else:
				count += 1
	if r > result:
		result = r
		
print(result)

Submission status: Wrong Answer

Solution code:
N, K = input().split()
N = int(N)
K = int(K)
S = list(str(input()))
test = []
x = '1'
test.append(0)
for i in range(N):
    if S[i] == x:
        test[len(test)-1] = test[len(test)-1] + 1
    else:
        x = S[i]
        test.append(1)
for i in range(K**2):
    test.append(0)
ans = 0
sum = 0
for i in range(K*2):
    sum += test[i]
for i in range(K*2, len(test)):
    sum += test[i]
    if i % 2 == 0:
        ans = max(ans, sum)
    sum -= test[i - K*2]
print(ans)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
S = input()
last_s = "0"
count = 0
A = []
for i, s in enumerate(S):
	if i == 0:
		count += 1
	else:
		if s != last_s:
			A.append(count)
			count = 0
		count += 1
	last_s = s

if count != 0:
	A.append(count)

A.append(0)
A.append(0)

val = 0
if S[0] == "0":
	val = sum(A[:2*K])
	currentMax = val
	old = A[0]
	new = sum(A[2*K:2*K+2])
	val += new - old
	if val > currentMax:
		currentMax = val
	
	for i in range(1, len(A)-2*K+2, 2):
		old_sum_of_two = sum(A[i:i+2])
		sum_of_two = sum(A[i+2*K:i+2*K+2])
		val += sum_of_two - old_sum_of_two
		if val > currentMax:
			currentMax = val
	print(currentMax)
else:
	val = sum(A[:2*K+1])
	currentMax = val
	for i in range(0, len(A)-2*K+2, 2):
		old_sum_of_two = sum(A[i:i+2])
		sum_of_two = sum(A[i+2*K+1:i+2*K+3])
		val += sum_of_two - old_sum_of_two
		if val > currentMax:
			currentMax = val
	print(currentMax)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3.4
# -*- coding: utf-8 -*-

# abc124_d

# input
n, k = [int(s) for s in input().split()]
S = input()
# get ans
node = [len(s) for s in S.split('0')]
node = [s for i, s in enumerate(node) if not (s==0 and i!=0 and i!=len(node)-1)]
arch = [len(s) for s in S.split('1')]
arch = list(filter(int, arch))
def gett():
    for i, a in enumerate(arch):
        yield a+node[i]+node[i+1]
tentative = gett()
def window(iterable, size):
    #TODO size = min(size, len(iterable))
    from itertools import tee
    iters = tee(iterable, size)
    for i in range(1, size):
        for each in iters[i:]:
            next(each, None)
    return zip(*iters)
k = min(k, len(arch))
m = list(map(sum, window(tentative, k)))
i = m.index(max(m)) if m else 0
j = i+k
ans = sum(arch[i:j] + node[i:j+1])
# output
# print(node)
# print(arch)
# print(list(gett()))
# print(list(window(gett(), k)))
print(ans)

Submission status: Wrong Answer

Solution code:
N, K = tuple([int(x) for x in input().split(' ')])
S = input()

y = '2'
tmp = [0]
ans = [0]

for x in S+'2':
    if x == y:
        tmp[-1] += 1
    else:
        if K*2+1 >= min(sum(tmp), len(tmp)+1):
            pass
        elif K*2 >= sum(tmp[:-1]):
            ans.append(sum(tmp[1:]))
        elif K*2 >= sum(tmp[1:]):
            ans.append(sum(tmp[:-1]))
        else:
            ans.append(sum(tmp))

        y = x
        if 2*K+1 > len(tmp):
            tmp.append(1)
        else:
            tmp = tmp[1:] + [1]

print(max(ans))

Submission status: Wrong Answer

Solution code:
import math

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

ans = 0
if s[0] == "0":
    change = min(k, math.ceil(len(t)/2))
    for j in range(math.ceil(len(t)/2) - change):
        u = 0
        if j != 0:
            for k in range(change * 2 + 1):
                u += t[2 * j + k - 1]
        else:
            for k in range(change * 2):
                u += t[2 * j + k]

        if u > ans:
            ans = u

elif s[0] == "1":
    change = min(k, math.floor(len(t)/2))
    for j in range(math.floor(len(t)/2) - change):
        u = 0
        for k in range(change * 2 + 1):
            u += t[2 * j + k]

        if u > ans:
            ans = u

    
print(ans)

Submission status: Wrong Answer

Solution code:
N,K = map(int, input().split())
S = input()
 
if S[0] == "0":
  List = []
  count = 0
  mark = "0"
  for i in range(N):
    if S[i] == mark:
      count += 1
    else:
      List = List + [count]
      count = 1
      if mark == "0":
        mark = "1"
      else:
        mark = "0"
  List = List + [count]
  l = len(List)
  if l <= 2 * K:
    print(N)
  else:
    List = [0] + List
    ans = sum(List[:2 * K + 1])
    List = List + [0, 0, 0]
    p = ans
    for i in range(1, (l - 2 * K) // 2 + 1):
      j = 2 * i
      p = p - List[j - 2] - List[j - 1] + List[j + 2 * K - 1] + List[j + 2 * K]
      if p > ans:
        ans = p
    print(ans)
    '''
    ans = sum(List[:2 * K])
    List = List + [0, 0, 0]
    p = sum(List[1:2 * K + 1])
    if p > ans:
      ans = p
    for i in range(3, l - 2 * K + 2, 2):
      p = p - List[i - 2] - List[i - 1] + List[i + 2 * K - 1] + List[i + 2 * K]
      if p > ans:
        ans = p
    print(ans)
    '''
    
if S[0] == "1":
  List = []
  count = 0
  mark = "1"
  for i in range(N):
    if S[i] == mark:
      count += 1
    else:
      List = List + [count]
      count = 1
      if mark == "0":
        mark = "1"
      else:
        mark = "0"
  List = List + [count]
  l = len(List)
  if l <= 2 * K + 1:
    print(N)
  else:
    ans = sum(List[:2 * K + 1])
    List = List + [0, 0, 0]
    p = ans
    for i in range(1, (l - 2 * K) // 2 + 1):
      j = 2 * i
      p = p - List[j - 2] - List[j - 1] + List[j + 2 * K - 1] + List[j + 2 * K]
      if p > ans:
        ans = p
    print(ans)

Submission status: Wrong Answer

Solution code:
n,k = (int(i) for i in input().split())
s = input()
li_0 = [0]*k
li_1 = [0]*(k+1)
ind_0 = 0
ind_1 = 0
flag = -1
cnt = 0
ans = 0

if s[0] == '0':
  flag = 0
else:
  flag = 1

for i in s:
  if flag == i:
    cnt += 1
  else:
    if flag == '1':
      li_1[ind_1] = cnt
      flag = i
      cnt = 1
      if ind_1 < k:
        ind_1 += 1
      else:
        ind_1 = 0
    else:
      li_0[ind_0] = cnt
      flag = i
      cnt = 1
      if ind_0 < k-1:
        ind_0 += 1
      else:
        ind_0 = 0
    ans = max(ans,sum(li_0)+sum(li_1))
  

if flag == '1':
  li_1[ind_1] = cnt
else:
  li_0[ind_0] = cnt
  li_1[ind_1] = 0

ans = max(ans,sum(li_0)+sum(li_1))

print(ans)

Submission status: Wrong Answer

Solution code:
import numpy as np

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

l = [list(np.repeat(0, K+1)), list(np.repeat(0, K+1))]
ans = []
k = [int(s) for s in S]
v0 = 1 if k[0] == 0 else 0
v1 = 1 if k[0] == 1 else 0
a = sum(l[0][-K:]+l[1][-K-1:])
for i in range(1, N):
    if (k[i]==0) & (k[i-1]==0):
        v0 += 1
    elif (k[i]==1) & (k[i-1]==0):
        l[0].append(v0)
        v0 = 0
        v1 += 1
    elif (k[i]==0) & (k[i-1]==1):
        l[1].append(v1)
        v0 += 1
        
        a -= l[0][-K-1] + l[1][-K-2]
        a += v1 + l[0][-1]
        v1 = 0
        ans.append(a)    
    else:
        v1 += 1
        
for i, v in enumerate([v0, v1]):
    if v > 0:
        l[i].append(v)

if len(l[0])-K <= K:
    print(N)

else:
    if (v1>0) & (v0==0):
        a -= l[0][-K-1] + l[1][-K-2]
        a += v1 + l[0][-1]
        ans.append(a)
    else:
        a -= l[0][-K-1] + l[1][-K-2]
        a += v0
    print(max(ans))

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
s=input()
s=[bool(int(s[i])) for i in range(n)]
true_index_list=[]
for i in range(k):
    true_index_list =[]
    for state_index in range(len(s)):
        if s[state_index] is True:
            true_index_list.append(state_index)
    if len(true_index_list) == 0:
        for false_index in range(n):
            s[false_index] = not s[false_index]
    elif len(true_index_list) == 1:
        for false_index in range(true_index_list[0]):
            s[false_index] = not s[false_index]
    else:
        start = 0
        end = 0
        diff = 0
        for index in range(len(true_index_list)):
            if index == len(true_index_list) - 1:
                break
            if true_index_list[index+1] - true_index_list[index] >= diff:
                start = true_index_list[index]
                end = true_index_list[index+1] 
                diff =  true_index_list[index+1] - true_index_list[index]
        for false_index in range(start+1, end):
            s[false_index] = not s[false_index]

cnt=0
max_cnt = 0
for state in s:
    if state:
        cnt+=1
    else:
        max_cnt = cnt
        cnt = 0


if cnt >= max_cnt:
  max_cnt = cnt
print(max_cnt)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**7)
input = sys.stdin.readline
INF = float('inf')
MOD = 10**9+7
dy = [0,1,0,-1]
dx = [1,0,-1,0]
ddy = [0,1,1,1,0,-1,-1,-1]
ddx = [1,1,0,-1,-1,-1,0,1]

n,k = map(int,input().split())
S = input()+'!'

get = [0]
tmp = 1
for i in range(1,n+1):
    if S[i] == S[i-1]:
        tmp += 1
    else:
        get.append(tmp)
        tmp = 1

get = get + [0]

for i in range(1,len(get)):
    get[i] += get[i-1]

res = 0
if S[0] == '0':
    for i in range(len(get)-2*k):
        if i%2 == 0:
            ans = get[i+2*k]-get[i]
        else:
            ans = get[min(i+2*k+1,len(get)-1)]-get[i]
        if res <= ans:
            res = ans
else:
    for i in range(len(get)-2*k):
        if i%2 == 0:
            ans = get[min(len(get)-1,i+2*k+1)]-get[i]
        else:
            ans = get[i+2*k]-get[i]
        if res <= ans:
            res = ans

print(res)