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 = str(input())

if n <= 2:
    print(1)
    exit()

a = [0]

ls = s.replace('10', '1,0')
ls = ls.replace('01', '0,1')
ls = ls.split(',')
for i in ls:
    a.append(a[-1] + len(i))

ans = 0
if s[0] == '1':
    kk = k * 2 + 1
    for i in range(len(a) - kk):
        if i % 2 == 0:
            if ans < a[i + kk] - a[i]:
                ans = a[i + kk] - a[i]
else:
    kk = k * 2
    for i in range(len(a) - kk):
        if i % 2 == 0:
            if ans < a[i + kk] - a[i]:
                ans = a[i + kk] - a[i]



print(ans)

Submission status: Wrong Answer

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

if n <= 2:
    print(1)
    exit()

a = [0]

ls = s.replace('10', '1,0')
ls = ls.replace('01', '0,1')
ls = ls.split(',')
for i in ls:
    a.append(a[-1] + len(i))

if s[0] == '1':
    kk = k * 2 + 1
else:
    kk = k * 2

ans = 0
for i in range(len(a) - kk):
    if ans < a[i + kk] - a[i]:
        ans = a[i + kk] - a[i]

print(ans)

Submission status: Wrong Answer

Solution code:
'''
WA
'''
n,k = map(int,input().split()) 
t = list(str(input()))
t = [int(s) for s in t]
last = t[0]
temp_c = 0
s = []
con_c = 0
temp_con_c = 0
corrent_p = 0
sum_t = []
for i in range(n):
    if last == t[i]:
        temp_c += 1
    else:
        last = t[i]
        s.append(temp_c)
        temp_c = 1
s.append(temp_c)

l = len(s)

if k*2 < l:
    sum_t.append(0)
    for i in range(l):
        sum_t.append(sum_t[i]+s[i])
    
    l = len(sum_t)
    if t[0] == 0:
        for i in range(l-k*2-1):
            if i%2 == 0:
                temp_con_c = sum_t[i+k*2]-sum_t[i]
            else:
                temp_con_c = sum_t[i+k*2+1]-sum_t[i]
            if temp_con_c > con_c:
                con_c = temp_con_c
    else:
        for i in range(l-k*2-1):
            if i%2 ==0:
                temp_con_c = sum_t[i+k*2+1]-sum_t[i]
            else:
                temp_con_c = sum_t[i+k*2]-sum_t[i]
            if temp_con_c > con_c:
                con_c = temp_con_c
else:
    con_c = n

print(con_c)

Submission status: Wrong Answer

Solution code:
# encoding:utf-8
import copy
import numpy as np
import random
import bisect #bisect_left　これで二部探索の大小検索が行える
import math


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

now,cnt = 1,0
Nums = []
for i in range(N):
    if S[i] == str(now):
        cnt += 1
    else:
        Nums.append(cnt)
        cnt = 1
        now = 1 - now

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

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

#累積和
"""
Sums = [0]
tmp = 0
for i in range(len(Nums)):
    tmp += Nums[i]
    Sums.append(tmp)

ans = 0
Add = 2*K+1
for i in range(len(Sums)):
    if i%2==0:
        left = i
        right = min(i+Add,len(Sums)-1)
        tmp = Sums[right]-Sums[left]
        ans = max(tmp,ans)
"""
# 尺取り法
ans = 0
add = 2*K+1
left = 0
right = min(0+add,len(Nums))
pre = sum(Nums[left:right])
ans = pre
for i in range(len(Nums)-add):
    if i%2==0:
        left = i
        right = max(i+add,len(Nums)-1)
        pre += sum(Nums[right:right+2])-sum(Nums[left:left+2])
        ans = max(pre,ans)

print(ans)

Submission status: Wrong Answer

Solution code:
# encoding:utf-8
import copy
import numpy as np
import random
import bisect #bisect_left　これで二部探索の大小検索が行える
import math


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

now,cnt = 1,0
Nums = []
for i in range(N):
    if S[i] == str(now):
        cnt += 1
    else:
        Nums.append(cnt)
        cnt = 1
        now = 1 - now

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

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

#累積和
"""
Sums = [0]
tmp = 0
for i in range(len(Nums)):
    tmp += Nums[i]
    Sums.append(tmp)

ans = 0
Add = 2*K+1
for i in range(len(Sums)):
    if i%2==0:
        left = i
        right = min(i+Add,len(Sums)-1)
        tmp = Sums[right]-Sums[left]
        ans = max(tmp,ans)
"""
# 尺取り法
ans = 0
add = 2*K+1
left = 0
right = max(0+add,len(Nums))
pre = sum(Nums[left:right])
ans = pre
for i in range(len(Nums)-add):
    if i%2==0:
        left = i
        right = max(i+add,len(Nums)-1)
        pre += sum(Nums[right:right+2])-sum(Nums[left:left+2])
        ans = max(pre,ans)

print(ans)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split()) 
t = list(str(input()))
t = [int(s) for s in t]
last = t[0]
temp_c = 0
s = []
con_c = 0
temp_con_c = 0
corrent_p = 0
sum_t = []
for i in range(n):
    if last == t[i]:
        temp_c += 1
    else:
        last = t[i]
        s.append(temp_c)
        temp_c = 1
s.append(temp_c)

l = len(s)

if k*2 <= l:
    sum_t.append(0)
    for i in range(l):
        sum_t.append(sum_t[i]+s[i])
    
    l = len(sum_t)
    if t[0] == 0:
        for i in range(l-k*2-1):
            if i%2 == 0:
                temp_con_c = sum_t[i+k*2]-sum_t[i]
            else:
                temp_con_c = sum_t[i+k*2+1]-sum_t[i]
            if temp_con_c > con_c:
                con_c = temp_con_c
    else:
        for i in range(l-k*2-1):
            if i%2 ==0:
                temp_con_c = sum_t[i+k*2+1]-sum_t[i]
            else:
                temp_con_c = sum_t[i+k*2]-sum_t[i]
            if temp_con_c > con_c:
                con_c = temp_con_c
else:
    con_c = n

print(con_c)

Submission status: Wrong Answer

Solution code:
# 良さげだけどバグが取れない！！！！


import sys
read = sys.stdin.readline


def read_ints():
    return list(map(int, read().split()))


def read_a_int():
    return int(read())


def read_matrix(H):
    '''
    H is number of rows
    '''
    return [list(map(int, read().split())) for _ in range(H)]


def read_map(H):
    '''
    H is number of rows
    文字列で与えられた盤面を読み取る用
    '''
    return [read() for _ in range(H)]


def read_col(H, n_cols):
    '''
    H is number of rows
    n_cols is number of cols

    A列、B列が与えられるようなとき
    '''
    ret = [[] for _ in range(n_cols)]
    for _ in range(H):
        tmp = list(map(int, read().split()))
        for col in range(n_cols):
            ret[col].append(tmp[col])

    return ret


N, K = read_ints()
S = read()[:-1]
sum_0 = [0]

# 0のブロックの累積和を作成する
#  1 1 0 1 0 0 1だったら
# 0 0 0 1 1 2 2 2みたいな
now = '1'
for s_next in S:
    if s_next == '0' and now == '1':
        sum_0.append(sum_0[-1] + 1)
    else:
        sum_0.append(sum_0[-1])

    now = s_next
# print(S)
# print(sum_0)

# [left,right)で考える
left = 0
right = 1
ans = 1  # 解初期値

# right-leftが答え
# sum_0[right]-sum0[left]がKを超えたらleftを進める
# print(sum_0)
# しゃくとり法で溶けそうでは？
for left in range(N - 1):
    while right < N:
        nextright = right+1
        if sum_0[nextright] - sum_0[left] > K:
            break
        right += 1

    ans = max(ans, right - left)  # 大きい方に更新
    # print(left, right, ans)

print(ans)

Submission status: Wrong Answer

Solution code:
import sys

N, K = [int(x) for x in input().split()] # N:Sの長さ K:操作を行える回数の最大値
S = [int(x) for x in list(input())] # S:0と1から成る列
index = [0] # index:Sにおいて0と1が切り替わる部分の添字を集めたリスト
length = [0] # length:1が連続して並んでいる長さの最大値の候補を集めたリスト
zeros = 0 # zeros:Sにおいて0が連続して並んでいる箇所がいくつあるか

for i in range(0, len(S) - 1): # indexの要素を取得
    if S[i] != S[i + 1]: # 0と1の変わり目を探索
        index.append(i + 1) 

if S[0] == 0: # Sが0から始まる場合
    
    for i in range(len(index)): # zerosの値を取得
        if len(index) % 2 == 0: # indexの長さが偶数である場合
            zeros = int(len(index) / 2)
        else: # indexの長さが奇数である場合
            zeros = int((len(index) + 1) / 2) 
            
    for i in range(len(index)): # max(length)の初期値を取得
        if S[len(S) - 1] == 0: # Sが0で終わっている場合
            if i % 2 == 1:
                length.append(index[i + 1] - index[i])
        else: # Sが1で終わっている場合
            if (i % 2 ==1 and (i + 1) < len(index)):
                length.append(index[i + 1] - index[i])
            elif (i % 2 ==1 and (i + 1) == len(index)): # 最後の1の連続を処理
                length.append((len(S) - 1) - index[i] + 1)

    for i in range(len(index)): # Sのindex[i]番目を左端としたときの1の連続回数の最大値をそれぞれ求める
        if K >= zeros: # Kがzeros以上なら0の連続を全て1に置き換えて終了
            print(len(S))
            sys.exit()
        
        elif i + (2 * K) <= (len(index) - 1): # 添字が飛び出ないように注意（飛び出るようなやつはそもそも最大値の候補にならない）
            if i % 2 == 0: # 左端が0の場合
                length.append(index[(i + (2 * K) - 1) + 1] - index[i])
            else: # 左端が1の場合
                if i + (2 * K) < (len(index) - 1):
                    length.append(index[i + (2 * K) + 1] - index[i])
                elif i + (2 * K) == (len(index) - 1):
                    length.append((len(S) - 1) - index[i] + 1)
        elif (i + (2 * K) == len(index) and i % 2 == 1):
            length.append(len(S) - index[i])
                
                
    print(max(length))
    sys.exit()

else: # Sが1から始まる場合
    
    for i in range(len(index)): # zerosの値を取得
        if len(index) % 2 == 0: # indexの長さが偶数である場合
            zeros = int(len(index) / 2)
        else: # indexの長さが奇数である場合
            zeros = int((len(index) - 1) / 2) 
            
    for i in range(len(index)): # max(length)の初期値を取得
        if S[len(S) - 1] == 0: # Sが0で終わっている場合
            if i % 2 == 0:
                length.append(index[i + 1] - index[i])
        else: # Sが1で終わっている場合
            if (i % 2 ==0 and (i + 1) < len(index)):
                length.append(index[i + 1] - index[i])
            elif (i % 2 ==0 and (i + 1) == len(index)): # 最後の1の連続を処理
                length.append((len(S) - 1) - index[i] + 1)

    for i in range(len(index)): # Sのindex[i]番目を左端としたときの1の連続回数の最大値をそれぞれ求める
        if K >= zeros: # Kがzeros以上なら0の連続を全て1に置き換えて終了
            print(len(S))
            sys.exit()
        
        elif i + (2 * K) <= (len(index) - 1): # 添字が飛び出ないように注意（飛び出るようなやつはそもそも最大値の候補にならない）
            if i % 2 == 0: # 左端が1の場合
                if i + (2 * K) < (len(index) - 1):
                    length.append(index[i + (2 * K) + 1] - index[i])
                elif i + (2 * K) == (len(index) - 1):
                    length.append((len(S) - 1) - index[i] + 1)
                
            else: # 左端が0の場合
                length.append(index[(i + (2 * K) - 1) + 1] - index[i])

                
    print(max(length))
    sys.exit()

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import bisect
import heapq
import math
import random
import sys
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations
from operator import add, mul, sub

sys.setrecursionlimit(100000)


def read_int():
    return int(input())


def read_int_n():
    return list(map(int, input().split()))


def read_float():
    return float(input())


def read_float_n():
    return list(map(float, input().split()))


def read_str():
    return input().strip()


def read_str_n():
    return list(map(str, input().split()))


def error_print(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.time()
        ret = f(*args, **kwargs)
        e = time.time()

        error_print(e - s, 'sec')
        return ret

    return wrap


@mt
def slv(N, K, S):
    s1 = list(map(lambda x: len(x), filter(lambda x: x != '', S.split('0'))))
    s0 = list(map(lambda x: len(x), filter(lambda x: x != '', S.split('1'))))
    if len(s0) <= K:
        return N

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

    q = deque()
    q.append(s1.pop())
    q.append(s0.pop())
    q.append(s1.pop())
    ans = 0
    tmp = sum(q)
    while s1 or s0:
        if len(q) >= 2*K + 1:
            tmp -= q.popleft()
            tmp -= q.popleft()
        q.append(s0.pop())
        tmp += q[-1]
        q.append(s1.pop())
        tmp += q[-1]
        ans = max(ans, tmp)

    return ans


def main():
    N, K = read_int_n()
    S = read_str()
    print(slv(N, K, S))

    # N = 20
    # K = random.randint(1, 10)
    # S = ''.join(random.choices('01', k=N))
    # rS = ''.join(['0' if c == '1' else '1' for c in S])
    # print(max(slv(N, K, S), slv(N, K-1, rS)))

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys

N, K = [int(x) for x in input().split()] # N:Sの長さ K:操作を行える回数の最大値
S = [int(x) for x in list(input())] # S:0と1から成る列
index = [0] # index:Sにおいて0と1が切り替わる部分の添字を集めたリスト
length = [0] # length:1が連続して並んでいる長さの最大値の候補を集めたリスト
zeros = 0 # zeros:Sにおいて0が連続して並んでいる箇所がいくつあるか

for i in range(0, len(S) - 1): # indexの要素を取得
    if S[i] != S[i + 1]: # 0と1の変わり目を探索
        index.append(i + 1) 

if S[0] == 0: # Sが0から始まる場合
    
    for i in range(len(index)): # zerosの値を取得
        if len(index) % 2 == 0: # indexの長さが偶数である場合
            zeros = int(len(index) / 2)
        else: # indexの長さが奇数である場合
            zeros = int((len(index) + 1) / 2) 
            
    for i in range(len(index)): # max(length)の初期値を取得
        if S[len(S) - 1] == 0: # Sが0で終わっている場合
            if i % 2 == 1:
                length.append(index[i + 1] - index[i])
        else: # Sが1で終わっている場合
            if (i % 2 ==1 and (i + 1) < len(index)):
                length.append(index[i + 1] - index[i])
            elif (i % 2 ==1 and (i + 1) == len(index)): # 最後の1の連続を処理
                length.append((len(S) - 1) - index[i] + 1)

    for i in range(len(index)): # Sのindex[i]番目を左端としたときの1の連続回数の最大値をそれぞれ求める
        if K >= zeros: # Kがzeros以上なら0の連続を全て1に置き換えて終了
            print(len(S))
            sys.exit()
        
        elif i + (2 * K) <= len(index): # 添字が飛び出ないように注意（飛び出るようなやつはそもそも最大値の候補にならない）
            if i % 2 == 0: # 左端が0の場合
                if (i + (2 * K) - 1) < len(index) - 1:
                    length.append(index[(i + (2 * K) - 1) + 1] - index[i])
                elif (i + (2 * K) - 1) == len(index) - 1:
                    length.append((len(S) - 1) - index[i] + 1)
                elif (i + (2 * K) - 1) == len(index):
                    length.append(len(S) - index[i])
            else: # 左端が1の場合
                if i + (2 * K) < (len(index) - 1):
                    length.append(index[i + (2 * K) + 1] - index[i])
                elif i + (2 * K) == (len(index) - 1):
                    length.append((len(S) - 1) - index[i] + 1)
                elif i + (2 * K) == len(index):
                    length.append(len(S) - index[i])
                
    print(length)
    sys.exit()

else: # Sが1から始まる場合
    
    for i in range(len(index)): # zerosの値を取得
        if len(index) % 2 == 0: # indexの長さが偶数である場合
            zeros = int(len(index) / 2)
        else: # indexの長さが奇数である場合
            zeros = int((len(index) - 1) / 2) 
            
    for i in range(len(index)): # max(length)の初期値を取得
        if S[len(S) - 1] == 0: # Sが0で終わっている場合
            if i % 2 == 0:
                length.append(index[i + 1] - index[i])
        else: # Sが1で終わっている場合
            if (i % 2 ==0 and (i + 1) < len(index)):
                length.append(index[i + 1] - index[i])
            elif (i % 2 ==0 and (i + 1) == len(index)): # 最後の1の連続を処理
                length.append((len(S) - 1) - index[i] + 1)

    for i in range(len(index)): # Sのindex[i]番目を左端としたときの1の連続回数の最大値をそれぞれ求める
        if K >= zeros: # Kがzeros以上なら0の連続を全て1に置き換えて終了
            print(len(S))
            sys.exit()
        
        elif i + (2 * K) <= len(index): # 添字が飛び出ないように注意（飛び出るようなやつはそもそも最大値の候補にならない）
            if i % 2 == 0: # 左端が1の場合
                if i + (2 * K) < (len(index) - 1):
                    length.append(index[i + (2 * K) + 1] - index[i])
                elif i + (2 * K) == (len(index) - 1):
                    length.append((len(S) - 1) - index[i] + 1)
                elif i + (2 * K) == len(index):
                    length.append(len(S) - index[i])
                
            else: # 左端が0の場合
                if i + (2 * K) - 1 < (len(index) - 1):
                    length.append(index[(i + (2 * K) - 1) + 1] - index[i])
                elif i + (2 * K) - 1 == (len(index) - 1):
                    length.append((len(S) - 1) - index[i] + 1)
                elif i + (2 * K) - 1 == len(index):
                    length.append(len(S) - index[i])

                
    print(length)
    sys.exit()

Submission status: Wrong Answer

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

streak = 1
arr = [[],[]]
for i in range(n):
    if i== n-1:
        arr[0].append(int(s[i]))
        arr[1].append(streak)
        break
    if s[i] != s[i+1]:
        arr[0].append(int(s[i]))
        arr[1].append(streak)
        streak = 0
    streak += 1
print(arr)

ans = 0
for i in range(len(arr[0])):
    if arr[0][i] == 0:
        ans = max(ans,sum(arr[1][i:i+2*k]))
        # print(arr[1][i:i+2*k])
    else:
        ans = max(ans,sum(arr[1][i:i+2*k+1]))
        # print(arr[1][i:i+2*k+1])
print(ans)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import bisect
import heapq
import math
import random
import sys
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations
from operator import add, mul, sub

sys.setrecursionlimit(100000)


def read_int():
    return int(input())


def read_int_n():
    return list(map(int, input().split()))


def read_float():
    return float(input())


def read_float_n():
    return list(map(float, input().split()))


def read_str():
    return input().strip()


def read_str_n():
    return list(map(str, input().split()))


def error_print(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.time()
        ret = f(*args, **kwargs)
        e = time.time()

        error_print(e - s, 'sec')
        return ret

    return wrap


@mt
def slv(N, K, S):
    s1 = list(map(lambda x: len(x), filter(lambda x: x != '', S.split('0'))))
    s0 = list(map(lambda x: len(x), filter(lambda x: x != '', S.split('1'))))
    if len(s0) <= K:
        return N

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

    q = deque()
    q.append(s1.pop())
    q.append(s0.pop())
    q.append(s1.pop())
    ans = 0
    tmp = sum(q)
    while s1 or s0:
        if len(q) >= 2*K + 1:
            tmp -= q.popleft()
            tmp -= q.popleft()
        if s0:
            q.append(s0.pop())
            tmp += q[-1]
        if s1:
            q.append(s1.pop())
            tmp += q[-1]
        ans = max(ans, tmp)

    return ans


def main():
    N, K = read_int_n()
    S = read_str()
    rS = ''.join(['0' if c == '1' else '1' for c in S])
    print(max(slv(N, K, S), slv(N, K-1, rS)))

    # N = 20
    # K = random.randint(1, 10)
    # S = ''.join(random.choices('01', k=N))
    # rS = ''.join(['0' if c == '1' else '1' for c in S])
    # print(max(slv(N, K, S), slv(N, K-1, rS)))

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n,k = map(int, input().split())
s = input()
cont_list = []
counter = 0
ans = 0

if s[0] == "0":
    cont_list.append(0)
    counter = 1
else:
    counter += 1
for i in range(1,n+1):
    if i == n:
        cont_list.append(counter)
    elif s[i] != s[i-1]:
        cont_list.append(counter)
        counter = 1
    else:
        counter += 1

if s[n-1] == "0":
    cont_list.append(0)

if n == 1:
    ans = 1
else:
    ans = 0
for i in range(k,n,2):
    if ans < sum(cont_list[i-k:i+k+1]):
        ans = sum(cont_list[i-k:i+k+1])
print(ans)

Submission status: Wrong Answer

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

while K > 0:
    CHANGE_POINTER = [0,N]
    for i in range(1,N):
        if S[i-1] != S[i]:
            CHANGE_POINTER.append(i)
    
    CHANGE_POINTER = sorted(CHANGE_POINTER)
    #print(CHANGE_POINTER)
    
    DIFFER = [] 
    for i in range(len(CHANGE_POINTER)-1):
        DIFFER.append(CHANGE_POINTER[i+1]-CHANGE_POINTER[i])
    #print(DIFFER)
    
    DIF_TOT_MAX = 0
    END = len(DIFFER) - 2
    NOW = 0
    
    
    if len(CHANGE_POINTER) <= 3:
        DIF_TOT_MAX = len(S)
        break
    
    else:
        while END > NOW:
            FLAG = 0
            DIF_TOT = 0
            if NOW == 0 and S[0] == "0":
                DIF_TOT = DIFFER[0] + DIFFER[1]
            elif NOW == 0 and S[0] == "1":
                DIF_TOT = DIFFER[NOW] + DIFFER[NOW+1] + DIFFER[NOW+2]
            else:
                if DIFFER[NOW+2] > DIFFER[NOW-1]:
                    DIF_TOT += DIFFER[NOW+2] - DIFFER[NOW-1]
                else:
                    FLAG = 1
                
            if FLAG == 0:
                POINTER = NOW
                DIF_TOT_MAX = DIF_TOT
            NOW += 1
        
        #print(DIF_TOT_MAX,POINTER)
        
        if POINTER == 0 and S[0] == "0":
            for i in range(CHANGE_POINTER[POINTER],CHANGE_POINTER[POINTER+1]+1):
                S[i] = "1"
            
        else:
            for i in range(CHANGE_POINTER[POINTER],CHANGE_POINTER[POINTER+2]+1):
                S[i] = "1"
        K -= 1
print(DIF_TOT_MAX)

Submission status: Wrong Answer

Solution code:
n,k = map(int, input().split())
s = input()
cont_list = []
counter = 0
ans = 0
#左端が0なら1が0個
if s[0] == "0":
    cont_list.append(0)
    counter = 1
else:
    counter += 1
for i in range(1,n+1):
    if i == n:
        cont_list.append(counter)
    elif s[i] != s[i-1]:
        cont_list.append(counter)
        counter = 1
    else:
        counter += 1
#右端が0なら1が0個
if s[n-1] == "0":
    cont_list.append(0)
print (cont_list)

if n == 1:
    ans = 1
else:
    ans = 0
for i in range(n):
    # i = 2a + kならチェック
    if (i - k)% 2 == 0 & (i - k) >= 0:
        if ans < sum(cont_list[i-k:i+k+1]):
            ans = sum(cont_list[i-k:i+k+1])
print(ans)

# k = 1なら1,3,5の周り3つ k＝２なら2,4,6の周り５つ　k＝３なら3、5、7の周り７つ
# kから2とびNまで2k+1個分のsum

Submission status: Wrong Answer

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

while K > 0:
    CHANGE_POINTER = [0,N]
    for i in range(1,N):
        if S[i-1] != S[i]:
            CHANGE_POINTER.append(i)
    
    CHANGE_POINTER = sorted(CHANGE_POINTER)
    #print(CHANGE_POINTER)
    
    DIFFER = [] 
    for i in range(len(CHANGE_POINTER)-1):
        DIFFER.append(CHANGE_POINTER[i+1]-CHANGE_POINTER[i])
    #print(DIFFER)
    
    DIF_TOT_MAX = 0
    END = len(DIFFER) - 2
    NOW = 0
    
    if len(CHANGE_POINTER) <= 3:
        DIF_TOT_MAX = len(S)
        break
    
    else:
        while END > NOW:
            DIF_TOT = 0
            if NOW == 0 and S[0] == "0":
                DIF_TOT = DIFFER[0] + DIFFER[1]
            else:
                DIF_TOT = DIFFER[NOW] + DIFFER[NOW+1] + DIFFER[NOW+2]
            
            if DIF_TOT > DIF_TOT_MAX:
                POINTER = NOW
                DIF_TOT_MAX = DIF_TOT
            NOW += 1
        
        #print(DIF_TOT_MAX,POINTER)
        
        if POINTER == 0 and S[0] == "0":
            for i in range(CHANGE_POINTER[POINTER],CHANGE_POINTER[POINTER+1]+1):
                S[i] = "1"
            
        else:
            for i in range(CHANGE_POINTER[POINTER],CHANGE_POINTER[POINTER+2]+1):
                S[i] = "1"
        K -= 1
print(DIF_TOT_MAX)

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)

ones1 = []
for k in range(0, len(ones), 2):
    temp_k = 0
    if k < len(ones):
        temp_k += ones[k]
    if (k + 1) < len(ones):
        temp_k += ones[k + 1]
    if (k + 2) < len(ones):
        temp_k += ones[k + 2]
    ones1.append(temp_k)


f = True
res = 0
for k in range(K):
    if k >= len(ones1):
        print(res)
        f = False
        break
    res += ones1[k]

temp = 0
if f:
    for k in range(k, len(ones1), 2):

        temp_k = 0
        if k < len(ones):
            temp_k += ones[k] - 1
        if (k + 1) < len(ones):
            temp_k += ones[k + 1] - 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 = list(map(int, input().split()))
s = str(input())

num = s.replace('10', '1,0')
num = num.replace('01', '0,1').split(',')
num = list(map(len, num))

if s[0] == '0':
    num.insert(0, 0)
if s[-1] == '0':
    num.append(0)

ans = 0
for i in range(0, len(num)-k, 2):
    
    left = i
    right = i + 2*k + 1
    length = sum(num[left:right])
    ans = max(ans, length)
    
if s.count('1') == len(s):
    print(len(s))
else:
    print(ans)

Submission status: Wrong Answer

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

num = s.replace('10', '1,0')
num = num.replace('01', '0,1').split(',')
num = list(map(len, num))

if s[0] == '0':
    num.insert(0, 0)
if s[-1] == '0':
    num.append(0)

ans = 0
for i in range(0, len(num)-2*k, 2):
    
    left = i
    right = i + 2*k + 1
    length = sum(num[left:right])
    ans = max(ans, length)
    
if s.count('1') == len(s):
    print(len(s))
else:
    print(ans)

Submission status: Wrong Answer

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

one = list(map(len, s.replace('0', ' ').split()))
zero = list(map(len, s.replace('1', ' ').split()))
if s[0] == '0':
    one.insert(0, 0)
if s[-1] == '0':
    one.append(0)
    
ans = 0
for i in range(len(zero)-k+1):
    length = sum(one[i:i+k+1]) + sum(zero[i:i+k])
    ans = max(ans, length)
    
if s.count('1') == len(s):
    print(len(s))
else:
    print(ans)

Submission status: Wrong Answer

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

one = list(map(len, s.replace('0', ' ').split()))
zero = list(map(len, s.replace('1', ' ').split()))
if s[0] == '0':
    one.insert(0, 0)
if s[-1] == '0':
    one.append(0)
    
ans = 0
for i in range(len(zero)-k):
    length = sum(one[i:i+k+1]) + sum(zero[i:i+k])
    ans = max(ans, length)
    
if s.count('1') == len(s):
    print(len(s))
else:
    print(ans)

Submission status: Wrong Answer

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

one = list(map(len, s.replace('0', ' ').split()))
zero = list(map(len, s.replace('1', ' ').split()))
if s[0] == '0':
    one.insert(0, 0)
if s[-1] == '0':
    one.append(0)
    
ans = 0
for i in range(len(zero)):
    length = sum(one[i:i+k+1]) + sum(zero[i:i+k])
    ans = max(ans, length)
    
print(ans)

Submission status: Wrong Answer

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

now = 1
cnt = 0
li = []

for i in range(n):
    if s[i] == str(now):
        cnt += 1
    else:
        li.append(cnt)
        now = 1 - now
        cnt = 1

if len(li)%2 == 0:
    li.append(cnt)

    

left = 0
right = 2*k + 1
length = sum(li[left:right])
ans = length

for i in range(2, len(li)+2, 2):
    
    length -= sum(li[left:left+2])
    
    left = i
    right = i + 2*k + 1
    
    length += sum(li[right-2:right])

    ans = max(ans, length)
            
print(ans)

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
S=input()
#print(N,K,S)
#print(type(S[0]))

def LINE_CHANGER(TIME,LENGTH,LINE):
    while True:
        if TIME == 0:
            return(MAX_ONE)
            break
        else:
            #print(TIME)
            LINE_LIST = list(LINE)
            #print(LINE_LIST)
            
            NOW = 0
            MAX_ONE = 0
            COUNTER = 0
            BEFORE = 0
            POINTER = 0
            LINE_ONE = 1
            
            if NOW < LENGTH:
                while NOW < LENGTH-1:
                    #print("LINECHECK",NOW,LINE_ONE)
                    #初めの数が０の時の補正
                    if LINE[0] == "0" and NOW == 0 or COUNTER == 1:
                        #print("COUNTER = 1")
                        COUNTER = 1
                    else:
                        #print("COUNTER = 0")
                        COUNTER = 0
                        
                    if COUNTER == 1 and LINE[NOW] == "0" and LINE[NOW+1] == "1":
                        #print("BEFORECHECK")
                        BEFORE = NOW
                        #print("BEFORE=",BEFORE)
                        NOW += 1
                        LINE_ONE += 1
                        
                    elif LINE[NOW] == "1" and LINE[NOW+1] == "0":
                        COUNTER += 1
                        #print("COUNTER  +1",COUNTER)
                        
                        
                        if COUNTER == 2:
                            #print("COUNTER = 2")
                            if LINE_ONE > MAX_ONE:
                                POINTER = NOW - LINE_ONE + 1
                                MAX_ONE = LINE_ONE
                                #print("CHANGE MAX_ONE",MAX_ONE,POINTER)
                            
                            NOW = BEFORE + 1
                            LINE_ONE = 1
                        
                        else:
                            NOW += 1
                            LINE_ONE += 1
                    else:
                        NOW += 1
                        LINE_ONE += 1
                        
                
                if LINE_ONE > MAX_ONE:
                    POINTER = NOW - LINE_ONE + 1
                    MAX_ONE = LINE_ONE
                    #print("CHANGE MAX_ONE",MAX_ONE,POINTER)
                
                
                
                
                
                
                           
            #LINECHANGE
            #print("POINTER,MAX_ONE",POINTER,MAX_ONE)
            for i in range(POINTER,POINTER+MAX_ONE):
                LINE_LIST[i] = "1"
            
            LINE = ''.join(LINE_LIST)
            #print("LINECHANGE",LINE)
            TIME -= 1
            

print(LINE_CHANGER(K,N,S))