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:
from collections import deque

def main():
    n,k = map(int,input().split())
    raw_list = input()
    
    q = deque([])

    startfeet = 0
    endfeet = 0
    max_len = 0
    for i in range(n):
        item = raw_list[i]
        if item == '0':
            endfeet = get_endhand(raw_list,i+1,n)
            append_list = [startfeet,endfeet]
            startfeet = get_startfeet(raw_list,i+1,n)

            if len(q) == k:
                if q[k-1][1] - q[0][0] + 1 > max_len:
                    max_len = q[k-1][1] - q[0][0] + 1
                q.popleft()
                q.append(append_list)

            else:
                q.append(append_list)
    print(q)
    print(max_len)

def get_startfeet(raw_list,i,n):
    for index in range(i,n):
        if raw_list[index-1] == '0' and raw_list[index] == '1':
            return index
    return index

def get_endhand(raw_list,i,n):
    for index in range(i,n):
        if raw_list[index-1] == '1' and raw_list[index] == '0':
            return index-1
    return index

if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
import sys
from heapq import heappush,heappop
sys.setrecursionlimit(10**9)
INF = 10**18


def input():
    return sys.stdin.readline().rstrip()


def main():
    N,K=map(int,input().split())
    S=list(map(int,list(input())))
    first=S[0]
    l=[]
    c=1
    for i in range(N-1):
        if S[i+1] == S[i]:
            c+=1
        else:
            l.append(c)
            c=1
    l.append(c)
    if K%2==1:
        if len(l) > K*2:
            ans = sum(l[:K*2])
            op=ans
            for i in range(len(l)-K*2):
                op = op-l[i]+l[K*2+i]
                ans=max(ans,op)
        else:
            ans = sum(l)
    else:
        if len(l) > K*2+1:
            ans=sum(l[:K*2+1])
            op=ans
            for i in range(not(first),len(l)-(K*2+1),2):
                op = op-(l[i]+l[i+1])+l[K*2+1+i]+l[K*2+1+i+1]
                ans = max(ans, op)
        else:
            ans=sum(l)
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
#include <bits/stdc++.h>
#define REP(i,n) for(int i=0;i<(n);++i)
using namespace std;
typedef long long ll;

int main() {
  int N, K;
  string S;
  cin >> N >> K >> S;

  if (K>=N) {
    cout << N << endl;
    return 0;
  }

  if (S[N-1]=='0' ) {
    S = S + '1';
    N++;
  }
  if (S[0]=='0' ) {
    S = '1' + S;
    N++;
  }
  S = S + 'Z';
  vector<int> C(N,0), R(N+1,0);
  int j = 0;
  int cnt = 1;
  int znum = 0;
  REP(i,N){
    if ( S[i] != S[i+1] ) {
      if ( i == 0 && S[i] == '1' ) cnt = 0;
      if ( i == N-2 && S[i+1] == '1' ) cnt = 0;
      C[j] = cnt;
      R[j+1] = R[j] + C[j];
      j++;
      cnt = 1;
      if ( S[i] == '0' ) znum++;
    } else {
      cnt++;
    }
  }
  int smax = 0;
  for ( int l=0; (l+K-1)<znum; ++l ) {
    smax = max( smax, R[2*(l+K-1)+3] - R[2*l] );
  }
//  REP(i,j+1)
//    cout << C[i] << " " << R[i] <<endl;
  cout << smax << endl;
}

Submission status: Runtime Error

Solution code:
from collections import deque

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

hoge = deque()

now = int(S[0])
Sum = 1
for i in range(1, N):
    if now == int(S[i]):
        Sum += 1
    else:
        hoge.append(Sum

        Sum = 1
        now = (now + 1) % 2

    if i == N - 1:
        hoge.append(Sum)

if N == 1:
    hoge.append(1)

rui = deque()
rui.append(0)
Sum = 0

for i in range(len(hoge)):
    Sum += hoge[i]
    rui.append(Sum)

first = int(S[0])
max = max(rui)

for i in range(first, len(rui), 2):

    if 2 * K + i == len(rui):
        i -= 1
    elif 2 * K + i > len(rui):
        break

    if rui[2 * K + i] - rui[i - first] > max:
        max = rui[2 * K + i] - rui[i - first]

    print(rui[2 * K + i] - rui[i - first])

print(max)

Submission status: Runtime Error

Solution code:
def MAP(): return list(map(int,input().split()))
def INT(): return int(input())
def FLOAT(): return float(input())
MOD = 10**9+7


n,k = MAP()
s = input()
k*=2

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

r = len(lst)
lst += [n]*10
ans=[]
for i in range(r):
    if s[lst[i]]=="0":
        ans.append(lst[i+k] - lst[i])
    else:
        ans.append(lst[i+k+1] - lst[i])

print(max(ans))

Submission status: Runtime Error

Solution code:
def MAP(): return list(map(int,input().split()))
def INT(): return int(input())
def FLOAT(): return float(input())
MOD = 10**9+7


n,k = MAP()
s = input()
k*=2

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

r = len(lst)
lst += [n]*10
ans=[]
for i in range(r):
    if s[lst[i]]=="0":
        ans.append(lst[i+k] - lst[max(i-1,0)])
    else:
        ans.append(lst[i+k+1] - lst[i])

print(max(ans))

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
s=input()
from collections import deque
l=deque()
c=1
for i in range(1,n):
    if s[i]!=s[i-1]:
        l.append([s[i-1],c])
        c=1
    else:
        c+=1
l.append([s[-1],c])
if l[0][0]=="1":
    a,c=l.popleft()
    l[0][1]+=c
if l[-1][0]=="1":
    a,c=l.pop()
    l[-1][1]+=c

ll=[]
for i in range(len(l)):
    ll.append(l[i][1])

ans=0
for i in range(len(ll)-2*k+1):
    ans=max(ans,sum(ll[i:i+2*k]))
print(ans)

Submission status: Runtime Error

Solution code:
# D - Handstand

# 逆立ちしている人が連続する区間のインデックスを保存する
# 指示1回あたり2つの区間を繋ぐことができる
# 連続したK+1個の区間を順に選んで繋ぎ、最も逆立ち区間が長くなる場合を探す。端から繋ぐ場合は例外処理

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

if len(set(S)) == 1 or K >= N//2 + 1:
    print(N)
    exit()

handstand_range_idx = []
range_start_idx = 0
for idx in range(1,N):
    if (S[idx-1] == "1" and S[idx] == "0"):
        range_end_idx = idx - 1
        handstand_range_idx.append([range_start_idx, range_end_idx])
    elif idx == N-1 and S[idx] == "1":
        range_end_idx = idx
        handstand_range_idx.append([range_start_idx, range_end_idx])
    elif S[idx-1] == "0" and S[idx] == "1":
        range_start_idx = idx

ans = max(handstand_range_idx[K-1][1]+1, (N-1) - handstand_range_idx[-K][0] + 1)

for first_link_range_idx in range(len(handstand_range_idx)):
    try:
        ans = max(ans, handstand_range_idx[first_link_range_idx + K][1] - handstand_range_idx[first_link_range_idx][0] + 1)
    except IndexError:
        pass

print(ans)

Submission status: Runtime Error

Solution code:
# D - Handstand

# 逆立ちしている人が連続する区間のインデックスを保存する
# 指示1回あたり2つの区間を繋ぐことができる
# 連続したK+1個の区間を順に選んで繋ぎ、最も逆立ち区間が長くなる場合を探す。端から繋ぐ場合は例外処理

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

if len(set(S)) == 1:
    print(N)
    exit()

handstand_range_idx = []
range_start_idx = 0
for idx in range(1,N):
    if (S[idx-1] == "1" and S[idx] == "0"):
        range_end_idx = idx - 1
        handstand_range_idx.append([range_start_idx, range_end_idx])
    elif idx == N-1 and S[idx] == "1":
        range_end_idx = idx
        handstand_range_idx.append([range_start_idx, range_end_idx])
    elif S[idx-1] == "0" and S[idx] == "1":
        range_start_idx = idx

ans = max(handstand_range_idx[K-1][1]+1, (N-1) - handstand_range_idx[-K][0] + 1)
for first_link_range_idx in range(len(handstand_range_idx)):
    try:
        ans = max(ans, handstand_range_idx[first_link_range_idx + K][1] - handstand_range_idx[first_link_range_idx][0] + 1)
    except IndexError:
        pass

print(ans)

Submission status: Runtime Error

Solution code:
n,k = map(int,input().split())
list1 = list(map(int,input()))
list2 = []
count = 1
for i in range(1,n):
	if list1[i] != list1[i-1]:
		list2.append(count)
		count = 0
	count += 1
list2.append(count) #最後の処理
length = 0
list3 = []
#以下のループはn=1のときのみうまく動作しないので、例外処理
if n ==1:
	print(1)
	exit()

if list1[0] == 0: #長さはk*2
	length1 = k*2 #初期値
	length2 = k*2 + 1
	for i in range(len(list2)):
		if length != length1:
			length = length1
		else:
			length = length2
		if i+length >= n:
			break
		list3.append(sum(list2[i:i+length]))

else: #長さはk*2+1
	length1 = k*2 +1 #初期値
	length2 = k*2
	for i in range(len(list2)):
		if length != length1:
			length = length1
		else:
			length = length2
		if i+length >= n:
			break
		list3.append(sum(list2[i:i+length]))

print(max(list3))

Submission status: Runtime Error

Solution code:
N,K=map(int,input().split())
S=input()
X,Y=[],[]
M=0
for i in range(len(S)):
  if(S[i]=='1' and M==0):
    X.append(i)
    M+=1
  elif(S[i]=='1'):
    M+=1
  else:
    if(M):
      Y.append(i-1)
    M=0
if(M):
  Y.append(len(S)-1)
ans=0
if(len(X)>=K):
  for i in range(len(X)-K):
    ans=max(ans,Y[i+K]-X[i]+1)
else:
  ans=len(S)
ans=max(ans,Y[min(K-1,len(X)-1)]+1)
ans=max(ans,len(S)-X[max(len(X)-K,0)])
print(ans)

Submission status: Runtime Error

Solution code:
N,K=map(int,input().split())
S=input()
X,Y=[],[]
M=0
for i in range(len(S)):
  if(S[i]=='1' and M==0):
    X.append(i)
    M+=1
  elif(S[i]=='1'):
    M+=1
  else:
    if(M):
      Y.append(i-1)
    M=0
if(M):
  Y.append(len(S)-1)
ans=0
if(len(X)>=K):
  for i in range(len(X)-K):
    ans=max(ans,Y[i+K]-X[i]+1)
else:
  ans=len(S)
ans=max(ans,Y[min(K-1,len(X))]+1)
ans=max(ans,len(S)-X[max(len(X)-K,0)])
print(ans)

Submission status: Runtime Error

Solution code:
N,K=map(int,input().split())
S=input()
X,Y=[],[]
M=0
for i in range(len(S)):
  if(S[i]=='1' and M==0):
    X.append(i)
    M+=1
  elif(S[i]=='1'):
    M+=1
  else:
    if(M):
      Y.append(i-1)
    M=0
if(M):
  Y.append(len(S)-1)
ans=0
for i in range(max(len(X)-K,0)):
  ans=max(ans,Y[i+K]-X[i]+1)
ans=max(ans,Y[min(K-1,len(X))]+1)
ans=max(ans,len(S)-X[max(len(X)-K,0)])
print(ans)

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
s=list(input())
a=[]
f=1
s.append(2)
for i in range(n):
    if s[i]==s[i+1]:
        f+=1
    else:
        a.append(f)
        f=1
g=len(a)
r=[0]*(g+1)
for i in range(g):
    r[i+1]=r[i]+a[i]
if s[0]=='1':
    ans=sun(a[2*i:2*i+2*k+1])
    for i in range(g):
        if 2*i+2*k+1<g+1:
            ans=max(ans,r[2*i+2*k+1]-r[2*i])
        else:
            break
    print(ans)
else:
    ans=sum(a[0:2*k])
    for i in range(g):
        if 2*i+2*k+2<g+1:
            ans=max(ans,r[2*i+2*k+2]-r[2*i+1])
        else:
            break
    print(ans)

Submission status: Runtime Error

Solution code:
import sys
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
mod = 10**9 + 7
inf = float('inf')
ans = int(0)

N, K = LI()
S = str(input())
S = S + "2"
d1 = []
d0 = []
k = S[0]
if k=="0":
    d1.append(0)
cou = int(1)
for i in range(N):
    if S[i]!=S[i+1]:
        if S[i]=="0":
            d0.append(cou)
        if S[i]=="1":
            d1.append(cou)
        cou = int(1)
    else:
        cou +=1

#print(d1)
#print(d0)

ansC = []
t = 2*K+1
newN = len(d1)
if len(d1)==1 or d0==[] or K>=N//2+1:
    ansC.append(N)

if newN!=len(d0):
    d0.append(0)
    d0.append(0)
sumd1 = [0]*(newN+t+5)
sumd0 = [0]*(newN+t+5)
for i in range(newN):
    sumd1[i+1] = d1[i]
    sumd0[i+1] = d0[i]
for i in range(newN+1):
    sumd1[i+1] += sumd1[i]
    sumd0[i+1] += sumd0[i]

#for i in range(max(newN-K+1,K)):
if ansC==[] and (newN - K + 1)>=1:
    for i in range(max(newN-K+1,K)):
        ans = (sumd1[i + K + 1] - sumd1[i]) + (sumd0[i + K] - sumd0[i])
        ansC.append(ans)

print(max(ansC))
#print(ansC)
#print(sumd1)
#print(sumd0)

Submission status: Runtime Error

Solution code:
import sys
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
mod = 10**9 + 7
inf = float('inf')
ans = int(0)

N, K = LI()
S = str(input())
S = S + "2"
d1 = []
d0 = []
k = S[0]
if k=="0":
    d1.append(0)
cou = int(1)
for i in range(N):
    if S[i]!=S[i+1]:
        if S[i]=="0":
            d0.append(cou)
        if S[i]=="1":
            d1.append(cou)
        cou = int(1)
    else:
        cou +=1

#print(d1)
#print(d0)

ansC = []
t = 2*K+1
newN = len(d1)
if len(d1)==1 or d0==[] or K>=N//2+1:
    ansC.append(N)

if newN!=len(d0):
    d0.append(0)
    d0.append(0)
sumd1 = [0]*(newN+t+5)
sumd0 = [0]*(newN+t+5)
for i in range(newN):
    sumd1[i+1] = d1[i]
    sumd0[i+1] = d0[i]
for i in range(newN+1):
    sumd1[i+1] += sumd1[i]
    sumd0[i+1] += sumd0[i]

#for i in range(max(newN-K+1,K)):
if ansC==[] and (newN - K + 1)>=1:
    for i in range(newN - K + 1):
        ans = (sumd1[i + K + 1] - sumd1[i]) + (sumd0[i + K] - sumd0[i])
        ansC.append(ans)

print(max(ansC))
#print(ansC)
#print(sumd1)
#print(sumd0)

Submission status: Runtime Error

Solution code:
import sys
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
mod = 10**9 + 7
inf = float('inf')
ans = int(0)

N, K = LI()
S = str(input())
S = S + "2"
d1 = []
d0 = []
k = S[0]
if k=="0":
    d1.append(0)
cou = int(1)
for i in range(N):
    if S[i]!=S[i+1]:
        if S[i]=="0":
            d0.append(cou)
        if S[i]=="1":
            d1.append(cou)
        cou = int(1)
    else:
        cou +=1

#print(d1)
#print(d0)

ansC = []
t = 2*K+1
newN = len(d1)
if len(d1)==1 or d0==[] or K>=N//2+1:
    ansC.append(N)

if newN!=len(d0):
    d0.append(0)
    d0.append(0)
sumd1 = [0]*(newN+t)
sumd0 = [0]*(newN+t)
for i in range(newN):
    sumd1[i+1] = d1[i]
    sumd0[i+1] = d0[i]
for i in range(newN+1):
    sumd1[i+1] += sumd1[i]
    sumd0[i+1] += sumd0[i]

#for i in range(max(newN-K+1,K)):
if ansC==[] and (newN - K + 1)>=1:
    for i in range(newN - K + 1):
        ans = (sumd1[i + K + 1] - sumd1[i]) + (sumd0[i + K] - sumd0[i])
        ansC.append(ans)

print(max(ansC))
#print(ansC)
#print(sumd1)
#print(sumd0)

Submission status: Runtime Error

Solution code:
import sys
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
mod = 10**9 + 7
inf = float('inf')
ans = int(0)

N, K = LI()
S = str(input())
S = S + "2"
d1 = []
d0 = []
k = S[0]
if k=="0":
    d1.append(0)
cou = int(1)
for i in range(N):
    if S[i]!=S[i+1]:
        if S[i]=="0":
            d0.append(cou)
        if S[i]=="1":
            d1.append(cou)
        cou = int(1)
    else:
        cou +=1

#print(d1)
#print(d0)

ansC = []
t = 2*K+1
newN = len(d1)
if len(d1)==1 or d0==[] or K>=N//2+1:
    ansC.append(N)

if newN!=len(d0):
    d0.append(0)
    d0.append(0)
sumd1 = [0]*(newN+t)
sumd0 = [0]*(newN+t)
for i in range(newN):
    sumd1[i+1] = d1[i]
    sumd0[i+1] = d0[i]
for i in range(newN+1):
    sumd1[i+1] += sumd1[i]
    sumd0[i+1] += sumd0[i]

#for i in range(max(newN-K+1,K)):
if ansC==[] and (newN - K + 1)>=0:
    for i in range(newN - K + 1):
        ans = (sumd1[i + K + 1] - sumd1[i]) + (sumd0[i + K] - sumd0[i])
        ansC.append(ans)

print(max(ansC))
#print(ansC)
#print(sumd1)
#print(sumd0)

Submission status: Runtime Error

Solution code:
import sys
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
mod = 10**9 + 7
inf = float('inf')
ans = int(0)

N, K = LI()
S = str(input())
S = S + "2"
d1 = []
d0 = []
k = S[0]
if k=="0":
    d1.append(0)
cou = int(1)
for i in range(N):
    if S[i]!=S[i+1]:
        if S[i]=="0":
            d0.append(cou)
        if S[i]=="1":
            d1.append(cou)
        cou = int(1)
    else:
        cou +=1

#print(d1)
#print(d0)

ansC = []
t = 2*K+1
newN = len(d1)
if len(d1)==1 or d0==[] or K>=N//2+1:
    ansC.append(N)

if newN!=len(d0):
    d0.append(0)
    d0.append(0)
sumd1 = [0]*(newN+t)
sumd0 = [0]*(newN+t)
for i in range(newN):
    sumd1[i+1] = d1[i]
    sumd0[i+1] = d0[i]
for i in range(newN+1):
    sumd1[i+1] += sumd1[i]
    sumd0[i+1] += sumd0[i]

#for i in range(max(newN-K+1,K)):
if ansC==[]:
    for i in range(newN - K + 1):
        ans = (sumd1[i + K + 1] - sumd1[i]) + (sumd0[i + K] - sumd0[i])
        ansC.append(ans)

print(max(ansC))
#print(ansC)
#print(sumd1)
#print(sumd0)

Submission status: Runtime Error

Solution code:
import sys
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
mod = 10**9 + 7
inf = float('inf')
ans = int(0)

N, K = LI()
S = str(input())
S = S + "2"
d1 = []
d0 = []
k = S[0]
if k=="0":
    d1.append(0)
cou = int(1)
for i in range(N):
    if S[i]!=S[i+1]:
        if S[i]=="0":
            d0.append(cou)
        if S[i]=="1":
            d1.append(cou)
        cou = int(1)
    else:
        cou +=1

#print(d1)
#print(d0)

ansC = []
t = 2*K+1
newN = len(d1)
if len(d1)==1 or d0==[] or K>=N//2+1:
    ansC.append(N)

if newN!=len(d0):
    d0.append(0)
    d0.append(0)
sumd1 = [0]*(newN+t)
sumd0 = [0]*(newN+t)
for i in range(newN):
    sumd1[i+1] = d1[i]
    sumd0[i+1] = d0[i]
for i in range(newN+1):
    sumd1[i+1] += sumd1[i]
    sumd0[i+1] += sumd0[i]

#for i in range(max(newN-K+1,K)):
for i in range(newN-K+1):
    ans = (sumd1[i+K+1]-sumd1[i]) + (sumd0[i+K]-sumd0[i])
    ansC.append(ans)


print(max(ansC))
#print(ansC)
#print(sumd1)
#print(sumd0)

Submission status: Runtime Error

Solution code:
import sys
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
mod = 10**9 + 7
inf = float('inf')
ans = int(0)

N, K = LI()
S = str(input())
S = S + "2"
d1 = []
d0 = []
k = S[0]
if k=="0":
    d1.append(0)
cou = int(1)
for i in range(N):
    if S[i]!=S[i+1]:
        if S[i]=="0":
            d0.append(cou)
        if S[i]=="1":
            d1.append(cou)
        cou = int(1)
    else:
        cou +=1

#print(d1)
#print(d0)

ansC = []
t = 2*K+1
newN = len(d1)
if len(d1)==1 or d0==[]:
    ansC.append(N)

if newN!=len(d0):
    d0.append(0)
    d0.append(0)
sumd1 = [0]*(newN+t)
sumd0 = [0]*(newN+t)
for i in range(newN):
    sumd1[i+1] = d1[i]
    sumd0[i+1] = d0[i]
for i in range(newN+1):
    sumd1[i+1] += sumd1[i]
    sumd0[i+1] += sumd0[i]

for i in range(newN-K+1):
    ans = (sumd1[i+K+1]-sumd1[i]) + (sumd0[i+K]-sumd0[i])
    ansC.append(ans)


print(max(ansC))

Submission status: Runtime Error

Solution code:
import sys
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
mod = 10**9 + 7
inf = float('inf')
ans = int(0)

N, K = LI()
S = str(input())
S = S + "2"
d1 = []
d0 = []
k = S[0]
if k=="0":
    d1.append(0)
cou = int(1)
for i in range(N):
    if S[i]!=S[i+1]:
        if S[i]=="0":
            d0.append(cou)
        if S[i]=="1":
            d1.append(cou)
        cou = int(1)
    else:
        cou +=1

#print(d1)
#print(d0)

ansC = []
t = 2*K+1
newN = len(d1)
if newN!=len(d0):
    d0.append(0)
sumd1 = [0]*(newN+t)
sumd0 = [0]*(newN+t)
for i in range(newN):
    sumd1[i+1] = d1[i]
    sumd0[i+1] = d0[i]
for i in range(newN+1):
    sumd1[i+1] += sumd1[i]
    sumd0[i+1] += sumd0[i]

for i in range(newN-K+1):
    ans = (sumd1[i+K+1]-sumd1[i]) + (sumd0[i+K]-sumd0[i])
    ansC.append(ans)

print(max(ansC))
#print(sumd1)
#print(sumd0)

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
s=input()
s+="end"
if n>=2:
    uselist=[]
    for i in range(len(s)-1):
        if i==0 and s[i]=="0":
            uselist.append(0)
        if s[i]=="1" and s[i+1]=="0":
            uselist.append(i+1)
        if s[i+1]=="end":
            continue
    ans=0
    if uselist[0]>=1:
        uselist.insert(0,0)
        length=len(uselist)

        for i in range(length):
            if i+k+1<=length-1:
                ans=max(ans,uselist[i+k+1]-uselist[i]-1)

            else:
                ans=max(ans,n-uselist[i]-1)
    
                break
        print(ans)


    elif uselist[0]==0:
        uselist.insert(0,-1)

        length=len(uselist)
        for i in range(length):
            if i+k+1<=length-1:
                ans=max(ans,uselist[i+k+1]-uselist[i]-1)

            else:
                ans=max(ans,n-uselist[i]-1)

                break
        print(ans)
else:
    print(1)

Submission status: Runtime Error

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

uselist=[]
for i in range(len(s)-1):
    if i==0 and s[i]=="0":
        uselist.append(0)
    if s[i]=="1" and s[i+1]=="0":
        uselist.append(i+1)
    if s[i+1]=="end":
        continue
ans=0
if uselist[0]>=1:
    uselist.insert(0,0)
    length=len(uselist)

    for i in range(length):
        if i+k+1<=length-1:
            ans=max(ans,uselist[i+k+1]-uselist[i]-1)

        else:
            ans=max(ans,n-uselist[i]-1)

            break
    print(ans)
    print(uselist)

elif uselist[0]==0:
    uselist.insert(0,-1)

    length=len(uselist)
    for i in range(length):
        if i+k+1<=length-1:
            ans=max(ans,uselist[i+k+1]-uselist[i]-1)

        else:
            ans=max(ans,n-uselist[i]-1)

            break
    print(ans)

Submission status: Runtime Error

Solution code:
# 累積和
N, K = map(int, input().split())
S = input()

p = '1'
count = 0
cs = []
for c in S:
  if c != p:
    cs.append(count)
    p = c
    count = 1
  else:
    count += 1
cs.append(count)
if p != '0':
  cs.append(0)

if len(cs) < 2 * K:
  print(N)

for i in range(1, len(cs)):
  cs[i] += cs[i - 1]

result = cs[2 * K]
for i in range(2, len(cs) - 2 * K, 2):
  t = cs[2 * K + i] - cs[i - 1]
  if t > result:
    result = t
print(result)

Submission status: Runtime Error

Solution code:
# 累積和
N, K = map(int, input().split())
S = input()

p = '1'
count = 0
cs = []
for c in S:
  if c != p:
    cs.append(count)
    p = c
    count = 1
  else:
    count += 1
cs.append(count)

if len(cs) < 2 * K:
  print(N)

for i in range(1, len(cs)):
  cs[i] += cs[i - 1]

result = cs[2 * K]
for i in range(2, len(cs) - 2 * K, 2):
  t = cs[2 * K + i] - cs[i - 1]
  if t > result:
    result = t
print(result)

Submission status: Runtime Error

Solution code:
import sys

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

kc = 0
inc = []
dec = []

if S == format(2**N - 1, 'b'):
  print(N)
  sys.exit()

if S[0] == "0":
  dec.append(0)

inc.append(0)

for i in range(1, N):
  if S[i-1] == "0" and S[i] == "1":
    inc.append(i)
  if S[i-1] == "1" and S[i] == "0":
    dec.append(i)

dec.append(N)

st = 0
j = 0
ans = dec[K]
for i in range(K, len(dec)):
  if j < len(inc):
    st = inc[j]
  ans = max(ans, dec[i] - st)
  j += 1

print(ans)

Submission status: Runtime Error

Solution code:
import sys

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

kc = 0
inc = []
dec = []

if S == format(2**N - 1, 'b'):
  print(N)
  sys.exit()

if S[0] == "0":
  dec.append(0)

inc.append(0)

for i in range(1, N):
  if S[i-1] == "0" and S[i] == "1":
    inc.append(i)
  if S[i-1] == "1" and S[i] == "0":
    dec.append(i)

dec.append(N)

st = 0
j = 0
ans = dec[K]
for i in range(K, len(dec)):
  st = inc[j]
  ans = max(ans, dec[i] - st)
  j += 1

print(ans)

Submission status: Runtime Error

Solution code:
import sys

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

kc = 0
inc = []
dec = []

if S == format(2**N - 1, 'b'):
  print(N)
  sys.exit()

if S[0] == "0":
  dec.append(0)

for i in range(1, N):
  if S[i-1] == "0" and S[i] == "1":
    inc.append(i)
  if S[i-1] == "1" and S[i] == "0":
    dec.append(i)

dec.append(N)

st = 0
j = 0
ans = dec[K]
for i in range(K+1, len(dec)):
  st = inc[j]
  ans = max(ans, dec[i] - st)
  j += 1

print(ans)

Submission status: Runtime Error

Solution code:
N,K = map(int,input().split())
S = input() + "#"
A = []
cnt = 1
for i in range(N) :
    if S[i] == S[i+1] :
        cnt += 1
    else :
        A.append(cnt)
        cnt = 1

if S[0] == "1" :
    ans = sum(A[:2*K+1])
    calc = ans
    for i in range(2*K+1, len(A)) :
        calc += A[i]
        if i % 2 == 1 :
            calc -= (A[i-1-2*K] + A[i-2*K])
        ans = max(ans, calc)
else :
    ans = sum(A[:2*K])
    for i in range(2*K,len(A)) :
        calc += A[i]
        if i == 2*K :
            calc -= A[0]
        elif i % 2 == 0  :
            calc -= (A[i-1-2*K] + A[i-2*K])
        ans = max(ans, calc)

print(ans)

Submission status: Runtime Error

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

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

count=0
for si in s:
  if count==0:
    flag = si
    count=1
  elif flag == si:
    count+=1
  else:        
    sp.append(count)
    flag = si
    count=1
if count>0:
  sp.append(count)
if s[-1]=='1' and flag=='1':
  sp.append(0)
  
ss=[]
for i in range(len(sp)-2*k+1):
  if i%2==0:    
    ss.append(sum(sp[i:i+2*k]))
  else:
    ss.append(sum(sp[i:i+2*k+1]))
print(max(ss))

Submission status: Runtime Error

Solution code:
N,K = map(int, input().split())
S = input()
S = S.replace('10','1,0').replace('01','0,1')
S = S.split(',')
S_len = [len(s) for s in S]
arr = []
for i in range(len(S)+1//2):
  if 2*(i+K)+1 > len(S):
    break
  arr.append(sum(S_len[(2*i):(2*(i+K)+1)]))
  ans = max(arr)
print(ans)

Submission status: Runtime Error

Solution code:
#D
from collections import deque
n,k=map(int,input().split())
s=input()
if n==1:
    print(1)
else:
    a=deque([])
    if s[0]=="1":
        a.append([0])
    for i in range(1,n):
        if s[i-1:i+1]=="01":
            a.append([i])
        elif s[i-1:i+1]=="10":
            a[-1].append(i-1)
    if s[n-1]=="1":
        a[-1].append(n-1)
    if a[0][0]!=0:
        a.appendleft([0,0])
    if a[-1][1]!=n-1:
        a.append([n-1,n-1])
    if len(a)-1<=k:
        print(n)
    else:
        t=0
        for i in range(len(a)-k):
            t=max(t,a[i+k][1]-a[i][0]+1)
        print(t)

Submission status: Runtime Error

Solution code:
from collections import deque
n,k=map(int,input().split())
s=input()
if n==1:
    print(1)
else:
	a=deque([])
	if s[0]=="1":
    	a.append([0])
	for i in range(1,n):
    	if s[i-1:i+1]=="01":
        	a.append([i])
    	elif s[i-1:i+1]=="10":
        	a[-1].append(i-1)
	if s[n-1]=="1":
    	a[-1].append(n-1)
	if a[0][0]!=0:
    	a.appendleft([0,0])
	if a[-1][1]!=n-1:
    	a.append([n-1,n-1])
        
	if len(a)-1<=k:
    	print(n)
	else:
    	t=0
    	for i in range(len(a)-k):
        	t=max(t,a[i+k][1]-a[i][0]+1)
    	print(t)

Submission status: Runtime Error

Solution code:
from collections import deque
n,k=map(int,input().split())
s=input()
if n==1:
    print(1)
else:
	a=deque([])
	if s[0]=="1":
    	a.append([0])
	for i in range(1,n):
    	if s[i-1:i+1]=="01":
        	a.append([i])
    	elif s[i-1:i+1]=="10":
        	a[-1].append(i-1)
	if s[n-1]=="1":
    	a[-1].append(n-1)
	if a[0][0]!=0:
    	a.appendleft([0,0])
	if a[-1][1]!=n-1:
    	a.append([n-1,n-1])
	if len(a)-1<=k:
    	print(n)
	else:
    	t=0
    	for i in range(len(a)-k):
        	t=max(t,a[i+k][1]-a[i][0]+1)
    	print(t)

Submission status: Runtime Error

Solution code:
#D
import sys
from collections import deque
n,k=map(int,input().split())
s=input()
if n==1:
    print(1)
    sys.exit()
a=deque([])
if s[0]=="1":
    a.append([0])
for i in range(1,n):
    if s[i-1:i+1]=="01":
        a.append([i])
    elif s[i-1:i+1]=="10":
        a[-1].append(i-1)
if s[n-1]=="1":
    a[-1].append(n-1)
if a[0][0]!=0:
    a.appendleft([0,0])
if a[-1][1]!=n-1:
    a.append([n-1,n-1])
if len(a)-1<=k:
    print(n)
else:
    t=0
    for i in range(len(a)-k):
        t=max(t,a[i+k][1]-a[i][0]+1)
    print(t)

Submission status: Runtime Error

Solution code:
#D
import sys
from collections import deque
n,k=map(int,input().split())
s=input()
if n==1:
    print(1)
    sys.exit()
a=deque([])
if s[0]=="1":
    a.append([0])
for i in range(1,n):
    if s[i-1:i+1]=="01":
        a.append([i])
    elif s[i-1:i+1]=="10":
        a[-1].append(i-1)
if s[n-1]=="1":
    a[-1].append(n-1)

if a[0][0]!="0":
    a.appendleft([0,0])
if a[-1][1]!="n-1":
    a.append([n-1,n-1])

if len(a)-1<=k:
    print(n)
else:
    t=0
    for i in range(len(a)-k):
        t=max(t,a[i+k][1]-a[i][0]+1)
    print(t)

Submission status: Runtime Error

Solution code:
#D
from collections import deque
n,k=map(int,input().split())
s=input()

a=deque([])
if s[0]=="1":
    a.append([0])
for i in range(1,n):
    if s[i-1:i+1]=="01":
        a.append([i])
    elif s[i-1:i+1]=="10":
        a[-1].append(i-1)
if s[n-1]=="1":
    a[-1].append(n-1)

if a[0][0]!="0":
    a.appendleft([0,0])
if a[-1][1]!="n-1":
    a.append([n-1,n-1])

if len(a)-1<=k:
    print(n)
else:
    t=0
    for i in range(len(a)-k):
        t=max(t,a[i+k][1]-a[i][0]+1)
    print(t)

Submission status: Runtime Error

Solution code:
#D
from collections import deque
n,k=map(int,input().split())
s=input()

a=deque([])
if s[0]=="1":
    a.append([0])
for i in range(1,n):
    if s[i-1:i+1]=="01":
        a.append([i])
    elif s[i-1:i+1]=="10":
        a[-1].append(i-1)
if s[n-1]=="1":
    a[-1].append(n-1)

if a[0][0]!="0":
    a.appendleft([0,0])
if a[-1][1]!="n-1":
    a.append([n-1,n-1])

if len(a)-1<=k:
    print(n)
else:
    t=[]
    for i in range(len(a)-1-k):
        t.append(a[i+k][1]-a[i][0])
    print(max(t)+1)

Submission status: Runtime Error

Solution code:
N, K = map(int,input().split(' '))
S = input()
S2=[]
key=S[0]
tmpcnt=1
if key=='0':
    S2.append(0)
for i in range(1,len(S)):
    if key!=S[i]:
        if key == '0':
            S2.append(-1*tmpcnt)
        else:
            S2.append(tmpcnt)
        key=S[i]
        tmpcnt=1
    else:
        tmpcnt+=1
if key == '0':
    S2.append(-1*tmpcnt)
    S2.append(0)
    S2.append(0)
else:
    S2.append(tmpcnt)
    S2.append(0)
    S2.append(0)
#print(S2)
max=0
tmp=S2[i]
maxj=len(S2)-1
for j in range(1,K*2+1):
    tmp+=abs(S2[min(0+j,maxj)])
    #print(S2[i],S2[min(i+j,maxj)])
if tmp >= max:
    max=tmp
    #print(tmp)

for i in range(2,len(S2)):
    tmp-=abs(S[i-1])
    tmp-=abs(S[i-2])
    tmp+=abs(S[min(i+j,maxj)])
    tmp+=abs(S[min(i+j+1,maxj)])
    if tmp>= max:
        max=tmp
print(max)

Submission status: Runtime Error

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

if(s[0] == '0'):
  arr.append(0)
for i in range(n-1):
  if(s[i] != s[i+1]):
    arr.append(i+1)
arr.append(n)
if(s[-1] == '0'):
  arr.append(n)
    
width = 2*k + 1
len_arr = len(arr)

ans = arr[2*k]
left = 2
while True:
  if(left + width > len_arr):
    break
  tmp = arr[left + width - 1] - arr[left - 1]
  ans = max(ans, tmp)
  left += 2

print(ans)