Problem:


Score : 400 points

Problem Statement

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

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

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

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

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

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

Constraints

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

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

The length of the string S is N.

Each character of the string S is 0 or 1.

Input

Input is given from Standard Input in the following format:

N K
S

Output

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

Sample Input 1

5 1
00010

Sample Output 1

4

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

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

Sample Input 2

14 2
11101010110011

Sample Output 2

8

Sample Input 3

1 1
1

Sample Output 3

1

No directions are necessary.



Sample input:
5 1
00010


Sample output:
4

Submission status: Wrong Answer

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

zero_positions = [0] + [i for i, c in enumerate(S) if c==0]
count0 = 0
start = 0
end = 0
numbers = 0
index = 0
load_0 = False
l_0 = False
scores = []
while True:
    if (not load_0) and S[index] == '0':
        count0 += 1
        load_0 = True
    elif load_0 == True and S[index] == '1':
        load_0 = False
    if count0 == K+1:
        scores.append(index - start)
        while count0 > K:
            start += 1
            if (not l_0) and S[start] == '0':
                l_0 = True
            elif l_0 == True and S[start] == '1':
                count0 -= 1
                l_0 = False
    index += 1
    if index == N:
        scores.append(index-start)
        break
        
print(max(scores))

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
s = input()
A = [0,0]
zerodig = 0
for i in range(N-1):
    if s[i] != s[i+1]:
        A.append(i+1)
        if  (s[i] == "0" and s[i+1] == "1") or (s[i] == "1" and s[i+1] == "0"):
            zerodig += 1

if len(s) <= 2:
    zerodig = 1
A.append(len(s))

if s[0] == "0":
    check = 0
else:
    check = 1

width = 2 * K + 1
score = 0

if zerodig <= K:
    print(len(s))
else:
    for i in range(len(A)-width):
        if score <= A[i+width] - A[i] and (i+check) % 2 == 0:
            score = A[i+width] - A[i]
    print(score)

Submission status: Wrong Answer

Solution code:
from itertools import accumulate
n, k = map(int,input().split())
s = input()
a = []
i = 0
cnt = 0
now = s[0]
while i < n:
    if s[i] == now:
        cnt += 1
    else:
        a.append(cnt)
        now = s[i]
        cnt = 1
    if i == n-1:
        a.append(cnt)
    i += 1

if len(a) == 1 and s[0] == '1':
    print(n)
    exit()

if s[0] == '0':
    a = [0] + a
    n += 1
if s[-1] == '0':
    a.append(0)
    n += 1
    
r = [0] + list(accumulate(a))
lr = len(r)
ans = -float('inf')
kk = 2*k+1
for i in range(kk, lr+1 , 2):
    ans = max(ans, r[i] - r[i - kk])
print(ans)

Submission status: Wrong Answer

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

# 数列に変換
a = []
c = s[0]
num = 1
for i in range(1,n):
	if c == s[i]:
		num += 1
	else:
		a.append(num)
		num = 1
		c = s[i]
a.append(num)

# k+3の区間の最大値を探す。
l = len(a)

maxsum = 0

if l==1:
	print(a[0])
	exit()

if k==1:
	for i in range(l-1):
		maxsum = max(maxsum, a[i]+a[i+1])
	print(maxsum)
	exit()
	
maxsum = 0
for i in range(l-(k+1)):
	if a[i] == 1:
		maxsum = max(maxsum, sum(a[i:i+3+(k-1)*2]))
	else:
		maxsum = max(maxsum, sum(a[i:i+3+(k-2)*2]))
print(maxsum)

Submission status: Wrong Answer

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

if(len(s) == 1):
    print(1)
    exit()

num = [1]
flag = True

ans = 0
tmp_ans = 0
len_num = 1
tmp = s[0]
if(tmp == "1"): flag = False

for i in s[1:]:
    if(tmp == i): num[-1] += 1
    else:
        len_num += 1
        num.append(1)
        if(len(num)%2 == 0):
            if(k < len_num//2):
                if(k == 1): tmp_ans = sum(num[:-1])
                else: tmp_ans += num[-2] - num[0]
                if(ans < tmp_ans): ans = tmp_ans
                num = num[1:]
                len_num -= 1
                flag = not flag
            else:
                if(len_num == 1): tmp_ans += num[0]
                else: tmp_ans += num[-2]
                if(ans < tmp_ans): ans = tmp_ans
        else:
            if(k < len(num)//2 + flag):
                if(k == 1): tmp_ans = sum(num[:-1])
                else: tmp_ans += num[-2] - num[0]
                if(ans < tmp_ans): ans = tmp_ans
                num = num[1:]
                len_num -= 1
                flag = not flag
            else:
                if(len_num == 1): tmp_ans += num[0]
                else: tmp_ans += num[-2]
                if(ans < tmp_ans): ans = tmp_ans
        tmp = i

if(ans < sum(num)): ans = sum(num)

print(ans)

Submission status: Wrong Answer

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

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

print(sl)
cl = []
for i in range(n):
    st = i - k
    ed = i + k + 1
    if (st < 0):
        st = 0
    if (ed > len(sl)):
        ed = len(sl)
    if (k % 2 == 1):
        if (i % 2 == 0):
            print(st, i, ed, sum(sl[st:ed]))
            cl.append(sum(sl[st:ed]))
    else:
        if (i % 2 == 1):
            print(st, i, ed, sum(sl[st:ed]))
            cl.append(sum(sl[st:ed]))

print(max(cl))

Submission status: Wrong Answer

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

change_points = [i for i in range(n) if i == 0 or s[i-1] != s[i]]
change_points.append(n)
max_ = 0
for i, p in enumerate(change_points):
    if i == len(change_points) - 1:
        continue
    if s[i] == '0':
        if i == 0:
            idx = min(i + 2 * k, len(change_points) - 1)
            m = change_points[idx]
            tmp = m - p
        else:
            continue
    else:
        idx = min(i + 2 * k + 1, len(change_points) - 1)
        m = change_points[idx]
        tmp = m - p
    if tmp > max_:
        max_ = tmp

print(max_)

Submission status: Wrong Answer

Solution code:
N, K = [int(c) for c in input().split()]
S = [int(c) for c in input()]

zero=False
ncluster=0
for s in S:
    if s==0 and zero==False:
        zero=True
        ncluster+=1
    if s==1:
        zero=False

longest=0

for bgn_flip in range(ncluster-K+1):

    S_copy=S[:]
    zero=False
    icluster=-1
    for i,s in enumerate(S):
        if s==0 and zero==False:
            zero=True
            icluster+=1
        if s==1:
            zero=False

        if s==0 and icluster>=bgn_flip and icluster < bgn_flip+K:
            S_copy[i]=1

    seq=0
    for sc in S_copy:
        if sc==1: seq+=1
        if sc==0: seq=0
        if seq>longest:longest=seq

print(longest)

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Your code here!

import time

t = time.time()
n,k = map(int,input().split())

s = input()

s = "10"*10**5

now = s[0]
tmp = 0

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

for i in s:
    if i != now:
        now = i
        h.append(tmp)
        tmp=0
    tmp+=1
h.append(tmp)

ans = 0

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

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
s = input()
def parse(s):
    arr = []
    prev = -1
    ans = 0
    for i in range(n):
        if s[i] == '0':
            if  prev == 0:
                ans += 1
            else:
                arr.append([prev,ans])
                ans = 1
                prev = 0
        elif s[i] == '1':
            if prev == 1:
                ans += 1
            else:
                arr.append([prev,ans])
                ans = 1
                prev = 1
    arr.append([prev,ans ])
    return arr

get = parse(s)[1:]
ll = len(get)
wL = wR = 0
bestL = bestWindow = 0
zeroCount = 0
ans = 0
while wR < ll:
    if zeroCount <= k :
        if get[wR][0] == 0 :
            zeroCount += 1
            ans += get[wR][1]
        else:
            ans += get[wR][1]
        wR += 1
    if zeroCount > k :
        if get[wL][0] == 0 :
            zeroCount -= 1
            ans -= get[wL][1]
        else:
            ans -= get[wL][1]
        wL += 1
    if zeroCount <= k :
        bestWindow = max(ans,bestWindow)
if zeroCount <= k:
    bestWindow = max(ans,bestWindow)
print(bestWindow)

Submission status: Wrong Answer

Solution code:
def solve():
  n,k = (int(i) for i in input().split())
  s = input()
  one = []
  zero = []  
  for i in range(n):
    if i == 0:
      if s[i] == "0":
        zero.append(1)
      else:
        one.append(1)
    
    else:     
     if s[i] == s[i-1]:
      if s[i] == "0":
        zero[-1]+= 1
      else:
        one[-1] += 1
     else:
      if s[i] == "0":
        zero.append(1)
      else:
        one.append(1)
  ans = 0 
  if len(zero) <= k:
    ans = n
    
  else:
    one = []
    zero = []
    i = 0
    while i!= n:
     if i == 0:
      if s[i] == "0":
        one.append(0)
        zero.append(1)
      else:
        one.append(1)
     else:
      if s[i] == s[i-1]:
        if s[i] =="0":
          zero[-1] += 1
        else:
          one[-1] += 1
      else:
       if s[i] == "0":
        zero.append(1)
       else:
        one.append(1)      
     if len(one)>k+1:
      one.pop(0)
     if len(zero)> k:
      zero.pop(0)
     ans = max(ans,sum(zero)+sum(one))
     i += 1
     #print (one)
     #print(zero)
  print(ans)
    
solve()

Submission status: Wrong Answer

Solution code:
n,k = list(map(int,input().split()))
s = input()
c = [0]
ans = -1
for i in range(1,n):
    if s[i-1] == '0' and s[i] == '1':
        c.append(c[i-1] + 1)
    else:
        if i == n-1 and s[i] == '0':
            c.append(c[i-1] + 1)
        else:
            c.append(c[i-1])

right = 0
for left in range(0,n):
    while right < n and c[right] - c[left] <= k:
        right += 1
    ans = max(right - left, ans)
    if right == left: right += 1
    else: pass
print(ans)

Submission status: Wrong Answer

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

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

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

ans=[]
m=ctr-1
if k>=m:
	print n
else:
	for j in range(m-k+1):
		start=j
		end=j+k+1
		ans.append(l.index(end)-l.index(start)-1)

	if len(ans)==0:
		print n
	else:
		print max(ans)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
s=str(input())
s=s+'e'
arr=[[] for _ in range(3)]
count=1
for i in range(n):
  if s[i]==s[i+1]:
    count+=1
  else:
    if s[i]=='1':
      arr[0].append(count)
      count=1
    else:
      arr[1].append(count)
      count=1
if len(arr[1])<=k:
  print(n)
else:
  if len(arr[0])==len(arr[1]):
    arr[0].append(0)
  elif len(arr[0])<len(arr[1]):
    arr[0].append(0)
    arr[0].append(0)
  tmp=0
  sum=0
  count=0
  max=0
  flag=False
  for i in range(len(arr[1])):
    sum+=arr[0][i]
    sum+=arr[1][i]
    count+=1
    if flag:      
      sum-=arr[0][i-k]
      sum-=arr[1][i-k]
    if count==k:
      tmp=sum
      tmp+=arr[0][i+1]
      if tmp>max:
        max=tmp
      count-=1
      flag=True
print(max)

Submission status: Wrong Answer

Solution code:
from sys import stdin
from collections import deque, defaultdict as ddic
from itertools import combinations, permutations, product
import bisect
import heapq

rs = lambda: stdin.readline().strip()
ri = lambda: int(rs())
rsl = lambda: stdin.readline().split()
rim = lambda: map(int, rsl())
ril = lambda: list(rim())

n, k = rim()
s = list(rs())
zeros = []
temp = []
instreak = False
for i, char in enumerate(s):
    if char == '0':
        if not instreak:
            zeros.append(i)
            instreak = True
    else:
        instreak = False

# 1111111
if not zeros:
    print(len(s))
else:
    zeros = [-1] + zeros + [len(s)]
    maximum = 0
    for kk in range(1, k+1):
        index = 0
        left = zeros[index]
        for i in range(kk+1, len(zeros)):
            right = zeros[i]
            streak = right - 1 - left
            if streak > maximum:
                maximum = streak
            index += 1
            left = zeros[index]

    print(maximum)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
S = input()
L = [0]
prev = S[0]
num = 1
for s in S[1:]:
    if s == prev:
        num += 1
    else:
        L.append(num)
        num = 1
        prev = s
L.append(num)
# print(L)
for i in range(1, len(L)):
    L[i] += L[i - 1]
# print(L)
left = 0
if S[0] == "1":
    right = min(2 * K + 1, len(L) - 1)
else:
    right = min(2 * K, len(L) - 1)
num = 0
while right < len(L):
    # print("L[" + str(right) + "] - L[" + str(left) + "] = " + str(L[right] - L[left]))
    num = max(num, L[right] - L[left])
    if left == 0 and S[0] == "0":
        left += 1
    else:
        left += 2
    right += 2
print(num)

Submission status: Wrong Answer

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

if s[0] == "1":
    retu = "whiteblack"
else:
    retu = "blackwhite"


count = 0
pre_person = s[0]
groups = []
for i in range(n):
    person = s[i]

    if person != pre_person:
        groups.append(count)
        count = 0
        pre_person = person
    count += 1
groups.append(count)


maxscore1 = 0
if retu == "whiteblack":
    score1 = 0
    blackgroups = len(groups)//2
    if k < blackgroups:
        for i in range(blackgroups-k+1):
            start_index = 2*i + 1
            score1 = groups[start_index-1]
            for j in range(k):
                score1 += groups[start_index+2*j]
                if start_index + 2*j+1 < len(groups):
                    score1 += groups[start_index+2*j+1]
        maxscore1 = max(score1, maxscore1)
    else:
        maxscore1 = n

else:
    score1 = 0
    blackgroups = len(groups) - len(groups)//2
    if k < blackgroups:
        for i in range(blackgroups-k+1):
            start_index = 2*i
            if start_index-1 >= 0:
                score1 = groups[start_index-1]
            else:
                score1 = 0
            for j in range(k):
                score1 += groups[start_index+2*j]
                if start_index + 2*j+1 < len(groups):
                    score1 += groups[start_index+2*j+1]
            maxscore1 = max(score1,maxscore1)
    else:
        maxscore1 = n

print(maxscore1)

Submission status: Wrong Answer

Solution code:
import numpy as np

N,K = list(map(int,input().rstrip().split()))
S = list(input())
if S[0] == '0':
    zero_start = True
else:
    zero_start = False

N_normalized = [0] if zero_start else []
tmp = '0' if zero_start else '1'
cnt = 0
for i in range(len(S)):
    if tmp == S[i]:
        cnt += 1
        if i == len(S)-1:
            N_normalized.append(cnt)
    else:
        N_normalized.append(cnt)
        cnt = 1
        tmp = S[i]
        if i == len(S)-1:
            N_normalized.append(1)
N_normalized.append(0)

ans = 0
if len(N_normalized)>K*2+1:
    for i in range(0, len(N_normalized)-(K*2), 2):
        tmp = sum(N_normalized[i:i+K*2+1])
        print(i,tmp)
        if ans < tmp:
            ans = tmp
else:
    ans = sum(N_normalized)

print(ans)

Submission status: Wrong Answer

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

zs = [0]
zero = False
for i in range(l):
    c = S[i]
    if c == '0':
        if not zero:
            zero = True
        if i < l-1 and S[i+1] == '1':
            zs.append(i)
    else:
        zero = False
zs.append(l)

best = 0
lzs = len(zs)
for i in range(lzs):
    for k in range(1, K+1):
        if i-k >= 0:
            v = zs[i] - zs[i-k]
            if i < l and S[i] == '1':
                v += 1
            if S[i-k] == '0':
                v += 1
            if v > best:
                best = v
print(best)

Submission status: Wrong Answer

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

one = []
cnt = 0
for i, s in enumerate(S):
    if s == '1':
        cnt += 1
        if i == N - 1:
            one.append(cnt)
    elif s == '0':
        if cnt > 0:
            one.append(cnt)
            cnt = 0
            
zero = []
cnt = 0
for i, s in enumerate(S):
    if s == '0':
        cnt += 1
        if i == N - 1:
            zero.append(cnt)
    elif s == '1':
        if cnt > 0:
            zero.append(cnt)
            cnt = 0
            
if len(one) < len(zero):
    one = [0] + one + [0]
    
for i in range(len(one) - 1):
    one[i + 1] = one[i] + one[i + 1]
one = [0] + one
    
for i in range(len(zero) - 1):
    zero[i + 1] = zero[i] + zero[i + 1]
zero = [0] + zero
    
ans = -1
for i in range(len(one) - (K + 1) + 1):
    tmp = one[i + K] - one[i - 1] + zero[i + K - 1] - zero[i - 1]
    ans = max(ans, tmp)
    
if S.count('0') == 0:
    print(len(S))
else:
    print(ans)

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
S = input()
L = []
cur = S[0]
cnt = 0
if S[len(S)-1] == '1':
  S += '0'
else:
  S += '1'
for i in range(len(S)):
  if S[i] == cur:
    cnt += 1
  else:
    L.append(cnt)
    cnt = 1
    cur = S[i]
ruiseki = [L[0]]
for i in range(len(L)-1):
  ruiseki.append(ruiseki[i]+L[i+1])
ruiseki.insert(0,0)
if len(ruiseki) == 2:
  print(len(S)-1)
else:
  if S[0] == '1' and S[len(S)-2] == '1':
    if (len(L)-1)/2 > K:
      ans = 0
      for i in range(len(ruiseki)-2*K-1):
        if i%2 == 0:
          if ans <= ruiseki[i+2*K+1]-ruiseki[i]:
            ans = ruiseki[i+2*K+1]-ruiseki[i]
      print(ans)
    else:
      print(len(S)-1)
  elif S[0] == '1' and S[len(S)-2] == '0':
    if len(L)/2 > K:
      ans = 0
      for i in range(len(ruiseki)-2*K-2):
        if i%2 == 0:
          if ans <= ruiseki[i+2*K+1]-ruiseki[i]:
            ans = ruiseki[i+2*K+1]-ruiseki[i]
      print(max(ans,ruiseki[len(ruiseki)-1]-ruiseki[len(ruiseki)-1-2*K]))
    else:
      print(len(S)-1)
  elif S[0] == '0' and S[len(S)-2] == '1':
    if len(L)/2 > K:
      ans = 0
      for i in range(len(ruiseki)-2*K):
        if i%2 == 0:
          if ans <= ruiseki[i+2*K]-ruiseki[i]:
            ans = ruiseki[i+2*K]-ruiseki[i]
      print(ans)
    else:
      print(len(S)-1)
  else:
    ans = 0
    if (len(L)+1)/2 > K:
      for i in range(len(ruiseki)-2*K-3):
        if i%2 != 0:
          if ans <= ruiseki[i+2*K+1]-ruiseki[i]:
            ans = ruiseki[i+2*K+1]-ruiseki[i]
      print(max(ans,ruiseki[2*K]-ruiseki[0],ruiseki[len(ruiseki)-1]-ruiseki[len(ruiseki)-1-2*K]))
    else:
      print(len(S)-1)

Submission status: Wrong Answer

Solution code:
N,K = [int(i) for i in input().split()]
S = list(input())

ans = 0
flag = 0
mae = S[0]
p = 0
i = 1
tmp = 1
while i < N:
    tmp += 1
    if mae != S[i]:
        if mae == '0':
            flag += 1

        else:
            if flag == K:
                ans = max(ans,tmp-1)
                i -= 1
                flag = 0
                tmp = 1

    if i < N:
        mae = S[i]
    i += 1

if N != 1:
    ans = max(ans,tmp)
else:
    ans = 1

print(ans)

Submission status: Wrong Answer

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

A = [0]
B = [0]
for s in S:
  if s == '1':
    A[-1] += 1
  elif A[-1] != 0:
    A.append(0)
  if s == '0':
    B[-1] += 1
  elif B[-1] != 0:
    B.append(0)

ans = 0
c,d,e,f = 0,0,0,0

for i in range(min(len(A),len(B))):
  a = 0
  if i == 0:
    if S[0] == '1':
      c,d = sum(A[i:min(i + K + 1,len(A))]),sum(B[i:min(i + K,len(B))])
      a = c + d
    else:
      c,d = sum(A[i:min(i + K,len(A))]) , sum(B[i:min(i + K,len(B))])
      a = c + d
  else:
    if S[0] == '1':
      if i + K < len(A):
        c += A[min(i + K,len(A) - 1)] - A[i - 1]
        d += B[min(i + K - 1,len(B) - 1)] - B[i - 1]
        a = c + d
    else:
      if i + K < len(A):
        c += A[min(i + K - 1,len(A) - 1)] - (0 if i == 1 else A[i - 2])
        d += B[min(i + K - 1,len(B) - 1)] - B[i - 1]
        a = c + d
  ans = max(ans,a)
print(ans)

Submission status: Wrong Answer

Solution code:
from itertools import groupby

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

num = []
cot = []
for k, n in groupby(S):
  num.append(k)
  cot.append(len(list(n)))

res = 0
o = 0
k = 0
tmp = []
for i in range(len(cot)-1):
  if num[i] == "1":
    o += cot[i]
  else:
    res += cot[i]
    k += 1
  if k == K:
    tmp.append(o+res+cot[i+1])
    k = 0
    o = 0
    res = 0
if tmp:
  print(max(tmp))
else:
  print(len(S))

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
s = list(input())
ans_0 = []
ans_1 = []
cnt = 0

for i in range(n):
    if s[i] == "1" and cnt == 0:
        ans_0.append(i)
        cnt += 1
    elif s[i] == "0":
        cnt = 0
cnt = 0
for i in range(n):
    if s[n-1 - i] == "1" and cnt == 0:
        ans_1.append(n-1-i)
        cnt += 1
    elif s[n-1 - i] == "0":
        cnt = 0
ans_1.sort()
ans_list = []
if len(ans_0) == 1:
    print(max(n-ans_0[0], ans_1[0]+1))
elif len(ans_0)-1 <= k:
    print(n)
else:
    for i in range(len(ans_0) - k):
        ans_list.append(ans_1[i+k] - ans_0[i])
    print(max(ans_list) + 1)

Submission status: Wrong Answer

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

intervals = []
if int(S[0]) == 1:
  intervals.append(0)

interval = 0
state = int(S[0])
for s in S:
  if int(s) == state:
    interval += 1
  else:
    intervals.append(interval)
    interval = 1
    state = int(s)
if int(S[N-1]) == 0:
  intervals.append(0)
else:
  intervals.append(interval)
    
lengths = []
lengths.append(sum(intervals[0:2*K]))
for i in range(N-2*K):
  if i % 2 == 0:
    lengths.append(sum(intervals[i-1:i+2*K]))
print(max(lengths))

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
s = input()
def parse(s):
    arr = []
    prev = -1
    ans = 0
    for i in range(n):
        if s[i] == '0':
            if  prev == 0:
                ans += 1
            else:
                arr.append([prev,ans])
                ans = 1
                prev = 0
        elif s[i] == '1':
            if prev == 1:
                ans += 1
            else:
                arr.append([prev,ans])
                ans = 1
                prev = 1
    arr.append([prev,ans ])
    return arr

get = parse(s)[1:]
ll = len(get)
wL = wR = 0
bestL = bestWindow = 0
zeroCount = 0
ans = 0
while wR < ll:
    if zeroCount <= k :
        if get[wR][0] == 0 :
            zeroCount += 1
            ans += get[wR][1]
        else:
            ans += get[wR][1]
        wR += 1
    if zeroCount > k :
        if get[wL][0] == 0 :
            zeroCount -= 1
            ans -= get[wL][1]
        else:
            ans -= get[wL][1]
        wL += 1
    bestWindow = max(bestWindow, ans)

print(max(ans,bestWindow))

Submission status: Wrong Answer

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

A = [0]
B = [0]
for s in S:
  if s == '1':
    A[-1] += 1
  elif A[-1] != 0:
    A.append(0)
  if s == '0':
    B[-1] += 1
  elif B[-1] != 0:
    B.append(0)

ans = 0
c,d,e,f = 0,0,0,0

for i in range(min(len(A),len(B))):
  a = 0
  if i == 0:
    if S[0] == '1':
      c,d = sum(A[i:min(i + K + 1,len(A))]),sum(B[i:min(i + K,len(B))])
      a = c + d
    else:
      c,d = sum(A[i:min(i + K,len(A))]) , sum(B[i:min(i + K,len(B))])
      a = c + d
  else:
    if S[0] == '1':
      if i + K < len(A):
        c += A[min(i + K,len(A) - 1)] - A[i - 1]
        d += B[min(i + K - 1,len(B) - 1)] - B[i - 1]
        a = c + d
    else:
      if i + K < len(A):
        c += A[min(i + K - 1,len(A) - 1)] - (0 if i == 1 else A[i - 2])
        d += B[min(i + K - 1,len(B) - 1)] - B[i - 1]
        a = c + d
  print(a,c,d)
  ans = max(ans,a)
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**7)
input = sys.stdin.readline
INF = float('inf')
MOD = 10**9+7
dy = [0,1,0,-1]
dx = [1,0,-1,0]
ddy = [0,1,1,1,0,-1,-1,-1]
ddx = [1,1,0,-1,-1,-1,0,1]

n,k = map(int,input().split())
S = input()+'!'

get = [0]
tmp = 1
for i in range(1,n+1):
    if S[i] == S[i-1]:
        tmp += 1
    else:
        get.append(tmp)
        tmp = 1

get = get + [0,0,0,0,0]
res = 0
if S[0] == '0':
    for i in range(1,len(get)-2*k):
        if i%2 == 0:
            ans = sum(get[i:i+2*k+1])
        else:
            ans = sum(get[i:i+2*k])
        if res <= ans:
            res = ans
else:
    for i in range(1,len(get)-2*k):
        if i%2 == 0:
            ans = sum(get[i:i+2*k])
        else:
            ans = sum(get[i:i+2*k+1])
        if res <= ans:
            res = ans

print(res)

Submission status: Wrong Answer

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

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

ans = 1
for i in range(len(L)-K*3+1):
    ans = max(ans, L[i+K*3-1]-L[i])
print(ans)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
import sys

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

if 0 not in S:
    print(0)
    sys.exit(0)

left = defaultdict(int)
right = defaultdict(int)
index = 0
for i in range(1, N):
    if S[i-1] == 0 and S[i] == 1:
        right[index] = i
        index += 1
    elif S[i-1] == 1 and S[i] == 0:
        left[index] = i-1

    if i == N-1:
        if S[i] == 0:
            right[index] = i
            left[index+1] = i
        if S[i] == 1:
            left[index] = i

ans = 0
for ind in range(index+1):
    a = left[ind+1] - right[ind-K] + 1
    if ans < a:
        ans = a


print(ans)

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
s = input()
A = [0,0]
zerodig = 0
for i in range(N-1):
    if s[i] != s[i+1]:
        A.append(i+1)
        if s[i] == "0" and s[i+1] == "1":
            zerodig += 1
if len(s) >= 2:
    if s[-2] == "1" and s[-1] == "0":
        zerodig += 1
else:
    zerodig = 1
A.append(len(s))

if s[0] == "0":
    check = 0
else:
    check = 1

width = 2 * K + 1
score = 0

if zerodig <= K:
    print(len(s))
else:
    for i in range(len(A)-width):
        if score <= A[i+width] - A[i] and (i+check) % 2 == 0:
            score = A[i+width] - A[i]
    print(score)

Submission status: Wrong Answer

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

if S[0] == "0":
  List = []
  count = 0
  mark = "0"
  for i in range(N):
    if S[i] == mark:
      count += 1
    else:
      List = List + [count]
      count = 1
      if mark == "0":
        mark = "1"
      else:
        mark = "0"
  List = List + [count]
  l = len(List)
  if l <= 2 * K:
    print(N)
  else:
    ans = sum(List[:2 * K])
    List = List + [0, 0, 0]
    p = sum(List[1:2 * K + 1])
    if p > ans:
      ans = p
    for i in range(3, l - 2 * K + 2, 2):
      p = p - List[i - 2] - List[i - 1] + List[i + 2 * K - 1] + List[i + 2 * K]
      if p > ans:
        ans = p
    print(ans)
if S[0] == "1":
  List = []
  count = 0
  mark = "1"
  for i in range(N):
    if S[i] == mark:
      count += 1
    else:
      List = List + [count]
      count = 1
      if mark == "0":
        mark = "1"
      else:
        mark = "0"
  List = List + [count]
  l = len(List)
  if l <= 2 * K + 1:
    print(N)
  else:
    ans = sum(List[:2 * K + 1])
    List = List + [0, 0, 0]
    p = ans
    for i in range(2, l - 2 * K + 1, 2):
      p = p - List[i - 2] - List[i - 1] + List[i + 2 * K - 1] + List[i + 2 * K]
      if p > ans:
        ans = p
    print(ans)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
*s,=input()
g=[]
t=1
for i in range(1,n):
    if s[i]==s[i-1]:
        t+=1
    else:
        g.append(t)
        t=1
g.append(t)
for i in range(1,len(g)):
    g[i]+=g[i-1]
g=[0]+g
a=0
for i in range(s[0]=='1',len(g),2):
    a=max(a,g[i]-g[max(0,i-3*k+1)])
print(a)

Submission status: Wrong Answer

Solution code:
"""
python3 c.py < input.txt
"""
import math
import itertools as it
import heapq

N,K=map(int, input().split())
S=input()
T=[S[0]]
U=[1]
for c in S[1:]:
    if c==T[-1]:
        U[-1]=U[-1]+1
    else:
        T.append(c)
        U.append(1) 
M=len(U)
V=[U[0]]
for i in range(1,M):
    V.append(V[-1]+U[i])
V=[0]+V
x=0
if len(V)==2:
    print(V[1])
    exit(0)

for l in range(1 if T[0] == "0" else 2, M+1):
    ll=max(0, l-1)
    r=max(l, min(M-1, l+2*K+1))
    x=max(x, V[r]-V[ll])

print(x)

Submission status: Wrong Answer

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

S = str(input())

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

ans = 1
if len(L)-K*3+1 >= 0:
    for i in range(len(L)-K*3+1):
        ans = max(ans, L[i+K*3-1]-L[i])
    print(ans)
else:
    print(len(S))

Submission status: Wrong Answer

Solution code:
def solve():
    N,K=map(int,input().split())
    S=input()
    s2=[]
    cp='_'
    cn=0
    count={'0':0, '1':0}
    for c in S:
        if cp != c:
            if cp != '_':
                s2.append(cn)
                count[cp]+=1
            cp = c
            cn = 0
        cn += 1
    s2.append(cn)
    count[cp]+=1

    csum=[0,0,0,0,0]
    csumval=0
    for v in s2:
        csumval+=v
        csum.append(csumval)

    csum+=[csumval,csumval,csumval,csumval,csumval]
    s2_len = len(s2)
    #print(s2,s2_len)
    #print(csum)
    csumlen=len(csum)

    if count['0'] <= K:
        return csum[-1]

    #print('csumlen =',csumlen)
    ans=0
    if S[0]=='1':
        for i in range(0,s2_len//2+3,2):
            if 5+i+K*2>=csumlen:
                break
            ans1=csum[5+i+K*2]-csum[5+i-1]
            #print(ans1,i,i+K*2)
            ans=max(ans,ans1)
    else:
        for i in range(-1,s2_len//2+3,2):
            if 5+i+K*2>=csumlen:
                break
            ans1=csum[5+i+K*2]-csum[5+i-1]
            #print(ans1,i,i+K*2)
            ans=max(ans,ans1)
    return ans

print(solve())

Submission status: Wrong Answer

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

now = "0"
nl = []
for i, s in enumerate(S):
    if s != now:
        now = s
        nl.append(i)

nl_even = nl[0::2]
nl_odd = nl[1::2]
nl_odd.append(len(S))
print(nl_even)
print(nl_odd)
x = 0
for i in range(len(nl_even)-k):
    x_ = nl_odd[i+k] - nl_even[i]
    x = max(x, x_)
    print(x)

print(x)

Submission status: Wrong Answer

Solution code:
n,k = list(map(int,input().split()))
s = input()
c = [0]
ans = -1
for i in range(1,n):
    if s[i-1] == '0' and s[i] == '1':
        c.append(c[i-1] + 1)
    else:
        if i == n-1 and s[i] == '0':
            c.append(c[i-1] + 1)
        else:
            c.append(c[i-1])

for left in range(0,n):
    right = left
    while right < n and c[right] - c[left] <= k:
        right += 1
    ans = max(right - left, ans)

    if right == left: right += 1
    else: pass
print(ans)