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:
def search_flip_points(S):
    context = S[0]
    flip_points = []
    for i, s in enumerate(S[1:]):
        if context != s:
            flip_points.append(i + 1)
        context = s
    return flip_points


def get_maximum_value(S, K):
    flip_points = search_flip_points(S)
    if flip_points == []:
        return len(S)
    return flip_points[min(K, len(flip_points) - 1)]


if __name__ == '__main__':
    N, K = [int(x) for x in input().split(' ')]
    S = [bool(int(x)) for x in input()]

    assert len(S) == N
    assert 1 <= N <= 10**5
    assert 1 <= K <= 10**5

    current_val = 0
    flip_points = search_flip_points(S)
    flip_points.insert(0, 0)
    for i in flip_points:
        if S[i]:
            val = get_maximum_value(S[i:], K)
        else:
            val = get_maximum_value(S[i:], K - 1)
        current_val = max(current_val, val)

    print(val)

Submission status: Wrong Answer

Solution code:
q, k = list(map(int, input().split()))
list = [int(i) for i in input()]

# 0 ,1 のまとまりを作る
sum01 = []
pre = 1
cnt = 0
# 111010
# 001010
for i, e in enumerate(list):
    if e != pre:
        sum01.append(cnt)
        cnt = 1
        pre = e
    else:
        cnt += 1
if cnt != 0:
    sum01.append(cnt)

if len(sum01) % 2 == 1:
    sum01.append(0)

# print(sum01)
Add = 2 * k + 1
ruiseki = [0]
for i, e in enumerate(sum01):
    ruiseki.append(ruiseki[i] + sum01[i])

# print(ruiseki)
answer = 0
for i in range(0, len(sum01), 2):
    left = i
    right = min(i + Add, len(sum01) - 1)
    temp = ruiseki[right] - ruiseki[left]
    answer = max(temp, answer)

print(answer)

Submission status: Wrong Answer

Solution code:
q, k = list(map(int, input().split()))
list = [int(i) for i in input()]

# 0 ,1 のまとまりを作る
sum01 = []
pre = 1
cnt = 0
# 111010
# 001010
for i, e in enumerate(list):
    if e != pre:
        sum01.append(cnt)
        cnt = 1
        pre = e
    else:
        cnt += 1
if cnt != 0:
    sum01.append(cnt)

if len(sum01) % 2 == 1:
    sum01.append(0)

# print(sum01)
Add = 2 * k + 1
ruiseki = [0]
for i, e in enumerate(sum01):
    ruiseki.append(ruiseki[i] + sum01[i])

# print(ruiseki)
answer = 0
for i in range(0, len(ruiseki), 2):
    left = i
    right = min(i + Add, len(sum01))
    temp = ruiseki[right] - ruiseki[left]
    answer = max(temp, answer)

Submission status: Wrong Answer

Solution code:
q, k = list(map(int, input().split()))
list = [int(i) for i in input()]

# 0 ,1 のまとまりを作る
sum01 = []
pre = 1
cnt = 0
# 111010
# 001010
for i, e in enumerate(list):
    if e != pre:
        sum01.append(cnt)
        cnt = 1
        pre = e
    else:
        cnt += 1
if cnt != 0:
    sum01.append(cnt)

if len(sum01) % 2 == 1:
    sum01.append(0)

#print(sum01)
Add = 2 * k + 1
ruiseki = [0]
for i, e in enumerate(sum01):
    ruiseki.append(ruiseki[i] + sum01[i])

#print(ruiseki)
answer = 0
for i in range(0, len(ruiseki), 2):
    left = i
    right = min(i + Add, len(sum01) - 1)
    temp = ruiseki[right] - ruiseki[left]
    answer = max(temp, answer)

print(answer)

Submission status: Wrong Answer

Solution code:
N, K = list(map(int, input().split()))
S = input()
 
S += "X"
i = 0
j = 0
max_len = 0
change = 0 if S[0] != "0" else 1
while j < len(S)-1:
  if change <= 2*K:
    #print(i, j)
    max_len = max([j-i+1, max_len])
    if S[j:j+2] == "10":
      change += 1
    elif S[j:j+2] == "01":
      change += 1
    j += 1
  else:
    if S[i:i+2] == "10":
      change -= 1
    elif S[i:i+2] == "01":
      change -= 1
    i += 1
  

print(max_len)

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
S=input()
po=[0]
X=[]
i=0
for i in range(N-1):
	if not S[i] == S[i+1]:
		po.append(i+1)
po.append(len(S))
for i in range(len(po)):
	if 2*K+i+1>=len(po):
		X.append(po[-1])
		break
	elif 2*K+i+1>=len(po):
		X.append(po[2*K+i]-po[i])
		break
	elif S[po[i]]=='0':
		X.append(po[2*K+i]-po[i])
	else:
		X.append(po[2*K+1+i]-po[i])
print(max(X))

Submission status: Wrong Answer

Solution code:
q, k = list(map(int, input().split()))
list = [int(i) for i in input()]

# 0 ,1 のまとまりを作る
sum01 = []
pre = 0
cnt = 1
# 111010
# 001010
for i, e in enumerate(list):
    if i == len(list) - 1:
        sum01.append(cnt)
        break;
    if e != list[i + 1]:
        sum01.append(cnt)
        cnt = 1
    else:
        cnt += 1
#print(sum01)

ruisekiwa = []
for i, e in enumerate(sum01):
    if i == 0:
        ruisekiwa.append(e)
    else:
        ruisekiwa.append(ruisekiwa[i - 1] + e)

#print(ruisekiwa)

result = 1
for i, e in enumerate(ruisekiwa):
    if 2 * i + 1 > len(ruisekiwa) - 1:
        break;
    last = 2 * (i - k) if 2 * (i - k) > 0 else 0
    if list[0] == 1:
        if last == 0:
            result = max(result, ruisekiwa[2 * i + 1] - 0)
        result = max(result, ruisekiwa[2 * i + 1] - ruisekiwa[last])
    if list[0] == 0:
        if last == 0:
            result = max(result, ruisekiwa[2 * i] - 0)
        result = max(result, ruisekiwa[2 * i] - ruisekiwa[last])

print(result)

Submission status: Wrong Answer

Solution code:
q, k = list(map(int, input().split()))
list = [int(i) for i in input()]

# 0 ,1 のまとまりを作る
sum01 = []
pre = 0
cnt = 1
# 111010
# 001010
for i, e in enumerate(list):
    if i == len(list) - 1:
        sum01.append(cnt)
        break;
    if e != list[i + 1]:
        sum01.append(cnt)
        cnt = 1
    else:
        cnt += 1
#print(sum01)

ruisekiwa = []
for i, e in enumerate(sum01):
    if i == 0:
        ruisekiwa.append(e)
    else:
        ruisekiwa.append(ruisekiwa[i - 1] + e)

#print(ruisekiwa)

result = 1
for i, e in enumerate(ruisekiwa):
    if 2 * i + 1 > len(ruisekiwa) - 1:
        break;
    last = 2 * (i - k) if 2 * (i - k) > 0 else 0
    if last == 0:
        result = max(result, ruisekiwa[2 * i + 1] - 0)
    result = max(result, ruisekiwa[2 * i + 1] - ruisekiwa[last])

print(result)

Submission status: Wrong Answer

Solution code:
def calc_seq(xs):
    seq = []
    count = 1
    for i in range(1, len(xs) - 1):
        if xs[i] == xs[i+1]:
            count += 1
        else:
            seq.append(count)
            count = 1
    seq.append(count)
    return seq

def form_seq(xs):
    seq = calc_seq(xs)
    if xs[0] == 0:
        seq.insert(0, 0)
    if len(seq) % 2 == 0:
        seq.append(0)
    return seq

def main(n, k, xs):
    seq = form_seq(xs)
    m = len(seq)
    i = 1
    if i + 2*k > m:
        print(n)
        return
    val = sum(seq[i-1:i-1+2*k])
    biggest = val
    i += 2
    while i + 2*k <= m:
        val += sum(seq[i-1+2*k:i-1+2*k]) - sum(seq[i-1-2:i-1-1])
        if biggest < val:
            biggest = val
        i += 2
    print(biggest)

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

main(n, k, xs)

Submission status: Wrong Answer

Solution code:
N, K = list(map(int, input().split()))
S = input()
 
S += "X"
j = 0
count = 1
change = [] if S[0] != "0" else [0]
for j in range(len(S)-1):
  if S[j:j+2] == "10":
    change.append(count)
    count = 0
  elif S[j:j+2] == "01":
    change.append(count)
    count = 0
  count += 1

change.append(count-1)
#print(change)
K = 2*K + 1
max_len = max([sum(change[i:i+K]) for i in range(0, len(change), 2)])

Submission status: Wrong Answer

Solution code:
N, K = list(map(int, input().split()))
S = input()
 
S += "X"
j = 0
count = 1
change = [] if S[0] != "0" else [0]
for j in range(len(S)-1):
  if S[j:j+2] == "10":
    change.append(count)
    count = 0
  elif S[j:j+2] == "01":
    change.append(count)
    count = 0
  count += 1

change.append(count-1)
#print(change)
max_len = 0
for i in (0, len(change), 2):
  max_len = max([sum(change[i:i+2*K+2]), max_len])
  
print(max_len)

Submission status: Wrong Answer

Solution code:
N,k = map(int,input().split())
S = input()
lenth = 0
AllLen =[]
preS0,preS1 = S.split('1'),S.split('0')
S0 = [i for i in preS0 if i != '']
S1 = [i for i in preS1 if i != '']
num0 = k
if len(S0) <= k:
	print(N)
	exit() 
if S[0] == '0':
	J = k
else:J = k + 1
FirstLen = sum([len(S0[i]) for i in range(k)]) + sum([len(S1[j]) for j in range(J)])
AllLen.append(FirstLen)
CheckLen = FirstLen
if S[0] == '1' and S[-1] == '1':
    for i in range(len(S0) - k):
	    CheckLen = CheckLen - len(S0[i]) - len(S1[i]) + len(S0[i + k]) + len(S1[i + k + 1])
	    AllLen.append(CheckLen)
if S[0] == '0' and S[-1] == '1':
	CheckLen = CheckLen - len(S0[0]) + len(S0[k]) + len(S1[k])
	AllLen.append(CheckLen)
	for i in range(len(S0) - k - 1):
		CheckLen = CheckLen - len(S0[i + 1]) - len(S1[i + 1]) + len(S0[i + k + 1]) + len(S1[i + k + 1])
		AllLen.append(CheckLen)
if S[0] == '1' and S[-1] == '0':
	for i in range(len(S0) - k - 1):
		CheckLen = CheckLen - len(S0[i]) - len(S1[i]) + len(S0[i + k]) + len(S1[i + k + 1])
		AllLen.append(CheckLen)
	CheckLen = CheckLen - len(S0[len(S0) - k - 1]) - len(S1[len(S0) - k - 1]) + len(S0[len(S0) - 1])
	AllLen.append(CheckLen)
if S[0] == '0' and S[-1] == '0':
	if len(S1) == 1:
		print(len(max(S0)) + len(S1[0]))
		exit()
	CheckLen = CheckLen - len(S0[0]) + len(S0[k]) + len(S1[k])
	AllLen.append(CheckLen)
	for i in range(len(S0) - k - 2):
		CheckLen = CheckLen - len(S0[i + 1]) - len(S1[i + 1]) + len(S0[i + k + 1]) + len(S1[i + k + 1])
		AllLen.append(CheckLen)
	CheckLen = CheckLen - len(S0[len(S0) - k - 1]) - len(S1[len(S0) - k - 1]) + len(S0[len(S0) - 1])
	AllLen.append(CheckLen)
print(max(AllLen))

Submission status: Wrong Answer

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

seq = []

if s[0] == '1':
    seq.append(0)

prev = s[0]
i = 0
"""
while i < n:
    f = True
    for j in range(i+1, n):
        if s[j] != prev:
            seq.append(j-i)
            f = False
            prev = s[j]
            break
    if f:
        i += 1
    else:
        i = j
"""
# 1回のforでseqを取得する
prev = s[0]
seq_val = 1
for i in range(1, n):
    if s[i] == prev:
        seq_val += 1
    else:
        seq.append(seq_val)
        seq_val = 1
        prev = s[i]

last_length = 1
prev = s[-1]
for i, c in enumerate(s[::-1][1:]):
    if c != prev:
        last_length += i
        break
seq += [last_length]

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

print(ans)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-

# スペース区切りの整数の入力
n, k = map(int, input().split())

# 01文字列の入力
s = str(input())

sakaMax = 0

for a in range (2):
    if a == 0:
        # 01文字列を配列に一文字ずつ格納
        hito = list(s)
        sakaCnt = 0
        irekae = 0
        prevIsZero = False
        startIndex = 0
    else:
        # 01文字列を配列に一文字ずつ格納
        hito = list(s)
        # 2回目は逆順にして後ろからやってみる
        hito.reverse()

        sakaCnt = 0
        irekae = 0
        prevIsZero = False
        startIndex = 0

    tmp01Cnt = 0
    max01Cnt = 0
    # 0 or 1が続いて出現するところを探す
    tmp01 = hito[0]
    kirikae = 0
    for h in range(1,len(hito)):
        if tmp01 == hito[h]:
            tmp01Cnt += 1
            if tmp01Cnt > max01Cnt:
                max01Cnt = tmp01Cnt
                startIndex = h-tmp01Cnt
        else:
            # 1 <-> 0 に切り替わったらインクリメント
            kirikae += 1
            # 1 <-> 0 の切り替わりが(k)回超えたらまた0からカウント
            if kirikae > k * 2 :
                tmp01Cnt = 0
                kirikae = 0
            tmp01 = hito[h]

        if h == len(hito)-1 and  tmp01Cnt > max01Cnt:
            max01Cnt = tmp01Cnt
            startIndex = h-tmp01Cnt
    
    # print ("startIndex:{}".format(startIndex))

    # # とりあえず頭から0を1に変換してみる
    # for i in range(len(hito)):
    # 0 or 1が続いて出現するところ以降でやってみる
    for i in range(startIndex, len(hito)):
        if hito[i] == "1":
            if prevIsZero == True:
                irekae += 1
                prevIsZero = False
        else:
            if irekae < k:
                hito[i] = "1"
            if prevIsZero == False:
                prevIsZero = True

    prevIsZero = False
    for j in range(len(hito)):
        # print ("hito[{}]:{}".format(str(j),hito[j]))
        if hito[j] == "1":
            sakaCnt += 1
        else:
            if sakaMax < sakaCnt:
                sakaMax = sakaCnt
            sakaCnt = 0

    if sakaMax < sakaCnt:
        sakaMax = sakaCnt

# 出力
print(sakaMax)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-

# スペース区切りの整数の入力
n, k = map(int, input().split())

# 01文字列の入力
s = str(input())

sakaMax = 0

for a in range (2):
    if a == 0:
        # 01文字列を配列に一文字ずつ格納
        hito = list(s)
        sakaCnt = 0
        irekae = 0
        prevIsZero = False
        startIndex = 0
    else:
        # 01文字列を配列に一文字ずつ格納
        hito = list(s)
        # 2回目は逆順にして後ろからやってみる
        hito.reverse()

        sakaCnt = 0
        irekae = 0
        prevIsZero = False
        startIndex = 0

    tmp01Cnt = 0
    max01Cnt = 0
    # 0 or 1が続いて出現するところを探す
    tmp01 = hito[0]
    kirikae = 0
    for h in range(1,len(hito)):
        if tmp01 == hito[h]:
            tmp01Cnt += 1
            if tmp01Cnt > max01Cnt:
                max01Cnt = tmp01Cnt
                startIndex = h-tmp01Cnt
        else:
            # 1 <-> 0 に切り替わったらインクリメント
            kirikae += 1
            # 1 <-> 0 の切り替わりが(k)回超えたらまた0からカウント
            if kirikae > k :
                tmp01Cnt = 0
            tmp01 = hito[h]

        if h == len(hito)-1 and  tmp01Cnt > max01Cnt:
            max01Cnt = tmp01Cnt
            startIndex = h-tmp01Cnt
    
    # print ("startIndex:{}".format(startIndex))

    # # とりあえず頭から0を1に変換してみる
    # for i in range(len(hito)):
    # 0 or 1が続いて出現するところ以降でやってみる
    for i in range(startIndex, len(hito)):
        if hito[i] == "1":
            if prevIsZero == True:
                irekae += 1
                prevIsZero = False
        else:
            if irekae < k:
                hito[i] = "1"
            if prevIsZero == False:
                prevIsZero = True

    prevIsZero = False
    for j in range(len(hito)):
        # print ("hito[{}]:{}".format(str(j),hito[j]))
        if hito[j] == "1":
            sakaCnt += 1
        else:
            if sakaMax < sakaCnt:
                sakaMax = sakaCnt
            sakaCnt = 0

    if sakaMax < sakaCnt:
        sakaMax = sakaCnt

# 出力
print(sakaMax)

Submission status: Wrong Answer

Solution code:
while True:

    N,k = map(int,input().split())
    S = input()
    lenth = 0
    AllLen =[]
    preS0,preS1 = S.split('1'),S.split('0')
    S0 = [i for i in preS0 if i != '']
    S1 = [i for i in preS1 if i != '']
    num0 = k
    if len(S0) <= k:
        print(len(S))
        break
    if S[0] == '0':
        J = k
    else:J = k + 1
    FirstLen = sum([len(S0[i]) for i in range(k)]) + sum([len(S1[j]) for j in range(J)])
    AllLen.append(FirstLen)
    CheckLen = FirstLen
    if S[0] == '1' and S[-1] == '1':
        for i in range(len(S0) - k):
            CheckLen = CheckLen - len(S0[i]) - len(S1[i]) + len(S0[i + k]) + len(S1[i + k + 1])
            AllLen.append(CheckLen)
    if S[0] == '0' and S[-1] == '1':
        CheckLen = CheckLen - len(S0[0]) + len(S0[k]) + len(S1[k])
        AllLen.append(CheckLen)
        for i in range(len(S0) - k - 1):
            CheckLen = CheckLen - len(S0[i + 1]) - len(S1[i + 1]) + len(S0[i + k + 1]) + len(S1[i + k + 1])
            AllLen.append(CheckLen)
    if S[0] == '1' and S[-1] == '0':
        for i in range(len(S0) - k - 1):
            CheckLen = CheckLen - len(S0[i]) - len(S1[i]) + len(S0[i + k]) + len(S1[i + k + 1])
            AllLen.append(CheckLen)
        CheckLen = CheckLen - len(S0[len(S0) - k - 1]) - len(S1[len(S0) - k - 1]) + len(S0[len(S0) - 1])
        AllLen.append(CheckLen)
    if S[0] == '0' and S[-1] == '0':
        if len(S1) == 1:
            print(len(max(S0)) + len(S1[0]))
            exit()
        CheckLen = CheckLen - len(S0[0]) + len(S0[k]) + len(S1[k])
        AllLen.append(CheckLen)
        for i in range(len(S0) - k - 2):
            CheckLen = CheckLen - len(S0[i + 1]) - len(S1[i + 1]) + len(S0[i + k + 1]) + len(S1[i + k + 1])
            AllLen.append(CheckLen)
        CheckLen = CheckLen - len(S0[len(S0) - k - 1]) - len(S1[len(S0) - k - 1]) + len(S0[len(S0) - 1])
        AllLen.append(CheckLen)
    print(max(AllLen))
    break

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-

# スペース区切りの整数の入力
n, k = map(int, input().split())

# 01文字列の入力
s = str(input())

sakaMax = 0

for a in range (2):
    if a == 0:
        # 01文字列を配列に一文字ずつ格納
        hito = list(s)
        sakaCnt = 0
        irekae = 0
        prevIsZero = False
        startIndex = 0
    else:
        # 01文字列を配列に一文字ずつ格納
        hito = list(s)
        # 2回目は逆順にして後ろからやってみる
        hito.reverse()

        sakaCnt = 0
        irekae = 0
        prevIsZero = False
        startIndex = 0

    tmp01Cnt = 0
    max01Cnt = 0
    # 0 or 1が続いて出現するところを探す
    tmp01 = hito[0]
    kirikae = 0
    for h in range(1,len(hito)):
        if tmp01 == hito[h]:
            tmp01Cnt += 1
            if tmp01Cnt > max01Cnt:
                max01Cnt = tmp01Cnt
                startIndex = h-tmp01Cnt
        else:
            # 1 <-> 0 に切り替わったらインクリメント
            kirikae += 1
            # 1 <-> 0 の切り替わりが(k * 3)回超えたらまた0からカウント
            if kirikae > k * 3:
                tmp01Cnt = 0
            tmp01 = hito[h]

        if h == len(hito)-1 and  tmp01Cnt > max01Cnt:
            max01Cnt = tmp01Cnt
            startIndex = h-tmp01Cnt
    
    # print ("startIndex:{}".format(startIndex))

    # # とりあえず頭から0を1に変換してみる
    # for i in range(len(hito)):
    # 0 or 1が続いて出現するところ以降でやってみる
    for i in range(startIndex, len(hito)):
        if hito[i] == "1":
            if prevIsZero == True:
                irekae += 1
                prevIsZero = False
        else:
            if irekae < k:
                hito[i] = "1"
            if prevIsZero == False:
                prevIsZero = True

    prevIsZero = False
    for j in range(len(hito)):
        # print ("hito[{}]:{}".format(str(j),hito[j]))
        if hito[j] == "1":
            sakaCnt += 1
        else:
            if sakaMax < sakaCnt:
                sakaMax = sakaCnt
            sakaCnt = 0

    if sakaMax < sakaCnt:
        sakaMax = sakaCnt

# 出力
print(sakaMax)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-

# スペース区切りの整数の入力
n, k = map(int, input().split())

# 01文字列の入力
s = str(input())

sakaMax = 0

for a in range (2):
    if a == 0:
        # 01文字列を配列に一文字ずつ格納
        hito = list(s)
        sakaCnt = 0
        irekae = 0
        prevIsZero = False
        startIndex = 0
    else:
        # 01文字列を配列に一文字ずつ格納
        hito = list(s)
        # 2回目は逆順にして後ろからやってみる
        hito.reverse()

        sakaCnt = 0
        irekae = 0
        prevIsZero = False
        startIndex = 0

    tmp01Cnt = 0
    max01Cnt = 0
    # 0 or 1が続いて出現するところを探す
    tmp01 = hito[0]
    kirikae = 0
    for h in range(1,len(hito)):
        if tmp01 == hito[h]:
            tmp01Cnt += 1
            if tmp01Cnt > max01Cnt:
                max01Cnt = tmp01Cnt
                startIndex = h-tmp01Cnt
        else:
            # 1 <-> 0 に切り替わったらインクリメント
            kirikae += 1
            # 1 <-> 0 の切り替わりが(k * 2)回超えたらまた0からカウント
            if kirikae > k * 2:
                tmp01Cnt = 0
            tmp01 = hito[h]

        if h == len(hito)-1 and  tmp01Cnt > max01Cnt:
            max01Cnt = tmp01Cnt
            startIndex = h-tmp01Cnt
    
    # print ("startIndex:{}".format(startIndex))

    # # とりあえず頭から0を1に変換してみる
    # for i in range(len(hito)):
    # 0 or 1が続いて出現するところ以降でやってみる
    for i in range(startIndex, len(hito)):
        if hito[i] == "1":
            if prevIsZero == True:
                irekae += 1
                prevIsZero = False
        else:
            if irekae < k:
                hito[i] = "1"
            if prevIsZero == False:
                prevIsZero = True

    prevIsZero = False
    for j in range(len(hito)):
        # print ("hito[{}]:{}".format(str(j),hito[j]))
        if hito[j] == "1":
            sakaCnt += 1
        else:
            if sakaMax < sakaCnt:
                sakaMax = sakaCnt
            sakaCnt = 0

    if sakaMax < sakaCnt:
        sakaMax = sakaCnt

# 出力
print(sakaMax)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-

# スペース区切りの整数の入力
n, k = map(int, input().split())

# 01文字列の入力
s = str(input())

sakaMax = 0

for a in range (2):
    if a == 0:
        # 01文字列を配列に一文字ずつ格納
        hito = list(s)
        sakaCnt = 0
        irekae = 0
        prevIsZero = False
        startIndex = 0
    else:
        # 01文字列を配列に一文字ずつ格納
        hito = list(s)
        # 2回目は逆順にして後ろからやってみる
        hito.reverse()

        sakaCnt = 0
        irekae = 0
        prevIsZero = False
        startIndex = 0

    tmp01Cnt = 0
    max01Cnt = 0
    # 0 or 1が続いて出現するところを探す
    tmp01 = hito[0]
    kirikae = 0
    for h in range(1,len(hito)):
        if tmp01 == hito[h]:
            tmp01Cnt += 1
            if tmp01Cnt > max01Cnt:
                max01Cnt = tmp01Cnt
                startIndex = h-tmp01Cnt
        else:
            # 1 <-> 0 に切り替わったらインクリメント
            kirikae += 1
            # 1 <-> 0 の切り替わりが4回超えたらまた0からカウント
            if kirikae > 4:
                tmp01Cnt = 0
            tmp01 = hito[h]

        if h == len(hito)-1 and  tmp01Cnt > max01Cnt:
            max01Cnt = tmp01Cnt
            startIndex = h-tmp01Cnt
    
    # print ("startIndex:{}".format(startIndex))

    # # とりあえず頭から0を1に変換してみる
    # for i in range(len(hito)):
    # 0 or 1が続いて出現するところ以降でやってみる
    for i in range(startIndex, len(hito)):
        if hito[i] == "1":
            if prevIsZero == True:
                irekae += 1
                prevIsZero = False
        else:
            if irekae < k:
                hito[i] = "1"
            if prevIsZero == False:
                prevIsZero = True

    prevIsZero = False
    for j in range(len(hito)):
        # print ("hito[{}]:{}".format(str(j),hito[j]))
        if hito[j] == "1":
            sakaCnt += 1
        else:
            if sakaMax < sakaCnt:
                sakaMax = sakaCnt
            sakaCnt = 0

    if sakaMax < sakaCnt:
        sakaMax = sakaCnt

# 出力
print(sakaMax)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-

# スペース区切りの整数の入力
n, k = map(int, input().split())

# 01文字列の入力
s = str(input())

for a in range (2):
    if a == 0:
        # 01文字列を配列に一文字ずつ格納
        hito = list(s)
        sakaCnt = 0
        sakaMax = 0
        irekae = 0
        prevIsZero = False
        startIndex = 0
    else:
        # 01文字列を配列に一文字ずつ格納
        hito = list(s)
        # 2回目は逆順にして後ろからやってみる
        hito.reverse()

        sakaCnt = 0
        sakaMax = 0
        irekae = 0
        prevIsZero = False
        startIndex = 0

    # 0 or 1が続いて出現するところを探す
    for h in range(len(hito)-1):
        if hito[h] == hito[h+1]:
            startIndex = h
            break

    # # とりあえず頭から0を1に変換してみる
    # for i in range(len(hito)):
    # 0 or 1が続いて出現するところ以降でやってみる
    for i in range(startIndex, len(hito)):
        if hito[i] == "1":
            if prevIsZero == True:
                irekae += 1
                prevIsZero = False
        else:
            if irekae < k:
                hito[i] = "1"
            if prevIsZero == False:
                prevIsZero = True

    prevIsZero = False
    for j in range(len(hito)):
        # print ("hito[{}]:{}".format(str(j),hito[j]))
        if hito[j] == "1":
            sakaCnt += 1
        else:
            if sakaMax < sakaCnt:
                sakaMax = sakaCnt
            sakaCnt = 0

    if sakaMax < sakaCnt:
        sakaMax = sakaCnt

# 出力
print(sakaMax)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-

# スペース区切りの整数の入力
n, k = map(int, input().split())

# 01文字列の入力
s = str(input())

# 01文字列を配列に一文字ずつ格納
hito = list(s)
sakaCnt = 0
sakaMax = 0
irekae = 0
prevIsZero = False

# とりあえず頭から0を1に変換してみる
for i in range(len(hito)):
    if hito[i] == "1":
        if prevIsZero == True:
            irekae += 1
            prevIsZero = False
    else:
        if irekae < k:
            hito[i] = "1"
        if prevIsZero == False:
            prevIsZero = True

prevIsZero = False
for j in range(len(hito)):
    # print ("hito[{}]:{}".format(str(j),hito[j]))
    if hito[j] == "1":
        sakaCnt += 1
    else:
        if sakaMax < sakaCnt:
            sakaMax = sakaCnt
        sakaCnt = 0

# 出力
print(sakaMax)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-

# スペース区切りの整数の入力
n, k = map(int, input().split())

# 01文字列の入力
s = str(input())

# 01文字列を配列に一文字ずつ格納
hito = list(s)
sakaCnt = 0
sakaMax = 0
irekae = 0
prevIsZero = False

# とりあえず頭から0を1に変換してみる
for i in range(len(hito)):
    if hito[i] == "1":
        if prevIsZero == True:
            irekae += 1
            prevIsZero = False
    else:
        if irekae < k:
            hito[i] = "1"
        if prevIsZero == False:
            prevIsZero = True

prevIsZero = False
for j in range(len(hito)):
    print ("hito[{}]:{}".format(str(j),hito[j]))
    if hito[j] == "1":
        sakaCnt += 1
    else:
        if sakaMax < sakaCnt:
            sakaMax = sakaCnt
        sakaCnt = 0

# 出力
print(sakaMax)

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
S=input()
po=[0]
ans,q,left,right=0,0,0,0
for i in range(1,N):
	p=S[i-1]+S[i]
	if (p=='10')|(p=='01'):
		po.append(i)
po.append(len(S))
for i in range(len(po)):
	if K*2>=len(po):
		ans=po[-1]
		break
	elif (S[0]=='0')&(right<1):
		q=po[2*K]
		left=2
		right=2*K+2
	elif (S[0]=='1')&(right<1)&(len(po)>2*K+1):
		left=3
		right=2*K+3
		q=po[2*K+1]
	elif (S[0]=='1')&(right<1):
		ans=po[-1]
		break
	elif right<len(po):
		q=po[right]-po[left]+1
		left+=2
		right+=2
	elif S[-1]=='0':
		q=po[-1]-po[-2*K]+1
		ans=max(q,ans)
		break
	elif S[-1]=='1':
		q=po[-1]-po[-2*K-1]+1
		ans=max(q,ans)
		break
	ans=max(q,ans)
print(ans)

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
S=input()
po=[0]
ans,q,left,right=0,0,0,0
for i in range(1,N):
	p=S[i-1]+S[i]
	if (p=='10')|(p=='01'):
		po.append(i)
po.append(len(S))
for i in range(len(po)):
	if K*2>=len(po):
		ans=po[-1]
		break
	elif (S[0]=='0')&(right<1):
		q=po[2*K]
		left=2
		right=2*K+2
	elif (S[0]=='1')&(right<1)&(len(po)>2*K+1):
		left=3
		right=2*K+3
		q=po[2*K+1]
		print(q)
	elif (S[0]=='1')&(right<1):
		ans=po[-1]
		break
	elif right<len(po):
		q=po[right]-po[left]+1
		left+=2
		right+=2
	elif S[-1]=='0':
		q=po[-1]-po[-2*K]+1
		ans=max(q,ans)
		break
	elif S[-1]=='1':
		q=po[-1]-po[-2*K-1]+1
		ans=max(q,ans)
		break
	ans=max(q,ans)
print(ans)

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
S=input()
po=[0]
ans,q,left,right=0,0,0,0
for i in range(1,N):
	p=S[i-1]+S[i]
	if (p=='10')|(p=='01'):
		po.append(i)
po.append(len(S))
for i in range(len(po)+1):
	if K*2>=len(po):
		ans=po[-1]
		break
	elif (S[0]=='0')&(right<1):
		q=po[2*K]
		left=2
		right=2*K+2
	elif (S[0]=='1')&(right<1):
		left=1
		right=2*K+1
	elif right<len(po):
		q=po[right]-po[left]+1
		left+=2
		right+=2
	elif S[-1]=='0':
		q=po[-1]-po[-2*K]+1
		ans=max(q,ans)
		break
	elif S[-1]=='1':
		q=po[-1]-po[-2*K-1]+1
		ans=max(q,ans)
		break
	ans=max(q,ans)
print(ans)

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
S=input()
po=[0]
ans,q,left,right=0,0,0,0
for i in range(1,N):
	p=S[i-1]+S[i]
	if (p=='10')|(p=='01'):
		po.append(i)
po.append(len(S))
for i in range(len(po)):
	if K*2>=len(po):
		ans=po[-1]
		break
	elif (S[0]=='0')&(right<1):
		q=po[2*K]
		left=2
		right=2*K+2
	elif (S[0]=='1')&(right<1):
		left=1
		right=2*K+1
	elif right<len(po):
		q=po[right]-po[left]+1
		left+=2
		right+=2
	elif S[-1]=='0':
		q=po[-1]-po[-2*K]+1
		ans=max(q,ans)
		break
	elif S[-1]=='1':
		q=po[-1]-po[-2*K-1]+1
		ans=max(q,ans)
		break
	ans=max(q,ans)
print(ans)