Problem:


Score : 400 points

Problem Statement

N people are arranged in a row from left to right.

You are given a string S of length N consisting of 0 and 1, and a positive integer K.

The i-th person from the left is standing on feet if the i-th character of S is 0, and standing on hands if that character is 1.

You will give the following direction at most K times (possibly zero):

Direction: Choose integers l and r satisfying 1 \leq l \leq r \leq N, and flip the l-th, (l+1)-th, ..., and r-th persons. That is, for each i = l, l+1, ..., r, the i-th person from the left now stands on hands if he/she was standing on feet, and stands on feet if he/she was standing on hands.

Find the maximum possible number of consecutive people standing on hands after at most K directions.

Constraints

N is an integer satisfying 1 \leq N \leq 10^5.

K is an integer satisfying 1 \leq K \leq 10^5.

The length of the string S is N.

Each character of the string S is 0 or 1.

Input

Input is given from Standard Input in the following format:

N K
S

Output

Print the maximum possible number of consecutive people standing on hands after at most K directions.

Sample Input 1

5 1
00010

Sample Output 1

4

We can have four consecutive people standing on hands, which is the maximum result, by giving the following direction:

Give the direction with l = 1, r = 3, which flips the first, second and third persons from the left.

Sample Input 2

14 2
11101010110011

Sample Output 2

8

Sample Input 3

1 1
1

Sample Output 3

1

No directions are necessary.



Sample input:
5 1
00010


Sample output:
4

Submission status: Wrong Answer

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

import sys

input = sys.stdin.readline

def main():
    N, K = map(int, input().split())
    S = list(input().strip())

    left = 0
    right = 0
    k = 0
    max_n = 0
    pos = []
    while left < N:
        while right+2 < N and not (k > K and S[right] == "0"):
            right += 1
            if S[right] == "0" and S[right+1] == "1":
                k += 1
                pos.append(right+1)
        max_n = max(max_n, right - left + 1)
        if len(pos) == 0:
            break
        left = pos[0]
        pos = pos[1:]
        k -= 1
    print(max_n)

main()

Submission status: Wrong Answer

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

tmp = 0
cs = []
s0 = ss[0]
if s0=="0":
  cs.append(0)

for s in ss:
  if s0 != s:
    if s0 == "1":
      cs.append(tmp)
      s0="0"
    else:
      s0="1"
      cs.append(-tmp)
    tmp = 1
  else:
    tmp += 1
cs.append(tmp)

if ss[-1]=="0":
  cs.append(0)

if len(cs[1::2]) < k:
  print(n)
else:
  print(max([sum(cs[0::2][i:i+k+1]) - sum(cs[1::2][i:i+k]) for i in range(len(cs[1::2])-k+1)]))

Submission status: Wrong Answer

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

tmp = 0
cs = []
s0 = ss[0]
if s0=="0":
  cs.append(0)

for s in ss:
  if s0 != s:
    if s0 == "1":
      cs.append(tmp)
      s0="0"
    else:
      s0="1"
      cs.append(-tmp)
    tmp = 1
  else:
    tmp += 1
cs.append(tmp)

if ss[-1]=="0":
  cs.append(0)

if len(cs[1::2]) <= k:
  print(n)
else:
  print(max([sum(cs[0::2][i:i+k+1]) - sum(cs[1::2][i:i+k]) for i in range(len(cs[1::2])-k+1)]))

Submission status: Wrong Answer

Solution code:
from sys import stdin

N, K = [int(x) for x in stdin.readline().rstrip().split()]
S = stdin.readline().rstrip()
cnt = 1
run = []
if len(S) == 1:
    run.append(1)
else:
    for i in range(1, len(S)):
        if S[i - 1] == S[i]:
            cnt += 1
        else:
            run.append(cnt)
            cnt = 1
        if i == len(S) - 1:
            run.append(cnt)
if S[0] == '0':
    run = [0] + run
if S[N - 1] == '0':
    run = run + [0]
if len(run) < 2 * K + 1:
    exit()
ans = sum(run[0: 2 * K + 1])
for j in range(2, len(run) - 2 * K):
    if j % 2 == 1:
        continue
    else:
        ans = max(ans, sum(run[j: j + 2 * K + 1]))
        print(ans)
print(ans)

Submission status: Wrong Answer

Solution code:
from sys import stdin

N, K = [int(x) for x in stdin.readline().rstrip().split()]
S = stdin.readline().rstrip()
cnt = 1
run = []
if len(S) == 1:
    run.append(1)
else:
    for i in range(1, len(S)):
        if S[i - 1] == S[i]:
            cnt += 1
        else:
            run.append(cnt)
            cnt = 1
        if i == len(S) - 1:
            run.append(cnt)
if S[0] == '0':
    run = [0] + run
if S[N - 1] == '0':
    run = run + [0]
if len(run) < 2 * K + 1:
    print(sum(run))
    exit()
ans = sum(run[0: 2 * K + 1])
for j in range(2, len(run) - 2 * K):
    if j % 2 == 1:
        continue
    else:
        ans = max(ans, ans - run[j - 1] - run[j - 2] + run[j + 2 * K] + run[j + 2 * K - 1])
print(ans)

Submission status: Wrong Answer

Solution code:
from sys import stdin

N, K = [int(x) for x in stdin.readline().rstrip().split()]
S = stdin.readline().rstrip()
cnt = 1
run = []
for i in range(1, len(S)):
    if S[i - 1] == S[i]:
        cnt += 1
    else:
        run.append(cnt)
        cnt = 1
    if i == len(S) - 1:
        run.append(cnt)
X = [sum(run[j: j + 2 * K + 1]) for j in range(max(len(run) - 2 * K, 1))]
if len(X) == 0:
    print(1)
else:
    print(max(X))

Submission status: Wrong Answer

Solution code:
def conv(a):
  if a=="0":
    return "1"
  else:
    return "0"

n, k=list(map(int, input().split()))
s=input()
arr=[0]

if s=="1":
  print("1")
  exit(0)

flag=s[0]; c=0;
for x in s:
  if flag==x:
    arr[c] += 1
  else:
    c+=1
    flag = conv(flag)
    arr.append(0)
    arr[c] += 1
m=0
for i in range(len(arr)-k*2):
  if (s[0]=="0" and i%2==1) or (s[0]=="1" and i%2==0):
    z=sum(arr[i:k*2+1+i])
    print(arr[i:k*2+1+i])
    if z>m:
      m=z
#print(m)
if s[0]=="0" and sum(arr[0:k*2])>m:
  m=sum(arr[0:k*2])

if s[-1]=="0" and sum(arr[-1:-1-k*2])>m:
  m=sum(arr[-1:-1-k*2])

print(m)

Submission status: Wrong Answer

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

count = 1
L = []
for i in range(N):
    if i < N-1:
        if S[i] == S[i+1]:
            count += 1
        else:
            L.append(count)
            count = 1
    else:
        L.append(count)
        count = 1

if S[0] == '1':
    ans = 0
    tmp = 0
    dist = 2 * (K + 1)
    for i in range(len(L)):
        tmp += L[i]
        if i % 2 == 0:
            if i - dist >= 0:
                tmp -= L[i-dist]
                tmp -= L[i-dist+1]
            ans = max(ans, tmp)
        else:
            if i == N - 1:
                ans = max(ans, tmp)
    print(ans)

if S[0] == '0':
    ans = 0
    tmp = 0
    dist = 2 * (K + 1)
    for i in range(len(L)):
        tmp += L[i]
        if i % 2 == 1:
            if i - dist + 1 >= 0:
                tmp -= L[i-dist+1]
            if i - dist >= 0:
                tmp -= L[i-dist]
            ans = max(ans, tmp)
        else:
            if i == N - 1:
                ans = max(ans, tmp)
    print(ans)

Submission status: Wrong Answer

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

count = 1
L = []
for i in range(N):
    if i < N-1:
        if S[i] == S[i+1]:
            count += 1
        else:
            L.append(count)
            count = 1
    else:
        L.append(count)
        count = 1

if S[0] == '1':
    ans = 0
    tmp = 0
    dist = 2 * (K + 1)
    for i in range(len(L)):
        tmp += L[i]
        if i % 2 == 0:
            if i - dist >= 0:
                tmp -= L[i-dist]
                tmp -= L[i-dist+1]
            ans = max(ans, tmp)
    print(ans)

if S[0] == '0':
    ans = 0
    tmp = 0
    dist = 2 * (K + 1)
    for i in range(len(L)):
        tmp += L[i]
        if i % 2 == 1:
            if i - dist + 1 >= 0:
                tmp -= L[i-dist+1]
            if i - dist >= 0:
                tmp -= L[i-dist]
            ans = max(ans, tmp)
        else:
            if i == N - 1:
                ans = max(ans, tmp)
    print(ans)

Submission status: Wrong Answer

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

count = 1
L = []
for i in range(N):
    if i < N-1:
        if S[i] == S[i+1]:
            count += 1
        else:
            L.append(count)
            count = 1
    else:
        L.append(count)
        count = 1

if S[0] == '1':
    ans = 0
    tmp = 0
    dist = 2 * (K + 1)
    for i in range(len(L)):
        tmp += L[i]
        if i % 2 == 0:
            if i - dist >= 0:
                tmp -= L[i-dist]
                tmp -= L[i-dist+1]
            print(tmp)
            ans = max(ans, tmp)
    print(ans)

if S[0] == '0':
    ans = 0
    tmp = 0
    dist = 2 * (K + 1)
    for i in range(len(L)):
        tmp += L[i]
        if i % 2 == 1:
            if i - dist + 1 >= 0:
                tmp -= L[i-dist+1]
            if i - dist >= 0:
                tmp -= L[i-dist]
            ans = max(ans, tmp)
        else:
            if i == N - 1:
                ans = max(ans, tmp)
    print(ans)

Submission status: Wrong Answer

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

count = 1
L = []
for i in range(N):
    if i < N-1:
        if S[i] == S[i+1]:
            count += 1
        else:
            L.append(count)
            count = 1
    else:
        L.append(count)
        count = 1

if S[0] == '1':
    ans = 0
    tmp = 0
    dist = 2 * (K + 1)
    for i in range(len(L)):
        tmp += L[i]
        if i % 2 == 0:
            if i - dist >= 0:
                tmp -= L[i-dist]
                tmp -= L[i-dist+1]
        ans = max(ans, tmp)
    print(ans)

if S[0] == '0':
    ans = 0
    tmp = 0
    dist = 2 * (K + 1)
    for i in range(len(L)):
        tmp += L[i]
        if i % 2 == 1:
            if i - dist + 1 >= 0:
                tmp -= L[i-dist+1]
            if i - dist >= 0:
                tmp -= L[i-dist]
        ans = max(ans, tmp)
    print(ans)

Submission status: Wrong Answer

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

count = 1
L = []
for i in range(N):
    if i < N-1:
        if S[i] == S[i+1]:
            count += 1
        else:
            L.append(count)
            count = 1
    else:
        L.append(count)
        count = 1

if S[0] == '1':
    ans = 0
    tmp = 0
    dist = 2 * (K + 1)
    for i in range(len(L)):
        tmp += L[i]
        if i % 2 == 0:
            if i - dist >= 0:
                tmp -= L[i-dist]
                tmp -= L[i-dist+1]
            ans = max(ans, tmp)
    print(ans)

if S[0] == '0':
    ans = 0
    tmp = 0
    dist = 2 * (K + 1)
    for i in range(len(L)):
        tmp += L[i]
        if i % 2 == 1:
            if i - dist + 1 >= 0:
                tmp -= L[i-dist+1]
            if i - dist >= 0:
                tmp -= L[i-dist]
            ans = max(ans, tmp)
    print(ans)

Submission status: Wrong Answer

Solution code:
def collect(inlist):
	if inlist[0] >= 0:
		return sum([abs(i) for i in inlist])
	else:
		return sum([abs(i) for i in inlist[1:k-1]]) + max(abs(inlist[0]),abs(inlist[k-1]))


n, k = [ int(v) for v in input().split() ]
s = list(map(int,list(input())))
binary_list = [0]
b = 0
for i in range(n):
	if s[i] == 1:
		b += 1
	else:
		b -= 1
	if i < n-1 and s[i] != s[i+1]:
		binary_list.append(b)
		b = 0
binary_list.append(b)
binary_list.append(0)

k = 2 * k + 1
ans = 0
for i in range(len(binary_list)-k+1):
	sumpart = collect(binary_list[i:i+k])
	ans = max(ans,sumpart)

print(ans)

Submission status: Wrong Answer

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

import sys

input = sys.stdin.readline

def main():
    N, K = map(int, input().split())
    S = list(input().strip())

    res = 0
    prev_zero = False
    i = 0
    k = 0
    resume = 0
    next_i = -1
    for j in range(N):
        # print(i, j)
        if S[j] == "1":
            if k == K and prev_zero:
                prev_zero = False
                i = next_i
                # print(j, i, j-i-1, next_i)
                res = max(res, j - i - 1)
                k = 1
            elif k == 1 and prev_zero:
                next_i = j
                prev_zero = False
            prev_zero = False
        elif not prev_zero:
            prev_zero = True
            k += 1
    if k != 0:
        res = max(res, N-i-2)
    else:
        res = N

    print(res)

main()

Submission status: Wrong Answer

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

import sys

input = sys.stdin.readline

def main():
    N, K = map(int, input().split())
    S = list(input().strip())

    res = 0
    cnt = 0
    prev_zero = False
    i = 0
    k = 0
    for i in range(N):
        if S[i] == "1":
            prev_zero = False
        elif not prev_zero:
            prev_zero = True
            k += 1
            if k == K+1:
                res = max(res, cnt)
                k = 0
                cnt = 1
        cnt += 1
    res = max(res, cnt)

    print(res)

main()

Submission status: Wrong Answer

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

count = 1
L = []
for i in range(N):
    if i != N-1:
        if S[i] != S[i+1]:
            L.append(count)
            count = 1
        else:
            count += 1
    else:
        L.append(count)
        count += 1
# print(L)

if S[0] == '1':
    tmp = 0
    ans = 0
    width = K * 2
    for i in range(len(L)):
        tmp += L[i]
        if i % 2 == 0:
            ans = max(ans, tmp)
            if i >= width:
                tmp = tmp - L[i-(width-1)] - L[i-width]
    print(ans)

if S[0] == '0':
    tmp = 0
    ans = 0
    width = K * 2
    for i in range(len(L)):
        tmp += L[i]
        if i % 2 == 1:
            if i >= width:
                tmp = tmp - L[i-(width-1)] - L[i-width]
            print(tmp)
            ans = max(ans, tmp)
    print(ans)

Submission status: Wrong Answer

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

count = 1
L = []
for i in range(N):
    if i != N-1:
        if S[i] != S[i+1]:
            L.append(count)
            count = 1
        else:
            count += 1
    else:
        L.append(count)
        count += 1
# print(L)

if S[0] == '1':
    tmp = 0
    ans = 0
    width = K * 2
    for i in range(len(L)):
        tmp += L[i]
        if i % 2 == 0:
            ans = max(ans, tmp)
            if i >= width:
                tmp = tmp - L[i-(width-1)] - L[i-width]
    print(ans)

if S[0] == '0':
    tmp = 0
    ans = 0
    width = K * 2
    for i in range(len(L)):
        tmp += L[i]
        if i % 2 == 1:
            ans = max(ans, tmp)
            if i >= width:
                tmp = tmp - L[i-(width-1)] - L[i-width]
    print(ans)

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
S=input()+"2"
_list=[]
if S[0]=="1":
    _list=[]
else:
    _list=[0]
cnt=1
for i in range(N):
    if S[i]==S[i+1]:
        cnt+=1
    else:
        _list.append(cnt)
        cnt=1
if S[-2]=="0":
    _list.append(0)
import itertools
__list=[0]+list(itertools.accumulate(_list))
C=len(__list)
K=min(C,K)
if (C+1)//2<=K:
    print(_list[0])
else:
    print(max([__list[2*i+2*K+1]-__list[2*i] for i in range((C+1)//2-K)]))

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
S=input()+"2"
_list=[]
if S[0]=="1":
    _list=[]
else:
    _list=[0]
cnt=1
for i in range(N):
    if S[i]==S[i+1]:
        cnt+=1
    else:
        _list.append(cnt)
        cnt=1
if S[-2]=="0":
    _list.append(0)
import itertools
__list=[0]+list(itertools.accumulate(_list))
C=len(__list)
K=min(C,K)
if C//2<=K:
    print(_list[0])
else:
    print(max([__list[2*i+2*K+1]-__list[2*i] for i in range(C//2-K)]))

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
S=input()
A=[]
a=S[0]
count=1
for i in range(1,N):
  if S[i]==a:
    count+=1
  else:
    A.append([a,count])
    count=1
    a=S[i]
A.append([a,count])
B=[]
C=[]
for i in range(len(A)):
  if A[i][0]=='0':
    B.append(A[i][1])
  else:
    C.append(A[i][1])
if K>=len(B):
  print(sum(B)+sum(C))
else:
  D=[]
  if S[0]=='0':
    D.append(sum(B[:K])+sum(C[:K]))
    if len(B)==len(C):
      for i in range(1,len(B)-K+1):
        D.append(sum(B[i:K+i])+sum(C[i-1:K+i]))
        print(max(D))
    else:
      for i in range(1,len(B)-K):
        D.append(sum(B[i:K+i])+sum(C[i-1:K+i]))
      D.append(sum(B[len(B)-K:len(B)+1])+sum(C[len(B)-K-1:len(B)]))
      print(max(D))
  else:
    if len(C)>len(B):
      for i in range(len(B)-K+1):
        D.append(sum(B[i:K+i])+sum(C[i:K+i+1]))
      print(max(D))
    else:
      for i in range(len(B)-K):
        D.append(sum(B[i:K+i])+sum(C[i:K+i+1]))
      D.append(sum(B[len(B)-K:len(B)+1])+sum(C[len(B)-K:len(B)+1]))
      print(max(D))

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
S=input()
A=[]
a=S[0]
count=1
for i in range(1,N):
  if S[i]==a:
    count+=1
  else:
    A.append([a,count])
    count=1
    a=S[i]
A.append([a,count])
B=[]
C=[]
for i in range(len(A)):
  if A[i][0]=='0':
    B.append(A[i][1])
  else:
    C.append(A[i][1])
if K>=len(B):
  print(sum(B)+sum(C))
else:
  D=[]
  if S[0]=='0':
    for i in range(len(B)-K+1):
      D.append(sum(B[i:K+i])+sum(C[i:K+i]))
    print(max(D))
  else:
    for i in range(len(B)-K+1):
      D.append(sum(B[i:K+i])+sum(C[i:K+i+1]))
    print(max(D))

Submission status: Wrong Answer

Solution code:
import sys
stdin = sys.stdin

sys.setrecursionlimit(10 ** 7)

def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x) - 1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())


n,k = li()
s = ns()

cur = "1"
cum = [0]

for i, si in enumerate(s):
    if cur != si:
        cur = si
        cum.append(i)

cum.append(n)

if cur == "0":
    cum.append(n)

if len(cum) == 2:
    cum.append(n)
    cum.append(n)

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

print(ans)

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
S=input()+"2"
if S[0]=="1":
    _list=[]
else:
    _list=[0]
cnt=1
for i in range(N):
    if S[i]==S[i+1]:
        cnt+=1
    else:
        _list.append(cnt)
        cnt=1
print(_list)
C=len(_list)
K=min(C,K)
print(max([sum(_list[2*i:2*i+2*K+1]) for i in range(C-K+1)]))

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
S=input()+"2"
if S[0]=="1":
    _list=[]
else:
    _list=[0]
cnt=1
for i in range(N):
    if S[i]==S[i+1]:
        cnt+=1
    else:
        _list.append(cnt)
        cnt=1
print(_list)
C=len(_list)
K=min(C,K)
max([sum(_list[2*i:2*i+2*K+1]) for i in range(C-K+1)])

Submission status: Wrong Answer

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

## make 'next_lst'
## S='1100010000' --> next_lst=[2,3,1,4,1]
next_lst = []
pre_state = S[0]
cnt = 1
for i in range(1,len(S)):
  if S[i] == pre_state:
    cnt += 1
  else:
    next_lst.append(cnt)
    pre_state = S[i]
    cnt = 1
else:
  next_lst.append(cnt)
if S[0] == '0':
  next_lst = [0] + next_lst
if S[-1] == '0':
  next_lst = next_lst + [0]
#print(next_lst)

ans = 0
if len(next_lst) <= 2*K+1:
  ans = sum(next_lst)
  print(ans)
  exit()
for i in range((len(next_lst)-2*K+1)//2):
  var = sum(next_lst[2*i:2*i+(2*K+1)])
  ans = max(ans, var)
  #print(i, var, next_lst[2*i:2*i+(2*K+1)])
  print(ans)

Submission status: Wrong Answer

Solution code:
from itertools import accumulate

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

def solve():
    l = []
    prev = '1'
    cnt = 0
    for c in s:
        if prev == c:
            cnt += 1
        else:
            l.append(cnt)
            cnt = 1
        prev = c
    l.append(cnt)
    # if prev == '0':
    #     l.append(0)
    # print('l:{}'.format(l))
    w = 2 * k + 1

    if len(l) <= w:
        return n

    acc = [0] + list(accumulate(l))
    # print('acc:{}'.format(acc))
    ans = 0
    for i in range(0, len(l) - w + 1, 2):
        ans = max(ans, acc[i + w] - acc[i])
    return ans


print(solve())

Submission status: Wrong Answer

Solution code:
from itertools import accumulate

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

def solve():
    l = []
    prev = '1'
    cnt = 0
    for c in s:
        if prev == c:
            cnt += 1
        else:
            l.append(cnt)
            cnt = 1
        prev = c
    l.append(cnt)
    # if prev == '0':
    #     l.append(0)
    print('l:{}'.format(l))
    w = 2 * k + 1

    if len(l) <= w:
        return n

    acc = [0] + list(accumulate(l))
    print('acc:{}'.format(acc))
    ans = 0
    for i in range(0, len(l) - w + 1, 2):
        ans = max(ans, acc[i + w] - acc[i])
    return ans


print(solve())

Submission status: Wrong Answer

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

length = []
count = 1

for i in range(N-1) :
    if S[i] == S[i+1] :
        count += 1
    else :
        length.append(count)
        count = 1
else :
    length.append(count)

ans = 0

for i in range(len(length)) :
    s = sum(length[i-K:i+K+1])
    ans = max(s,ans)

print(ans)

Submission status: Wrong Answer

Solution code:
import sys
stdin = sys.stdin

sys.setrecursionlimit(10 ** 7)

def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x) - 1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())

n,k = li()
s = ns()

num = [[] for _ in range(2)]

cur = 0
state = "0"

cnt = 0
num[0].append(0)
num[1].append(0)

for i, si in enumerate(s):
    if si == state:
        cnt += 1
    else:
        num[int(state)].append(i)
        cnt = 0
        state = si

if state == "1":
    num[1].append(n)
    num[0].append(n)
else:
    num[0].append(n)

ans = 0

for zi in range(len(num[0])-k-1):
    ans = max(ans, num[1][zi+k] - num[0][zi])

print(ans)

Submission status: Wrong Answer

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

t = []
l = r = 0

for i in range(1, n + 1):
    if s[i - 1] == '0' and s[i] == '1':
        l = i
    if s[i] == '1' and s[i + 1] == '0':
        r = i
        t.append((l, r))

if len(t) > 0:
    res = 0

    if k < len(t):
        for i in range(len(t) - k):
            tmp = t[i + k][1] - t[i][0]
            res = max(res, tmp)
        if t[0][0] != 1:
            tmp = t[k - 1][1] - 1
            res = max(res, tmp)
    else:
        for i in range(len(t)):
            res = t[-1][1] - t[0][0]
        if t[0][0] != 1:
            tmp = t[min(k, len(t))-1][1] - 1
            res = max(res, tmp)

    print(res + 1)
else:
    print(n)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split(" "))
arr=list(map(int,list(input())))
arr.append(2)
huff=[]
hoge=arr[0]
before=0
for i in range(n):
    if hoge!=arr[i+1]:
        huff.append([hoge,before,i])
        before=i+1
        hoge=arr[i+1]
for i in range(k):
    saidai=-1
    greed=-1
    if len(huff) == 1 and huff[0][0] == 1:
        break
    for j,index in zip(huff,range(len(huff))):
        if j[0]==0:
            v=j[2]-j[1]+1
            if index - 1 >= 0:
                v += huff[index - 1][2] - huff[index - 1][1] + 1
            if index + 1 < len(huff):
                v += huff[index + 1][2] - huff[index + 1][1] + 1
            if saidai < v:
                saidai = v
                greed = index
    if greed - 1 >= 0:
        if greed + 1 < len(huff):
            huff[greed - 1][2] = huff[greed + 1][2]
            huff.remove(huff[greed])
            huff.remove(huff[greed])
        else:
            huff[greed - 1][2] = huff[greed][2]
            huff.remove(huff[greed])
    else:
        if greed + 1 < len(huff):
            huff[greed + 1][1] = huff[greed][1]
            huff.remove(huff[greed])
        else:
            huff[greed][0] = 1
fed=list(filter(lambda y:y[0]==1,huff))
aaaa=-1
for i in fed:
    v = i[2] - i[1] + 1
    if v > aaaa:
        aaaa = v
print(aaaa)

Submission status: Wrong Answer

Solution code:
# abcde	s=input()	s='abcde'
# abcde	s=list(input())	s=['a', 'b', 'c', 'd', 'e']
# 5(1つだけ)	a=int(input())	a=5
# 1 2	| x,y = s_inpl()   |	x=1,y=2
# 1 2 3 4 5 ... n 　	li = input().split()	li=['1','2','3',...,'n']
# 1 2 3 4 5 ... n 　	li = inpl()	li=[1,2,3,4,5,...,n]
# FFFTFTTFF 　	li = input().split('T')	li=['FFF', 'F', '', 'FF']

# INPUT
# 3
# hoge
# foo
# bar
# ANSWER
# n=int(input())
# string_list=[input() for i in range(n)]

import math
import copy
from collections import defaultdict
from collections import Counter
from collections import deque
# 直積 A={a, b, c}, B={d, e}:のとき，A×B={(a,d),(a,e),(b,d),(b,e),(c,d),(c,e)}: product(A, B)
from itertools import product
# 階乗 P!: permutations(seq), 順列 {}_len(seq) P_n: permutations(seq, n)
from itertools import permutations
# 組み合わせ {}_len(seq) C_n: combinations(seq, n)
from itertools import combinations
from bisect import bisect_left, bisect_right
# import numpy as np

def inside(y, x, H, W):
    return 0 <= y < H and 0 <= x < W

# 四方向: 右, 下, 左, 上
dy = [0, -1, 0, 1]
dx = [1, 0, -1, 0]

def i_inpl(): return int(input())
def s_inpl(): return map(int,input().split())
def l_inpl(): return list(map(int, input().split()))
INF = float("inf")

############
############
############


N, K = l_inpl()
S = input()

nums = []
now = 1 # 今見ている数
cnt = 0
for i in range(N):
    if S[i] == str(now):
        cnt += 1
    else:
        nums.append(cnt)
        now = 1 - now
        cnt = 1

if cnt != 0:
    nums.append(cnt)

if len(nums) % 2 == 0:
    nums.append(0)

ruiseki = [0]

for i in range(1, len(nums)+1):
    ruiseki.append(ruiseki[i-1] + nums[i-1])


ans = 0
i = 0
while True:
    if i*2+1 >= len(ruiseki):
        break
    ans = max(ans, ruiseki[i*2+1] - ruiseki[i])
    i+=1

print(ans)

Submission status: Wrong Answer

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

q = []
tmp = 0

if len(s) == 1:
    if k >= 1 or s[0] == '1':
        print(1)
    else:
        print(0)
else:
    if s[0] == '0':
        q.append(0)
    for i in range(len(s)):
        if i == 0:
            tmp += 1
        elif i != len(s)-1:
            if s[i] == s[i-1]:
                tmp += 1
            else:
                q.append(tmp)
                tmp = 1
        else:
            if s[i] == s[i-1]:
                tmp += 1
                q.append(tmp)
            else:
                q.append(tmp)
                q.append(1)
    if s[len(s)-1] == '0':
        q.append(0)

    tmp = 0
    max_k = min(math.ceil(len(q)/2), k)
    """
    if len(q) == 1:
        if s[0] == '0' and k == 0:
            ans.append(0)
        else:
            ans.append(q[0])

    #先頭、末尾が0の時
    elif s[0] == '0' and len(q) % 2 == 1:
        for i in range(1, len(q) - 2 * max_k, 2):
            ans.append(sum(q[i:i+2*max_k+1]))
        ans.append(sum(q[:+2*max_k]))
        ans.append(sum(q[-2*max_k+1:]))
    #先頭0末尾1
    elif s[0] == '0' :
        for i in range(1, len(q) - 2 * max_k, 2):
            ans.append(sum(q[i:i+2*max_k+1]))
        ans.append(sum(q[:2*max_k]))
    #先頭1末尾0
    elif len(q) % 2 == 0:
        for i in range(1, len(q) - 2 * max_k, 2):
            ans.append(sum(q[i:i+2*max_k+1]))
        ans.append(sum(q[-2*max_k+1:]))
    else:
        if max_k == math.ceil(len(q)/2):
            max_k -= 1
        for i in range(0, len(q) - 2 * max_k, 2):
            ans.append(sum(q[i:i+2*max_k+1]))

    """
    #先頭1末尾1
    left = 0
    right = 0
    tmp = 0
    ans = 0
    for i in range(0, len(q), 2):
        next_left = i
        next_right = min(i + 2 * k+1, len(q))

        while (next_left > left):
            tmp -= q[left]
            left += 1

        while (next_right > right):
            tmp += q[right]
            right += 1

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

Submission status: Wrong Answer

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

q = []
tmp = 0

if len(s) == 1:
    if k >= 1 or s[0] == '1':
        print(1)
    else:
        print(0)
else:
    if s[0] == '0':
        q.append(0)
    for i in range(len(s)):
        if i == 0:
            tmp += 1
        elif i != len(s)-1:
            if s[i] == s[i-1]:
                tmp += 1
            else:
                q.append(tmp)
                tmp = 1
        else:
            if s[i] == s[i-1]:
                tmp += 1
                q.append(tmp)
            else:
                q.append(tmp)
                q.append(1)
    if s[len(s)-1] == '0':
        q.append(0)

    left = 0
    right = 0
    tmp = 0
    ans = 0
    for i in range(0, len(q), 2):
        next_left = i
        next_right = min(i + 2 * k+1, len(q))

        while (next_left > left):
            tmp -= q[left]
            left += 1

        while (next_right > right):
            tmp += q[right]
            right += 1

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

Submission status: Wrong Answer

Solution code:
n,k=[int(i) for i in input().split()]
s=input()

ope=s[0]
cnt=1
l=[];
if s[0]=='0':
	l.append(0)
for i in range(1,len(s)):
	if s[i]==ope:
		cnt+=1
	else:
		l.append(cnt)
		ope=s[i]
		cnt=1

if s[len(s)-1]=='0':
	l.append(0)
	
start=0
end=2*k

max_sum=sum(l[start:end+1])
cur_sum=max_sum
start+=2
end+=2
while end<len(l):
  cur_sum=cur_sum-l[start-2]-l[start-1]+l[end-2]+l[end-1]
  max_sum=max([max_sum,cur_sum])
  start+=2
  end+=2
print(max_sum)

Submission status: Wrong Answer

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

q = []
tmp = 0

if len(s) == 1:
    if k >= 1 or s[0] == '1':
        print(1)
    else:
        print(0)
else:
    for i in range(len(s)):
        if i == 0:
            tmp += 1
        elif i != len(s)-1:
            if s[i] == s[i-1]:
                tmp += 1
            else:
                q.append(tmp)
                tmp = 1
        else:
            if s[i] == s[i-1]:
                tmp += 1
                q.append(tmp)
            else:
                q.append(tmp)
                q.append(1)

    odd = []
    even = []
    ans = []
    for i in range(len(q)):
        if i % 2 == 0:
            even.append(i)
        else:
            odd.append(i)

    tmp = 0
    max_k = min(math.ceil(len(q)/2), k)

    if len(q) == 1:
        if s[0] == '0' and k == 0:
            ans.append(0)
        else:
            ans.append(q[0])
    #先頭、末尾が0の時
    elif s[0] == '0' and len(q) % 2 == 1:
        for i in range(1, len(q) - 2 * max_k, 2):
            ans.append(sum(q[i:i+2*max_k+1]))
        ans.append(sum(q[:+2*max_k]))
        ans.append(sum(q[-2*max_k+1:]))
    #先頭0末尾1
    elif s[0] == '0' :
        for i in range(1, len(q) - 2 * max_k, 2):
            ans.append(sum(q[i:i+2*max_k+1]))
        ans.append(sum(q[:2*max_k]))
    #先頭1末尾0
    elif len(q) % 2 == 0:
        for i in range(1, len(q) - 2 * max_k, 2):
            ans.append(sum(q[i:i+2*max_k+1]))
        ans.append(sum(q[-2*max_k+1:]))
    #先頭1末尾1
    else:
        if max_k == math.ceil(len(q)/2):
            max_k -= 1
        for i in range(0, len(q) - 2 * max_k, 2):
            ans.append(sum(q[i:i+2*max_k+1]))

    print(max(ans))

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
S = list(input())
cnt = 1
k = {}
if S[0] == "0":
    k[cnt] = [0]
    cnt += 1


flg = False
for i in range(N):
    if S[i] == '1':
        if cnt not in k.keys() :
            k[cnt] = []
        k[cnt].append(i)
        flg = True
    else:
        if flg == True:
            flg = False
            cnt += 1

if S[-1] == '0':
    k[cnt] = [N-1]

if '0' not in S:
    print(1)
    exit()
if N == 1:
    print(1)
    exit()

ans = 0
for i in range(1,cnt - K+1):
    ans = max(ans , k[i+K][-1]-k[i][0] + 1)
print(ans)