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, raw_input().split())
S=raw_input()

if "0" not in S:
	print len(S)
	quit()

L=[]
for i,x in enumerate(S):
	if i==0:
		cnt=1
	else:
		if prev==x:
			cnt+=1
		else:
			L.append(cnt)
			cnt=1
	prev=x
else:
	L.append(cnt)	


if K*2>=len(L):
	print len(S)
	quit()


LSUM=[0]
for x in L:
	LSUM.append(LSUM[-1]+x)

l=len(LSUM)


if S[0]=="0":
	h=2*K
else:
	h=2*K+1

ans=float("-inf")
for i in range(l-h):
	ans=max(ans, LSUM[i+h]-LSUM[i])

print ans

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
S = input()
L=[]
n=0
for i in range(len(S)-1):
    n+=1
    if(S[i]!=S[i+1]):
        L.append(n)
        n=0
if S[0]=="0":
    L.insert(0,0)

max_r = 0
for i in range(len(L)-K-2):
    R = sum(L[2*(i+1)-2:2*(i+1)-1+2*K+1])
    #print(R)
    if max_r < R:
        max_r = R
print(max_r)

Submission status: Wrong Answer

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

if a=='0':
  f=0
else:
  f=1

for x in s[1:]:
  if a[0]!=x:
    l.append(len(a))
    a=x
  else:
    a+=x

if len(l)%2==0:
  pass
else:
  l=l+[0]

l2=[]

if f==1:
  pass
else:
  l2=[0]+l2

for i in range(len(l)):
  if i%2==0:
    l2.append(l[i]+l[i+1])

mx=sum(l2[:2*(k-1)+1])
a=l2[0]
for i in range(1,len(l2)):
  x=l2[i]
  if a<x:
    mx+=x
    mx-=a
    a=x

print(mx-1)

Submission status: Wrong Answer

Solution code:
import sys
from itertools import accumulate
N, K  = map(int, input().split())
ans = 0
num = 0
S = input()
a = S.split('1')
b = S.split('0')
ls = []
for x in a:
    if len(x) != 0:
        ls.append(len(x))
c = [0] + list(accumulate(ls))
ls_ = []
for x in b:
    if len(x) != 0:
        ls_.append(len(x))
d = [0] + list(accumulate(ls_))

if len(c)-1 <= K:
    print(N)
    sys.exit()

if len(c) == len(d):
    if S[0] == '0':
        q = c[-1]
        c.append(q)
        r = d[-1]
        d.append(r)
        for x, y, u, t in zip(c, c[K:], d, d[K+1:]):
            ans = max(ans, y-x+t-u)
        print(ans)
    elif S[0] == '1':
        q = c[-1]
        c.append(q)
        r = d[-1]
        d.append(r)
        for x, y, u, t in zip(c, c[K:], d, d[K+1:]):
            ans = max(ans, y-x+t-u)
        print(ans)
elif len(c) == len(d)+1:
    q = c[-1]
    c.append(q)
    for x, y, u, t in zip(c, c[K:], d, d[K+1:]):
        ans = max(ans, y-x+t-u)
    print(ans)
elif len(c) == len(d)-1:
    r = d[-1]
    d.append(r)
    for x, y, u, t in zip(c, c[K:], d, d[K+1:]):
        ans = max(ans, y-x+t-u)
    print(ans)

Submission status: Wrong Answer

Solution code:
import sys
read = sys.stdin.readline


def read_ints():
    return list(map(int, read().split()))


def read_a_int():
    return int(read())


def read_matrix(H):
    '''
    H is number of rows
    '''
    return [list(map(int, read().split())) for _ in range(H)]


def read_map(H):
    '''
    H is number of rows
    文字列で与えられた盤面を読み取る用
    '''
    return [read() for _ in range(H)]


def read_col(H, n_cols):
    '''
    H is number of rows
    n_cols is number of cols

    A列、B列が与えられるようなとき
    '''
    ret = [[] for _ in range(n_cols)]
    for _ in range(H):
        tmp = list(map(int, read().split()))
        for col in range(n_cols):
            ret[col].append(tmp[col])

    return ret


N, K = read_ints()
S = read()

if S[0] == '0':
    sum_0 = [0, 1]
else:
    sum_0 = [0, 0]

# prev = S[0]
for s, s_next in zip(S[:-2], S[1:-1]):
    if s == s_next:
        sum_0.append(sum_0[-1])
        continue

    if s == '1' and s_next == '0':
        sum_0.append(sum_0[-1] + 1)
        continue
    sum_0.append(sum_0[-1])


# def get_1length(left, right):
#     return len()


left = 0  # 左端初期値
ans = 1  # 解初期値
right = 1

# right-leftが答え
# sum_0[right]-sum0[left]がKを超えたらleftを進める
# print(sum_0)
# しゃくとり法で溶けそうでは？
for left in range(N):
    # if sum_0[right] - sum_0[left] > K:
    #     continue

    while sum_0[right] - sum_0[left] <= K:
        if right == N:
            break
        right += 1

    # print(left, right)
    ans = max(ans, right - left)  # 大きい方に更新
    # print(ans)

print(ans)

Submission status: Wrong Answer

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

li_s = []
count = 1
temp = S[0]
if temp == "0":
    li_s.append(0)
for i in range(1,N):
    if S[i] != temp:
        if i == N-1:
            li_s.append(count)
            li_s.append(1)
        else:
            li_s.append(count)
            temp = S[i]
            count = 1
    elif i == N-1:
        count += 1
        li_s.append(count)
    else:
        count += 1

for i in range(100000):
    li_s.append(0)
max = 0

if K == 1:
    print(1)
for i in range(N - K*2+1):
    temp = 0
    if i % 2 == 0:
        for j in range(i,i+K*2+1):
            temp += li_s[j]
    else:
        for j in range(i,i+K*2):
            temp += li_s[j]

    if temp > max:
        max = temp
print(max)

Submission status: Wrong Answer

Solution code:
n, k = [int(x) for x in input().split()]
s = input()
ans = 0
for i in range(n):
  if s[i - 1] == s[i] and i != 0:
    continue
  a, c = 1, 0
  for j in range(i, n):
    if a == '1' and s[j] == '0':
      c += 1
    elif i == 0 and j == n-1:
      ans = n
      break
    if c == k + 1 or j == n-1:
      ans = max(ans, j-i-1)
      if i == 0:
        ans += 1
      break
    a = s[j]
print(ans)

Submission status: Wrong Answer

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

co = 1
for i in range(N-1):
    if S[i] != S[i+1]:
        s.append(co)
        co = 1
    else:
        co += 1

s.append(co)

ans = []

for i in range(len(s)):
    if S[0] == '0':
        ans.append(sum(s[i:K*2+i]))
    else:
        ans.append(sum(s[i:K*2+1+i]))

print(max(ans))

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
S=input()
ind10=[]
ind01=[]
if N==1:
    if S=='0':
        print(1)
    else:
        print(0)
else:
    for i in range(N-1):
        if S[i]==0 and S[i+1]==1:
            ind01.append(i)
        elif S[i]==1 and S[i+1]==0:
            ind10.append(i)
    if len(ind10)>=K:
        print(N)
    else:
        a=0
        for k in range(len(ind10)-K+1):
            if k==0:
                a=max(a,ind10[K]+1)
            else:
                a=max(a,ind10[K+k]-ind01[k-1])
        print(a)

Submission status: Wrong Answer

Solution code:
inputs1 = input()
n = int(inputs1.split(" ")[0])
k = int(inputs1.split(" ")[1])
s = [int(i) for i in list(input())]

count = 0
countMax = 0
order = [0, 0]
pre = -1
state = 0
for ki in range(k):
    for i in range(n):
        if s[i] == 0:
            if state == 0:
                pre = 0
                count = count + 1
                order[0] = i
                state = 1
            elif state == 1 and pre == 0:
                count = count + 1
            elif state == 1 and pre == 1:
                pre = 0
                count = count + 1
                state = 2
                order[0] = i
            elif state == 2 and pre == 1:
                if count > countMax:
                    countMax = count
                    orderMax = order
                pre = 0
                count = 1
                state = 1
            elif state == 2 and pre == 0:
                count = count + 1
        else:
            if state == 0:
                pre = 1
                count = count + 1
                state = 1
            elif state == 1 and pre == 1:
                count = count + 1
            elif state == 1 and pre == 0:
                pre = 1
                count = count + 1
                order[1] = i - 1
                state = 2
            elif state == 2 and pre == 1:
                count = count + 1
            elif state == 2 and pre == 0:
                count = count + 1
                order[1] = i - 1
                pre = 1

    if state == 1 and count > countMax and pre == 0:
        order[1] = n-1
        orderMax = order
        countMax = count
    elif state == 1 and count > countMax and pre == 1:
        order[0] = 0
        order[1] = 0
        orderMax = order
        countMax = count
    elif state == 2 and count > countMax and pre == 0:
        order[1] = n-1
        orderMax = order
        countMax = count
    elif state == 2 and count > countMax and pre == 1:
        orderMax = order
        countMax = count

    for j in range(orderMax[0], orderMax[1]+1):
        s[j] = 1

    count = 0
    order = [0, 0]
    orderMax = [0, 0]
    pre = -1
    state = 0
    if ki != k -1:
        countMax = 0
print(countMax)

Submission status: Wrong Answer

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

S=input()

#0の位置がK回きたら次の1がきた時点まででカウント終了
#そこからまたカウントスタートするけど前回の終了いちを忘れるな

# max_len = 0
# now_len = 0
# count0 = 0
# last0_ind = -1
# for i in range(N):
#     now_len += 1
#     if S[i]==1:
#         continue
#     else:
#         count0+=1
#     if count0 == K:
#         last0_ind = i
#     elif count0 == K+1:
#         max_len = max(max_len,now_len-1)
#         now_len = 
    
# max_len = max(max_len,now_len)

ind_0 = []
max_len = 0
left = 0
right = 0
flag = False
left_flag = 0
if S.count('0')==0 or S.count('0')==N:
    max_len = N
    flag = True



for i in range(1,len(S)):
    if(flag==True): 
        max_len = N
        break
    if S[i]=='1':
            continue
    if i == len(S)-1:
        if S[i]=='1':
            continue
        else:
            right = i
            if (left_flag==0):
                ind_0.append([right,right])
            else:
                ind_0.append([left,right])

            continue
    if S[i]=='0' and S[i-1]!='0' and S[i+1]!='0':
        ind_0.append([i,i])
    elif S[i-1]!='0' and S[i+1]=='0':
        left = i
        left_flag = 1
    elif S[i-1]=='0' and S[i+1]!='0':
        right = i
        ind_0.append([left,right])
        left_flag = 0
    else:
        continue


# print(ind_0)



#Kがlenより大きい時の例外

# if 
for i in range(len(ind_0)):
    if(flag==True): break
    if i+K>len(ind_0)-1:
        # print('1',max(N-ind_0[i-1][1]-1,max_len))
        max_len =max(N-ind_0[i-1][1]-1,max_len)
    elif i == 0:
        # print('2',ind_0[K][0])
        max_len = ind_0[K][0]
    else:
        # print('3',abs(ind_0[i-1][1]+1-ind_0[i+K][0]))
        max_len = max(ind_0[i-1][1]+1-ind_0[i+K][0]-1+1,max_len)

print(max_len)

Submission status: Wrong Answer

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

L = [1]

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

n = len(L)
if S[0] == "1":
	if n // 2 <= K:
		print(N)
	else:
		if S[-1] == "1":
			x = L[0]
			for i in range(K):
				x += L[2 * i + 1] + L[2 * i + 2]
			val = x
			for i in range(int(n // 2) - K):
				val += L[2 * (i + K) + 1] + L[2 * (i + K) + 2] - L[2 * i] - L[2 * i + 1]
				if val > x:
					x = val
			print(x)
		else:
			x = L[0]
			for i in range(K):
				x += L[2 * i + 1] + L[2 * i + 2]
			val = x
			for i in range(int(n // 2) - K - 1):
				val += L[2 * (i + K) + 1] + L[2 * (i + K) + 2] - L[2 * i] - L[2 * i + 1]
				if val > x:
					x = val
			val += L[-1] - L[-2 * K - 1] - L[-2 * K - 2]
			if val > x:
				x = val
			print(x)			

else:
	if (n - 1) // 2 <= K:
		print(N)
	else:
		if S[-1] == "1":
			LL = [L[-1 - i] for i in range(n)]
			x = LL[0]
			for i in range(K):
				x += LL[2 * i + 1] + LL[2 * i + 2]
			val = x
			for i in range(int(n // 2) - K - 1):
				val += LL[2 * (i + K) + 1] + LL[2 * (i + K) + 2] - LL[2 * i] - LL[2 * i + 1]
				if val > x:
					x = val
			print(x)
		else:
			LL = L[1:]
			x = LL[0]
			for i in range(K):
				x += LL[2 * i + 1] + LL[2 * i + 2]
			val = x
			for i in range(int((n - 1) // 2) - K - 1):
				val += LL[2 * (i + K) + 1] + LL[2 * (i + K) + 2] - LL[2 * i] - LL[2 * i + 1]
				if val > x:
					x = val
			val += L[-1] - LL[-2 * K - 1] - LL[-2 * K - 2]
			if val > x:
				x = val

			LL = [L[-2 - i] for i in range(n - 1)]
			y = LL[0]
			for i in range(K):
				y += LL[2 * i + 1] + LL[2 * i + 2]
			val = y
			for i in range(int((n - 1) // 2) - K - 1):
				val += LL[2 * (i + K) + 1] + LL[2 * (i + K) + 2] - LL[2 * i] - LL[2 * i + 1]
				if val > y:
					y = val
			val += L[0] - LL[-2 * K - 1] - LL[-2 * K - 2]
			if val > y:
				y = val
			print(max(x, y))

Submission status: Wrong Answer

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

count = 0
flag = True

l = []
for c in S:
    if int(flag) == c:
        count += 1
    else:
        l.append(count)
        count = 1
        flag = not flag

l.append(count)

if K >= (len(l) / 2):
    print(N)
    exit()

last_a = []
for i in range(K, len(l) - K, 2):
    last_a.append(sum(l[i-K:i+K+1]))

last_b = []
K = K-1
for i in range(K, len(l) - K, 2):
    last_b.append(sum(l[i-K:i+K+1]))

l.extend(last_a)
l.extend(last_b)
print(max(l))

Submission status: Wrong Answer

Solution code:
import math                                                                                            

n,k = raw_input().split()
s = list(raw_input())


cnt = 0

prev = ''
tmp = ''
ar = []
for i in s:
    if i == '0' and prev !=  1:
        tmp += '0'
        prev = 0
    elif i == '0' and prev == 1:
        prev = 0
        ar.append(tmp)
        tmp = '0'
    elif i == '1' and prev != 0:
        tmp += '1'
        prev = 1
    elif i == '1' and prev == 0:
        prev = 1
        ar.append(tmp)
        tmp = '1'

ar.append(tmp)
k = int(k)


j = 0
cnt = 0
total = 0
m = 0

for i in ar:
    total += len(i)
    if '0' in i:
        cnt += 1
    if '0' in i and cnt == k + 1:
        total -= len(ar[j-1-2*k]) + len(ar[j-1-2*k])
    m = max(m, total)
    j += 1

print m

Submission status: Wrong Answer

Solution code:
def solve():
  N, K = map(int, input().split())
  _S = input()
  S = []
  b = _S[0]
  s = 0
  for _s in _S:
    if b == _s:
      s = s + 1
    else:
      b = _s
      S.append(s)
      s = 1
  S.append(s)

  num = min(len(S), 2 * K + 1)
  n = sum(S[0:num if _S[0] == '1' else (num - 1)])
  m = n
  for i in range(0 if _S[0] == '1' else -1, len(S) - num, 2):
    n = n - sum(S[max(0, i):i+2])
    n = n + sum(S[i + num:i + num + 2])
    m = max(m, n)

  return m

print(solve())

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import sys
# ----------------------------------------------------------------
# Use Solve Function

def solve(lines):
    N,K = map(int, lines.pop(0).split(' '))
    S = lines.pop(0)
    NLIST = []
    t = S[0]
    n = 0
    zeros = 0
    for s in S:
        if s == t:
            n += 1
        else:
            NLIST.append((int(t), n))
            if int(t) == 0:
                zeros += 1
            t = s
            n = 1
    NLIST.append((int(t), n))
    if int(t) == 0:
        zeros += 1

    if zeros <= K:
        print(N)
        sys.exit(0)

    if NLIST[0][0] == 1:
        first = True
    else:
        first = False


    if NLIST[-1][0] == 1:
        end = True
    else:
        end = False


    maxh = 0
    for i in range(zeros - K + 1):
        if first:
            idx = 2*K + 2*i + 1
        else:
            idx = 2*K + 2*i

        if idx > len(NLIST):
            idx -= 1

        maxh = max(sum([i for n, i in NLIST[i:idx]]), maxh)
    print(maxh)


#
# main
#
lines = [x.strip() for x in sys.stdin.readlines()]

#
# solve !!
#
solve(lines)

Submission status: Wrong Answer

Solution code:
nums = input().rstrip().split(' ')
N = int(nums[0])
K = int(nums[1])
next_s = list(input())
f_max_score = 0
for x in range(K):
    if next_s.count('0') == 0:
        max_score = next_s.count('1')
        break
    for r in range(1,N):
        for l in range(r):
            s = next_s
            max_score = 0
            for i in range(l,r):
                if s[i] == '0':
                    s[i] = '1'
                elif s[i] == '1':
                    s[i] = '0'
            score = 0
            for y in range(N):
                if s[y] == '1':
                    score = score + 1
                    if score > max_score:
                        max_score = score
            else:
                score = 0

            if max_score > f_max_score:
                f_max_score = max_score
                next_s = s 
print(max_score)

Submission status: Wrong Answer

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

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

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



i = 0
r = 0
k = 0

if K >= N//2:
    print(N)
    exit()

while True:
    if ll[r] < 0:
        k += 1
    r += 1
    if k >= K:
        break
    if r >= len(l) - 1:
        print(N)
        exit()

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

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

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

Submission status: Wrong Answer

Solution code:
import sys
from itertools import accumulate
N, K  = map(int, input().split())
ans = 0
num = 0
S = input()
a = S.split('1')
b = S.split('0')
ls = []
for x in a:
    if len(x) != 0:
        ls.append(len(x))
c = [0] + list(accumulate(ls))
ls_ = []
for x in b:
    if len(x) != 0:
        ls_.append(len(x))
d = [0] + list(accumulate(ls_))

if len(c)-1 <= K:
    print(N)
    sys.exit()

if len(c) == len(d):
    if S[0] == '0':
        q = c[-1]
        c.append(q)
        r = d[-1]
        d.append(r)
        for x, y, u, t in zip(c, c[K:], d, d[K+1:]):
            ans = max(ans, y-x+t-u)
        print(ans)
    elif S[0] == '1':
        q = c[-1]
        c.append(q)
        r = d[-1]
        d.append(r)
        for x, y, u, t in zip(c, c[K:], d, d[K+1:]):
            ans = max(ans, y-x+t-u)
        print(ans)
elif len(c) == len(d)+1:
    q = c[-1]
    c.append(q)
    for x, y, u, t in zip(c, c[K:], d, d[K:]):
        ans = max(ans, y-x+t-u)
    print(ans)
elif len(c) == len(d)-1:
    r = d[-1]
    d.append(r)
    for x, y, u, t in zip(c, c[K:], d, d[K+1:]):
        ans = max(ans, y-x+t-u)
    print(ans)

Submission status: Wrong Answer

Solution code:
def kakaka():
    a = list(map(int, input().split()))
    b = map(int, input().split())
    c = int(input())

def start_process():
    return
def main():
    all_num, inst_num = map(int, input().split())
    data = list(input())

    data_la = []
    ans_0, ans_1 = 0, 0
    for i in range(all_num):
        if data[i] == '1':
            ans_1 += 1
            if ans_0 > 0:
                data_la.append(ans_0)
                ans_0 = 0
        else:
            ans_0 += 1
            if ans_1 > 0:
                data_la.append(ans_1)
                ans_1 = 0
    if ans_1 > 0:
        data_la.append(ans_1)
    elif ans_0 > 0:
        data_la.append(ans_0)

    ans = 0
    ans_data = 0
    count_0 = 0

    if data[0] == '0':
        flg = 0
        flg_hajime = 0
    else:
        flg= 1
        flg_hajime = 1
    # print(data_la)

    if len(data_la) <= 2:
        print(sum(data_la))
    else:
        pre_data1, pre_data2 = data_la[0], data_la[1]
        pre_data1_ind, pre_data2_ind = 0, 1
        for i in range(len(data_la)):
            if i % 2 == flg:
                count_0 += 1
            if count_0 == inst_num + 1:
                if flg_hajime:
                    ans = ans - pre_data1 - pre_data2
                    pre_data1_ind += 2
                    pre_data2_ind += 2
                    if pre_data1_ind >= len(data_la):
                        pre_data1 = 0
                    else:
                        pre_data1 = data_la[pre_data1_ind]
                    if pre_data2_ind >= len(data_la):
                        pre_data2 = 0
                    else:
                        pre_data2 = data_la[pre_data1_ind]
                else:
                    ans = ans - pre_data1
                    pre_data1_ind += 1
                    pre_data2_ind += 1
                    if pre_data1_ind >= len(data_la):
                        pre_data1 = 0
                    else:
                        pre_data1 = data_la[pre_data1_ind]
                    if pre_data2_ind >= len(data_la):
                        pre_data2 = 0
                    else:
                        pre_data2 = data_la[pre_data2_ind]
                    flg_hajime = 1


                count_0 -= 1
            ans += data_la[i]
            # print(ans)
            if ans_data < ans:
                ans_data = ans






        print(ans_data)






if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-

import sys
input = sys.stdin.readline

def io_generator():
	return input()

#+++++++++++++++++++

def main(io):
	n,k= map(int, io().split())
	s=[int(i) for i in io()[:n]]
	init=s[0]
	ll=[]
	m=0
	pre=init
	for c in s:
		if c == pre:
			m+=1
		else:
			ll.append(m)
			m=1
			pre=c
	off= 1 if init==1 else 0
	if len(ll)<=2*k + off:
		return n
	pre=sum(ll[:2*k+off])
	rr_max=pre
	for i in range(len(ll)//2 - k):
		cc=pre - ll[i] - ll[i+1] + ll[k]+ll[k+1]
		rr_max = max(rr_max, cc)
		pre=cc
	return rr_max


#++++++++++++++++++++

if __name__ == "__main__":
	io= lambda : io_generator()
	print (main(io))

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-

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

number_list = []
num = 1

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

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


max_num = 0
if S[0] == '0':
    if K * 2 <= len(number_list):
        max_num = ruiseki[2 * K - 1]
    else:
        max_num = ruiseki[-1]
    ruiseki = ruiseki[1:]

for i in range(0, len(ruiseki), 2):
    if i + K * 2 > len(ruiseki) - 1:
        break
    if i == 0:
        max_num = ruiseki[i + K - 1]
    else:
        buff = ruiseki[i + K * 2] - ruiseki[i - 1]
        if buff > max_num:
            max_num = buff

if max_num == 0:
    max_num = number_list[0]

print (max_num)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
S = list(input())
#print(S)
ss = [] # set of 01 sequense
s = []
s_prev = S[0]
for i in range(N):
    si = int(S[i])
    #print(s_prev, si)
    if int(si) != int(s_prev):
        #print("push")
        ss.append(s)
        s = [si]
    else:
        s.append(si)
    s_prev = si
ss.append(s)
#print(ss)
result = 1
# get max len of ss[i]
for k in range(K):
    max_ss = 0
    max_i = -1
    for i in range(len(ss)):
        if ss[i][0] == 1: continue
        if i > 0:
            if i < len(ss) - 1:
                len_ss = len(ss[i-1]) + len(ss[i]) + len(ss[i+1])
            else:
                len_ss = len(ss[i-1]) + len(ss[i])
        else:
            if i < len(ss) - 1:
                len_ss = len(ss[i]) + len(ss[i+1])
            else:
                len_ss = len(ss[i])

        #print("i", i, len_ss)
        if len_ss > max_ss:
            max_i = i
            max_ss = len_ss
    #print(max_i, max_ss)
    if max_i != -1:
        # reverse ss
        if max_i > 0:
            if max_i < len(ss) - 1:
                ss[max_i - 1] = ss[max_i - 1] + [1 for _ in range(len(ss[max_i]))] + ss[max_i + 1]
                del ss[max_i + 1]
                del ss[max_i]
            else:
                ss[max_i - 1] = ss[max_i - 1] + [1 for _ in range(len(ss[max_i]))]
                del ss[max_i]
        else:
            if max_i < len(ss) - 1:
                ss[max_i - 1] = [1 for _ in range(len(ss[max_i]))] + ss[max_i + 1]
                del ss[max_i]
            else:
                ss[max_i - 1] = ss[max_i - 1]
        #print("ss", ss)
        result = max_ss
print(result)

Submission status: Wrong Answer

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

if a=='0':
  f=0
else:
  f=1

for x in s[1:]:
  if a[0]!=x:
    l.append(len(a))
    a=x
  else:
    a+=x

if len(l)%2==0:
  pass
else:
  l=l+[0]

l2=[]

if f==1:
  pass
else:
  l2=[0]+l2

for i in range(len(l)):
  if i%2==0:
    l2.append(l[i]+l[i+1])

mx=sum(l2[:2*(k-1)+1])
a=l2[0]
for i in range(1,len(l2)):
  x=l2[i]
  if a<x:
    mx+=x
    mx-=a
    a=x

print(mx)

Submission status: Wrong Answer

Solution code:
import numpy as np

N,K = map(int, input().split())
s = list(input())
sl = []
S = np.array(s, dtype=np.int64)

S = np.append(S, 1 - S[-1])

#print(S)
a = 1
for i in range(1,N+1):
    if S[i] != S[i-1]:
        sl.append(a)
        #print(i,a)
        a = 1
    else :
        a += 1
if len(sl) - (2*K) > -1:

    slm = [] 
    for i in range (len(sl) - (2*K)):
        #print(i,K)
        #print(sl[0:2])
        if i == 0:
            slm.append(sum(sl[i:i+2*K]))
        else:
            slm.append(sum(sl[i:i+2*K+1]))
            
    print(max(slm))

else:
    print(max(sl))

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
s=list(input())
s=list(map(int,s))
stack=[]
tmp=1
flag=True
for i in range(1,len(s)):
  if s[i]!=s[i-1]:
    stack.append(tmp)
    tmp=1
  else:
    tmp+=1
stack.append(tmp)
odd=[]
even=[]
tmp=0
if s[0]==0:
  flag=False
else:
  flag=True
for i in range(len(stack)):
  tmp+=stack[i]
  if i%2==0:
    if flag:
      odd.append(tmp)
    else:
      even.append(tmp)
  else:
    if flag:
      even.append(tmp)
    else:
      odd.append(tmp)
ans=0
for i in range(len(even)-k-1):
  ans=max(ans,even[i+k+1]-even[i])
print(ans)

Submission status: Wrong Answer

Solution code:
# yield count
def aggregate(S):
  prev = S[0]
  count = 0
  for i in S:
    if i != prev:
      prev = i
      yield count
      count = 0
    count += 1
  if count != 0:
    yield count
  return
 
# yield count
def find_max(S, K):
  counts = list(aggregate(S))
  if len(counts) <= K*2:
    yield sum(counts)
    return
  s = 0
  e = K*2+1
  if S[0] == '1':
    s += 1
    e += 1
    yield sum(counts[:e])
  for i in range(0, len(counts)-e+1, 2):
    yield sum(counts[i+s:i+e])
 
N, K = map(int, input().split())
S = input()
print(max(find_max(S, K)))

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
s = input()
a = []
maxLen = 0
now = ''
for i in range(n):
    decriment = 0
    if now != s[i]:
        now = s[i]
        decriment = 1
        a.append([k, i, 1])
    for j in a:
        j[0] -= decriment
        if j[0] >= 0:
            j[2] += 1
for j in a:
    maxLen = max(maxLen, j[2])
print(maxLen)

Submission status: Wrong Answer

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

X=[]
cnt=0
if len(s)>=2:
    for i in range(n-1):
        cnt+=1
        if s[i]=='0' and s[i+1]=='1':
            X.append(cnt)
            cnt=0
        elif s[i]=='1' and s[i+1]=='0':
            X.append(cnt)
            cnt=0
        elif i==n-2:
            X.append(cnt)
    
    if s[-1]!=s[-2]:
        X.append(1)
    else:
        X[-1]+=1
else:
    X=[s]


if s[0]=='1':
    if k>=len(X[1::2]):
        print(n)
    else:
        z=X[1::2]
        o=X[::2]
        ans=sum(o[:k+1])+sum(z[:k])
        for i in range(len(z)-k):
            ans=max(ans,ans-z[i]-o[i]+z[i+2]+o[i+3])
        print(ans)
else:
    if k>=len(X[0::2]):
        print(n)
    else:
        z=X[::2]
        o=X[1::2]
        ans=sum(z[:k])+sum(o[:k])
        for i in range(len(o)-k):
            ans=max(ans,ans-o[i]-z[i]+o[i+2]+z[i+3])
        print(ans)

Submission status: Wrong Answer

Solution code:
N, K = list(map(int, input().split()))
S = str(input())
list = []
cnt = 1
for i in range(len(S)):
  if(i + 2) > len(S): 
    list.append(cnt)
    break
  if (S[i+1] == S[i]):
    cnt += 1
  else:
    list.append(cnt)
    cnt = 1

# 配列ができた
ans = 0
  #配列の奇数のところに0が連続して入っている
if (S[0] == '1'):
  # 0の塊の数がK以下の時は全て変化させられる
  if(len(list) // 2 <= K):
    print(len(S))
  else:
    # 0の塊を1にした時の最大値を出してみる
    for i in range((len(list) // 2) + 1):
      if(i + 2 * K + 1) > len(list): break
      tmp = 0
      for j in range(2 * K + 1):
        tmp += int(list[i + j])
      if(tmp > ans):
        ans = tmp
#配列の奇数のところに1が連続して入っている
else:
  # 0の塊の数がK以下の時は全て変化させられる
  if((len(list) // 2)  + 1 <= K):
    print(len(S))
  else:
    # 0の塊を1にした時の最大値を出してみる
    for i in range(len(list) // 2):
      if(i + 2 * K) > len(list): break
      tmp = 0
      for j in range(2 * K):
        tmp += int(list[i + j])
      if(tmp > ans):
        ans = tmp
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
from itertools import accumulate
N, K  = map(int, input().split())
ans = 0
num = 0
S = input()
a = S.split('1')
b = S.split('0')
ls = []
for x in a:
    if len(x) != 0:
        ls.append(len(x))
c = [0] + list(accumulate(ls))
ls_ = []
for x in b:
    if len(x) != 0:
        ls_.append(len(x))
d = [0] + list(accumulate(ls_))

if len(c)-1 <= K:
    print(N)
    sys.exit()

if len(c) == len(d):
    if S[0] == '0':
        q = c[-1]
        c.append(q)
        r = d[-1]
        d.append(r)
        for x, y, u, t in zip(c, c[K:], d, d[K+1:]):
            ans = max(ans, y-x+t-u)
        print(ans)
    elif S[0] == '1':
        q = c[-1]
        c.append(q)
        r = d[-1]
        d.append(r)
        for x, y, u, t in zip(c, c[K:], d, d[K+1:]):
            ans = max(ans, y-x+t-u)
        print(ans)
elif len(c) == len(d)+1:
    r = d[-1]
    d.append(r)
    q = c[-1]
    c.append(q)
    for x, y, u, t in zip(c, c[K:], d, d[K:]):
        ans = max(ans, y-x+t-u)
    print(ans)
elif len(c) == len(d)-1:
    r = d[-1]
    d.append(r)
    q = c[-1]
    c.append(q)
    for x, y, u, t in zip(c, c[K:], d, d[K+1:]):
        ans = max(ans, y-x+t-u)
    print(ans)

Submission status: Wrong Answer

Solution code:
N, K = [int(i) for i in input().split()]
S = input()
arr = []
val = 1
temp = S[0]
if temp == '1':
    arr += [0]
for s in S[1:]:
    if s == temp:
        val+=1
    else:
        arr += [val]
        val = 1
    temp = s
if val != 0:
    arr += [val]

max_ = 0
for i in range(len(arr)-(K*2)+1):
    res = sum(arr[i:i+K*2]) if i%2==0 else sum(arr[i:i+K*2+1])
    if res > max_:
        max_ = res
    if max_ > N//2:
        break

print(max_)