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: Runtime Error

Solution code:
package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"strings"
)

// An Item is something we manage in a priority queue.
type Item struct {
	value    []int
	priority int64
	// The index is needed by update and is maintained by the heap.Interface methods.
	index int // The index of the item in the heap.
}

// A PriorityQueue implements heap.Interface and holds Items.
type PriorityQueue []*Item

func (pq PriorityQueue) Len() int { return len(pq) }

func (pq PriorityQueue) Less(i, j int) bool {
	//We want Pop to give us the highest, not lowest, priority so we use greater than here.
	return pq[i].priority > pq[j].priority
}

func (pq PriorityQueue) Swap(i, j int) {
	pq[i], pq[j] = pq[j], pq[i]
	pq[i].index = i
	pq[j].index = j
}

func (pq *PriorityQueue) Push(x interface{}) {
	n := len(*pq)
	item := x.(*Item)
	item.index = n
	*pq = append(*pq, item)
}

func (pq *PriorityQueue) Pop() interface{} {
	old := *pq
	n := len(old)
	item := old[n-1]
	item.index = -1 // for safety
	*pq = old[0 : n-1]
	return item
}

func Equal(a, b []int) bool {
	if len(a) != len(b) {
		return false
	}
	for i, v := range a {
		if v != b[i] {
			return false
		}
	}
	return true
}

type IntArray []int64

func (a IntArray) Len() int {
	return len(a)
}

func (a IntArray) Less(i, j int) bool {
	return a[i] > a[j]
}

func (a IntArray) Swap(i, j int) {
	a[i], a[j] = a[j], a[i]
}

var sc = bufio.NewScanner(os.Stdin)

func nextLine() string {
	sc.Scan()
	return sc.Text()
}

func exists(pq PriorityQueue, item *Item) bool {
	for i := 0; i < pq.Len(); i++ {
		if pq[i].priority == item.priority && Equal(pq[i].value, item.value) {
			return true
		}
	}
	return false
}

func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}

func main() {
	line1 := nextLine()
	NK := strings.Split(line1, " ")
	N, _ := strconv.Atoi(NK[0])
	K, _ := strconv.Atoi(NK[1])
	S := nextLine()
	l := make([]int, 0)
	if string(S[0]) == "0" {
		l = append(l, 0)
	}
	for i := 0; i < N; i++ {
		j := i
		for j < N && S[j] == S[i] {
			j++
		}
		l = append(l, j-i)
		i = j
	}
	if string(S[N-1]) == "0" {
		l = append(l, 0)
	}

	sums := make([]int, len(l)+1)
	for i := 0; i < len(l); i++ {
		sums[i+1] = sums[i] + l[i]
	}

	ans := 0
	for left := 0; left < len(sums); left += 2 {
		right := left + 2*K + 1
		if right >= len(sums) {
			right = len(sums) - 1
		}
		ans = max(ans, sums[right]-sums[left])
	}
	fmt.Println(ans)
}

Submission status: Runtime Error

Solution code:

import sys

N, K = map(int, sys.stdin.readline().split())

S = input()

countk = 0
start = 0
max = 0

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

last_flag = 0
if S[N-1] == S[N-2] and S[N-2] == "1":
    last_flag =1

temp = 0
max = 0
for i in range(len(list_change)):
    if (S[list_change[i]] == "0" and (2 * K + i) > (len(list_change)-1)) or (S[list_change[i]] == "1" and (2 * K + i + 1) > (len(list_change)-1)):
        break
    if S[list_change[i]] == "0":
        temp = list_change[2 * K + i -1] - list_change[i] + 1
    else:
        temp = list_change[2 * K + i] - list_change[i] + 1
    if max < temp:
        max = temp

if last_flag ==1:
    temp = (N-1) - list_change[(len(list_change)-1 ) - (2 * K + 1)]
    if max < temp:
        max = temp


print("ans")
print(max)

Submission status: Runtime Error

Solution code:
import sys
from collections import deque
input = sys.stdin.readline

def main():
    n, k = map(int, input().split())
    s = list(input())
    
    tank = []
    cnt = 1
    for i in range(1, n):
        if s[i] != s[i-1]:
            tank.append(cnt)
            cnt = 0
        cnt += 1
        if i == n-1:
            tank.append(cnt)
            
    p = len(tank)
    
    if s[0] == '0' and (p+1)//2 <= k:
        print(n)
        exit()
    if p//2 <= k and s[0] == '1':
        print(n)
        exit()
        
    ans = 0
    res = 0
    if s[0] == '0':
        for j in range(2*k):
            res += tank[j]
        ans = res
        for j in range(2*k, p):
            if j == 2*k:
                res -= tank[0]
            elif j%2 == 0:
                res = res - tank[j-2*k] - tank[j*2*k-1]
            res += tank[j]
            ans = max(ans, res)
            
    else:
        for j in range(2*k+1):
            res += tank[j]
        ans = res
        for j in range(2*k+1, p):
            if j%2 == 1:
                res = res - tank[j-2*k-1] - tank[j-2*k]
            res += tank[j]
            ans = max(ans, res)
            
    print(ans)
    
if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
s=input()
cnt=0
siten=s[0]
onezero=[]
for i in range(n-1):
    if s[i:i+2]=='01':
        cnt+=1
        onezero+=[cnt]
        cnt=0
        
    else:
        cnt+=1
        if n-2==i:
            cnt+=1
            onezero[-1]+=cnt

ans=[1]
for i in range(max(1,len(onezero)-k+1)):
    #print(onezero[i:i+k])
    if siten=='0':
        ans+=[sum(onezero[i:i+k+1])-1]
    else:
        ans+=[sum(onezero[i:i+k])]

print(max(ans))

Submission status: Runtime Error

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+left
	elif (S[0]=='1')&(right<1):
		q=po[2*K+1]
		left=1
		right=2*K+left
	elif right<=len(po):
		q=po[right]-po[left]+1
		left+=2
		right+=2
	elif S[-1]=='0':
		right=len(po)-1
		left=right-2*K
		q=po[right]-po[left]
		ans=max(q,ans)
		break
	else:
		right=len(po)-1
		left=right-2*K-1
		q=po[right]-po[left]
		ans=max(q,ans)
		break
	ans=max(q,ans)
print(ans)

Submission status: Runtime Error

Solution code:
N,K=map(int,input().split())
S=input()
po=[0]
p=0
ans=0
left,right=0,0
q=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)-K+2):
	if K*2>len(po):
		ans=po[-1]
		break
	elif (S[0]=='0')&(right<=1):
		q=po[2*K]
		left=2
		right=2*K+left
	elif (S[0]=='1')&(right<=1):
		q=po[2*K+1]
		left=1
		right=2*K+left
	elif right<len(po):
		q=po[right]-po[left]
		left+=2
		right+=2
	elif S[-1]=='0':
		right=len(po)-1
		left=right-2*K
		q=po[right]-po[left]
	else:
		right=len(po)-1
		left=right-2*K-1
		q=po[right]-po[left]
	ans=max(q,ans)
print(ans)

Submission status: Runtime Error

Solution code:
n,k = map(int,input().split())
s = input()
li = [0]
count = 1

for i in range(n-1):
    if s[i] != s[i+1]:
        li.append(count)
    count += 1
li.append(count)

m = len(li)

if s[0] == '1':
    t = int(m/2-1)
    if k >= t:
        print(n)
    else:
        max1 = 0
        for i in range(t-k+1):
            max1 = max(li[2*(i+k)+1] - li[2*i],max1)
        print(max1)
else:
    t = int(m/2)
    if k >= t:
        print(n)
    else:
        max1 = li[2*k]
        for i in range(t-k):
            max1 = max(li[2*(i+k+1)] - li[2*i+1],max1)
        print(max1)

Submission status: Runtime Error

Solution code:
n,k = map(int,input().split())
s = input()
li = [0]
count = 1

for i in range(n-1):
    if s[i] != s[i+1]:
        li.append(count)
    count += 1
li.append(count)

print(li)

m = len(li)

if s[0] == '1':
    t = int(m/2-1)
    if k >= t:
        print(n)
    else:
        max1 = 0
        for i in range(t-k+1):
            max1 = max(li[2*(i+k)+1] - li[2*i],max1)
        print(max1)
else:
    t = int(m/2)
    if k >= t:
        print(n)
    else:
        max1 = li[2*k]
        for i in range(t-k):
            max1 = max(li[2*(i+k+1)] - li[2*i+1],max1)
        print(max1)

Submission status: Runtime Error

Solution code:
import numpy as np

def main():
    n,k = map(int,input().split())
    s = input()
    li = np.array([])
    count = 1


    for i in range(n-1):
        if s[i] == s[i+1]:
            count += 1
        else:
            li = np.append(li,count)
            count = 1
    li = np.append(li,count)

    m = len(li)

    if s[0] == '1':
        t = int(m/2)
        if k >= t:
            print(sum(li))
        else:
            for i in range(t-k+1):
                max1 = max(np.sum(li[2*i:2*(i+k)+1]),max1)
            print(max1)
    else:
        t = int(m/2+1)
        if k >= t:
            print(sum(li))
        else:
            max1 = sum(li[:2*k])
            for i in range(t-k):
                max1 = max(np.sum(li[2*i+1:2*(i+k)+2]),max1)
            print(max1)
main()

Submission status: Runtime Error

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

  def findEx(s, f, pos, notfound):
    pos = s.find(f, pos)
    if pos == -1:
      pos = notfound
    return pos

  ans = 0
  left = 0
  right = 0
  while right < n:
      if k > 0:
          right = findEx(s, '01', right, n-1) + 1
          k = k - 1
          right = findEx(s, '10', right, n-1) + 1
          ans = max(ans, right - left)
      else:
          left = findEx(s, '01', left, n-1) + 1
          k = k + 1

  print(ans)

  if input() == "":
    break

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())
s = input().rstrip()
ans = 1
i = 0
left = 0
right = 1
while i < n:
    if k > 0:
        pos = s.find('01', i)
        if pos == -1:
            i = n
            right = n - 1
        else:
            pos = s.find('10', pos + 1)
            if pos == -1:
                i = n
                right = n - 1
            else:
                right = pos
                i = pos + 1
        k = k - 1
        print(s[left:right+1])
        ans = max(ans, right - left + 1)
    else:
        pos = s.find('01', left)
        left = pos + 1
        k = k + 1

print(ans)

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())
s = input().rstrip()
ans = 1
left = 0
right = -1
pos = s.find('10', 0)
if pos == -1:
  i = n
else
  i = pos + 1

while i < n:
    if k > 0:
        pos = s.find('01', i)
        if pos == -1:
            i = n
            right = n - 1
        else:
            pos = s.find('10', pos + 1)
            if pos == -1:
                i = n
                right = n - 1
            else:
                right = pos
                i = pos + 1
        k = k - 1
        ans = max(ans, right - left + 1)
    else:
        pos = s.find('01', left)
        left = pos + 1
        k = k + 1

print(ans)

Submission status: Runtime Error

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

l_switch = []
l_ans = []

#print(S)
if not 0 in S:
    l_ans.extend([1])

#N = 5
#K = 1
#S = [1,1,1,0,1,0,1,0,1,1,0,0,1,1]
#S = [0,0,0,1,0,1,0,1,0,0,1,1,0,0]
#S = [0,0,0,1,0]

if S[0] == 1:
    S.insert(0,0)
elif S[0] == 0:
    S.insert(0,1)
if S[-1] == 1:
    S.extend([0])
elif S[-1] == 0:
    S.extend([1])

#print(S)



for i in range(1,len(S)):
    if S[i-1] == 0 and S[i] == 1:
        l_switch.extend([i])
    elif S[i-1] == 1 and S[i] == 0:
        l_switch.extend([i])
#print(l_switch)

if S[1] == 1:
    for j in range(0,len(l_switch)-(2*K+1),2):
        #print("J: " + str(l_switch[j]),"   2*K+1+j: " + str(l_switch[2*K+1+j]))
        #print(l_switch[2*K+1+j]-l_switch[j])
        l_ans.extend([l_switch[2*K+1+j]-l_switch[j]])

if S[1] == 0:
    #print("J: " + str(l_switch[0]),"   2*K+j: " + str(l_switch[2*K+0]))
    #print(l_switch[2*K+0]-l_switch[0])
    l_ans.extend([l_switch[2*K+0]-l_switch[0]])
    for j in range(1,len(l_switch)-(2*K+1),2):
        #print("J: " + str(l_switch[j]),"   2*K+1+j: " + str(l_switch[2*K+1+j]))
        #   sprint(l_switch[2*K+1+j]-l_switch[j])
        l_ans.extend([l_switch[2*K+1+j]-l_switch[j]])



print(max(l_ans))

Submission status: Runtime Error

Solution code:
def ret_arr(N, K, S):
    a0 = str(S).split('1')
    remove_specified_values(a0, '')
    a1 = str(S).split('0')
    remove_specified_values(a1, '')

    a = [2] * (len(a1) + len(a0))
    if int(S[0]) == 0:
        for i in range(0, len(a0)):
            if i > (len(a0) - 2) and int(S[-1]) == 0:
                a[i * 2] = a0[i]
            else:
                a[i * 2] = a0[i]
                a[i * 2 + 1] = a1[i]
    else:
        for i in range(0, len(a1)):
            if i > (len(a1) - 2) and int(S[-1]) == 1:
                a[i * 2] = a1[i]
            else:
                a[i * 2] = a1[i]
                a[i * 2 + 1] = a0[i]
    return a


def remove_specified_values(arr, value):
    while value in arr:
        arr.remove(value)


def ret_arr_len(arr):
    ret_a = []
    for i in arr:
        ret_a.append(len(i))
    return ret_a


def count(arr, K):
    k = K * 2 + 1
    c_a = []
    for i in range(0, len(arr) - (k - 1)):
        c = 0
        for _k in range(0, k):
            c = c + arr[(i + _k)]
        # print(c)
        c_a.append(c)
    try:
        m = max(c_a)
    except Exception as e:
        m = 0

    return m


def main(N, K, S):
    a = ret_arr(N, K, S)
    len_a = ret_arr_len(a)

    c_a = []
    for i in range(0, K + 1):
        c = count(len_a, i)
        c_a.append(c)

    return max(c_a)
  
if __name__ == '__main__':
    input_str=input()
	N=int(input_str.split()[0])
	K=int(input_str.split()[1])
    str_teams = str(input())
    print(main(N, K, str_teams))

Submission status: Runtime Error

Solution code:
def ret_arr(N, K, S):
    a0 = str(S).split('1')
    remove_specified_values(a0, '')
    a1 = str(S).split('0')
    remove_specified_values(a1, '')

    a = [2] * (len(a1) + len(a0))
    if int(S[0]) == 0:
        for i in range(0, len(a0)):
            if i > (len(a0) - 2) and int(S[-1]) == 0:
                a[i * 2] = a0[i]
            else:
                a[i * 2] = a0[i]
                a[i * 2 + 1] = a1[i]
    else:
        for i in range(0, len(a1)):
            if i > (len(a1) - 2) and int(S[-1]) == 1:
                a[i * 2] = a1[i]
            else:
                a[i * 2] = a1[i]
                a[i * 2 + 1] = a0[i]
    return a


def remove_specified_values(arr, value):
    while value in arr:
        arr.remove(value)


def ret_arr_len(arr):
    ret_a = []
    for i in arr:
        ret_a.append(len(i))
    return ret_a


def count(arr, K):
    k = K * 2 + 1
    c_a = []
    for i in range(0, len(arr) - (k - 1)):
        c = 0
        for _k in range(0, k):
            c = c + arr[(i + _k)]
        # print(c)
        c_a.append(c)
    try:
        m = max(c_a)
    except Exception as e:
        m = 0

    return m


def main(N, K, S):
    print('~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~')
    a = ret_arr(N, K, S)
    len_a = ret_arr_len(a)

    c_a = []
    for i in range(0, K + 1):
        c = count(len_a, i)
        print(f'{i} : {c}')
        c_a.append(c)

    return max(c_a)
  
if __name__ == '__main__':
    #inp = {'N': 11, 'K': 1, 'S': '1'}
    #ret = main(**inp)
    #print(f'ret = {ret} (input N,K,S={inp}')
    input_str=input()
	N=int(input_str.split()[0])
	K=int(input_str.split()[1])
    print(main(N, K, 0))

Submission status: Runtime Error

Solution code:
import math
 
N,K = (int(x) for x in input().split())
S = input()
 
list1 = []
now = 1
count = 0
 
for i in range(N):
    if now == int(S[i]):
        count += 1
    else:
        list1.append(count)
        now = 1-now
        count = 1
else:
    list1.append(count)

if len(list1) % 2 == 0:
    list1.append(0) 
tmp=0
left=0
right=0
ans=0
for i in range(0,len(list1),2):
    nextleft = i
    while left<nextleft:
        tmp = tmp - list1[left]
        left = left + 1

nextright = min(i+2*K+1,len(list1))
while right<nextright:
    tmp = tmp + list1[right]
    right = right + 1

ans=max(ans,tmp)print(ans)

print(ans)

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
s = input()
S = [int(x) for x in list(str(s))]

l = []
tmp = 1
p = S[0]

for i in range(1, N):
    if p == S[i]:
        tmp += 1
    else:
        l.append(tmp)
        p = S[i]
        tmp = 1
l.append(tmp)

i = 0
check = False

if S[0] == 0:
    r = 2 * K - 1
    check = True
else:
    r = 2 * K

ans = sum(l[i:r + 1])
tmp = ans

while r < len(l) - 2:
    tmp += l[r + 1] + l[r + 2]
    r += 2
    if check:
        tmp -= l[i]
        i += 1
        check = False
    else:
        tmp -= l[i] + l[i + 1]
        i += 2
    ans = max(tmp, ans)

if S[-1] == 0:
    tmp += l[r + 1]
    if check:
        tmp -= l[i]
    else:
        tmp -= l[i] + l[i + 1]

ans = max(ans, tmp)
print(ans)

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
s = input()
S = [int(x) for x in list(str(s))]
S.append(2)

l = []
tmp = 1
p = S[0]

if N == 1:
    print(1)
    exit()

for i in range(1, N + 1):
    if p == S[i]:
        tmp += 1
    else:
        l.append(tmp)
        p = S[i]
        tmp = 1

i = 0
check = False

if S[0] == 0:
    r = 2 * K - 1
    check = True
else:
    r = 2 * K

ans = sum(l[i:r + 1])
tmp = ans

while r < len(l) - 2:
    tmp += l[r + 1] + l[r + 2]
    r += 2
    if check:
        tmp -= l[i]
        i += 1
        check = False
    else:
        tmp -= l[i] + l[i + 1]
        i += 2
    ans = max(tmp, ans)

if S[-2] == 0:
    tmp += l[r + 1]
    if check:
        tmp -= l[i]
    else:
        tmp -= l[i] + l[i + 1]

ans = max(ans, tmp)
print(ans)

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
s = input()
S = [int(x) for x in list(str(s))]
S.append(2)

l = []
tmp = 1
p = S[0]

for i in range(1, N + 1):
    if p == S[i]:
        tmp += 1
    else:
        l.append(tmp)
        p = S[i]
        tmp = 1

i = 0
check = False

if S[0] == 0:
    r = 2 * K - 1
    check = True
else:
    r = 2 * K

ans = sum(l[i:r + 1])
tmp = ans

while r < len(l) - 2:
    tmp += l[r + 1] + l[r + 2]
    r += 2
    if check:
        tmp -= l[i]
        i += 1
        check = False
    else:
        tmp -= l[i] + l[i + 1]
        i += 2
    ans = max(tmp, ans)

if S[-2] == 0:
    tmp += l[r + 1]
    if check:
        tmp -= l[i]
    else:
        tmp -= l[i] + l[i + 1]

ans = max(ans, tmp)
print(ans)

Submission status: Runtime Error

Solution code:
n,k = map(int, input().split())
s = input()
cnt = 0
init = (s[0]=='1')
h=[1]
h_cnt = 0

for i in range(1,n):
    if(s[i]!=s[i-1]):
        h_cnt+=1
        h.append(1)
    else:
        h[h_cnt]+=1

h_cnt+=1

zero_num = 0
ans = 0

if(init): #start with 1
    zero_num = h_cnt//2;
    if(zero_num<=k):
        ans = n
    else:
        sum_0 = sum(h[0:k*2+1])
        ans = sum_0
        h = h +[0]
        for j in range(0,zero_num-k): #j is 0's order
            sum_0 = sum_0 - h[j*2] - h[j*2+1] + h[(j+k)*2+1] + h[(j+k)*2+2]
            if(ans<=sum_0):
                ans = sum_0

else: #start with 0
    zero_num = h_cnt//2 +1;
    if(zero_num<=k):
        ans = n
    else:
        sum_0 = sum(h[0:k*2])
        ans = sum_0
        h = [0] + h +[0]
        for j in range(0,zero_num-k): #j is 0's order
            sum_0 = sum_0 - h[j*2] - h[j*2+1] + h[(j+k)*2+1] + h[(j+k)*2+2]
            if(ans<=sum_0):
                ans = sum_0

print(ans)

Submission status: Runtime Error

Solution code:
n,k = map(int, input().split())
s = input()
cnt = 0
init = (s[0]=='1')
h=[1]
h_cnt = 0

for i in range(1,n):
    if(s[i]!=s[i-1]):
        h_cnt+=1
        h.append(1)
    else:
        h[h_cnt]+=1

h_cnt+=1

zero_num = 0
ans = 0

if(init): #start with 1
    zero_num = h_cnt//2;
    if(zero_num<=k):
        ans = n
    else:
        sum_0 = sum(h[0:k*2+1])
        ans = sum_0
        h = h +[0]
        for j in range(0,zero_num-k): #j is 0's order
            sum_0 = sum_0 - h[j*2] - h[j*2+1] + h[(j+k)*2+1] + h[(j+k)*2+2]
            if(ans<=sum_0):
                ans = sum_0

else: #start with 0
    zero_num = h_cnt//2 +1;
    if(zero_num<=k):
        ans = n
    else:
        sum_0 = sum(h[0:k*2])
        ans = sum_0
        h = [0] + h +[0]
        for j in range(0,zero_num-k):#j is 0's order
            sum_0 = sum_0 - h[j*2] - h[j*2+1] + h[(j+k)*2+1] + h[(j+k)*2+2]
            if(ans<=sum_0):
                ans = sum_0

print(ans)

Submission status: Runtime Error

Solution code:
[n,k] = input().split(" ")
s = input()

cnt = 0
init = (s[0]=='1')
h=[1]
h_cnt = 0

for i in range(1,n):
    if(s[i]!=s[i-1]):
        h_cnt+=1
        h.append(1)
    else:
        h[h_cnt]+=1

h_cnt+=1

zero_num = 0
ans = 0

if(init): #start with 1
    zero_num = h_cnt//2;
    if(zero_num<=k):
        ans = n
    else:
        sum_0 = sum(h[0:k*2+1])
        ans = sum_0
        for j in int range(0,zero_num-k+1): #j is 0's order
            sum_0 = sum_0 - h[j*2] - h[j*2+1] + h[(j+k)*2+1] + h[(j+k)*2+2]
            if(ans<=sum_0):
                ans = sum_0

else: #start with 0
    zero_num = h_cnt//2 +1;
    if(zero_num<=k):
        ans = n
    else:
        sum_0 = sum(h[0:k*2])
        ans = sum_0
        for j in range(0,zero_num-k+1): #j is 0's order
            sum_0 = sum_0 - h[j*2-1] - h[j*2] + h[(j+k)*2] + h[(j+k)*2+1]
            if(ans<=sum_0):
                ans = sum_0

printf(ans)

Submission status: Runtime Error

Solution code:
from itertools import groupby, accumulate

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

# 連続する0または1の数に関する累積和
A = list(accumulate(len(list(seq)) for _, seq in groupby(S))) + [N]
m = (
    1 if S[0] == '0' else
    0
)
# 累積和を用いて解を求める
ans = (
    max(
        A[2 * K - 1],
        A[-1] - A[-(2 * K)],
        max(
            (
                a - b
                for a, b in zip(A[(2 * K + m)::2], ([0] + A)[m::2])
            ),
            default=0
        )
    ) if m < len(A) else
    A[-1]
)

# 出力
print(ans)

Submission status: Runtime Error

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

if n == 1:
    print(1)
    exit(0)

l = []
if s[0] == "0":
    l.append(0)

temp = 1
for i in range(1, n):
    if s[i] == s[i - 1]:
        temp += 1
        if i == n - 1:
            l.append(temp)
    else:
        l.append(temp)
        temp = 1

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

ans = 0
if 2 * k + 1 > len(l):
    sum(l[i * 2 : min(i * 2 + 2 * k + 1, len(l)) : 1])

b = []
b.append(l[0])
for i in range(1, len(l)):
    b.append(b[-1] + l[i])


for i in range((len(l) + 1)):
    if i * 2 + 2 * k + 1 > len(l):
        break
    ans = max(ans, b[min(i * 2 + 2 * k + 1, len(l))] - b[i * 2 - 1])
print(ans)

Submission status: Runtime Error

Solution code:
n,k=map(int, input().split())
s=input()
t=[]
now=s[0]
count=0
for i in s:
    if now==i:
        count+=1
    else:
        t.append(count)
        now=i
        count=1
t.append(count)
if k>len(t)//2:print(sum(t))
else:
    ans=0
    if s[0]=='0':
        ans=max(ans,t[2*k])
        t=t[1:]
    if s[-1]=='0':
        ans=max(ans,t[-2*k:-1])
        t=t[:-2]
    ans2=sum(t[:2*k+1])
    ans=max(ans,ans2)
    for i in range(len(t)//2-k):
        ans2=ans2-sum(t[2*i:2*i+2])+sum(t[2*i+2*k+1: 2*i+2*k+1+2])
        ans=max(ans,ans2)
    print(ans)

Submission status: Runtime Error

Solution code:
n,k=map(int, input().split())
s=input()
t=[]
now=s[0]
count=0
for i in s:
    if now==i:
        count+=1
    else:
        t.append(count)
        now=i
        count=1
t.append(count)
if k>len(t)//2:print(sum(t))
else:
    ans=0
    if s[0]=='0':
        ans=max(ans,t[2*k])
        t=t[1:]
    if s[-1]=='0':
        ans=max(ans,t[-2*k:-1])
        t=t[:-2]
    ans2=sum(t[:2*k+1])
    ans=max(ans,ans2)
    for i in range(len(t)//2-k):
        ans2=ans2-sum(t[2*i:2*i+2])+sum(t[2*i+2*k+1: 2*i+2*k+1+2])
        ans=max(ans,ans2)
	print(ans)

Submission status: Runtime Error

Solution code:
n,k=map(int, input().split())
s=input()
t=[]
now=s[0]
count=0
for i in s:
    if now==i:
        count+=1
    else:
        t.append(count)
        now=i
        count=1
t.append(count)
if k>len(t)//2:print(sum(t))
else:
    ans=0
    if s[0]=='0':
        ans=max(ans,t[2*k])
        t=t[1:]
    if s[-1]=='0':
        ans=max(ans,t[-2*k:-1])
        t=t[:-2]
    ans2=sum(t[:2*k+1])
    ans=max(ans,ans2)
    for i in range(len(t)//2-k):
        ans2=ans2-sum(t[2*i:2*i+2])+sum(t[2*i+2*k+1: 2*i+2*k+1+2])
        ans=max(ans,ans2)
print(ans)

Submission status: Runtime Error

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

nums = []
now = 1
count = 0
for i in range(len(S)):
    if S[i] == str(now):
        count += 1
    else:
        nums.append(count)
        now = 1 - now
        count = 1
if count != 0:
    nums.append(count)
if len(nums) % 2 == 0:
    nums.append(0)  # ? 1を足すのでは？？

add = 2 * K + 1

sum = []
sum.append(0)
for i in range(len(nums)+1):
    sum.append(sum[i] + nums[i])

ans = 0

for i in range(len(nums),2):
    left = i
    right = min(i + add, len(nums))
    tmp = sum[right] - sum[left] 
    ans = max(tmp, ans)

print(ans)

Submission status: Runtime Error

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

nums = []
now = 1
count = 0
for i in range(len(S)):
    if S[i] == str(now):
        count += 1
    else:
        nums.append(count)
        now = 1 - now
        count = 1
if count != 0:
    nums.append(count)
if len(nums) % 2 == 0:
    nums.append(0)  # ? 1を足すのでは？？

add = 2 * k + 1

sum = []
sum.append(0)
for i in range(len(nums) + 1):
    sum[i+1] = sum[i] + nums[i]

ans = 0

for i in range(len(nums),2):
    left = i
    right = min(i + add, len(nums))
    tmp = sum[right] - sum[left] 
    ans = max(tmp, ans)

print(ans)

Submission status: Runtime Error

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

nums = []
now = 1
count = 0
for i in range(len(S)):
    if S[i] == str(now):
        count += 1
    else:
        nums.append(count)
        now = 1 - now
        count = 1
if count != 0:
    nums.append(count)
if len(nums) % 2 == 0:
    nums.append(0)  # ? 1を足すのでは？？

add = 2 * k + 1

sum = []
sum.append(0)
for i in range(len(nums) + 1):
    sum[i+1] = sum[i] + nums[i]

ans = 0

for i in range(len(nums) / 2):
    left = i
    right = min(i + add, len(nums))
    tmp = sum[right] - sum[left]
    ans = max(tmp, ans)

print(ans)

Submission status: Runtime Error

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

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

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

m = 0
l = []
if S[0] == '1':
    for i in range(len(memo)):
        if i%2 == 0:
            l.append(memo[i+2*K+1]-memo[i])
        else:
            l.append(memo[i+2*K]-memo[i])



        # if s > m:
        #     m = s
else:
    for i in range(len(memo)):
        if i%2 == 1:
            l.append(memo[i+2*K+1]-memo[i])
        else:
            l.append(memo[i+2*K]-memo[i])


        # if s > m:
        #     m = s

print(max(l))

Submission status: Runtime Error

Solution code:


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

State = [0] * N
counter0 = 0
counter1 = 0
flag = -1
jj = 0
for i in range(N):
    if S[i] == '0':
        if flag == 1:
            State[jj] = counter1
            jj +=1
            counter1 = 0
        counter0 += 1
        flag = 0
    if S[i] == '1':
        if flag == 0:
            State[jj] = counter0
            jj += 1
            counter0 = 0
        counter1 += 1
        flag = 1
    if i == N-1:
        State[jj] = max(counter0,counter1)
        

State = int(S[0] == '0') * [0] + State[:]
N2 = len(State)
ans = 0
total = 0
K2 = 2*K+1

if K2 >= N2:
    K2 = N2

for i in range(K2):
    total += State[i]
        
ans = total
for i in range(0,N2-K2,2):
    total2 = total - State[i] - State[i+1] + State[i+K2] + State[i+1+K2]
    ans = max(ans,total2)
    total = total2
    
print(ans)

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
S = input()
new_S = []
count_S = []

cur_c = S[0]
cur_count = 0

new_S.append(cur_c)
for c in S:
    if c == cur_c:
        cur_count += 1
    else:
        new_S.append(c)
        count_S.append(cur_count)
        cur_count = 1
        cur_c = c
count_S.append(cur_count)        

new_count = []
st_ix = 0
if S[0] == '0':
    cur_sum = sum(count_S[:2*K])
    st_ix = 1
    new_count.append(cur_sum)
else:
    cur_sum = sum(count_S[:2*K+1])
    new_count.append(cur_sum)

en_ix = st_ix + 2*K + 1
cur_sum = sum(count_S[st_ix:en_ix])
new_count.append(cur_sum)

while en_ix < len(count_S):
    # shift st_ix
    st_ix = st_ix + 2
    en_ix = en_ix + 2
    cur_sum -= (count_S[st_ix-2] + count_S[st_ix-1])
    cur_sum += (count_S[min(en_ix-2, len(count_S)-1]
                        + count_S[min(en_ix-1, len(count_S)-1])
    new_count.append(cur_sum)

print(max(new_count))

Submission status: Runtime Error

Solution code:
from itertools import accumulate

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

nums = []
tmp = "1" #はじめに0が入ったら、0からスタートというのがわかる。
count = 0
for s in S:
    if tmp == s:
        count += 1
    else:
        nums.append(count)
        count = 1
        tmp = s
nums.append(count)
if tmp == "0":
    l.append(0) #エッジの条件

# print(nums)
# 累積和表示にする(listって足し算できるのね)
# これによって、各文字最初の値が明らかになる
acc = [0]+list(accumulate(nums))
# print(acc)
n = len(nums)
w = 2*K + 1

if len(nums) <= w:
    print(N)
else:
    ans = 0
    for i in range(0,n-w+1,2): #2個飛ばし
        ans = max(ans,acc[i+w]-acc[i])

    print(ans)

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
S = input()
new_S = []
count_S = []

cur_c = S[0]
cur_count = 0

new_S.append(cur_c)
for c in S:
    if c == cur_c:
        cur_count += 1
    else:
        new_S.append(c)
        count_S.append(cur_count)
        cur_count = 1
        cur_c = c
count_S.append(cur_count)        

new_count = []
st_ix = 0
if S[0] == '0':
    cur_sum = sum(count_S[:2*K])
    st_ix = 1
    new_count.append(cur_sum)
else:
    cur_sum = sum(count_S[:2*K+1])
    new_count.append(cur_sum)

en_ix = st_ix + 2*K + 1
cur_sum = sum(count_S[st_ix:en_ix])
new_count.append(cur_sum)

while en_ix < len(count_S):
    # shift st_ix
    st_ix = st_ix + 2
    en_ix = en_ix + 2
    cur_sum -= (count_S[st_ix-2] + count_S[st_ix-1])
    cur_sum += (count_S[max(en_ix-2, len(count_S)-1]
                        + count_S[max(en_ix-1, len(count_S)-1])
    new_count.append(cur_sum)

print(max(new_count))