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:
n,k=map(int,input().split())
s=list(map(int,str(input())))
if s[0]==1:
  one_st=[0]+[i+1 for i in range(n-1) if s[i]==0 and s[i+1]==1]
  zero_st=[i+1 for i in range(n-1) if s[i]==1 and s[i+1]==0]
else:
  one_st=[i+1 for i in range(n-1) if s[i]==0 and s[i+1]==1]
  zero_st=[0]+[i+1 for i in range(n-1) if s[i]==1 and s[i+1]==0]
if s[n-1]==1:
  one_en=[i for i in range(n-1) if s[i]==1 and s[i+1]==0]+[n-1]
  zero_en=[i for i in range(n-1) if s[i]==0 and s[i+1]==1]
else:
  one_en=[i for i in range(n-1) if s[i]==1 and s[i+1]==0]
  zero_en=[i for i in range(n-1) if s[i]==0 and s[i+1]==1]+[n-1]
if len(one_st) <= k:
  x=n
else:
  y=[one_en[i+k]-one_st[i]+1 for i in range(len(zero_st)-k+1)]
  y=y+[one_en[k-1],one_st[len(one_st)-k-1]]
  x=max(y)
print(x)

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
s=list(map(int,str(input())))
if s[0]==1:
  one_st=[0]+[i+1 for i in range(n-1) if s[i]==0 and s[i+1]==1]
  zero_st=[i+1 for i in range(n-1) if s[i]==1 and s[i+1]==0]
else:
  one_st=[i+1 for i in range(n-1) if s[i]==0 and s[i+1]==1]
  zero_st=[0]+[i+1 for i in range(n-1) if s[i]==1 and s[i+1]==0]
if s[n-1]==1:
  one_en=[i for i in range(n-1) if s[i]==1 and s[i+1]==0]+[n-1]
  zero_en=[i for i in range(n-1) if s[i]==0 and s[i+1]==1]
else:
  one_en=[i for i in range(n-1) if s[i]==1 and s[i+1]==0]
  zero_en=[i for i in range(n-1) if s[i]==0 and s[i+1]==1]+[n-1]
if len(one_st) <= k:
  x=n
else:
  y=[one_en[i+k]-one_st[i] for i in range(len(one_st)-k+1)]
  y=y+[one_en[k-1],one_st[len(one_st)-k-1]]
  x=max(y)
print(x)

Submission status: Runtime Error

Solution code:
def solve(string):
    n, k, s = string.split()
    n, k = map(int, [n, k])
    s = "1" + s + "0"
    starts = [1]
    ends = []
    for i, (s0, s1) in enumerate(zip(s, s[1:])):
        if s0 == "0" and s1 == "1":
            starts.append(i + 1)
        elif s0 == "1" and s1 == "0":
            ends.append(i + 1)
    return str(max([e - s for s, e in zip(starts, ends[k:])])) if len(starts) > k else str(n)


if __name__ == '__main__':
    print(solve(input()))

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
s=list(map(int,str(input())))
if s[0]==1:
  a=[0]+[i+1 for i in range(n-1) if s[i]==0 and s[i+1]==1]
else:
  a=[i+1 for i in range(n-1) if s[i]==0 and s[i+1]==1]
if s[n-1]==1:
  b=[i for i in range(n-1) if s[i]==1 and s[i+1]==0]+[n-1]
else:
  b=[i for i in range(n-1) if s[i]==1 and s[i+1]==0]
if s[0]==0:
  c=[0]+[i+1 for i in range(n-1) if s[i]==1 and s[i+1]==0]
else:
  c=[i+1 for i in range(n-1) if s[i]==1 and s[i+1]==0]
if len(c) <= k:
  x=n
else:
    x=max([b[i+k]-a[i]+1 for i in range(len(c)-k+1)])
print(x)

Submission status: Runtime Error

Solution code:
n, k = (int(i) for i in input().split())
s = input()
s += "2"
l = [0]
cnt = 0
for i in range(n):
  cnt += 1
  if s[i] != s[i+1]:
    l.append(cnt)
    cnt = 0
s[-1] == "0":
  l += [0]
#print(l)
from itertools import accumulate as ac
l = list(ac(l))
if s[0] == "0":
  l = [0] + l
#print(l)
max=0
for i in range(2*k+1, len(l))[::2]:
  kukan = l[i] - l[i - (2*k+1)]
  if kukan > max:
    max = kukan
print(max)

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split()) # 横に2個
S = [int(i) for i in list(input())]

N0 = []
N1 = []
n0 = 0
n1 = 0
for s in S:
    if s == 0:
        if n1 > 0:
            N1.append(n1)
            n1 = 0
        n0 += 1
    if s == 1:
        if n0 > 0:
            N0.append(n0)
            n0 = 0
        n1 += 1
if n0 > 0:
    N0.append(n0)
if n1 > 0:
    N1.append(n1)

M0 = [0]
m0 = 0
for n0 in N0:
    m0 += n0
    M0.append(m0)

M1 = [0]
m1 = 0
for n1 in N1:
    m1 += n1
    M1.append(m1)
#print(M0)
#print(M1)

c = 0 if S[0] == 0 else 1

if len(N1) == 1 and len(N0) == 0:
    ans = N1[0]
elif len(N1) == 0 and len(N0) == 1:
    ans = N0[0]
else:
    ans = 0
    for i in range(N):
        if i + K >= len(M0):
            break
        if c == 0:
            if i == 0:
                l = M0[K] + M1[i+K]
            elif i == len(M1)-1:
                l = M0[i+K]-M0[i] + M1[i+K-1]-M1[i-1]
            else:
                l = M0[i+K]-M0[i] + M1[i+K]-M1[i-1]
        else:
            if i == len(M1)-2:
                l = M0[i+K]-M0[i] + M1[i+K]-M1[i]
            else:
                l = M0[i+K]-M0[i] + M1[i+K+1]-M1[i]
        #print(i, l)
        ans = max(ans, l)
print(ans)

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split()) # 横に2個
S = [int(i) for i in list(input())]

N0 = []
N1 = []
n0 = 0
n1 = 0
for s in S:
    if s == 0:
        if n1 > 0:
            N1.append(n1)
            n1 = 0
        n0 += 1
    if s == 1:
        if n0 > 0:
            N0.append(n0)
            n0 = 0
        n1 += 1
if n0 > 0:
    N0.append(n0)
if n1 > 0:
    N1.append(n1)

M0 = [0]
m0 = 0
for n0 in N0:
    m0 += n0
    M0.append(m0)

M1 = [0]
m1 = 0
for n1 in N1:
    m1 += n1
    M1.append(m1)
#print(M0)
#print(M1)

#d = 0 if len(M0)==len(M1) and c == 0


if len(N1) == 1 and len(N0) == 0:
    ans = N1[0]
elif len(N1) == 0 and len(N0) == 1:
    ans = N0[0]
else:
    ans = 0
    for i in range(N):
        if i+K+1 >= len(M1):
            a = M1[-1]-M1[-1-K]
            if i == 0:
                ans = a
        else:
            a = M1[i+K+1]-M1[i]
        if i + K >= len(M0):
            break
        l = M0[i+K]-M0[i] + a
        #print(l)
        ans = max(ans, l)
print(ans)

Submission status: Runtime Error

Solution code:
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 204,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "14 2\n",
      "11101010110011\n"
     ]
    }
   ],
   "source": [
    "N,K = map(int,input().split())\n",
    "S = (input())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 205,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = S.split('1')\n",
    "a = [len(i) for i in a if len(i)!=0]\n",
    "\n",
    "b = S.split('0')\n",
    "b = [len(i) for i in b if len(i)!=0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 206,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 1, 1, 2]"
      ]
     },
     "execution_count": 206,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 207,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[3, 1, 1, 2, 2]"
      ]
     },
     "execution_count": 207,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 208,
   "metadata": {},
   "outputs": [],
   "source": [
    "m = 0\n",
    "c = [0]\n",
    "for i in range(len(a)+len(b)):\n",
    "    if S[0] == '0':\n",
    "        if i%2==0:\n",
    "            m+=a[int(i/2)]\n",
    "        else:\n",
    "            m+=b[int(i/2)]\n",
    "        c.append(m)\n",
    "    else:\n",
    "        if i%2==0:\n",
    "            m+=b[int(i/2)]\n",
    "        else:\n",
    "            m+=a[int(i/2)]\n",
    "        c.append(m)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 209,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[0, 3, 4, 5, 6, 7, 8, 10, 12, 14]"
      ]
     },
     "execution_count": 209,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 211,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "8\n"
     ]
    }
   ],
   "source": [
    "mc=0\n",
    "try:\n",
    "    if S[0]=='0':\n",
    "        if 1+2*K-1 >len(c) -1:\n",
    "            mc = 1\n",
    "    else:\n",
    "        if 1+2*K > len(c) -1:\n",
    "            mc =1\n",
    "    for i in range(1,len(c)):\n",
    "        d=0\n",
    "        if S[0]=='0':\n",
    "            if i%2!=0:\n",
    "                d=c[i+2*K-1] - c[i-1]\n",
    "            else:\n",
    "                d=c[i+2*K] - c[i-1]\n",
    "        else:\n",
    "            if i%2==0:\n",
    "                d=c[i+2*K-1] - c[i-1]\n",
    "            else:\n",
    "                d=c[i+2*K] - c[i-1]\n",
    "        if mc < d:\n",
    "            mc = d\n",
    "except:        \n",
    "    print(mc)\n",
    "else:\n",
    "   \n",
    "    print(mc)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}

Submission status: Runtime Error

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

count_list = []
count = 0

if S[0] == '0':
    check = 'b'
    for i in range(N):
        if i != N-1:
            if check == 'a' and S[i] == '1':
                count += 1
            elif check == 'a'and S[i] == '0':
                count_list.append(count)
                count = 1
                check = 'b'
            elif check == 'b' and S[i] == '1':
                count_list.append(count)
                count = 1
                check = 'a'
            elif check == 'b' and S[i] == '0':
                count += 1
        elif i == N-1:
            if check == 'a' and S[i] == '1':
                count += 1
                count_list.append(count)
            elif check == 'a'and S[i] == '0':
                count_list.append(count)
                count = 1
                check = 'b'
                count_list.append(count)
            elif check == 'b' and S[i] == '1':
                count_list.append(count)
                count = 1
                check = 'a'
                count_list.append(count)
            elif check == 'b' and S[i] == '0':
                count += 1
                count_list.append(count)


elif S[0] == '1':
    check = 'a'
    for i in range(N):
        if i != N-1:
            if check == 'a' and S[i] == '1':
                count += 1
            elif check == 'a'and S[i] == '0':
                count_list.append(count)
                count = 1
                check = 'b'
            elif check == 'b' and S[i] == '1':
                count_list.append(count)
                count = 1
                check = 'a'
            elif check == 'b' and S[i] == '0':
                count += 1
        elif i == N-1:
            if check == 'a' and S[i] == '1':
                count += 1
                count_list.append(count)
            elif check == 'a'and S[i] == '0':
                count_list.append(count)
                count = 1
                check = 'b'
                count_list.append(count)
            elif check == 'b' and S[i] == '1':
                count_list.append(count)
                count = 1
                check = 'a'
                count_list.append(count)
            elif check == 'b' and S[i] == '0':
                count += 1
                count_list.append(count)

                    



if len(count_list) == 1:
    m = 1
elif S[0] == '1' and S[-1] == '1':
    m = (len(count_list) + 1) // 2 - K  

elif S[0] == '1' and S[-1] == '0':
    m =  (len(count_list)) // 2 - K +1
    
elif S[0] == '0' and S[-1] == '1':
    m =  (len(count_list)) // 2 - K +1
    
elif S[0] == '0' and S[-1] == '0':
    m =  (len(count_list) - 1) // 2 - K + 2



ans_list = [0] * m
if S[0] == '1' and S[-1] == '1':
    for i in range(m):
        j = 2*i
        ans_list[i] = sum(count_list[j:j+2*K+1])  


elif S[0] == '1' and S[-1] == '0':
    for i in range(m):
        j = 2*i
        if i != m-1:
            ans_list[i] = sum(count_list[j:j+2*K+1])  
        else:
            ans_list[i] = sum(count_list[j:j+2*K])
    
elif S[0] == '0' and S[-1] == '1':
    for i in range(m):
        j = 2*i
        if i != 0:
            ans_list[i] = sum(count_list[j-1:j+2*K])  
        else:
            ans_list[i] = sum(count_list[j:j+2*K])
    
elif S[0] == '0' and S[-1] == '0':
    for i in range(m):
        j = 2*i
        if i == 0:
            ans_list[i] = sum(count_list[j:j+2*K])  
        elif i == m-1:
            ans_list[i] = sum(count_list[j-1:j+2*K]) 
        else:
            ans_list[i] = sum(count_list[j:j+2*K+1])

print(max(ans_list))

Submission status: Runtime Error

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

count_list = [0]*N
count = 0

if S[0] == '0':
    check = 'b'
    for i in range(N):
        if i != N-1:
            if check == 'a' and S[i] == '1':
                count += 1
            elif check == 'a'and S[i] == '0':
                count_list[i]= count
                count = 1
                check = 'b'
            elif check == 'b' and S[i] == '1':
                count_list[i]= count
                count = 1
                check = 'a'
            elif check == 'b' and S[i] == '0':
                count += 1
        elif i == N-1:
            if check == 'a' and S[i] == '1':
                count += 1
                count_list[i]= count
            elif check == 'a'and S[i] == '0':
                count_list[i]= count
                count = 1
                check = 'b'
                count_list.append(count)
            elif check == 'b' and S[i] == '1':
                count_list[i]= count
                count = 1
                check = 'a'
                count_list[i]= count
            elif check == 'b' and S[i] == '0':
                count += 1
                count_list[i]= count



elif S[0] == '1':
    check = 'a'
    for i in range(N):
        if i != N-1:
            if check == 'a' and S[i] == '1':
                count += 1
            elif check == 'a'and S[i] == '0':
                count_list.append(count)
                count = 1
                check = 'b'
            elif check == 'b' and S[i] == '1':
                count_list.append(count)
                count = 1
                check = 'a'
            elif check == 'b' and S[i] == '0':
                count += 1
        elif i == N-1:
            if check == 'a' and S[i] == '1':
                count += 1
                count_list.append(count)
            elif check == 'a'and S[i] == '0':
                count_list.append(count)
                count = 1
                check = 'b'
                count_list.append(count)
            elif check == 'b' and S[i] == '1':
                count_list.append(count)
                count = 1
                check = 'a'
                count_list.append(count)
            elif check == 'b' and S[i] == '0':
                count += 1
                count_list.append(count)

                    


for i in range(count_list.count(0)):
    count_list.remove(0)

if len(S) == 1:
    m = 1
elif S[0] == '1' and S[-1] == '1':
    m = (len(count_list) + 1) // 2 - K  

elif S[0] == '1' and S[-1] == '0':
    m =  (len(count_list)) // 2 - K +1
    
elif S[0] == '0' and S[-1] == '1':
    m =  (len(count_list)) // 2 - K +1
    
elif S[0] == '0' and S[-1] == '0':
    m =  (len(count_list) - 1) // 2 - K + 2



ans_list = [0] * m
if S[0] == '1' and S[-1] == '1':
    for i in range(m):
        j = 2*i
        ans_list[i] = sum(count_list[j:j+2*K+1])  


elif S[0] == '1' and S[-1] == '0':
    for i in range(m):
        j = 2*i
        if i != m-1:
            ans_list[i] = sum(count_list[j:j+2*K+1])  
        else:
            ans_list[i] = sum(count_list[j:j+2*K])
    
elif S[0] == '0' and S[-1] == '1':
    for i in range(m):
        j = 2*i
        if i != 0:
            ans_list[i] = sum(count_list[j-1:j+2*K])  
        else:
            ans_list[i] = sum(count_list[j:j+2*K])
    
elif S[0] == '0' and S[-1] == '0':
    for i in range(m):
        j = 2*i
        if i == 0:
            ans_list[i] = sum(count_list[j:j+2*K])  
        elif i == m-1:
            ans_list[i] = sum(count_list[j-1:j+2*K]) 
        else:
            ans_list[i] = sum(count_list[j:j+2*K+1])

print(max(ans_list))

Submission status: Runtime Error

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

count_list = []
count = 0

if S[0] == '0':
    check = 'b'
    for i in range(N):
        if i != N-1:
            if check == 'a' and S[i] == '1':
                count += 1
            elif check == 'a'and S[i] == '0':
                count_list.append(count)
                count = 1
                check = 'b'
            elif check == 'b' and S[i] == '1':
                count_list.append(count)
                count = 1
                check = 'a'
            elif check == 'b' and S[i] == '0':
                count += 1
        elif i == N-1:
            if check == 'a' and S[i] == '1':
                count += 1
                count_list.append(count)
            elif check == 'a'and S[i] == '0':
                count_list.append(count)
                count = 1
                check = 'b'
                count_list.append(count)
            elif check == 'b' and S[i] == '1':
                count_list.append(count)
                count = 1
                check = 'a'
                count_list.append(count)
            elif check == 'b' and S[i] == '0':
                count += 1
                count_list.append(count)


elif S[0] == '1':
    check = 'a'
    for i in range(N):
        if i != N-1:
            if check == 'a' and S[i] == '1':
                count += 1
            elif check == 'a'and S[i] == '0':
                count_list.append(count)
                count = 1
                check = 'b'
            elif check == 'b' and S[i] == '1':
                count_list.append(count)
                count = 1
                check = 'a'
            elif check == 'b' and S[i] == '0':
                count += 1
        elif i == N-1:
            if check == 'a' and S[i] == '1':
                count += 1
                count_list.append(count)
            elif check == 'a'and S[i] == '0':
                count_list.append(count)
                count = 1
                check = 'b'
                count_list.append(count)
            elif check == 'b' and S[i] == '1':
                count_list.append(count)
                count = 1
                check = 'a'
                count_list.append(count)
            elif check == 'b' and S[i] == '0':
                count += 1
                count_list.append(count)

                    



if len(S) == 1:
    m = 1
elif S[0] == '1' and S[-1] == '1':
    m = (len(count_list) + 1) // 2 - K  

elif S[0] == '1' and S[-1] == '0':
    m =  (len(count_list)) // 2 - K +1
    
elif S[0] == '0' and S[-1] == '1':
    m =  (len(count_list)) // 2 - K +1
    
elif S[0] == '0' and S[-1] == '0':
    m =  (len(count_list) - 1) // 2 - K + 2



ans_list = [0] * m
if S[0] == '1' and S[-1] == '1':
    for i in range(m):
        j = 2*i
        ans_list[i] = sum(count_list[j:j+2*K+1])  


elif S[0] == '1' and S[-1] == '0':
    for i in range(m):
        j = 2*i
        if i != m-1:
            ans_list[i] = sum(count_list[j:j+2*K+1])  
        else:
            ans_list[i] = sum(count_list[j:j+2*K])
    
elif S[0] == '0' and S[-1] == '1':
    for i in range(m):
        j = 2*i
        if i != 0:
            ans_list[i] = sum(count_list[j-1:j+2*K])  
        else:
            ans_list[i] = sum(count_list[j:j+2*K])
    
elif S[0] == '0' and S[-1] == '0':
    for i in range(m):
        j = 2*i
        if i == 0:
            ans_list[i] = sum(count_list[j:j+2*K])  
        elif i == m-1:
            ans_list[i] = sum(count_list[j-1:j+2*K]) 
        else:
            ans_list[i] = sum(count_list[j:j+2*K+1])

print(max(ans_list))

Submission status: Runtime Error

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

before = S[0]
cnt = 0
same_nums = []
for i in range(N):
  if S[i] == before:
    cnt += 1
  else:
    same_nums.append(cnt)
    before = S[i]
    cnt = 1
same_nums.append(cnt)
if S[0] == '0':
  sum_temp = sum(same_nums[:2*K+1])
  ans = sum_temp
  for i in range(0, K+1, 2):
    sum_temp = sum_temp - sum(same_nums[i:i+2]) + sum(same_nums[i+2*K+1:i+2*K+3])
    ans = max(ans, sum_temp)
  print(ans)
else:
  sum_temp = sum(same_nums[:2*K])
  ans = sum_temp
  sum_temp = sum_temp - same_nums[1] + sum(same_nums[2*K:2*K+2])
  ans = max(ans, sum_temp)
  for i in range(3, K+1, 2):
    sum_temp = sum_temp - sum(same_nums[i:i+2]) + sum(same_nums[i+2*K:i+2*K+2])
    ans = max(ans, sum_temp)
  print(ans)

Submission status: Runtime Error

Solution code:
from collections import deque
N,K=map(int,input().split())
S=input()
z,o=deque(),deque()
memo=S[0]
cnt=1
if len(S)==1:
    print(1)
    exit()
if memo=="0":o.append(0)
for i in range(1,N):
    s=S[i]
    if s==memo:
        cnt+=1
        memo=s
    else:
        if memo=="0":z.append(cnt)
        else:o.append(cnt)
        memo=s
        cnt=1
    if i==N-1:
        if memo=="0":
            z.append(cnt)
            o.append(0)
        else:o.append(cnt)
ostack=deque()
zstack=deque()
memo=0
if len(z)<=K:print(len(S))
else:
    for _ in range(K):
        zz=z.popleft()
        memo+=zz
        zstack.append(zz)
    for _ in range(K+1):
        oo=o.popleft()
        memo+=oo
        ostack.append(oo)
    ans=memo
    for _ in range(len(z)):
        memo-=zstack.popleft()+ostack.popleft()
        zz=z.popleft()
        oo=o.popleft()
        memo+=zz+oo
        ans=max(ans,memo)
print(ans)

Submission status: Runtime Error

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

before = S[0]
zeros = []
ones = []
zero_cnt = 0
one_cnt = 0
for char in S:
  if char == before:
    if char == '0':
      zero_cnt += 1
    else:
      one_cnt += 1
    before = char
  else:
    if char == '0':
      ones.append(one_cnt)
      zero_cnt = 1
    else:
      zeros.append(zero_cnt)
      one_cnt = 1
    before = char
if S[-1] == '0':
  zeros.append(zero_cnt)
elif S[-1] == '1':
  ones.append(one_cnt)
if len(zeros) <= K:
  print(N)
else:
  if S[1] == '0':
    sum_temp = sum(zeros[:K]) + sum(ones[:K])
    sum_o = sum_temp
    for i in range(len(zeros)-K+1):
      sum_temp = sum_temp - zeros[i] + zeros[i+K] - ones[i] + ones[i+K]
      sum_o = max(sum_o, sum_temp)
    print(sum_o)
  else:
    sum_temp = sum(zeros[:K]) + sum(ones[:K+1])
    sum_o = sum_temp
    for i in range(len(zeros)-K+1):
      sum_temp = sum_temp - zeros[i] + zeros[i+K] - ones[i] + ones[i+K+1]
      sum_o = max(sum_o, sum_temp)
    print(sum_o)

Submission status: Runtime Error

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

n = 1
c = []
if S[0]=='0': c.append(0)
for i in range(1,N):
    if S[i]!=S[i-1]:
    c.append(n)
        n=1
    else:
        n+=1
c.append(n)
if S[-1]=='0': c.append(0)

t = sum(c[:2*K+1])
m = t
for i in range(2*K+1, len(c), 2):
    t += c[i] + c[i+1] - c[i-(2*K+1)] - c[i+1-(2*K+1)]
    m = max(m,t)

print(m)

Submission status: Runtime Error

Solution code:
# coding:utf-8
import sys
from collections import deque
sys.setrecursionlimit(2**31-1)
input = sys.stdin.readline
write = sys.stdout.write
LMIIS = lambda : list(map(int,input().split()))
II = lambda : int(input())
dbg = lambda *something : print(*something) if DEBUG is True else 0
DEBUG = False

def main():
    N,K = LMIIS()
    S = input()

    leftc1 = 0
    rightc1 = 0
    tmpnum0 = 0
    cnum1s = []


    max_left = 0
    max_0 = 0
    max_right = 0
    max_c = 0
    max_c_idx = 0
    for i in range(N):

        if S[i] == '1':

            rightc1 += 1


        else:
            if rightc1 != 0:
                cnum1s.append([leftc1,tmpnum0,rightc1])

                if max_c < leftc1+tmpnum0+rightc1:
                    max_c = leftc1+tmpnum0+rightc1
                    max_c_idx = len(cnum1s) - 1
                    max_right = rightc1
                    max_0 = tmpnum0
                    max_left = leftc1

                leftc1 = rightc1
                rightc1 = 0
                tmpnum0 = 1
            else:
                tmpnum0 += 1
    cnum1s.append([leftc1 , tmpnum0 , rightc1])
    if max_c < leftc1 + tmpnum0 + rightc1:
        max_c = leftc1 + tmpnum0 + rightc1
        max_c_idx = len(cnum1s) - 1
        max_right = rightc1
        max_0 = tmpnum0
        max_left = leftc1
    if S[0] == '1':
        cnum1s.pop(0)
        max_c_idx -= 1

    ans = 0

    count = 0
    while count < K and len(cnum1s) != 0:
        dbg(cnum1s)
        count += 1
        sumleft = 0
        sumright = 0

        dbg(cnum1s, max_c_idx,max_left,max_0,max_right)
        if max_c_idx >= 1:

            cnum1s[max_c_idx - 1][2] += max_0 + max_right
            sumleft = sum(cnum1s[max_c_idx - 1])
        if max_c_idx < len(cnum1s) - 1:
            cnum1s[max_c_idx + 1][0] += max_0 + max_left
            sumright = sum(cnum1s[max_c_idx + 1])




        if sumleft > sumright:

            max_c = sumleft
            max_left = cnum1s[max_c_idx - 1][0]
            max_0 = cnum1s[max_c_idx - 1][1]
            max_right = cnum1s[max_c_idx - 1][2]
            cnum1s.pop(max_c_idx)
            max_c_idx -= 1
        elif sumright > sumleft:
            max_c = sumright
            max_left = cnum1s[max_c_idx +1][0]
            max_0 = cnum1s[max_c_idx + 1][1]
            max_right = cnum1s[max_c_idx + 1][2]
            cnum1s.pop(max_c_idx)
        else:
            break



    dbg(cnum1s)



    # for i in range(K):
    #     dbg(cnum1s,ans)
    #     max_left = 0
    #     max_0 = 0
    #     max_right = 0
    #     max_c = 0
    #     max_c_idx = 0
    #     if len(cnum1s) <= 1:
    #         break
    #     for j,(leftc1,tmpnum0,rightc1) in enumerate(cnum1s):
    #         if max_c < leftc1+tmpnum0+rightc1:
    #             max_c = leftc1+tmpnum0+rightc1
    #             max_left = leftc1
    #             max_0 = tmpnum0
    #             max_right = rightc1
    #             max_c_idx = j
    #
    #     if max_c_idx >= 1:
    #
    #         cnum1s[max_c_idx - 1][2] += max_0 + max_right
    #     if max_c_idx < len(cnum1s) -1:
    #         cnum1s[max_c_idx + 1][0] += max_0 + max_left
    #     cnum1s.pop(max_c_idx)
    #     ans += max_c

    print(max(cnum1s[max_c_idx][0],cnum1s[max_c_idx][2]))





    pass
if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
s=list(input())
l,tmpl,cnt0,tmp,flag=[],0,0,s[0],0
if tmp=="0":cnt0+=1;flag=1
for i in s:
    if i==tmp:tmpl+=1
    else:
        if i=="0":cnt0+=1
        tmp=i
        l.append(tmpl)
        tmpl=1
l.append(tmpl)
if cnt0<=k:print(n);exit()
ans=max(sum(l[:2*k]),sum(l[::-1][:2*k]))
suml,tmp=[0],0
if flag==1:l=l[1:]
for i in l:
    tmp+=i
    suml.append(tmp)
for i in range((len(suml)-2*k-1)//2+1):
    tmp=suml[2*k+2*i+1]-suml[2*i]
    if tmp>ans:ans=tmp
print(ans)

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
s=list(input())
l,tmpl,cnt0,tmp,flag=[],0,0,s[0],0
if tmp=="0":cnt0+=1;flag=1
for i in s:
    if i==tmp:tmpl+=1
    else:
        if i=="0":cnt0+=1
        tmp=i
        l.append(tmpl)
        tmpl=1
l.append(tmpl)
if cnt0<=k:print(n);exit()
ans=max(sum(l[:2*k]),sum(l[::-1][:2*k]))
suml,tmp=[0],0
if flag==1:l=l[1:]
for i in l:
    tmp+=i
    suml.append(tmp)
for i in range((len(suml)-2*k-1)//2+1):
    tmp=suml[2*k+2*i+1]-suml[2*i]
    print(tmp)
    if tmp>ans:ans=tmp
print(ans)

Submission status: Runtime Error

Solution code:
N,K = map(int,input().split())
S_list = ['0'] + list(input()) + ['-1']

s_count_list =[]

cnt = 1
for i in range(1,N+2) :
    if S_list[i] == S_list[i-1] :
        cnt +=1
    else:
        s_count_list.append(cnt)
        cnt = 1
if S_list[1] == '0':
    s_count_list[0] = s_count_list[0] -1
else:
    s_count_list.pop(0)

ruisekiwa = [0]
for i,s in enumerate(s_count_list):
    ruisekiwa.append(s_count_list[i] + s)

for i in range(0,len(ruisekiwa),2):
    left = i
    right = min((i + (2 * K) + 1),len(ruisekiwa))
    tmp = ruisekiwa[right] - ruisekiwa[left]
    answer = max(tmp,answer)

print(answer)

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-


def main():
    n, k = map(int, input().split())
    s = str(input())

    l = list()
    now = ""
    for i in s:
        if now == "":
            now += i
            continue

        if now[-1] == i:
            now += i
        else:
            l.append(now)
            now = i
    l.append(now)

    a = list()
    for index in l:
        if int(index[0]) == 0:
            a.append(-len(index))
        else:
            a.append(len(index))

    c = list()
    index = 0
    for b in a:
        if b < 0:
            c.append(index)
        index += 1

    if len(c) <= k:
        print(n)
        return

    ans = list()
    index = 0
    for i in range(len(c) - 1):  # 1, 2, 3, 4
        tmp = a.copy()
        for j in range(k):  # 1, 2
            tmp[c[j + index]] = tmp[c[j + index]] * (-1)
        index += 1
        ans.append(tmp.copy())
        tmp.clear()

    # print(ans)

    x = list()
    sum = 0
    for a in ans:
        for i in a:
            if i > 0:
                sum += i
            else:
                x.append(sum)
                sum = 0
    x.append(sum)

    print(max(x))


if __name__ == '__main__':
    main()