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

ans=-1
l=0

r=0
R=0

q=[0]
for i in range(1,N-1):
    if S[i]!=S[i-1]:
        q.append(i)
q.append(N+1)
        
L=q[l]
R=q[r]

S[L:R]

for l in range(len(q)):
    count=0
    for r in range(l+1,len(q)):        
        L=q[l]
        R=q[r]
        if S[L:R][-1]=='0':
            count+=1
            if count>K:
                break
        ans=max(ans,len(S[L:R]))
        
print(ans)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import re
import itertools

N,K = map(int, input().split())
S = str(input())
pattern = r"0+"
iterator = re.finditer(pattern ,S)
l = [match.span() for match in iterator]

if len(l) <= K:
    ans = N
else:
    ans = 0
    for i in range(len(l)-K+1):
        v = l[i:i+K]
        s = S
        for j in range(K):
            add = "1"*(v[j][1]-v[j][0])
            s =s[:v[j][0]] + add + s[v[j][1]:]
        splt = re.split(pattern, s)
        print(splt)
        a = [len(splt[i]) for i in range(len(splt))]
        if max(a) >= ans:
            ans = max(a)
print(ans)

Submission status: Wrong Answer

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

q=[0]
for i in range(1,N-1):
    if S[i]!=S[i-1]:
        q.append(i)

answers=[]

for i,p in enumerate(q):
    if S[p]=='1':
        if i+2*K+1>=len(q)-1:
            answers.append(N-p)
        else:
            answers.append(q[min(i+2*K+1,len(q)-1)]-p)
    else:
        if i+2*K>=len(q)-1:
            answers.append(N-p)        
        answers.append(q[min(i+2*K,len(q)-1)]-p)
        
print(max(answers))

Submission status: Wrong Answer

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

q=[0]
for i in range(1,N-1):
    if S[i]!=S[i-1]:
        q.append(i)

answers=[]

for i,p in enumerate(q):
    if S[p]=='1':
        if i+2*K+1>len(q)-1:
            answers.append(N-p)
        else:
            answers.append(q[min(i+2*K+1,len(q)-1)]-p)
    else:
        answers.append(q[min(2*K,len(q)-1)]-p)
        
print(max(answers))

Submission status: Wrong Answer

Solution code:
from itertools import*;from numpy import*;n,k=map(int,input().split());k=2*k+1;s=input();x=[0]*(s[0]=="0")+[len(list(v))for k,v in groupby(s)]+[0]*(s[-1]=="0");u=sum(x[:k]);print(max([u]+list(u+cumsum(array(x[k:])-array(x[:-k])))[::2]))

Submission status: Wrong Answer

Solution code:
import numpy as np
[N,K] = list(map(int,input().split()))
S_tmp = str(input())
tmp = 1
S = []
for i in range(1,N):
    if S_tmp[i] == S_tmp[i-1]:
        tmp += 1
        
    else:
        S.append(tmp)
        tmp = 1
    if i == N-1 and S_tmp[i] == S_tmp[i-1]:
        S.append(tmp)
if S_tmp[0] == "0":
    S_sign = [ i % 2 for i in range(len(S)) ]
else:
    S_sign = [ (i+1) % 2 for i in range(len(S)) ]
S_dummy = [0]*K
S = S+S_dummy
ans = 0
for i in range(len(S) - K):
    if S_sign[i] == 1:
        tmp = sum(S[i:i+2*K+1])
        if tmp > ans:
            ans = tmp
    else:
        if i == 0:
            tmp = sum(S[i:i+2*K+1])
            if tmp > ans:
                ans = tmp
        else:
            tmp = sum(S[i-1:i+2*K+1])
            if tmp > ans:
                ans = tmp

print(ans)

Submission status: Wrong Answer

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

q=[0]
for i in range(1,N):
    if S[i]!=S[i-1]:
        q.append(i)

answers=[]

for p in q:
    if S[p]=='1':
        answers.append(q[min(2*K+1,len(q)-1)]-p+1)
    else:
        answers.append(q[min(2*K,len(q)-1)]-p+1)
        
print(max(answers))

Submission status: Wrong Answer

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

# 部分文字列の和を算出
array = []
now_num = 1
count = 0
for i in s:
  if now_num == int(i):
    count += 1
  else:
    array.append(count)
    count = 1
    now_num = 1 - now_num

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

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

# 累積和のリストを算出

sum_array=[0]
for i in range(len(array)):
  sum_array.append(sum_array[i] + array[i])

# 累積和のリストから最大を算出
res = 1

skip_array = sum_array[1::2]
for i in range(len(skip_array)-1):
  left = i
  right = min(i+k, len(skip_array)-1)
  if skip_array[right] - skip_array[left] > res:
    res = skip_array[right] - skip_array[left]

#if res == -1:
#    print(1)
#else:
print(res)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
S = str(input())
S_list = []
cum_list = [0]
tmp = 1
count = 0

for i in range(N):
    if i == 0:
        if int(S[i]) == tmp:
             count += 1
        else:
            S_list.append(count)
            count = 1
    else:
        if S[i] == S[i - 1]:
            count += 1
            
        else:
            S_list.append(count)
            count = 1

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


now = 0
for i in range(len(S_list)):
    now += S_list[i]
    cum_list.append(now)

def check(x,K):
    answer_list = []
    i = 0
    if 2 * K + 1 < len(x):
        while i + 2 * K + 1 < len(x):
            answer_list.append(x[i + 2 * K + 1] - x[i])
            i += 2
        return answer_list
    else:
        tmp = 0
        for y in x:
            tmp += y
        answer_list.append(tmp)
        return answer_list
answer_list = check(cum_list,K)
answer = max(answer_list)
print(answer)

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
while idx + 2 * K + diff < len(index):
    ans = max(ans, index[idx + 2 * K + diff] - index[idx])
    idx += 1

while idx == 0:
    diff -= 1
    if idx + 2 * K + diff < 0:
        break
    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:
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
while i + 2 * K + diff < len(index):
    ans = max(ans, index[idx + 2 * K + diff] - index[idx])
    idx += 1

for i in range(len(index)):
    for j in range(i, len(index)):
        if j - i <= 2 * K + diff:
            ans = max(ans, index[j] - index[i])
print(ans)

Submission status: Wrong Answer

Solution code:
import math

N, K = map(int, input().split())
S = input()
consecutive = 0
alternately = []
if S[0] == "0":
    first_num = 0
else:
    first_num = 1

current_num = first_num

for i in S:
    if i == str(current_num):
        consecutive += 1
    else:
        alternately.append(consecutive)
        consecutive = 1
        current_num = i
else:
    alternately.append(consecutive)

res = 0
max_res = 0
left = 0

for i, v in enumerate(alternately):
    right = i
    res += v
    if right - left >= 2*K + 1:
        res -= alternately[i - (2*K + 1)]
        left += 1
    if (i+1) % 2 == first_num and i >= 2*K - 1:
        if res > max_res:
            max_res = res

if len(alternately) == 1 or len(alternately) == 2 or K >= math.ceil(len(alternately) / 2):
    max_res = sum(alternately)

print(max_res)

Submission status: Wrong Answer

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

now = 1 # 今見ている数
cnt = 0 # nowがいくつ並んでいるか
nums =[]

for i in range(n):
    if s[i] == '0'+str(now):
        cnt += 1
    else:
        nums.append(cnt)
        now ^= 1    #見る値の切り替え
        cnt += 1

if cnt != 0:
    nums.append(cnt)

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

add = 2 * k + 1
ans = 0
left = 0
right = 0
r_l_sum = 0
for i in range(len(nums))[::2]:
    next_left = i
    next_right = min(i+add,len(nums))
    # 尺取法
    while next_left > left:
        r_l_sum -= nums[left]
        left += 1
    while next_right > right:
        r_l_sum += nums[right]
        right += 1
    ans = max(r_l_sum,ans)

print(ans)

Submission status: Wrong Answer

Solution code:
import math

N, K = map(int, input().split())
S = input()
consecutive = 0
alternately = []
if S[0] == "0":
    first_num = 0
else:
    first_num = 1

current_num = first_num

for i in S:
    if i == str(current_num):
        consecutive += 1
    else:
        alternately.append(consecutive)
        consecutive = 1
        current_num = i
else:
    alternately.append(consecutive)

res = 0
max_res = 0
left = 0

for i, v in enumerate(alternately):
    right = i
    res += v
    if right - left >= 2*K + 1:
        res -= alternately[i - (2*K + 1)]
        left += 1
    if (i+1) % 2 == first_num and i >= 2*K:
        if res > max_res:
            max_res = res

if len(alternately) == 1 or len(alternately) == 2 or K >= math.ceil(len(alternately) / 2):
    max_res = sum(alternately)

print(max_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])
i = 0
while i + 2 * K + diff < len(index):
    ans = max(ans, index[i + 2 * K + diff] - index[i])
    i += 1
for i in range(2 * K + diff):
    if i < len(index):
        ans = max(ans, index[i] - index[0])
print(ans)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
S = input()
consecutive = 0
alternately = []
if S[0] == "0":
    first_num = 0
else:
    first_num = 1

current_num = first_num

for i in S:
    if i == str(current_num):
        consecutive += 1
    else:
        alternately.append(consecutive)
        consecutive = 1
        current_num = i
else:
    alternately.append(consecutive)

res = 0
max_res = 0
left = 0

for i, v in enumerate(alternately):
    right = i
    res += v
    if right - left >= 2*K + 1:
        res -= alternately[i - (2*K + 1)]
        left += 1
    if (i+1) % 2 == first_num and i >= 2*K - 1:
        if res > max_res:
            max_res = res

if len(alternately) == 1 or len(alternately) == 2 or K >= len(alternately) // 2:
    max_res = sum(alternately)

print(max_res)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
S = input()
consecutive = 0
alternately = []
if S[0] == "0":
    first_num = 0
else:
    first_num = 1

current_num = first_num

for i in S:
    if i == str(current_num):
        consecutive += 1
    else:
        alternately.append(consecutive)
        consecutive = 1
        current_num = i
else:
    alternately.append(consecutive)

res = 0
max_res = 0
left = 0

for i, v in enumerate(alternately):
    right = i
    res += v
    if right - left >= 2*K + 1:
        res -= alternately[i - (2*K + 1)]
        left += 1
    if (i+1) % 2 == first_num and i >= 2*K - 1:
        if res > max_res:
            max_res = res

if len(alternately) == 1 or len(alternately) == 2:
    max_res = sum(alternately)

print(max_res)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
S = input()
consecutive = 0
alternately = []
if S[0] == "0":
    first_num = 0
else:
    first_num = 1

current_num = first_num

for i in S:
    if i == str(current_num):
        consecutive += 1
    else:
        alternately.append(consecutive)
        consecutive = 1
        current_num = i
else:
    alternately.append(consecutive)

res = 0
max_res = 0
left = 0

for i, v in enumerate(alternately):
    right = i
    res += v
    if right - left > 2*K + 1:
        res -= alternately[i - (2*K + 1)]
        left += 1
    if (i+1) % 2 == first_num and i >= 2*K - 1:
        if res > max_res:
            max_res = res

if len(alternately) == 1 or len(alternately) == 2:
    max_res = sum(alternately)

print(max_res)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
S = input()
consecutive = 0
alternately = []
if S[0] == "0":
    first_num = 0
else:
    first_num = 1

current_num = first_num

for i in S:
    if i == str(current_num):
        consecutive += 1
    else:
        alternately.append(consecutive)
        consecutive = 1
        current_num = i
else:
    alternately.append(consecutive)

res = 0
max_res = 0
left = 0

for i, v in enumerate(alternately):
    right = i + 1
    res += v
    if right - left > 2*K + 1:
        res -= alternately[i - (2*K + 1)]
        left += 1
    if (i+1) % 2 == first_num and i >= 2*K - 1:
        if res > max_res:
            max_res = res

if len(alternately) == 1 or len(alternately) == 2:
    max_res = sum(alternately)

print(max_res)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
S = input()
consecutive = 0
alternately = []
if S[0] == "0":
    first_num = 0
else:
    first_num = 1

current_num = first_num

for i in S:
    if i == str(current_num):
        consecutive += 1
    else:
        alternately.append(consecutive)
        consecutive = 1
        current_num = i
else:
    alternately.append(consecutive)

res = 0
max_res = 0
left = 0

for i, v in enumerate(alternately):
    right = i + 1
    res += v
    if right - left > 2*K + 1:
        res -= alternately[i - (2+K + 1)]
        left += 1
    if (i+1) % 2 == first_num and i >= 2*K - 1:
        if res > max_res:
            max_res = res

if len(alternately) == 1 or len(alternately) == 2:
    max_res = sum(alternately)

print(max_res)

Submission status: Wrong Answer

Solution code:
# d
import re
N, K = map(int, input().split())
S = input()

# ランレングス
run_length_S = []
count = 1
for i in range(len(S)):
    try:
        if S[i] == S[i+1]:
            count += 1
        else:
            run_length_S.append(S[i] + str(count))
            count = 1
    except:
        run_length_S.append(S[i] + str(count))

# しゃくとり法
right = 0
total = 0
max_total = 0
reverse_count = 0

for left in range(len(run_length_S)):
    
    # Kより下なら右に移動
    while right < len(run_length_S) and reverse_count <= K:
        # 1なら移動
        if run_length_S[right][0] == "1":
            total += int(run_length_S[right][1])
            right += 1
        # 0ならKより下で移動
        elif reverse_count == K and run_length_S[right][0] == "0":
            break
        else:
            total += int(run_length_S[right][1])
            right += 1
            reverse_count += 1
    
    # 最大合計の更新
    max_total = max(total, max_total)    
    # leftをインクリメントする準備
    if right == left:
        right += 1
    else:
        total -= int(run_length_S[left][1])
    
    # 外されるleftが0だった場合、反転回数を増やす
    if run_length_S[left][0] == "0":
        reverse_count -= 1

print(max_total)

Submission status: Wrong Answer

Solution code:
import numpy as np 

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

li = []
pre = "1"
tmp = 0
for i in range(len(s)):
    if pre!=s[i]:
        li.append(tmp)
        tmp = 0
    pre=s[i]
    tmp += 1
li.append(tmp)
# print(li)

maxv = 0
for i in range(len(li)):
    if i+K*2+1 > len(li):
        break
    if i%2==0:
        # print(li[i:i+K*2+1])
        maxv = max(maxv, sum(li[i:i+K*2+1]))
print(maxv)

Submission status: Wrong Answer

Solution code:
from itertools import accumulate

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


def solve():
    l=[]
    prev = '1'
    cnt = 0
    for c in s:
        if prev ==c:
            cnt+=1
        else:
            l.append(cnt)
            cnt = 1
        prev=c
    l.append(cnt)
    if prev=='0':
        l.append(0)

    w = 2*k+1

    if len(l)<=w:
        return n

    acc = [0]+list(accumulate(l))
    ans = 0
    for i in range(0, len(l)-w+1,2):
        ans = max(ans,acc[i+w]-acc[i])
    return ans

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
T=input()
#print(N,K,S)
#print(type(T[0]))
def ONETIME_ACTION(S):
    MAX_LENGTH = 0
    POINT = 0
    if len(S) == 1:
        print("1")
        
    start = 0    
    flag_A = 0
    flag_B = 0
    
    while flag_B == 0:    
        if S[0] == "0" and start == "0":
            a = 0
            while S[a] == "1" or S[a+1] == "0":
                a += 1
            b = a + 1
            while S[b] == "0" or S[b+1] == "1":
                b += 1
                
            LENGTH = b + 1
            
            if LENGTH > MAX_LENGTH:
                MAX_LENGTH = LENGTH
                POINT = start
                
            start = b + 1
                
        else:
            print("ELSE")
            a = start
            COUNTER = 0
            flag_A = 0
            while flag_A == 0:
                print("FLAGA=0",a,S[a])
                if a+1 >= len(S):
                    flag_B = 1
                    flag_A = 1 
                    LENGTH = a - start + 2
                    print("END",a,start)
                elif S[a] == "1" and S[a+1] == "0":
                    COUNTER += 1
                    print("COUNT")
                    if COUNTER == 2:
                        print("COUNTMAX")
                        flag_A = 1
                        LENGTH = a - start + 1
                        a -= 1
                a += 1
            
            if LENGTH > MAX_LENGTH:
                print("NEWMAX",LENGTH)
                MAX_LENGTH = LENGTH
                POINT = start
                
            start = a
        
            
    S_LIST = list(S)
    print("P,L",POINT,LENGTH)
    
    for i in range(POINT-1,POINT-1+LENGTH):
        S_LIST[i] = "1"
    print(S_LIST)
    S_NEW = "".join(S_LIST)
    return(S_NEW)
    

print(ONETIME_ACTION(T))

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
s=list(input())
if s[len(s)-1]=='0':
  s.append('1')
count=1
ss=[]
for i in range(len(s)-1):
  if s[i]==s[i+1]:
    count=count+1
  else:
    ss.append(count)
    count=1
ans=0
if s[0]=='0':
  for i in range(0,len(ss)-2*k,2):
    temp=0
    for j in range(i,i+2*k):
      temp=temp+ss[j]
    if ans<=temp:
      ans=temp
else:
  for i in range(0,len(ss)-2*k,2):
    temp=0
    for j in range(i,i+2*k+1):
      temp=temp+ss[j]
    if ans<=temp:
      ans=temp
print(ans)

Submission status: Wrong Answer

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

ruiwa = [0]

for i in range(1,len(s)):
    if s[i] != s[i-1]:
        ruiwa.append(i)
ruiwa.append(n)

if s[0] == "1":
    ans = ruiwa[1]
    for i in range(((len(ruiwa)-1)//2)-k+1):
        ans = max(ans,ruiwa[min(2*k+1+2*i,len(ruiwa)-1)] - ruiwa[2*i])
else:
    ans = 0
    for i in range(len(ruiwa) - 1 - (len(ruiwa)-1)//2 -k + 1):
        ans = max(ans, ruiwa[min(2*k+2*i, len(ruiwa)-1)] - ruiwa[max(0,2*i-1)])

print(ans)

Submission status: Wrong Answer

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

ruiwa = [0]

for i in range(1,len(s)):
    if s[i] != s[i-1]:
        ruiwa.append(i)
ruiwa.append(n)

if s[0] == "1":
    ans = ruiwa[1]
    for i in range(((len(ruiwa)-1)//2)-k+1):
        ans = max(ans,ruiwa[min(2*k+1+2*i,len(ruiwa)-1)] - ruiwa[2*i])
else:
    ans = 0
    for i in range(len(ruiwa) - 1 - (len(ruiwa)-1)//2 -k + 1):
        ans = max(ans, ruiwa[min(2*k+2*i, len(ruiwa)-1)] - ruiwa[min(0,2*i-1)])

print(ans)

Submission status: Wrong Answer

Solution code:

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':
        data_la.insert(0, 0)

    print(data_la)

    if len(data_la) <= 2:
        ans_data = sum(data_la)
    else:
        pre_data1_ind, pre_data2_ind = 0, 1
        pre_data1, pre_data2 = data_la[pre_data1_ind], data_la[pre_data2_ind]

        for i in range(len(data_la)):
            if i % 2 == 1:
                count_0 += 1

            if count_0 == inst_num + 1:
                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_data2_ind]
                count_0 -= 1

            ans += data_la[i]
            if ans_data < ans:
                ans_data = ans

    print(ans_data)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
#input
N, K = map(int, input().split())#0~K-1回目と考える。
s = input() #0~N-1人目と考える。


#output

first_pos = int(s[0:1]) #0人目が○なら1, ×なら0

nowpos = first_pos #今チェックしている立ち方
nowposcount = 0 #1にすると0人目を考えるときにバグる
Accum = [0] * (N+1) #累積和 取り出し方は[まとまり個数目] 要素数はまとまりの個数+1
nowRuisekiCount = 1 #何番目のまとまりを見ているか 0~まとまり個数-1
zeroRenzoku = 1

for i in range(len(s)): #0~N-1人目につき、全てをチェックして累積和のリストを作る
    if int(s[i:i+1]) == nowpos: #今チェックしている立ち方と同じ(左の人と同じ)なら
        nowposcount += 1 #左のまとまりの個数を1増やす
        Accum[nowRuisekiCount] = Accum[nowRuisekiCount-1] + nowposcount #まとまりの数字数を入れる
    else:
        nowpos = abs(nowpos - 1)
        nowposcount = 1 #nowposcountは1に戻る
        nowRuisekiCount += 1 #次のまとまりに移ったことになる
        Accum[nowRuisekiCount] = Accum[nowRuisekiCount-1] + nowposcount #まとまりの文字数を入れる(1を入れる)

matomariCount = nowRuisekiCount #まとまりの数

#累積和を出す
if (first_pos == 1) & (matomariCount <= (K*2+1)): #まとまり数がK*2+1以下なら、全部逆立ちさせられる
    print(len(s))
elif (first_pos == 0) & (matomariCount <= (K*2)): #最初が0だと同様にK*2以下なら
    print(len(s))
else:
    #一番最初の合計文字数
    if first_pos == 1:
        maxaccum = Accum[(K*2+1)]
    else: #最初のまとまりが0なら
        maxaccum = Accum[(K*2)]

    #2個目のまとまりからも同様に見ていく
    for i in range(2, matomariCount+1, 1):
        if abs((first_pos - ((i-1)%2))) == 0: #これが0なら左端が0なので右端を落とす
            if i+(K*2)-1 > matomariCount:
                break
            maxaccum = max(maxaccum, Accum[i+(K*2)-1]-Accum[i-1])
        else:
            if i+(K*2) > matomariCount:
                break
            maxaccum = max(maxaccum, Accum[i+(K*2)]-Accum[i-1])
    print(maxaccum)

Submission status: Wrong Answer

Solution code:
import numpy as np
N, K = map(int, input().split())
S = input()
len0 = [len(i) for i in S.split('1') if (len(i)) != 0]
len1 = [len(i) for i in S.split('0') if (len(i)) != 0]

lens = np.zeros(len(len0)+len(len1), dtype=np.int)
if(int(S[0])==0):
    lens[::2] = len0
    lens[1::2] = len1
else:
    lens[::2] = len1
    lens[1::2] = len0
lens = np.cumsum(lens)
    
even_odd = int(S[0])

if(K >= len(len0)):
    print(len(S))
else:
    ans = []
    max_num = 0
    for first, i in enumerate(range(K*2-1+even_odd, len(lens), K)):
        if(first == 0):
            ans = lens[i]
        else:
            ans = lens[i] - lens[i-(K*2+even_odd)+1] + 1
        if(ans > max_num):
            max_num = ans
    
    print(max_num)

Submission status: Wrong Answer

Solution code:
N, trial = input().split()
trial = int(trial)
row = input()
temp = row[0]
row = row[1:]
ppl = []
count = 1
for i in row:
  if i != temp:
    ppl.append(count)
    count = 1
    temp = i
  else:
    count += 1
ppl.append(count) 
if temp == '1':
  temp = 0
  for i in range(len(ppl)):
    comp = sum(ppl[i:i+2*trial+1])
    if temp < comp:
      temp = comp
else:
  temp = 0
  for i in range(len(ppl)):
    comp = sum(ppl[i:i+2*trial])
    if temp < comp:
      temp = comp
print(temp)

Submission status: Wrong Answer

Solution code:
import numpy as np
N, K = map(int, input().split())
S = input()
len0 = [len(i) for i in S.split('1') if (len(i)) != 0]
len1 = [len(i) for i in S.split('0') if (len(i)) != 0]

lens = np.zeros(len(len0)+len(len1), dtype=np.int)
if(int(S[0])==0):
    lens[::2] = len0
    lens[1::2] = len1
else:
    lens[::2] = len1
    lens[1::2] = len0
lens = np.cumsum(lens)
    
even_odd = int(S[0])

if(K >= len(len0)):
    print(len(S))
else:
    ans = []
    max_num = 0
    for first, i in enumerate(range(K*2-1+even_odd, len(lens), K)):
        if(first == 0):
            ans = lens[i]
        else:
            ans = lens[i] - lens[i-(K*2+even_odd)+1] + 1
        print(i, i-(K*2+even_odd), ans)
        if(ans > max_num):
            max_num = ans
    
    print(max_num)

Submission status: Wrong Answer

Solution code:

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':
        data_la.insert(0, 0)

    if len(data_la) <= 2:
        ans_data = sum(data_la)
    else:
        pre_data1_ind, pre_data2_ind = 0, 1
        pre_data1, pre_data2 = data_la[pre_data1_ind], data_la[pre_data2_ind]

        for i in range(len(data_la)):
            if i % 2 == 1:
                count_0 += 1

            if count_0 == inst_num + 1:
                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]
                count_0 -= 1

            ans += data_la[i]
            if ans_data < ans:
                ans_data = ans

    print(ans_data)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
from itertools import accumulate

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


def solve():
    l = []
    prev = '1'
    cnt = 0
    for c in s:
        if prev == c:
            cnt += 1
        else:
            l.append(cnt)
            cnt = 1
        prev = c
    l.append(cnt)
    if prev == '0':
        l.append(0)
    print(l)

    w = 2 * k + 1

    if len(l) <= w:
        return n

    acc = [0] + list(accumulate(l))
    ans = 0
    for i in range(0, len(l) - w + 1, 2):
        ans = max(ans, acc[i + w] - acc[i])
    return ans


print(solve())

Submission status: Wrong Answer

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

runlength_one = []
runlength_zero = []
counter = 0
prev = '1'
for s in S:
    if s == prev:
        counter += 1
    else:
        if s == '1':
            runlength_zero.append(counter)
        else:
            runlength_one.append(counter)
        counter = 1
        prev = s
if S[-1] == '1':
    runlength_one.append(counter)
else:
    runlength_zero.append(counter)

max = 0
sum_one = 0
sum_zero = 0
for i in range(len(runlength_one)):
    if i + K >= len(runlength_one):
        break

    if i == 0:
        sum_one = sum(runlength_one[0:K + 1])
        sum_zero = sum(runlength_zero[0:K])
    else:
        sum_one = sum_one - runlength_one[i - 1] + runlength_one[i + K]
        sum_zero = sum_zero - runlength_zero[i - 1] + runlength_zero[i + K - 1]
        
    length = sum_one + sum_zero
    max = length if max < length else max

if max == 0:
    max = sum(runlength_one) + sum(runlength_zero)

print(str(max))

Submission status: Wrong Answer

Solution code:
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
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
        # print(ruisekiwa[right], "-", ruisekiwa[left],
        #      "=", ruisekiwa[right]-ruisekiwa[left])
        ans = max(ans, ruisekiwa[right]-ruisekiwa[left])
print(ans)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
s = 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
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
        # print(ruisekiwa[right], "-", ruisekiwa[left],
        #      "=", ruisekiwa[right]-ruisekiwa[left])
        ans = max(ans, ruisekiwa[right]-ruisekiwa[left])
print(ans)

Submission status: Wrong Answer

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

runlength_one = []
runlength_zero = []
counter = 0
prev = '1'
for s in S:
    if s == prev:
        counter += 1
    else:
        if s == '1':
            runlength_zero.append(counter)
        else:
            runlength_one.append(counter)
        counter = 1
        prev = s
if S[-1] == '1':
    runlength_one.append(counter)
else:
    runlength_zero.append(counter)

max = 0
sum_one = 0
sum_zero = 0
for i in range(len(runlength_one)):
    if i == 0:
        sum_one = sum(runlength_one[0:K + 1])
        sum_zero = sum(runlength_zero[0:K])
    elif i + K >= len(runlength_one):
        break
    else:
        sum_one = sum_one - runlength_one[i - 1] + runlength_one[i + K]
        sum_zero = sum_zero - runlength_zero[i-1] + runlength_zero[i+K-1]
    length = sum_one + sum_zero
    max = length if max < length else max

if max == 0:
    max = sum(runlength_one) + sum(runlength_zero)

print(str(max))