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() + "#"

L = []
cnt = 1

for i in range(1,len(S)) :
    if S[i] == S[i-1] :
        cnt += 1
    else :
        L.append(cnt)
        cnt = 1

ans = 0
A = []
B = []

if S[0] == "1" :
    B.extend(L[:2*K+1])
    A.append(sum(B))
    
    for i in range(2*K+2,len(L),2) :
        B.extend(L[i-1:i+1])
        
        calc = A[-1] + (B[-2] + B[-1]) - (B[0] + B[1])
        
        B.pop(0)
        B.pop(0)
        
        A.append(calc)

else :
    B.extend(L[:2*K])
    A.append(sum(B))
    B.pop(0)
    flg = 0
    for i in range(2*K+1,len(L),2) :
        if not flg :
            B.extend(L[i-1:i+1])
            calc = A[-1] + (B[-2] + B[-1]) - B[0]
            B.pop(0)
            A.append(calc)
            flg = 1
        else :
            B.extend(L[i-1:i+1])
            calc = A[-1] + (B[-2] + B[-1]) - (B[0] + B[1])
            B.pop(0)
            B.pop(0)        
            A.append(calc)

ans = max(A)

print(ans)

Submission status: Wrong Answer

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

# 累積和
a = [0]
if s[0] == "0":
	a.append(0)
for i in range(n):
	if i == n-1 or s[i+1] != s[i]:
		a.append(i+1) # 累積和

# print(s[-1])
if s[-1] == "0":
	a.append(0)

if len(a) <= 2*k+1: # 2*k+1
	print(n)
else:
	ans = 0
	for j in range(0, len(a) - 2*k - 1, 2): # len(a)は奇数
		ans = max(ans, a[j+2*k+1] - a[j])
	print(ans)

Submission status: Wrong Answer

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

# 累積和
cnt = 1
a = []
a.append(0)
if s[0] == "0":
	a.append(0)
for i in range(n-1):
	if s[i+1] != s[i]:
		a.append(cnt) # 累積和
	cnt += 1
a.append(cnt)
if s[-1] == "0":
	a.append(0)

# print(a)

if len(a) <= 2*k+1: # 2*k+1
	print(n)
else:
	ans = 0
	for j in range(0, len(a) - 2*k, 2): # len(a)は奇数
		ans = max(ans, a[j+2*k+1] - a[j])
	print(ans)

Submission status: Wrong Answer

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

# 累積和
cnt = 1
a = []
a.append(0)
if s[0] == "0":
	a.append(0)
for i in range(n-1):
	if s[i+1] != s[i]:
		a.append(cnt) # 累積和
	cnt += 1
a.append(cnt)
if s[-1] == "0":
	a.append(0)

# print(a)

if len(a) <= 2*k+1: # 2*k+1
	print(n)
else:
	ans = 0
	for j in range(0, len(a) - 2*k - 1, 2): # len(a)は奇数
		ans = max(ans, a[j+2*k+1] - a[j])
	print(ans)

Submission status: Wrong Answer

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

# print(n, k, s)

prevcCt = 0 # 前に連続逆立ちの数
nextCnt = 0 # 後ろに連続逆立ちの数

# "00010" ⇒ [-3, 1, -1]のような配列に変換

val = 0
vals = []
for i in range(len(s)):
	if s[i] == "0":
		val -= 1
	else:
		val += 1

	#print(i, val, s[i], s[i+1])
	# print(i,len(s))
	if i == len(s) - 1 or s[i] != s[i+1]:
		vals.append(val)
		val = 0

#print(vals)

#操作しやすいようにマイナス要素が偶数になるように先頭に0を追加
if vals[0] < 0:
	vals.insert(0,0)

vals.append(0)
vals.append(0)

#print(vals)

vals = list(map(abs, vals))
#print(vals)


# K回操作できるということは連続した2k+1個の要素の最大値を求めればよい
max = 0
for i in range(len(vals) - k):
	#print(i, len(vals), k)
	# 偶数の時だけ判定
	if i%2 == 0:
		# print(vals[i:i + 2*k + 1])

		sumVal = sum(vals[i:i + 2*k + 1])
		# for e in vals[i:i + 2*k + 1]:
		# 	sumVal += abs(e)
		if sumVal > max:
			max = sumVal

print(max)

Submission status: Wrong Answer

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

# print(n, k, s)

prevcCt = 0 # 前に連続逆立ちの数
nextCnt = 0 # 後ろに連続逆立ちの数

# "00010" ⇒ [-3, 1, -1]のような配列に変換

val = 0
vals = []
for i in range(len(s)):
	if s[i] == "0":
		val -= 1
	else:
		val += 1

	#print(i, val, s[i], s[i+1])
	# print(i,len(s))
	if i == len(s) - 1 or s[i] != s[i+1]:
		vals.append(val)
		val = 0

print(vals)

#操作しやすいようにマイナス要素が偶数になるように先頭に0を追加
if vals[0] < 0:
	vals.insert(0,0)

vals.append(0)
vals.append(0)

print(vals)


# K回操作できるということは連続した2k+1個の要素の最大値を求めればよい
max = 0
for i in range(len(vals) - k):
	# 偶数の時だけ判定
	if i%2 == 0:
		# print(vals[i:i + 2*k + 1])

		sum = 0
		for e in vals[i:i + 2*k + 1]:
			sum += abs(e)
			if sum > max:
				max = sum

print(max)

Submission status: Wrong Answer

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

# print(n, k, s)

prevcCt = 0 # 前に連続逆立ちの数
nextCnt = 0 # 後ろに連続逆立ちの数

# "00010" ⇒ [-3, 1, -1]のような配列に変換

val = 0
vals = []
for i in range(len(s)):
	if s[i] == "0":
		val -= 1
	else:
		val += 1

	#print(i, val, s[i], s[i+1])
	# print(i,len(s))
	if i == len(s) - 1 or s[i] != s[i+1]:
		vals.append(val)
		val = 0

#print(vals)

#操作しやすいようにマイナス要素が偶数になるように先頭に0を追加
if vals[0] < 0:
	vals.insert(0,0)

#print(vals)


# K回操作できるということは連続した2k+1個の要素の最大値を求めればよい
max = 0
for i in range(len(vals) - k):
	# 偶数の時だけ判定
	if i%2 == 0:
		# print(vals[i:i + 2*k + 1])

		sum = 0
		for e in vals[i:i + 2*k + 1]:
			sum += abs(e)
			if sum > max:
				max = sum

print(max)

Submission status: Wrong Answer

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

# print(n, k, s)

prevcCt = 0 # 前に連続逆立ちの数
nextCnt = 0 # 後ろに連続逆立ちの数

# "00010" ⇒ [-3, 1, -1]のような配列に変換

val = 0
vals = []
for i in range(len(s)):
	if s[i] == "0":
		val -= 1
	else:
		val += 1

	#print(i, val, s[i], s[i+1])
	# print(i,len(s))
	if i == len(s) - 1 or s[i] != s[i+1]:
		vals.append(val)
		val = 0

# print(vals)

def getMAXArray(arr):
	maxVal = 0 # 最大値
	maxConvCol = 0 #最大値になるカラム位置 
	maxArr = arr[:]

	for i in range(len(arr)):
		if arr[i] < 0:
			copy = arr[:]

			# プラスに変えたら最大値がどうなるか計算
			copy[i] = copy[i] * -1

			# print(copy)

			# 最大値を確認
			tempMax = 0
			for e in copy:
				if e > 0:
					tempMax += e
					if tempMax > maxVal:
						maxVal = tempMax
						maxConvCol = i
						maxArr = copy
				else:
					# マイナス値ならリセット
					tempMax = 0

	# print(maxVal, maxConvCol)
	return(maxArr)


# print(getMAXArray(vals))
# print(getMAXArray(getMAXArray(vals)))
# print(getMAXArray(getMAXArray(getMAXArray(vals))))

for i in range(k):
	vals = getMAXArray(vals)

# 	print(vals)

# 最大値をカウント
max = 0
temp = 0
for i in range(len(vals)):
	if vals[i] > 0:
		temp += vals[i]
	else:
		temp = 0
	
	if temp > max:
		max = temp

print(max)

Submission status: Wrong Answer

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

nums = [0]
prev, count = S[0], 1

if S[0] == '0':
    start = 0
else:
    start = 1

for s in S[1:]:
    if prev == s:
        count += 1
    else:
        nums.append(count)
        prev = s
        count = 1
nums.append(count)

ans = 1

for i in range(start+1, len(nums), 2):
    ans = max(ans, sum(nums[i-1:i+2*K]))
print(ans)

Submission status: Wrong Answer

Solution code:
def sub():
    N, K = [int(e) for e in input().split(" ")]
    ss = [int(e) for e in input()]
    # if len(set(ss)) == 1:
    #     print(len(ss))
    #     return

    dd = [ss[i+1] - ss[i] for i in range(N-1)]
    idd = [0] + [i+1 for i, d in enumerate(dd) if d != 0]
    # print(idd)
    maxScore = 0
    for i in range(len(idd)):
        if ss[i] == 0:
            ni = i + 2*K
            if ni < len(idd):
                score = idd[ni] - idd[i]
            else:
                score = N - idd[i]
            if score > maxScore:
                maxScore = score
        else:
            ni = i + 2*K + 1
            if ni < len(idd):
                score = idd[ni] - idd[i]
            else:
                score = N - idd[i]
            if score > maxScore:
                maxScore = score
        
    print(maxScore)

sub()

Submission status: Wrong Answer

Solution code:
def sub():
    N, K = [int(e) for e in input().split(" ")]
    ss = [int(e) for e in input()]
    # if len(set(ss)) == 1:
    #     print(len(ss))
    #     return

    dd = [ss[i+1] - ss[i] for i in range(N-1)]
    idd = [0] + [i+1 for i, d in enumerate(dd) if d != 0]
    # print(idd)
    maxScore = 0
    for i in range(len(idd)):
        if ss[i] == 0:
            ni = i + 2*K
            if ni < len(idd):
                score = idd[ni] - idd[i]
            else:
                score = N - idd[i]
            if score > maxScore:
                maxScore = score
        else:
            ni = i + 2*K + 1
            if ni < len(idd):
                score = idd[ni] - idd[i]
            else:
                score = N - idd[i]
            if score > maxScore:
                maxScore = score
        
    print(maxScore)

Submission status: Wrong Answer

Solution code:
def sub():
    N, K = [int(e) for e in input().split(" ")]
    ss = [int(e) for e in input()]
    if len(set(ss)) == 1:
        print(len(ss))
        return

    dd = [ss[i+1] - ss[i] for i in range(N-1)]
    idd = [0] + [i+1 for i, d in enumerate(dd) if d != 0]
    # print(idd)
    maxScore = 0
    for i in range(len(idd)):
        if ss[i] == 0:
            ni = i + 2*K
            if ni < len(idd):
                score = idd[ni] - idd[i]
            else:
                score = N - idd[i]
            if score > maxScore:
                maxScore = score
        else:
            ni = i + 2*K + 1
            if ni < len(idd):
                score = idd[ni] - idd[i]
            else:
                score = N - idd[i]
            if score > maxScore:
                maxScore = score
        
    print(maxScore)

sub()

Submission status: Wrong Answer

Solution code:
def sub():
    N, K = [int(e) for e in input().split(" ")]
    ss = [int(e) for e in input()]
    if len(set(ss)) == 1:
        print(len(ss))
        return

    dd = [ss[i+1] - ss[i] for i in range(N-1)]
    memIdx = 0
    buf_zeros = []
    memNbOne = 0
    memChange = None
    for i, d in enumerate(dd):
        if d > 0:
            e = [(memIdx, i+1), memNbOne]
            buf_zeros.append(e)
            memIdx = i+1
            memChange = d
        elif d < 0:
            memNbOne = i+1 - memIdx
            memIdx = i+1
            memChange = d
    if memChange > 0:
        memNbOne = N - memIdx
        buf_zeros[-1].append(memNbOne)
    else:
        buf_zeros.append([(memIdx, N), memNbOne])
    # print(buf_zeros)

    for i, e in enumerate(buf_zeros):
        if len(e) == 2:
            if i < len(buf_zeros)-1:
                e.append(buf_zeros[i+1][1])
            else:
                e.append(0)
    memMaxScore = 0
    for _ in range(K):
        memIdx = 0
        maxScore = -1
        for i, e in enumerate(buf_zeros):
            score = e[0][1]-e[0][0] + e[1] + e[2]
            if score > maxScore:
                memIdx = i
                maxScore = score
        leftUpdate = memIdx - 1
        if leftUpdate >= 0:
            buf_zeros[leftUpdate][2] = maxScore
        rightUpdate = memIdx + 1
        if rightUpdate < len(buf_zeros):
            buf_zeros[rightUpdate][1] = maxScore
        buf_zeros = buf_zeros[:memIdx] + buf_zeros[memIdx+1:]
        memMaxScore = maxScore

    print(memMaxScore)
    
sub()

Submission status: Wrong Answer

Solution code:
from sys import stdin
from itertools import combinations
from functools import reduce
from operator import add

def consecutive_ranges(s, char):
    ranges = []
    i = 0
    while i < len(s):
        cur = s[i]
        if cur == char:
            start = i
            end = i
            while i < len(s) and s[i] == char:
                end = end + 1
                i = i + 1
            r = start, end 
            ranges.append(r)
        i = i + 1
    return ranges


def merge_range(r1, r2):
    s1, e1 = r1
    s2, e2 = r2
    return s1, e2


# n: sの長さ
# k: 正整数(指示を出せる回数)
# s: 長さnの文字の配列
def solve(n, k, s):
    # ゼロが連続している範囲
    ranges = consecutive_ranges(s, '0')

    # 何回指示を出すか
    num_ops = min(len(ranges), k)
    max_length = max([e - s for s, e in consecutive_ranges(s, '1')])
    for i in range(0, min(len(ranges) - num_ops + 1, len(ranges))):
        start, end = merge_range(ranges[i], ranges[i + num_ops - 1])
        s2 = s[:]
        s2[start:end] = ['1'] * (end - start)
        length = max([e - s for s, e in consecutive_ranges(s2, '1')])
        if length > max_length:
            max_length = length
    return max_length


def main():
    lines = stdin.readlines()
    n, k = lines[0].split(' ')
    s = list(lines[1].strip())
    ans = solve(n, int(k), s)
    print(ans)

Submission status: Wrong Answer

Solution code:
from sys import stdin
from itertools import combinations


def consecutive_ranges(s, char):
    ranges = []
    i = 0
    while i < len(s):
        cur = s[i]
        if cur == char:
            start = i
            length = 0
            while i < len(s) and s[i] == char:
                length = length + 1
                i = i + 1
            r = start, length
            ranges.append(r)
        i = i + 1
    return ranges

# n: sの長さ
# k: 正整数(指示を出せる回数)
# s: 長さnの文字の配列
def solve(n, k, s):
    # array of tuple indicates idx, length
    ranges = consecutive_ranges(s, '0')
    combs = list(combinations(ranges, k))
    max_len = 1 if len(combs) <= 0 else 0
    for comb in combs:
        # toggle range in `comb`
        s2 = s[:]
        for start, length in comb:
            s2[start : start + length] = ['1'] * length
        lengths = [l for s, l in consecutive_ranges(s2, '1')]
        length = lengths[0] if len(lengths) < 2 else max(*lengths)
        if max_len < length:
            max_len = length

    return max_len


def main():
    lines = stdin.readlines()
    n, k = lines[0].split(' ')
    s = list(lines[1].strip())
    ans = solve(n, int(k), s)
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
N, K = list(map(int, input().split()))
S = input()
nums = []
prev, count = S[0], 1

for s in S[1:]:
    if prev == s:
        count += 1
    else:
        nums.append(count)
        prev = s
        count = 1
nums.append(count)
if K == 1:
    k = 1
else:
    k = 2 * K + 1
ans = total = sum(nums[0:k+1])
for i in range(len(nums)-k):
    total = total - nums[i] + nums[k]
    ans = max(ans, total)
print(ans)

Submission status: Wrong Answer

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

a = []
x = 0 #1の個数
y = 0 #0の個数
if s[0] == 0:
  a.append(0)
for i in range(n):
  if s[i] == 1:
    x += 1
  else:
    y += 1
  if x > 0 and y > 0:
    if s[i] == 0:
      a.append(x)
      x = 0
    else:
      a.append(y)
      y = 0
if x > y:
  a.append(x)
else:
  a.append(y)
  a.append(0)


m = len(a)
left=[0]*(m+1)
right=[0]*(m+1)

for i in range(m):
  left[i+1] = left[i] + a[i]
for i in range(m):
  right[m-i-1] = right[m-i] + a[m-i-1]

ans = 0
for i in range(int(m/2)):
  if 2*i+2*k+1 <= m:
    x = left[2*i+2*k+1]-left[2*i]
  else:
    x = left[-1] - left[2*i]
  if ans < x:
    ans = x
print(ans)

Submission status: Wrong Answer

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

a = []
x = 0 #1の個数
y = 0 #0の個数
if s[0] == 0:
  a.append(0)
for i in range(n):
  if s[i] == 1:
    x += 1
  else:
    y += 1
  if x > 0 and y > 0:
    if s[i] == 0:
      a.append(x)
      x = 0
    else:
      a.append(y)
      y = 0
if x > y:
  a.append(x)
else:
  a.append(y)
  a.append(0)


m = len(a)
left=[0]*(m+1)
right=[0]*(m+1)

for i in range(m):
  left[i+1] = left[i] + a[i]
for i in range(m):
  right[m-i-1] = right[m-i] + a[m-i-1]

ans = 0
for i in range(int((m-1)/2)-k+1):
  x = left[2*i+2*k+1]-left[2*i]
  if ans < x:
    ans = x
print(ans)

Submission status: Wrong Answer

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

a = []
x = 0 #1の個数
y = 0 #0の個数
if s[0] == 0:
  a.append(0)
for i in range(n):
  if s[i] == 1:
    x += 1
  else:
    y += 1
  if x > 0 and y > 0:
    if s[i] == 0:
      a.append(x)
      x = 0
    else:
      a.append(y)
      y = 0
if x > y:
  a.append(x)
else:
  a.append(y)
  a.append(0)


m = len(a)
left=[0]*(m+1)
right=[0]*(m+1)

for i in range(m):
  left[i+1] = left[i] + a[i]
for i in range(m):
  right[m-i-1] = right[m-i] + a[m-i-1]

ans = 0
for i in range(int(m/2)-k+1):
  x = left[2*i+2*k+1]-left[2*i]
  if ans < x:
    ans = x
print(ans)

Submission status: Wrong Answer

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


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


l = len(change_point)
ans_candidates = []
for i in range(l):
    if i+2*K < l and S[i] == '0':
        ans_candidates.append(change_point[i+2*K]-change_point[i])
    elif i+2*K+1 < l and S[i] == '1':
        ans_candidates.append(change_point[i+2*K+1]-change_point[i])
    else:
        ans_candidates.append(N-change_point[i])

print( max(ans_candidates) )

Submission status: Wrong Answer

Solution code:
def main():
    n, k = [int(i) for i in input().split()]
    s = [int(i) for i in list(input())]
    table = []
    now = 1
    cnt = 0
    for i in range(n):
        if s[i] == now:
            cnt += 1
        else:
            table.append(cnt)
            cnt = 1
            now ^= 1 # 0　と 1 の切り替え
    if cnt != 0:
        table.append(cnt)
    if len(table)%2 == 0:
        # 奇数で終わらせれば、1 で終わったことになる
        table.append(0)
    ans = 0
    l, r = 0, min(len(table)-1, 2*k+1)
    while r < len(table):
        ans = max(ans, sum(table[l:r+1]))
        l += 2
        r += 2
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def main():
    n, k = [int(i) for i in input().split()]
    s = [int(i) for i in list(input())]
    table = []
    now = 1
    cnt = 0
    for i in range(n):
        if s[i] == now:
            cnt += 1
        else:
            table.append(cnt)
            cnt = 1
            now ^= 1 # 0　と 1 の切り替え
    if cnt != 0:
        table.append(cnt)
    if len(table)%2 == 0:
        # 奇数で終わらせれば、1 で終わったことになる
        table.append(0)
    ans = 0
    l, r = 0, min(len(table)-1, 2*k+1)
    while r < len(table):
        ans = max(ans, sum(table[:r+1]))
        l += 2
        r += 2
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def main():
    n, k = [int(i) for i in input().split()]
    s = [int(i) for i in list(input())]
    table = []
    now = 1
    cnt = 0
    for i in range(n):
        if s[i] == now:
            cnt += 1
        else:
            table.append(cnt)
            cnt = 1
            now ^= 1 # 0　と 1 の切り替え
    if cnt != 0:
        table.append(cnt)
    if len(table)%2 == 0:
        # 奇数で終わらせれば、1 で終わったことになる
        table.append(0)
    ans = 0
    l, r = 0, min(len(table)-1, 2*k+1)
    while r < len(table):
        ans = max(ans, sum(table[:r]))
        l += 2
        r += 2
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def main():
    n, k = [int(i) for i in input().split()]
    s = [int(i) for i in list(input())]
    table = []
    now = 1
    cnt = 0
    for i in range(n):
        if s[i] == now:
            cnt += 1
        else:
            table.append(cnt)
            cnt = 1
            now ^= 1 # 0　と 1 の切り替え
    if cnt != 0:
        table.append(cnt)
    if len(table)%2 == 0:
        # 奇数で終わらせれば、1 で終わったことになる
        table.append(0)
    ans = 0
    l, r = 0, 2*k+1
    if r > len(table):
        ans = max(ans, sum(table))
    while r < n:
        ans = max(sum(table[l: r+1]), ans)
        l, r = l + 2, r + 2
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

if len(S) == 1:
    print(1)
elif len(S) == 2:
    print(2)
elif S[0] == '0':
    LA = [0]
    LB = [0]
    for i in range(0,N-1):
        if (S[i] != S[i+1]) & (S[i] == '0'):
            LA.append(i+1)
    for i in range(1,N-1):
        if (S[i] != S[i+1]) & (S[i] == '1'):
            LB.append(i)
    LB.append(N-1)
    ANS = 0
    if K >= len(LB)-1:
        print(N)
    else:
        for i in range(len(LB)-K):
            J = LB[i+K]-LA[i]+1
            if J > ANS:
                ANS = J
        print(ANS)
else:
    LA = [0]
    LB = []
    for i in range(0,N-1):
        if (S[i] != S[i+1]) & (S[i] == '0'):
            LA.append(i+1)
        elif (S[i] != S[i+1]) & (S[i] == '1'):
            LB.append(i)
    LB.append(N-1)
    print(LA)
    print(LB)
    ANS = 0
    if K >= len(LB)-1:
        print(N)
    else:
        for i in range(len(LB)-K):
            J = LB[i+K]-LA[i]+1
            if J > ANS:
                ANS = J
        print(ANS)

Submission status: Wrong Answer

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

if len(S) == 1:
    print(1)
elif len(S) == 2:
    print(2)
elif S[0] == '0':
    LA = [0]
    LB = [0]
    for i in range(0,N-1):
        if (S[i] != S[i+1]) & (S[i] == '0'):
            LA.append(i+1)
    for i in range(1,N-1):
        if (S[i] != S[i+1]) & (S[i] == '1'):
            LB.append(i)
    LB.append(N-1)
    ANS = 0
    if K >= len(LB)-1:
        print(N)
    else:
        for i in range(len(LA)-K):
            J = LB[i+K]-LB[i]+1
            if J > ANS:
                ANS = J
        print(ANS)
else:
    LA = [0]
    LB = []
    for i in range(0,N-1):
        if (S[i] != S[i+1]) & (S[i] == '0'):
            LA.append(i+1)
        elif (S[i] != S[i+1]) & (S[i] == '1'):
            LB.append(i)
    LB.append(N-1)
    ANS = 0
    if K >= len(LB)-1:
        print(N)
    else:
        for i in range(len(LB)-K):
            J = LB[i+K]-LA[i]+1
            if J > ANS:
                ANS = J
        print(ANS)

Submission status: Wrong Answer

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

if len(S) == 1:
    print(1)
elif len(S) == 2:
    print(2)
elif S[0] == '0':
    LA = [0]
    LB = [0]
    for i in range(1,N-1):
        if (S[i] != S[i+1]) & (S[i] == '0'):
            LA.append(i+1)
        elif (S[i] != S[i+1]) & (S[i] == '1'):
            LB.append(i)
    LB.append(N-1)
    ANS = 0
    if K >= len(LB)-1:
        print(N)
    else:
        for i in range(len(LA)-K):
            J = LB[i+K]-LB[i]+1
            if J > ANS:
                ANS = J
        print(ANS)
else:
    LA = [0]
    LB = []
    for i in range(0,N-1):
        if (S[i] != S[i+1]) & (S[i] == '0'):
            LA.append(i+1)
        elif (S[i] != S[i+1]) & (S[i] == '1'):
            LB.append(i)
    LB.append(N-1)
    ANS = 0
    if K >= len(LB)-1:
        print(N)
    else:
        for i in range(len(LB)-K):
            J = LB[i+K]-LA[i]+1
            if J > ANS:
                ANS = J
        print(ANS)

Submission status: Wrong Answer

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

if len(S) == 1:
    print(1)
elif len(S) == 2:
    print(2)
elif S[0] == '0':
    LA = [0]
    LB = [0]
    for i in range(1,N-1):
        if (S[i] != S[i+1]) & (S[i] == '0'):
            LA.append(i+1)
        elif (S[i] != S[i+1]) & (S[i] == '1'):
            LB.append(i)
    LB.append(N-1)
    ANS = 0
    if K > len(LB)-1:
        print(len(S))
    else:
        for i in range(len(LA)-K):
            J = LB[i+K]-LB[i]+1
            if J > ANS:
                ANS = J
        print(ANS)
else:
    LA = [0]
    LB = []
    for i in range(0,N-1):
        if (S[i] != S[i+1]) & (S[i] == '0'):
            LA.append(i+1)
        elif (S[i] != S[i+1]) & (S[i] == '1'):
            LB.append(i)
    LB.append(N-1)
    ANS = 0
    if K > len(LB)-1:
        print(len(S))
    else:
        for i in range(len(LB)-K):
            J = LB[i+K]-LA[i]+1
            if J > ANS:
                ANS = J
        print(ANS)

Submission status: Wrong Answer

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

if len(S) == 1:
    print(1)
elif len(S) == 2:
    print(2)
elif S[0] == '0':
    LA = [0]
    LB = [0]
    for i in range(1,N-1):
        if (S[i] != S[i+1]) & (S[i] == '0'):
            LA.append(i+1)
        elif (S[i] != S[i+1]) & (S[i] == '1'):
            LB.append(i)
    LB.append(N-1)
    print(LA)
    print(LB)
    ANS = 0
    if K > len(LB)-1:
        print(len(S))
    else:
        for i in range(len(LA)-K):
            J = LB[i+K]-LB[i]+1
            if J > ANS:
                ANS = J
        print(ANS)
else:
    LA = [0]
    LB = []
    for i in range(0,N-1):
        if (S[i] != S[i+1]) & (S[i] == '0'):
            LA.append(i+1)
        elif (S[i] != S[i+1]) & (S[i] == '1'):
            LB.append(i)
    LB.append(N-1)
    print(LA)
    print(LB)
    ANS = 0
    if K > len(LB)-1:
        print(len(S))
    else:
        for i in range(len(LB)-K):
            J = LB[i+K]-LA[i]+1
            if J > ANS:
                ANS = J
        print(ANS)

Submission status: Wrong Answer

Solution code:
N, K = map(int,input().split())
S = input()
L = []
t = 1
for k in range(1,N):
    if S[k-1] == S[k]:
        t += 1
    else:
        L.append(t)
        t = 1
L.append(t)
l = len(L)
A = [0 for k in range(l+2)]

for k in range(l):
    A[k+1] = A[k] + L[k]
A[l+1] = A[l]
if S[0] == "0":
    ans = A[min(l,2*K)]
    for k in range(l-2*K):
        if k%2 == 1:
            ans = max(ans,A[k+2*K+1]-A[k])
else:
    ans = A[min(l,2*K+1)]
    if l%2 == 1:
        for k in range(l-2*K):
            if k%2 == 0:
                ans = max(ans,A[k+2*K+1]-A[k])
    else:
        for k in range(l-2*K+1):
            if k%2 == 0:
                ans = max(ans,A[k+2*K+1]-A[k])

print(ans)

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
S = list(input())
A = [1]
for i in range(len(S)-1):
    if S[i] == S[i+1]:
        A[-1] += 1
    else:
        A.append(1)
J = len(A)

if (S[0] == '1') & (S[-1] == '1'):
    ANSL = []
    if J <= 2*K+1:
        print(sum(A))
    else:
        for i in range((J+1)//2-K):
            ANS = 0
            for j in range(2*K+1):
                ANS += A[2*i+j]
            ANSL.append(ANS)
        print(max(ANSL))
elif (S[0] == '0') & (S[-1] == '0'):
    if J <= 2*K-1:
        print(sum(A))
    else:
        ANSL = []
        ANSM = 0
        for i in range(2*K):
            ANSM += A[i]
        ANSN = 0
        for i in range(J-2*K,J):
            ANSN += A[i]
        for i in range((J-1)//2-K):
            ANS = 0
            for j in range(2*K+1):
                ANS += A[2*i+1+j]
            ANSL.append(ANS)
        ANSL.append(ANSM)
        ANSL.append(ANSN)
        print(max(ANSL))
elif (S[0] == '0') & (S[-1] == '1'):
    if J <= 2*K:
        print(sum(A))
    else:
        ANSL = []
        ANSM = 0
        for i in range(2*K):
            ANSM += A[i]
        for i in range(J//2-K):
            ANS = 0
            for j in range(2*K+1):
                ANS += A[2*i+1+j]
            ANSL.append(ANS)
        ANSL.append(ANSM)
        print(max(ANSL))
else:
    A.reverse()
    if J <= 2*K-1:
        print(sum(A))
    else:
        ANSL = []
        ANSM = 0
        for i in range(2*K):
            ANSM += A[i]
        for i in range(J//2-K):
            ANS = 0
            for j in range(2*K+1):
                ANS += A[2*i-1+j]
            ANSL.append(ANS)
        ANSL.append(ANSM)
        print(max(ANSL))

Submission status: Wrong Answer

Solution code:
import copy
import sys
N, K = (int(x) for x in input().split())
string = input()
ls = list(map(int, string))

if string == "1" or string == "0":
    print(1)
    sys.exit()


def change_1(lis, num):
    i = num
    while lis[i] != 1:
        lis[i] = 1
        i += 1
        if i > N-1:
            break
    return lis


def count_1(lis):
    key = 0
    count = 0
    for h in range(len(lis)):
        if lis[h] == 1:
            count += 1
        else:
            key = max(count, key)
            count = 0
    return key


def cal_list(pb):
    comp = []
    k = 0
    m = 0
    if pb[1][0] == "0":
        pb[0].insert(0, 0)
        pb[1].insert(0, "1")
    if pb[1][len(pb[1][:])-1] == "0":
        pb[0].append(0)
        pb[1].append("1")
    comp.append(pb[0][0])
    for i in range(1, len(pb[0][:])):
        comp.append(comp[i-1]+pb[0][i])
    # if comp[0] != 0:
    #     comp.insert(0, 0)
    ans = 0
    j = 0
    while 2*K+1+2*j <= len(comp):
        if j == 0:
            ans = comp[2*K]
        else:
            ans = max(ans, comp[2*K+2*j]-comp[j*2-1])
        j += 1
    print(ans)


def split_01(lis):
    pb = [[] for i in range(2)]
    i = 0
    while i < len(lis):
        count_0, count_1 = 0, 0
        if lis[i] == 0:
            while lis[i] == 0:
                count_0 += 1
                i += 1
                if i >= len(lis):
                    break
            pb[0].append(count_0)
            pb[1].append("0")
        if i >= len(lis):
            break
        if lis[i] == 1:
            while lis[i] == 1:
                count_1 += 1
                i += 1
                if i >= len(lis):
                    break
            pb[0].append(count_1)
            pb[1].append("1")
        if i >= len(lis):
            break
    return pb


pr = split_01(copy.copy(ls))
cal_list(copy.copy(pr))

Submission status: Wrong Answer

Solution code:
x = list(map(int,input().split()))
a = input()
a = list(a)
for t in range(len(a)):
    a[t] = int(a[t])
k = 1
loup = 0
rengi = 0
goukei = []
renzoku = []
if a[0] == 0:
    for i in range(x[0]-1):
        if a[i] == a[i+1]:
            k += 1
            if i == (x[0] - 2):
                renzoku.append(k)
        else :
            renzoku.append(k)
            k = 1
elif a[0] == 1:
    for j in range(x[0]-1):
        if a[j] == a[j+1]:
            k += 1
            if j == (x[0] - 2):
                renzoku.append(k)
        else :
            renzoku.append(k)
            k = 1
if int(len(renzoku)/2) <= x[1]:
    print(sum(renzoku))
else:
    rengi = 2*x[1]
    loup = len(renzoku) - rengi
    for k in range(loup-1):
        if k == 0:
            f = sum(renzoku[k:(k+rengi+a[0]+1)])
            goukei.append(f)
        else:
            f = sum(renzoku[k:(k+rengi+a[sum(renzoku[:k])]+1)])
            goukei.append(f)
    print(max(goukei))