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:
#4
n,k=map(int,input().split())
s=str(input())

import numpy as np
line = np.zeros([2*n,],dtype=np.int)
counter1 = 0
counter0 = 0
ind = 0
for i in range(n):
    c = int(s[i])
    if ind%2 ==0:
        if c == 1:
            counter1 += 1
        else:
            counter0 += 1
            if ind == 0:
                line[ind] = counter1
            else:
                line[ind] = line[ind-1]+counter1
            counter1 = 0
            ind+=1
    elif ind%2 ==1:
        if c == 0:
            counter0 += 1
        else:
            counter1 += 1
            line[ind] = line[ind-1] + counter0
            counter0 = 0
            ind+=1

if ind%2 ==0:
        if ind == 0:
                line[ind] = counter1
        else:
                line[ind] = line[ind-1]+counter1
        ind+=1
elif ind%2 ==1:
        line[ind] = line[ind-1] + counter0
        ind+=1
        

line = line[:ind]    
#print(line)

if (len(line)//2)<=k:
    print(n)
else:
    maxchain = 0
    for i in range(0,ind-(2*k+1)+1,2):
        if i==0:
            chain = line[i+2*k]-0
        else:
            chain = line[i+2*k]-line[i-1]
        #print(chain)
        if chain>maxchain:
            maxchain=chain
    print(maxchain)

Submission status: Wrong Answer

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

S = input()
para = [0]
for i, s in enumerate(S):
    if i == 0:
        para.append(1)
    else:
        if S[i] == S[i-1]:
            para[-1] += 1
        else:
            para.append(1+para[-1])
i = 0

tmpmax = 0

if S[0] == '1':
    if i + 2*k + 1 >= len(para) - 1:
        print(n)
    else:
        while i + 2*k + 1 <= len(para) - 1:
            j = i + 2*k + 1
            tmpmax = max(tmpmax, para[j] - para[i])
            #if para[j] - para[i-1] > tmpmax:
            #    tmpmax = para[j] - para[i]

            i += 2
        print(tmpmax)
else:
    if i + 2*k >= len(para) - 1:
        print(n)
    else:
        while i + 2*k <= len(para) - 1:
            j = i + 2*k
            if i == 0:
                tmpmax = max(tmpmax, para[j] - para[i])
                #if para[j] - para[i] > tmpmax:
                #    tmpmax = para[j] - para[i]
            else:
                tmpmax = max(tmpmax, para[j] - para[i-1])
                #if para[j] - para[i-1] > tmpmax:
                #    tmpmax = para[j] - para[i-1]

            i += 2
        print(tmpmax)

Submission status: Wrong Answer

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

def return_ans(S_initial, S):
    ans = 0
    block_size_list = []
    tmp_block_size = 1
    for i in range(1, N):
        if S[i - 1] == S[i]:
            tmp_block_size += 1

        else:
            block_size_list.append(tmp_block_size)
            tmp_block_size = 1
        if i == N - 1:
            block_size_list.append(tmp_block_size)

    times = len(block_size_list) - K * 2

    block_size_list.append(0)
    if K * 2 >= len(block_size_list):
        ans = len(S)
    else:
        if S_initial == 0:
            ans = max([sum(block_size_list[i:i + K * 2 + i % 2]) for i in range(times)])

        else:
            block_size_list.append(0)
            times = len(block_size_list) - K * 2
            ans = max([sum(block_size_list[i:i + K * 2 + (i + 1) % 2]) for i in range(times)])

    return ans

print(return_ans(S[0], S))

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
s=str(input())
s=s+'e'
arr=[[] for _ in range(2)]
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]):
    arr2=[0]
    arr2.extend(arr[0])
    arr[0]=arr2
    arr[1].append(0)
  elif len(arr[0])<len(arr[1]):
    arr2=[0]
    arr2.extend(arr[0])
    arr[0]=arr2
    arr[0].append(0)
    arr[1].append(0)
  else:
    arr[1].append(0)
  tmp=0
  sum=0
  count=0
  max=0
  flag=False
  for i in range(len(arr[1])-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:
N, K = [int(i) for i in input().split()]
S = input()
arr_0,arr_1 = [],[]
val = 1
temp = S[0]
if temp == '0':
    arr_1 += [0]
for s in S[1:]+"_":
    if s == temp:
        val+=1
    else:
        if temp=="1":
            arr_1 += [val]
        else:
            arr_0 += [val]
        val = 1
    temp = s
if S[-1] == "0":
    arr_1 += [0]

max_ = sum(arr_0[:K]+arr_1[:K+1])
for i in range(1, len(arr_0)-K+1):
    if (arr_0[i+K-1]+arr_1[i+K]) > (arr_0[i-1]+arr_1[i-1]):
        max_ = sum(arr_0[i:i+K])+sum(arr_1[i:i+K+1])
print(max_)

Submission status: Wrong Answer

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

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

    cl = []
    c = 0
    for i in range(n):
        st = i-k
        ed = i + k+1
        if (st < 0):
            st = 0
        if (k % 2 == 1 and i % 2 == 0 or k % 2 == 0 and i % 2 == 1):  # kが奇数の時、0の箇所（すなわちiが偶数の箇所）を中心に反転
            if(c == 0):
                cl.append(sum(sl[st:ed]))
            else:
                t = cl[c - 1]
                if(len(sl) >= ed):
                    t += sum(sl[ed - 2:ed])
                elif (len(sl) == ed + 1):
                    t += sum(sl[ed - 2:ed-1])
                if (st > 0):
                    t -= sum(sl[st-2:st])
                cl.append(t)
            c += 1

    print(max(cl))
else:
    print(1)

Submission status: Wrong Answer

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

S = input()
para = [0]
for i, s in enumerate(S):
    if i == 0:
        para.append(1)
    else:
        if S[i] == S[i-1]:
            para[-1] += 1
        else:
            para.append(1+para[-1])
i = 0

tmpmax = 0

if S[0] == '1':
    if i + 2*k + 1 >= len(para) - 1:
        print(n)
    else:
        while i + 2*k + 1 <= len(para) - 1:
            j = i + 2*k + 1
            tmpmax = max(tmpmax, para[j] - para[i-1])
            #if para[j] - para[i-1] > tmpmax:
            #    tmpmax = para[j] - para[i]

            i += 2
        print(tmpmax)
else:
    if i + 2*k >= len(para) - 1:
        print(n)
    else:
        while i + 2*k <= len(para) - 1:
            j = i + 2*k
            if i == 0:
                tmpmax = max(tmpmax, para[j] - para[i])
                #if para[j] - para[i] > tmpmax:
                #    tmpmax = para[j] - para[i]
            else:
                tmpmax = max(tmpmax, para[j] - para[i-1])
                #if para[j] - para[i-1] > tmpmax:
                #    tmpmax = para[j] - para[i-1]

            i += 2
        print(tmpmax)

Submission status: Wrong Answer

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

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

    print(sl)
    cl = []
    c = 0
    for i in range(n):
        st = i-k
        ed = i + k+1
        if (st < 0):
            st = 0
        if (k % 2 == 1 and i % 2 == 0 or k % 2 == 0 and i % 2 == 1):  # kが奇数の時、0の箇所（すなわちiが偶数の箇所）を中心に反転
            if(c == 0):
                cl.append(sum(sl[st:ed]))
            else:
                t = cl[c - 1]
                if(len(sl) >= ed):
                    t += sum(sl[ed - 2:ed])
                elif (len(sl) == ed + 1):
                    t += sum(sl[ed - 2:ed-1])
                if (st > 0):
                    t -= sum(sl[st-2:st])
                cl.append(t)
            c += 1

    print(max(cl))
else:
    print(1)

Submission status: Wrong Answer

Solution code:
l = list(map(int,input().split()))
k = l[1]
s_str = input()
s = list(map(int,s_str))
t = []
u = 1
ans = 0
for i in range(len(s)):
    if i == 0:
        if s[0] == 1:
            t.append(0)
        else:
            u = 1
    elif i == len(s)-1:
        if s[i] != s[i-1]:
            t.append(u)
            u = 1
        else:
            u = u + 1 
        t.append(u)
    else:
        if s[i] != s[i-1]:
            t.append(u)
            u = 1
        else:
            u = u + 1
for i in range(0,len(t),2):
    temp_sum = 0
    if i + 2 * k <= len(t):
        if i == 0:
            for j in range(0,i+2*k):
                temp_sum += t[j]
        else:
            for j in range(i-1,i+2*k):
                temp_sum += t[j]
    if ans < temp_sum:
        ans = temp_sum
if len(t) == 2:
    if t[0] == 0:
        ans = t[1]
if len(t) == 1:
    ans = t[0]
if len(s) == 1:
    ans = 1

print(ans)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
s=str(input())
s=s+'e'
arr=[[] for _ in range(2)]
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]):
    arr2=[0]
    arr2.extend(arr[0])
    arr[0]=arr2
    arr[1].append(0)
  elif len(arr[0])<len(arr[1]):
    arr2=[0]
    arr2.extend(arr[0])
    arr[0]=arr2
    arr[0].append(0)
    arr[1].append(0)
  else:
    arr[1].append(0)
  print(arr[0])
  print(arr[1])
  tmp=0
  sum=0
  count=0
  max=0
  flag=False
  for i in range(len(arr[1])-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:
n,k=map(int,input().split())
s=str(input())
s=s+'e'
arr=[[] for _ in range(2)]
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]):
    arr2=[0]
    arr2.extend(arr[0])
    arr[0]=arr2
    arr[1].append(0)
  elif len(arr[0])<len(arr[1]):
    arr2=[0]
    arr2.extend(arr[0])
    arr[0]=arr2
    arr[0].append(0)
    arr[1].append(0)
  tmp=0
  sum=0
  count=0
  max=0
  flag=False
  for i in range(1,len(arr[1])-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 itertools import groupby
import sys
stdin = sys.stdin
 
sys.setrecursionlimit(10**5) 
 
def ii(): return int(stdin.readline())
def li(): return map(int, stdin.readline().split())
N, K = tuple(li())
S = stdin.readline() + "2"
# use window
l, r = 0, 0
cnt = 0
r_iter = groupby(S)
l_iter = groupby(S)
r_next = next(r_iter)
l_next = next(l_iter)
while r < N:
    # print(l, r)
    if K > 0:
        if r_next[0] == '1':
            r += len(list(r_next[1]))
            r_next = next(r_iter)
            cnt = max(cnt, r - l)
            K -= 1
        elif r_next[0] == "0":
            pass
        r += len(list(r_next[1]))
        r_next = next(r_iter)
    elif K == 0 and r_next[0] == "1":
        r += len(list(r_next[1]))
        r_next = next(r_iter)
        cnt = max(cnt, r - l)
    else:
        if l_next[0] == "0":
            K += 1
        l += len(list(l_next[1]))
        l_next = next(l_iter)
if K > 0:
    cnt = max(cnt, r-l)
print(cnt)

Submission status: Wrong Answer

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

import numpy as np
line = np.zeros([2*n,],dtype=np.int)
counter1 = 0
counter0 = 0
ind = 0
for i in range(n):
    c = int(s[i])
    if ind%2 ==0:
        if c == 1:
            counter1 += 1
        else:
            counter0 += 1
            if ind == 0:
                line[ind] = counter1
            else:
                line[ind] = line[ind-1]+counter1
            counter1 = 0
            ind+=1
    elif ind%2 ==1:
        if c == 0:
            counter0 += 1
        else:
            counter1 += 1
            line[ind] = line[ind-1] + counter0
            counter0 = 0
            ind+=1

if ind%2 ==0:
        if ind == 0:
                line[ind] = counter1
        else:
                line[ind] = line[ind-1]+counter1
        ind+=1
elif ind%2 ==1:
        line[ind] = line[ind-1] + counter0
        ind+=1
        
if ind%2 ==0:
        line[ind] = 0
        ind+=1
line = line[:ind]    
#print(line)

if (len(line)//2)<=k:
    print(n)
else:
    maxchain = 0
    for i in range(0,ind-(2*k+1)+1,2):
        if i==0:
            chain = line[i+2*k]-0
        else:
            chain = line[i+2*k]-line[i-1]
        #print(chain)
        if chain>maxchain:
            maxchain=chain
    print(maxchain)

Submission status: Wrong Answer

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

# abc124_d

# input
n, k = [int(s) for s in input().split()]
S = input()
# get ans
def getp():
    now = S[0]
    for i, c in enumerate(S):
        if now != c:
            now = c
            yield i
pulse = list(getp())
pulse = [0] + pulse + [len(S)]*k*3
def getm():
    for i in range(len(pulse)):
        p = pulse[i]
        if p == len(S): continue
        j = i+k*2 if S[p]==0 else i+k*2+1
        yield pulse[j] - pulse[i]
ans = max(getm())
# output
# print(pulse)
# print(S)
# for i, c in enumerate(S):
#     a = 1 if i in pulse else 0
#     print(a, end='')
# print()
# print(list(getm()))
print(ans)

Submission status: Wrong Answer

Solution code:
import copy

data = input().split()
N = int(data[0])
K = int(data[1])

TL = input()
TL = list(TL)
TLlist = []
TLLlist = []
now = TL[0]
nowlist = now
anslist = []

for lop in range(1,len(TL)):
    if not (now == TL[lop]):
        TLlist.append(nowlist)
        TLLlist.append(len(nowlist))
        nowlist = TL[lop]
        now = TL[lop]
    else:
        nowlist += now
TLlist.append(nowlist)
TLLlist.append(len(nowlist))


if TLlist[0][:1] == "0" :
    anslist.append( sum(TLLlist[:2*K]) )
    fg = True
elif TLlist[0][:1] == "1" :
    anslist.append( sum(TLLlist[:2*K + 1 ]) )
    fg = False

for lop in range(2,len(TLlist),2):
    if 2*K + lop > len(TLlist):
        break
    if fg == True :
        anslist.append( sum(TLLlist[lop:2*K + lop]) )
    elif fg == False :
        anslist.append( sum(TLLlist[lop:2*K + 1 + lop]) )


#print(TLlist)
#print(TLLlist)
#print(anslist)
print(max(anslist))

Submission status: Wrong Answer

Solution code:
import sys
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7

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

# stand = []
# reverse = []
# state = None
# count = 0
# tmp = None
# for i in S:
# 	if i == "0":
# 		state = "stand"
# 	else:
# 		state = "reverse"
# 	if tmp == "stand" and state == "reverse":
# 		stand.append(count)
# 		count = 1
# 	elif tmp == "reverse" and state == "stand":
# 		reverse.append(count)
# 		count = 1
# 	else: # 状態が前と同じ
# 		count += 1
# 	tmp = state
# if state == "stand":
# 	stand.append(count)
# else:
# 	reverse.append(count)

# if "0" not in S:
# 	print(len(S))
# else:
# 	if K >= len(stand): # 全員逆立ちにできる
# 		print(len(S))
# 	else:
# 		if len(stand) > len(reverse):
# 			reverse.append(0)
# 			reverse.insert(0, 0)
# 		if len(stand) == len(reverse) and S[0] == "0":
# 			reverse.insert(0, 0)
# 		if len(stand) == len(reverse) and S[0] == "1":
# 			reverse.append(0)
# 		# print(stand)
# 		# print(reverse)
# 		max_ = 0
# 		for i in range(max(1, 1+len(reverse)-(K+1))):
# 			# print(reverse[i:i+K+1],stand[i:i+K])
# 			tmp = sum(reverse[i:i+K+1]+stand[i:i+K])
# 			if tmp > max_:
# 				max_ = tmp
# 		print(max_)

indexes = []
tmp = ""
for i in range(len(S)):
	if S[i] == "0":
		state = "stand"
	else:
		state = "reverse"
	if tmp != state:
		indexes.append(i)
	else: # 状態が前と同じ
		pass
	tmp = state
indexes.append(N)
ans = []
# print(indexes)
for i in range(len(indexes)):
	# print(i)
	if S[indexes[i]] == "0":
		if i+2*K >= len(indexes):
			break
		ans.append(indexes[i+2*K]-indexes[i])
	else:
		if i+2*K+1 >= len(indexes):
			break
		ans.append(indexes[i+2*K+1]-indexes[i])
if ans == []:
	print(len(S))
else:
	print(max(ans))

Submission status: Wrong Answer

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

zero=[0 for i in range(n)]
h=0
for i in range(n):
    if s[i]=="0":
        zero[h]+=1
    else:
        h+=1
ans=0

zero.sort(reverse=True)
for i in range(k):
    ans+=zero[i]+1
print(ans)

Submission status: Wrong Answer

Solution code:
import re

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

store = []
start = -1
end = -1

if 1 == len(S):
    print(1)
else:
    for i in range(N):
        print(len(S)-1, i)
        if '0' == S[i] and -1 == start:
            start = i
        if '1' == S[i] and -1 != start or len(S)-1 == i:
            end = i+1
        if -1 != start and -1 != end:
            store.append([start, end, len(S[start:end])])
            start = end = -1

    store.sort(key=lambda x: x[2])
    store.reverse()

    li = list(S)
    for i in range(K):
        if len(store) == i:
            break
        start = store[i][0]
        end = store[i][1]
        count = store[i][2]
        li[start:end] = '1' * count

    count = 0
    temp = 0
    for i in range(N):
        if '1' == li[i]:
            temp += 1
        if '0' == li[i] or N-1 == i:
            count = max(temp, count)
            temp = 0
    print(count)

Submission status: Wrong Answer

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

zero=[0 for i in range(n)]
h=0
for i in range(n):
    if s[i]=="0":
        zero[h]+=1
    else:
        h+=1
ans=0
print(zero)
zero.sort(reverse=True)
for i in range(k):
    ans+=zero[i]+1
print(ans)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
S = input()
last_s = "0"
count = 0
A = []
for i, s in enumerate(S):
	if i == 0:
		count += 1
	else:
		if s != last_s:
			A.append(count)
			count = 0
		count += 1
	last_s = s

if count != 0:
	A.append(count)

A.append(0)
A.append(0)

#print(A)

val = 0
if S[0] == "0":
	val = sum(A[:2*K])
	currentMax = val
	old = A[0]
	new = sum(A[2*K:2*K+2])
	val += new - old
	if val > currentMax:
		currentMax = val
	#print(old)
	#print(new)
	#print(val)
	
	for i in range(3, len(A)-2*K+2, 2):
		old_sum_of_two = sum(A[i:i+2])
		sum_of_two = sum(A[i+2*K:i+2*K+2])
		#print("i=", i)
		#print(old_sum_of_two)
		#print(sum_of_two)
		val += sum_of_two - old_sum_of_two
		#print(val)
		if val > currentMax:
			currentMax = val
	print(currentMax)
else:
	val = sum(A[:2*K+1])
	currentMax = val
	for i in range(0, len(A)-2*K+2, 2):
		old_sum_of_two = sum(A[i:i+2])
		sum_of_two = sum(A[i+2*K+1:i+2*K+3])
		val += sum_of_two - old_sum_of_two
		if val > currentMax:
			currentMax = val
	print(currentMax)

Submission status: Wrong Answer

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

sa = []

last = s[0]
nn = 1
for i in range(1,len(s)):
	if s[i] == last:
		nn += 1
	else:
		sa.append(nn)
		last = s[i]
		nn = 1
sa.append(nn)

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

#print(sa)

ans = -1

for i in range(len(sa)):
	maxa = sa[i]
	if i%2 == amari:
		kk = k+1
	else:
		kk = k+3
	for j in range(1, kk):
		if i+j < len(sa):
			maxa += sa[i+j]
	if maxa > ans:
		ans = maxa
print(ans)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
S = input()
last_s = "0"
count = 0
A = []
for i, s in enumerate(S):
	if i == 0:
		count += 1
	else:
		if s != last_s:
			A.append(count)
			count = 0
		count += 1
	last_s = s

if count != 0:
	A.append(count)

A.append(0)
A.append(0)

print(A)

val = 0
if S[0] == "0":
	val = sum(A[:2*K])
	currentMax = val
	old = A[0]
	new = sum(A[2*K:2*K+2])
	val += new - old
	if val > currentMax:
		currentMax = val
	print(old)
	print(new)
	print(val)
	
	for i in range(3, len(A)-2*K+2, 2):
		old_sum_of_two = sum(A[i:i+2])
		sum_of_two = sum(A[i+2*K:i+2*K+2])
		print("i=", i)
		print(old_sum_of_two)
		print(sum_of_two)
		val += sum_of_two - old_sum_of_two
		print(val)
		if val > currentMax:
			currentMax = val
	print(currentMax)
else:
	val = sum(A[:2*K+1])
	currentMax = val
	for i in range(0, len(A)-2*K+2, 2):
		old_sum_of_two = sum(A[i:i+2])
		sum_of_two = sum(A[i+2*K+1:i+2*K+3])
		val += sum_of_two - old_sum_of_two
		if val > currentMax:
			currentMax = val
	print(currentMax)

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Your code here!
n,k = map(int,input().split())

s = input()

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,len(h)-k,2):
    ans = max(ans,sum(h[i:i+k*2+1]))
    
print(ans)

Submission status: Wrong Answer

Solution code:
a, b = map(int, input().split())
print(max(a+b, a*2-1, b*2-1))

Submission status: Wrong Answer

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

x = '01'+x+'10'
N2 = N[1]*2

l = [i-3 for i in range(3,len(x)+4) if x[i-2:i] in ['01','10']]
l.append(N[0])
ll = [l[i+N2]-l[i] for i in range(len(l)-N2)]

if len(ll) == 0:
    print(l[len(l)-1])
else:
    print(max(ll))

Submission status: Wrong Answer

Solution code:
import re

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

store = []
start = -1
end = -1

if 1 == len(S):
    print(1)
else:
    for i in range(N):
        if '0' == S[i] and -1 == start:
            start = i

        if '1' == S[i] and -1 != start:
            end = i
        if '0' == S[i] and -1 != start and len(S)-1 == i:
            end = i+1
        if -1 != start and -1 != end:
            store.append([start, end, len(S[start:end])])
            start = end = -1

    store.sort(key=lambda x: x[2])
    store.reverse()

    li = list(S)
    for i in range(K):
        if len(store) == i:
            break
        start = store[i][0]
        end = store[i][1]
        count = store[i][2]
        li[start:end] = '1' * count

    count = 0
    temp = 0
    for i in range(N):
        if '1' == li[i]:
            temp += 1
        if '0' == li[i] or N-1 == i:
            count = max(temp, count)
            temp = 0
    print(count)

Submission status: Wrong Answer

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

l = list(map(int,input()))
ll = []
p = l[0]
co = 1
ppp = 0
su = 0
lll = []
for i in range(n-1):
    if p == l[i+1]:
        co += 1
    else:
        if p == 0:
            ppp += 1
        ll.append([p,co])
        p = l[i+1]
        co = 1
if p == 0:
    ppp += 1
ll.append([p,co])
co = 0

if len(ll) == 1:
    print(n)
    exit(0)
if ppp <= k:
    print(n)
    exit(0)
if ll[0][0] == 0:
    if ll[-1][0] == 0:
        for i in range(2*k):
            co += ll[i][1]
        lll.append(co)
        co -= ll[0][1]
        for i in range(ppp-k):
            if ppp-k-1 == i and i == 0:
                co += ll[-1][1]
                lll.append(co)
            elif ppp-k-1 == i:
                co += ll[-1][1] - (ll[i*2-1][1] + ll[i*2][1])
                lll.append(co)
            elif i == 0:
                co += ll[i*2+1+2*k][1] + ll[i*2+2*k][1]
                lll.append(co)
            else:
                co += ll[i*2+1+2*k][1] + ll[i*2+2*k][1] - (ll[i*2-1][1] + ll[i*2][1])
                lll.append(co)

    else:
        for i in range(2*k):
            co += ll[i][1]
        lll.append(co)
        co -= ll[0][1]
        for i in range(ppp-k):
            if i == 0:
                co += ll[i*2+1+2*k][1] + ll[i*2+2*k][1]
                lll.append(co)
            else:
                co += ll[i*2+1+2*k][1] + ll[i*2+2*k][1] - (ll[i*2-1][1] + ll[i*2+2][1])
                lll.append(co)
else:
    if ll[-1][0] == 1:
        for i in range(2*k+1):
            co += ll[i][1]
        lll.append(co)
        co -= ll[0][1] + ll[1][1]
        for i in range(ppp-k):
            if i == 0:
                co += ll[i*2+1+2*k][1] + ll[i*2+2+2*k][1]
                lll.append(co)
            else:
                co += ll[i*2+1+2*k][1] + ll[i*2+2+2*k][1] - (ll[i*2][1] + ll[i*2+1][1])
                lll.append(co)
    else:
        for i in range(2*k+1):
            co += ll[i][1]
        lll.append(co)
        co -= ll[0][1] + ll[1][1]
        for i in range(ppp-k):
            if ppp-k-1 == i and i == 0:
                co += ll[-1][1]
                lll.append(co)
            elif ppp-k-1 == i:
                co += ll[-1][1] - (ll[i*2][1] + ll[i*2+1][1])
                lll.append(co)
            elif i == 0:
                co += ll[i*2+1+2*k][1] + ll[i*2+2+2*k][1]
                lll.append(co)
            else:
                co += ll[i*2+1+2*k][1] + ll[i*2+2+2*k][1] - (ll[i*2][1] + ll[i*2+1][1])
                lll.append(co)
print(max(lll))

Submission status: Wrong Answer

Solution code:
N,K = (int(x) for x in(input().split()))
n=list(map(int,list(input())))
if n.count(0)<=K:
    print(N)
else:
    p=0
    q=0
    last=N
    for i in range(N-1):
        if p==min(K,n.count(0)):
            break
        elif n[i]==0 and n[i+1]==1:
            p=p+1
            q=i
        else:
            pass
    for j in range(q+1):
        n[j]=1
    for f in range(N):
        if n[f]!=1 :
            last=f
            break
        else:
            pass
    print(n[:last+1].count(1))

Submission status: Wrong Answer

Solution code:
import re
import numpy as np
N, K = [int(item) for item in re.split(r"\s", input())]
S = [int(item) for item in list(input())]

if sum(S) == len(S) or sum(S) == 0:
    print(len(S))
else:
    S = S + [2]

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

    count = np.diff(np.array(index)).tolist()

    if S[0] == 0:
        count = [0] + count

    # print(index)
    # print(count)

    cumsum = [0] + np.cumsum(count).tolist()
    c_len = len(cumsum)

    if S[0] == 0:
        win = min(2 * K + 1, c_len - 2)
    else:
        win = min(2 * K + 1, c_len - 1)
    # print(cumsum)
    # print(win)

    print(max([cumsum[i + win] - cumsum[i] for i in range(0, c_len - win, 2)]))

Submission status: Wrong Answer

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

x = '01'+x
N2 = N[1]*2

l = [i-3 for i in range(3,len(x)+4) if x[i-2:i] in ['01','10']]
l.append(N[0])
ll = [l[i+N2]-l[i] for i in range(len(l)-N2)]

if len(ll) == 0:
    print(l[len(l)-1])
else:
    print(max(ll))

Submission status: Wrong Answer

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

l = list(map(int,input()))
ll = []
p = l[0]
co = 1
ppp = 0
su = 0
lll = []
for i in range(n-1):
    if p == l[i+1]:
        co += 1
    else:
        if p == 0:
            ppp += 1
        ll.append([p,co])
        p = l[i+1]
        co = 1
if p == 0:
    ppp += 1
ll.append([p,co])
co = 0

if len(ll) == 1:
    print(n)
    exit(0)
if ppp <= k:
    print(n)
    exit(0)
if ll[0][0] == 0:
    if ll[-1][0] == 0:
        for i in range(2*k):
            co += ll[i][1]
        lll.append(co)
        co -= ll[0][1]
        for i in range(ppp-k):
            if ppp-k-1 == i and i == 0:
                co += ll[-1][1]
                lll.append(co)
            elif ppp-k-1 == i:
                co += ll[-1][1] - (ll[i*2-1][1] + ll[i*2][1])
                lll.append(co)
            elif i == 0:
                co += ll[i*2+1+2*k][1] + ll[i*2+2*k][1]
                lll.append(co)
            else:
                co += ll[i*2+1+2*k][1] + ll[i*2+2*k][1] - (ll[i*2-1][1] + ll[i*2][1])
                lll.append(co)

    else:
        for i in range(2*k):
            co += ll[i][1]
        lll.append(co)
        co -= ll[0][1]
        for i in range(ppp-k):
            if i == 0:
                co += ll[i*2+1+2*k][1] + ll[i*2+2*k][1]
                lll.append(co)
            else:
                co += ll[i*2+1+2*k][1] + ll[i*2+2*k][1] - (ll[i*2+1][1] + ll[i*2+2][1])
                lll.append(co)
else:
    if ll[-1][0] == 1:
        for i in range(2*k+1):
            co += ll[i][1]
        lll.append(co)
        co -= ll[0][1] + ll[1][1]
        for i in range(ppp-k):
            if i == 0:
                co += ll[i*2+1+2*k][1] + ll[i*2+2+2*k][1]
                lll.append(co)
            else:
                co += ll[i*2+1+2*k][1] + ll[i*2+2+2*k][1] - (ll[i*2][1] + ll[i*2+1][1])
                lll.append(co)
    else:
        for i in range(2*k+1):
            co += ll[i][1]
        lll.append(co)
        co -= ll[0][1] + ll[1][1]
        for i in range(ppp-k):
            if ppp-k-1 == i and i == 0:
                co += ll[-1][1]
                lll.append(co)
            elif ppp-k-1 == i:
                co += ll[-1][1] - (ll[i*2][1] + ll[i*2+1][1])
                lll.append(co)
            elif i == 0:
                co += ll[i*2+1+2*k][1] + ll[i*2+2+2*k][1]
                lll.append(co)
            else:
                co += ll[i*2+1+2*k][1] + ll[i*2+2+2*k][1] - (ll[i*2][1] + ll[i*2+1][1])
                lll.append(co)
print(max(lll))