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=list(str(raw_input()))

l=[0]
if s[0]=="0":
	l.append(1)
else:
	pass

for i in range(1,n):
	if s[i-1]=="1" and s[i]=="0":
		l.append(i+1)
	else:
		pass
l.append(n+1)
ans=[]
m=len(l)
if k>=m:
	print n
else:
	for j in range(1,m-k):
		ans.append(l[j+k]-l[j-1]-1)
	if len(ans)==0:
		print n
	else:
		print max(ans)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
s = input()
l = r = 0
a = []
while l < n:
    while r < n and s[l] == s[r]:
        r += 1
    a.append(r-l)
    l = r
m = len(a)
asum = [0 for _ in range(m+1)]
ans = 0
for i in range(m):
    asum[i+1] = asum[i] + a[i]
if s[0] == '0':
    if k*2 >= m:
        ans = n
    else:
        if s[-1] == '0':
            a.append(0)
            asum.append(asum[-1])
        for i in range(1, m-2*k):
            if i % 2 == 0:
                continue
            ans = max(ans, asum[2*k+i+1] - asum[i])
        ans = max(ans, asum[2*k])
else:
    if k*2 >= m:
        ans = n
    else:
        if s[-1] == '0':
            a.append(0)
            asum.append(asum[-1])
        for i in range(0, m-2*k):
            if i % 2 == 1:
                continue
            ans = max(ans, asum[2*k+i+1] - asum[i])
print(ans)

Submission status: Wrong Answer

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

s = list(str(input()))

first_index=0
last_index=0

res = 0
for f in range (first_index, n):
  print("f: " + str(f))
  for l in range (last_index, n):
    print("  l: " + str(l))
    if f > l:
      continue
    check = 0
    for i in range(f, l):
      if s[i] == "0":
        check += 1
    if check <= k:
      if l-f > res:
        res = l-f +1

print(res)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
from string import ascii_uppercase
import sys, bisect, math
stdin = sys.stdin
read_int = lambda : list(map(int,stdin.readline().split()))
read_str = lambda : stdin.readline().rstrip()

N, K = read_int()
S = read_str()

chunks = []
zero = []
def solve():
    ans = 0

    dif = False
    ren = 1
    prev = ''
    for i in range(N):
        if i == N-1:
            chunks.append(ren)
        elif dif and S[i] != prev:
            chunks.append(ren)
            dif = False
            ren = 1

        elif S[i] != prev:
            dif = True
            ren += 1
        else:
            ren += 1
        prev = S[i]
    if len(chunks) > 2:
        p = chunks.pop()
        chunks[len(chunks)-1] += p

    index = 0
    while index + K <= len(chunks):
        if len(chunks) <= K:
            ans = sum(chunks)
            break
        ans = max(sum(chunks[index:index+K]), ans)
        index += K

    return ans

if __name__ == "__main__":
    print(solve())

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) > 0:

    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:
# D
n, k = map(int, input().split())
s = str(input())
r = 0
l = 0
max_len = 0
count = k
ans = 0
if all([int(i) == 1 for i in list(s)]):
    ans = len(s)
else:
    while r < n:
        if r == n-1:
            break
        while count > 0:
            if r == n-1:
                break
            if int(s[r]) == 1:
                r += 1
                continue
            else:
                count -= 1
                while int(s[r]) == 0:
                    if r == n-1:
                        break
                    r += 1
                while int(s[r]) == 1:
                    if r == n-1:
                        break
                    if r == n-2 and int(s[n-1]) == 0:
                        break
                    if int(s[r+1]) == 0:
                        break
                    r += 1
                continue
        print(l,r)
        ans = max(ans, r - l + 1)
        while int(s[l]) == 1:
            if l == n-1:
                break
            l += 1
        while int(s[l]) == 0:
            if l == n-1:
                break
            l += 1
        count += 1
print(ans)

Submission status: Wrong Answer

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

count = []
c = 0
now_one = True if S[0] == "1" else False

for i, s in enumerate(S):
    if s == "1" and now_one:
        c += 1
    elif s == "0" and now_one:
        count.append(c)
        c = 1
        now_one ^= True
    elif s == "0" and not now_one:
        c += 1
    elif s == "1" and not now_one:
        count.append(-c)
        c = 1
        now_one ^= True
    if i == len(S) - 1:
        if S[i] == "1":
            count.append(c)
        else:
            count.append(-c)

r = 0
res = 0
for i, x in enumerate(count):
    if x > 0:
        res += abs(count[i])
    elif x < 0:
        res += abs(count[i])
        K -= 1
        if K == 0 and i + 1 < len(count):
            res += abs(count[i + 1])
            r = i + 1
            break
        elif K == 0:
            r = i
            break

left = 0
tmp = res
if count[0] > 0 and r + 1 < len(count):
    for right in range(r + 1, len(count)):
        if left == right:
            break
        if count[right] < 0:
            tmp += abs(count[right])
            tmp -= abs(count[left])
            tmp -= abs(count[left + 1])
            left += 2
        elif count[right] > 0:
            tmp += abs(count[right])
        res = max(res, tmp)
elif r + 1 < len(count):
    tmp += abs(count[r + 1])
    tmp -= abs(count[left])
    res = max(res, tmp)
    for right in range(r + 2, len(count)):
        if count[right] < 0:
            tmp += abs(count[right])
            tmp -= abs(count[left])
            tmp -= abs(count[left + 1])
            left += 2
        elif count[right] > 0:
            tmp += abs(count[right])
        res = max(res, tmp)
print(res)

Submission status: Wrong Answer

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

zeros = []
num_zeros = 0
start = 0
cont = False
for i in range(N):
    s = int(S[i])
    if s == 0 and not cont:
        start = i
        cont = True
        num_zeros += 1
        zeros.append([start,1])
        continue

    elif s == 0 and cont:
        zeros[num_zeros-1][1] += 1
        continue

    elif s == 1:
        cont = False

print(zeros)

max_length = 0
search_num = len(zeros) - K + 1
# if search_num == 0:
#     if S[0] == "1":
#         max_length = 1
# else:
#     for i in range(search_num):
#         S_ = S
#         for k in range(K):
#             zero = zeros[i+k]
#             S_ = S_[:zero[0]] + '1' * zero[1] + S_[zero[0]+zero[1]:]

#         sub_max_length = 0
#         sub_sub_max_length = 0
#         for j in range(len(S_)):
#             s = int(S_[j])
#             if s == 1:
#                 sub_sub_max_length += 1
#             elif s == 0:
#                 if sub_max_length < sub_sub_max_length:
#                     sub_max_length = sub_sub_max_length
#                 sub_sub_max_length = 0
#             if j == len(S_)-1:
#                 if sub_max_length < sub_sub_max_length:
#                     sub_max_length = sub_sub_max_length

#         if max_length < sub_max_length:
#             max_length = sub_max_length
print(max_length)

Submission status: Wrong Answer

Solution code:
#https://atcoder.jp/contests/abc124/tasks/abc124_d


NK = [int(i) for i in input().split()]
S = str(input())

N = NK[0]
K = NK[1]
Sarray = list(S)

for i in range(len(Sarray)):
    Sarray[i] = int(Sarray[i])
    if Sarray[i] == 0:
        Sarray[i] = -1

mode = Sarray[0]
a = [0]

for i in range(len(Sarray)):
    c = (len(a) - 1) % 2
    b = (-1)** c * mode
    if Sarray[i] != b:
        a.append(0)
    a[len(a) - 1] += 1

#print(mode,a)

#aはmodeを先頭に同じのが何個続くか

adjust = 0
if mode == 1:
    adjust = 1

tmp = 0
count = 0

for l in range(adjust,len(a) + 1,2):
    for i in range(l, l + 2 * K - 1):
        u = l
        v = l + 2 * K - 2
        if l > 0:
            u = l - 1
        if l + 2 * K - 1 < len(a):
            v = l + 2 * K - 1
        if max(a[u:v]) * (2 * K + 1) < count:
            break
            
        if i < len(a):
            tmp += a[i]
        else:
            break
    
    if l > 0:
        #print('under+')
        tmp += a[l - 1]
    if l + 2 * K - 1 < len(a):
        #print('over+')
        tmp += a[l + 2 * K - 1]
    #print(tmp)

    if count < tmp:
        count = tmp
    tmp = 0

print(count)

Submission status: Wrong Answer

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

s2 = s.replace("01","0,1").replace("10","1,0").split(",")

if s[0]== "1":
  patterns = list(map(len, s2))
else:
  patterns = [0] + list(map(len, s2))

max_len = 1



#偶数番目が１の人たち
if len(patterns) <= 2 * k:
  print(len(s))
else:
  lengths = [0] * (len(patterns)+1)
  for i in range(2*k+1 , len(patterns) + 1, 2):
    lengths[i] = sum(patterns[i-2*k-1 : i])
    
  print(max(lengths))

Submission status: Wrong Answer

Solution code:
import math, string, itertools, fractions, heapq, collections, re,  array, bisect, sys, random, time, copy, functools
sys.setrecursionlimit(10**7)
inf = 10 ** 20
eps = 1.0 / 10**10
mod = 10**9+7
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]
ddn = [(-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1)]
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def _I(): return int(sys.stdin.readline())
def _F(): return float(sys.stdin.readline())
def pf(s): return print(s, flush=True)

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

"""
逆立ちを連続で何人
まず1を反転させることはないだろう
どの0を選ぶかという問題になる
仮に0が010101...と10**5/2個あったとすると、K個選ぶのを愚直にやると間に合わない
1が一番つながってるところは使いたいみたいな？
もしくは0が一番つながってるところは使いたい？
dpやるなら、何回操作してて、何番目までやってるか
メモ化再起でやりたいい
memo.get('i箇所目でk回反転したとき最長がL')

ある0の場所を反転させる時、そこから離れたところは反転させる必要がないことに着目
計算を無駄にしないようにメモする？

K回指示する場合
K回以下指示はありえない
"""
positions = []
idx = 0
keep = None
one_keep = None
ones = []
while idx < N:
    if S[idx] == '0':
        if keep == None:
            keep = idx
        if one_keep != None:
            ones.append(idx - one_keep)
            one_keep = None
    else:
        if one_keep == None:
            one_keep = idx
        else:
            pass

        # 1の場合は無視
        if keep != None:
            positions.append([keep, idx])
            keep = None
        else:
            # keepしない場合
            keep = None
    idx += 1
else:
    if keep != None:
        positions.append([keep, keep+1])
#  print(positions)
if len(positions) == 0:
    print(N)
    exit()

#もともと右のほうが長い可能性もあり
if len(ones) == 0:
    # 全部0
    print(N)
    exit()
ans = max(ones)

left = 0
for idx in range(len(positions)):
    left = positions[idx][0]
    if left != 0:
        minus = 1
        while True:
            if left - minus < 0:
                #  minus -= 1
                minus -= 1
                break
            if S[left - minus] == '1':
                # 1ならまだ伸ばせる
                minus += 1
            else:
                # 0の場合はもう終わり
                minus -= 1
                break
        left -= minus
    # positionから右にk個反転させる
    if idx + K -1 >= len(positions):
        # もうみなくていい
        break
    # endはplus1idxになってる
    end = positions[idx + K-1][1]
    if end != N:
        a = 0
        while True:
            #  print(end, a)
            if end + a >= N:
                break
            if S[end + a] == '1':
                a += 1
            else:
                break
        end += a

    ans = max(ans, end - left)
print(ans)

Submission status: Wrong Answer

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

# 0or1がいくつずつ連続しているかをリスト化する
ss = []
num = s[0]
cnt = 1

for i in range(1, len(s)):
  if num == s[i]:
    cnt += 1
  else:
    ss.append(cnt)
    num = s[i]
    cnt = 1
ss.append(cnt)

result = 0
# 0始まりでk=2の場合、[0~3],[1~5],[3~7],[5~9],,,[2*n-1 ~ (n+k)*2-1] ただしn=0のとき0はじまり
# nの最大値は？
# len(ss)//2 - k + 1
if s[0] == '0':
  if len(ss) % 2 == 1:
    if result < sum(ss[0: (k)*2]):
      result = sum(ss[0: (k)*2])
    
    for i in range(1, len(ss)//2 - k + 1):
      if result < sum(ss[2*i-1: (i+k)*2]):
        result = sum(ss[2*i-1: (i+k)*2])
        
    if result < sum(ss[2* (len(ss)//2-k+1) -1: ]):
      result = sum(ss[2* (len(ss)//2-k+1) -1: ])
        
  else:
    for i in range(len(ss)//2 - k):
      if result < sum(ss[2*i-1: (i+k)*2]):
        result = sum(ss[2*i-1: (i+k)*2])
        
    if result < sum(ss[2* (len(ss)//2-k) -1: ]):
      result = sum(ss[2* (len(ss)//2-k) -1: ])

# 1始まりでk=2の場合、[0~4],[2~6],[4~8],[6~10],,,[2*n ~ (n+k)*2]
# len(ss)//2 - k
else:
  if len(ss) % 2 == 0:
    for i in range(len(ss) // 2 - k):
      if result < sum(ss[2*i: (i+k)*2+1]):
        result = sum(ss[2*i: (i+k)*2+1])
    
    if result < sum(ss[2* (len(ss)//2-k): ]):
      result = sum(ss[2* (len(ss)//2-k): ])
        
  else:
    for i in range(len(ss) // 2 - k):
      if result < sum(ss[2*i: (i+k)*2+1]):
        result = sum(ss[2*i: (i+k)*2+1])

    if result < sum(ss[2* (len(ss)//2-k): ]):
      result = sum(ss[2* (len(ss)//2-k): ])

print(result)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
people = list(input())
lst = []
count = 0
flag = people[0]
if flag == '1':
	lst.append(0)
for i in range(len(people)):
	if i == len(people) - 1:
		lst.append(count + 1)
	elif people[i] == flag and people[i+1] != flag:
		lst.append(count + 1)
		count = 0
		flag = people[i + 1]
	else:
		count += 1

maximum = 0
l = len(lst)
temp = sum(lst[0:2*K])
maximum = max(maximum, temp)
for i in range(2, l, 2):
	end = i+2*K
	if end > l:
		break
	maximum = max(maximum, temp - lst[i-2] + sum(lst[end-2:end]))
print(maximum)

Submission status: Wrong Answer

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

DP = [0]
ans = 0
l = 0

for index,x in enumerate(S):
    if(index==0):
        if(x=='0'):
            DP.append(0)
            DP.append(1)
        else:
            DP.append(1)
    else:
        if(S[index-1]!=x):
            DP.append(DP[-1]+1)
        else:
            DP[-1]+=1
    if(index==N-1):
        if(x=='1'):
            DP.append(DP[-1])
        DP.append(DP[-1])

while(1+2*K+l<len(DP)):
    ans = max(DP[2*K+1+l]-DP[l],ans)
    l+=2

print(ans)

Submission status: Wrong Answer

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

size = []

now = S[0]
cnt = 1

END = False

for i in range(1,N,1):
  before = now
  now = S[i]
  if now == before:
    cnt += 1
  else:
    size.append(cnt)
    cnt = 1

size.append(cnt)

if 3*K > len(size):
  print(sum(size))
  END = True

max_sum = 0

if  S[0] == '0':
  start = 1
else:
  start = 0
  
if END:
  pass
else:
  for i in range(start,len(size),2):
    try:
      now_sum = sum(size[i:i+3*K])
      if max_sum < now_sum:
          max_sum = now_sum
    except:
      pass
  print(max_sum)

Submission status: Wrong Answer

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

sr = k
tr = k + 1

ss = []
tt = []

if s[0] == '0':
  tt += [0]
  
mm = 0

c = 1
for i in range(1, n):
  if s[i-1:i+1] == '00':
    c += 1
  if s[i-1:i+1] == '10':
    tt += [c]
    if len(tt) > tr:
      tt = tt[1:]
    if len(tt) == tr:
      m = sum(ss) + sum(tt)
      if mm < m:
        mm = m
    c = 1
  if s[i-1:i+1] == '11':
    c += 1
  if s[i-1:i+1] == '01':
    ss += [c]
    if len(ss) > sr:
      ss = ss[1:]
    c = 1
if s[n-1] == '0':
  ss += [c]
else:
  tt += [c]
  if len(tt) > tr:
    tt = tt[1:]
  if len(tt) == tr:
    m = sum(ss) + sum(tt)
    if mm < m:
      mm = m
print(mm)

Submission status: Wrong Answer

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

flag = True
check = 0
if s[0] == '0':
    flag = True
    check = 2*k
else:
    flag = False
    check = 2*k+1

c = 0
a = list()
for i in s:
    if i == '0' and flag:
        c += 1
    elif i == '1' and not flag:
        c += 1
    else:
        flag = not flag
        a.append(c)
        c = 1
a.append(c)
init = 0
b=list()
if (init+check-1) >= len(a):
    b.append(sum(a[0:]))
while (init+check-1) < len(a):
    aaa = a[init:check+init]
    b.append(sum(aaa))
    init += 2

print(max(b))

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:
# -*- coding: utf-8 -*-
"""
Created on Sat Apr 13 21:27:24 2019
ABC124D
@author: maezawa
"""

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

max1 = 0
ki = 0
ser1s = 0
inv01 = []
i = 0
while i < n:
    #print(i, max1, ki, ser1s)
    #print(inv01)
    if s[i] == 1:
        ser1s += 1
        i += 1
        continue
    else:
        if ki+1 <= k:
            ki += 1
            ser1s += 1
            for j in range(i+1,n):
                if s[j] == 0:
                    ser1s += 1
                else:
                    i = j-1
                    break
            inv01.append(i)
        else:
            if max1 < ser1s:
                max1 = ser1s
            for j in range(i+1,n):
                if s[j] == 0:
                    ser1s += 1
                else:
                    i = j-1
                    break
            inv01.append(i)
            rm = inv01.pop(0)
            ser1s = i - rm
            #ki -= 1
    i += 1
if max1 < ser1s:
    max1 = ser1s
print(max1)

Submission status: Wrong Answer

Solution code:
times = input().split()
N = int(times[0])
K = int(times[1])
S = input()


def one_counter(S):
    tmp = 0
    high = 0
    for z in S:
        if z == "1":
            tmp += 1
        else:
            if tmp > high:
                high = tmp
                tmp = 0
            else:
                tmp = 0
    if tmp > high:
        high = tmp
    return high


last_score = 0
for i in range(K):
    if S.find("0") == -1:
        last_score = len(S)
        break
    zero_counter = []
    start_index = -1
    end_index = -1
    diff = 0
    for j, value in enumerate(S):
        if value == "0" and start_index == -1:
            start_index = j
        elif value == "1" and start_index != -1:
            end_index = j
            diff = end_index - start_index
            zero_counter.append([start_index, end_index, diff])
            start_index = -1
            end_index = -1
            diff = 0
    high_score = one_counter(S)
    high_S = S
    for k in zero_counter:
        S_f = S[:k[0]]
        S_b = S[k[1]:]
        ones = "1"*k[2]
        tmp_S = S_f+ones+S_b
        score = one_counter(tmp_S)
        if score > high_score:
            high_S = tmp_S
            last_score = score
    S = high_S


print(last_score)

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 = 0
# 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] = ss[max_i - 1] + [1 for _ in range(len(ss[max_i]))]
                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()
ls = []
cnt=0
for i in range(1,N,1):
    if S[i] == S[i-1]:
        cnt+=1
    else:
        ls.append([S[i-1],cnt+1])
        cnt=0
    if i == N-1:
        ls.append([S[i],cnt+1])
nu = []
if K // 2 == 0:
    st = K*2
else:
    st = K*2+1
for j in range(len(ls)):
    sum = 0
    for p in range(st):
        if p + j < len(ls):
            sum += ls[p+j][1]
        else:
            break
    nu.append(sum)
if nu == []:
    print(1)
else:
    print(max(nu))

Submission status: Wrong Answer

Solution code:
import sys
n,k = list(map(int, input().split()))
s = input()
s = s[:n]
un = False
ww = [w for w in range(n)]
ww.reverse()


if k == 1:
	un = True
	
if un:
	for _ in ww:
		a = "0" * _
		if a in s and a != "0":
			
			un = True
			s = s.replace(a,"1"*_,1)
				
	
else:
	
	for gaha in range(1,k):
		for _ in ww:
			a = "0" * _
			if a in s and a != "0":
				un = True
				s = s.replace(a,"1"*_,1)
					
		un = False
		

ww = [w for w in range(n)]
ww.reverse()

			
for da in ww:
	r = "1" * (int(da) + 1)
	if r in s:
		print(len(r))
		sys.exit()

Submission status: Wrong Answer

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

if S[0] == '0':
    cnt0, cnt1 = 1, 0
else:
    cnt0, cnt1 = 0, 1

for i in range(N):
    if i == 0:
        continue
    elif S[i] == '0':
        if S[i] == S[i-1]:
            cnt0 += 1
        else:
            lt1.append(cnt1)
            cnt0 = 1
            cnt1 = 0
    else:
        if S[i] == S[i-1]:
            cnt1 += 1
        else:
            lt0.append(cnt0)
            cnt0 = 0
            cnt1 = 1
if S[N-1] == '0':
    lt0.append(cnt0)
elif S[N-1] == '1':
    lt1.append(cnt1)

n = len(lt0)
mxcnt = 0
flg = int(S[0])
if n <= K:
    mxcnt = N
else:
    for i in range(n-K+1):
        cnt = sum(lt0[i:i+K]) + sum(lt1[i:i+K+flg])
        mxcnt = max(mxcnt, cnt)
print(mxcnt)

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)	


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:
import numpy as np

#input N K S
input_NK = input().split()
input_S = input()
N = int(input_NK[0])
K = int(input_NK[1])
S = list(input_S)

max = 0

for l in range(N):
        for r in range(l,N):
            #reverse from l to r -> count 1_list
            #reverse 0 1
            for i in range(l,r):
                if S[i] == '0':
                    S[i] = '1'
                elif S[i] == '1':
                    S[i] = '0'
            #count number 1_list
            max_list1 = 0
            for i in range(N):
                if S[i] == '0':
                    continue
                for j in range(i, N):
                    if S[j] == '0':
                        num_list1 = j - i
                        if num_list1 > max_list1:
                            max_list1 = num_list1
                        continue
            if max_list1 > max:
                max = max_list1
print(int(max))

Submission status: Wrong Answer

Solution code:
import sys
n,k = map(int,input().split())
st = input()
s = []
for i in st:
    s.append(i)
s.append('-1')
man = 1 if s[0] == '0' else 0

check_ls = [];check_num = 0;tyokuritu = []
for i in range(len(s)):
    check_num += 1
    if s[i] != s[i+1]:
        check_ls.append(check_num)
        check_num = 0
        if s[i] == '0':
            tyokuritu.append(True)
    if s[i+1] == '-1':break
if k >= len(tyokuritu):print(n)
else:
    ios_check = [0]+[check_ls[0]]*(len(check_ls))
    for i in range(len(check_ls)-1):
        ios_check[i+1+1] = check_ls[i+1] + ios_check[i+1]
    ans = 0
    for i in range(0,10**5,2):
        if (k*2+1+i+man) > (len(ios_check)-1):break
        ans = max(ans,ios_check[k*2+1+i+man] - ios_check[i+man])
    if man == 1:ans = max(ans,ios_check[k*2])
    print(ans)

Submission status: Wrong Answer

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

if s[0] != "0":
  s = "0" + s
if s[-1] != "0":
  s =  s + "0"

l = []
count = 1
for i in range(len(s)-1):
  if s[i] == s[i+1]:
    count += 1
  else:
    l.append(count)
    count = 1

ans = 0

for i in range(len(l)):
  total = 0
  try:
    for j in range(k):
      if j == 0:
        total += l[2*j+i-1]  + l[2*j+i] + l[2*j+i+1]
      else:
        total += l[2*j+i] + l[2*j+i+1]
  except:
    pass
  ans = max(ans,total)


print(ans)

Submission status: Wrong Answer

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

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

def array_count(arr):
    arr_target = [[]]
    cnt = 1
    for i in range(1,len(arr)):
        if arr[i] == arr[i-1]:
            cnt += 1
        else:
            arr_target.append([arr[i-1],cnt])
            cnt = 1
    arr_target.append([arr[len(arr)-1],cnt])
    del arr_target[0]
    return arr_target

ss = array_count(s)

if ss[0][0] == 0:
    ss = [[1,0]] + ss

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

ss_sum = []
temp = 0
for i in range(len(ss)):
    temp += ss[i][1]
    ss_sum.append(temp)

ans = -1
for i in range(len(ss_sum)):
    temp = 0
    if i % 2 == 0:
        if i + k * 2 < len(ss_sum):
            temp = ss_sum[i+k*2] - ss_sum[i]
    else:
        if i + k * 2 + 1 < len(ss_sum):
            temp = ss_sum[i+k*2+1] -  ss_sum[i]
    ans = max(temp,ans)

if k * 2 >= len(ss):
    ans = ss_sum[-1]

print(ans)

Submission status: Wrong Answer

Solution code:
def main():
    n, k = map(int, input().split())
    s = input()
    if s.count("0") == 0:
        print(n)
        return
    first = s[0]
    starts = [0]
    ends = []
    if first == "0" or s[0] == s[1]:
        ends.append(0)
    prev = first
    for i, c in enumerate(s[1:]):
        if c != prev:
            if prev == "0":
                starts.append(i + 1)
            else:
                ends.append(i)
        prev = c

    if ends[-1] != n - 1:
        ends.append(n - 1)

    m = 0
    l = len(ends)
    for i in range(l - k - 1):
        v = ends[i + k + 1] - starts[i]
        if v > m:
            m = v
    print(m + 1)


main()

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:]):
            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:]):
            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+1:], 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:
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
  # print(S)
  # print(num, n)
  for i in range(0, len(S) - num, 2):
    # print(S[i], S[i + 1], S[i + num - 1:2])
    n = n - sum(S[i:i+2])
    n = n + sum(S[i + num:i + num + 2])
    m = max(m, n)

  return m

print(solve())