Problem:


Score : 400 points

Problem Statement

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

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

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

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

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

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

Constraints

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

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

The length of the string S is N.

Each character of the string S is 0 or 1.

Input

Input is given from Standard Input in the following format:

N K
S

Output

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

Sample Input 1

5 1
00010

Sample Output 1

4

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

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

Sample Input 2

14 2
11101010110011

Sample Output 2

8

Sample Input 3

1 1
1

Sample Output 3

1

No directions are necessary.



Sample input:
5 1
00010


Sample output:
4

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
s = 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)が
# 7 7//2 - k + 1   k = 3, 13  13//2 - 3 + 1  14  14//2 - 3 
# len(ss)//2 - k + 1 ただし偶数のラストは、[2*n-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]
# 8 8//2 - k  9 9//2 - k（ただし）   k=3  12 12//2 - k
# len(ss)//2 - k ただし奇数のラストは、[2*n ~ ラスト]
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())
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])+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:
import sys

def i2s():
    return sys.stdin.readline().rstrip()

def ii2ss(n):
    return [sys.stdin.readline() for _ in range(n)]

def sp2nn(sp, sep=' '):
    return [int(s) for s in sp.split(sep)]

def ss2nn(ss):
    return [int(s) for s in list(ss)]

def analyze(S):
    data = []
    i0 = 0
    s0 = S[0]
    sc = 0
    scmax = 0
    for i, s in enumerate(S):
        if s == s0:
            sc += 1
        else:
            data.append((i0, s0, sc))
            if s0 == '1' and sc > scmax:
                scmax = sc
            i0 = i
            s0 = s
            sc = 1
    data.append((i0, s0, sc))
    if s0 == '1' and sc > scmax:
        scmax = sc
    return data, scmax

def rev(S, l, r):
    r += 1
    s0 = S[0:l]
    s1 = S[l:r]
    s2 = S[r:]
    s1r = ''.join(['1' if s == '0' else '0' for s in s1])
    return s0 + s1r + s2

def main(ss):
    N, K = sp2nn(ss[0])
    S = ss[1]
    smax = -1
    
    for _ in range(K):
        data, smax = analyze(S)
        print(data, smax)
        il0 = -1
        ir0 = -1
        S_max = S
        for il in range(len(data)):
            for ir in range(len(data)):
                S_ = rev(S, data[il][0], data[ir][0]+data[ir][2])
                _, sc = analyze(S_)
                #print(S_, sc)
                if smax < sc:
                    il0 = il
                    ir0 = ir
                    smax = sc
                    S_max = S_
        if il0 == -1:
            break
        S = S_max
        #print(il0, ir0, S_max)
    
    print(smax)

main(ii2ss(2))

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
S = input()
S = "0" + S + "0"
lis = []
for i in range(1, N + 2):
  if S[i - 1] == "0" and S[i] == "1":
    lis.append([i])
  if S[i - 1] == "1" and S[i] == "0":
    lis[-1].append(i - 1)
leng = len(lis)
ans = 0
if S[1] == "1" and S[N] == "1":
  if K >= leng - 1:
    ans = N
  else:
    for i in range(leng - K):
      newan = lis[i + K][1] - lis[i][0] + 1
      ans = max(ans, newan)
  print(ans)
elif S[1] == "1" and S[N] == "0":
  if K >= leng:
    ans = N
  else:
    for i in range(leng - K):
      newan = lis[i + K][1] - lis[i][0] + 1
      ans = max(ans, newan)
  print(ans)
elif S[1] == "0" and S[N] == "1":
  if K >= leng:
    ans = N
  else:
    for i in range(leng - K):
      newan = lis[i + K][1] - lis[i][0] + 1
      ans = max(ans, newan)
    ans = max(ans, lis[K - 1][1])
  print(ans)
elif S[1] == "0" and S[N] == "0":
  if K - 1 >= leng:
    ans = N
  else:
    for i in range(leng - K):
      newan = lis[i + K][1] - lis[i][0] + 1
      ans = max(ans, newan)
    ans = max(ans, lis[K - 1][1])
    ans = max(ans, N - lis[leng - K][0] + 1)
  print(ans)

Submission status: Wrong Answer

Solution code:

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


for ik in range(k):
    before = 0
    before2 = 0
    maxv = 0
    index = 0
    for i in range(n):
        if s[i] - s[i - 1] == -1:
            if maxv < i + 1 - before:
                maxv = i + 1 - before
                index = i
            before2 = before
            before = i + 1

    if maxv < i + 1 - before2:
        maxv = i + 1 - before2
        index = i
    for i in range(maxv):
        s[index-i] = 1

print(maxv)

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 = ruiseki[0]

print (max_num)

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
S=input()
l=[]
count=[0,0] #count[0]が0,count[1]が1
if S[0]=="0":
    now=0
    a=0
else:
    now=1
    a=1
for i in range(len(S)):
    if int(S[i])==now:
        count[now]+=1
    else:
        if now==0:
            l+=[count[now]]
        else:
            l+=[count[now]]
        count[now]=0
        now=int(S[i])
        count[now]+=1
if now==0:
    l+=[count[now]]
else:
    l+=[count[now]]

mx=0
if a==1:
    if len(l)-2*K>0:
        for i in range(len(l)-2*K):
            if mx<sum(l[i:i+2*K+1]) and i%2==0:
                mx=sum(l[i:i+2*K+1])
    else:
        mx=sum(l)
else:
    if len(l)-2*K>0:
        for i in range(len(l)-2*K):
            if i==0:
                mx=sum(l[0:2*K])
            else:
                if mx<sum(l[i:i+2*K+1]) and i%2==1:
                    mx=sum(l[i:i+2*K+1]) 
    else:
        mx=sum(l) 
if len(l)==1 and a==1:
    print(l[0])
else:
    print(mx)

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+1:], 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+1:], 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+1:], 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+1:], d, d[K+1:]):
        ans = max(ans, y-x+t-u)
    print(ans)

Submission status: Wrong Answer

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

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

S_ren = []
ren = 0
for s in S:
    if s == '0':
        if ren > 0:
            S_ren.append(ren)
            ren = -1
        else:
            ren += -1
    else:
        if ren < 0:
            S_ren.append(ren)
            ren = 1
        else:
            ren += 1
S_ren.append(ren)

if S_ren[-1] < 0:
    S_ren.append(0)

if S_ren[0] < 0:
    S_ren = S_ren[::-1] 
    S_ren.append(0)
    
S_ren = [abs(sren) for sren in S_ren]

ans = 0
if K*2 > len(S_ren):
    K = len(S_ren)//2
for i in range(((len(S_ren)+1)//2)-K):
    print(i)
    a = sum(S_ren[i*2:(i+K)*2+1])
    if a > ans:
        ans = a

print(ans)

Submission status: Wrong Answer

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

is1 = False
firstIs1 = False

if S[0:1] == '1' :
    is1 = True
    firstIs1 = True

l0 = []
l1 = []
cnt = 1
isDeli = False

for i in range(0, len(S)) :
    if i == len(S) - 1 or S[i] != S[i+1] :
        if is1 == False :
            l0.append(cnt)
        else :
            l1.append(cnt)
        if i != len(S) - 1 :
            if S[i + 1] == '1' :
                is1 = True
            else :
                is1 = False
        cnt = 1
    else :
        cnt += 1

ans = 0

if len(l0) > 0 :
    for i in range(min(len(l0), len(l1)) - min(K, len(l0)) + 1) :
        sum = 0
        for j in range(i, i + min(K, len(l0))) :
            sum += l0[j] + l1[j]
        if i != 0 and firstIs1 == False:
            sum += l1[i - 1]
        if i != min(len(l0), len(l1)) - K - 1 and firstIs1 == True:
            sum += l1[i + 1]
        ans = max(ans, sum)

if min(len(l0), len(l1)) == 0 :
    if K > 0 or S == '1':
        ans = 1
    else :
        ans = 0


print(ans)




#print(l0)
#print(l1)

"""
for i in range(1, len(S)) :
    if isDeli = True :

        if S[i] == '0' :
            is1 = False
        else :
            is1 = True

        cnt = 1
    if S[i-1] == S[i] :
        cnt += 1
    else :
        if is1 == True :
            l1 =
"""

Submission status: Wrong Answer

Solution code:
import math
import functools
import itertools
import numpy as np
import sys
MAX_INT = int(10e10)
MIN_INT = -MAX_INT
mod = 1000000007
sys.setrecursionlimit(1000000)
def IL(): return list(map(int,input().split()))
def SL(): return input().split()
def I(): return int(sys.stdin.readline())
def S(): return input()

N,K = IL()
s = S()

l = []

if s.count("1")==N:
    print(N)
else:
    cnt = 1
    for i in range(N):
        if s[i] == "1":
            cnt += 1
        else:
            if i != N-1:
                if s[i+1]=="0":
                    l.append([cnt,0])
                else:
                    l.append([cnt,-1])
            else:
                l.append([cnt,-1])
            cnt = 1
    else:
        l.append([cnt,-1])
    tmp = 0
    ccnt = 0
    nn = 0
    ans = 0
    while True:
        for i in range(tmp,len(l)-1):
            if l[i][1]==-1:
                ccnt += 1
                if ccnt == 1:
                    tmp = i+1
            nn += l[i][0]
            if ccnt == K:
                nn += l[i+1][0] -1
                ans = max(ans,nn)
                nn = 0
                ccnt = 0
                break
        else:
            break
    print(ans)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
seq = [bool(int(c)) for c in input()]

ones = []
if seq[0] is False:
    ones.append(0)
state = True
cnt = 0
for i, c in enumerate(seq):
    if i == 0:
        state = c
        cnt = 1
        continue

    if c == state:
        cnt += 1
    else:
        ones.append(cnt)
        cnt = 1
        state = c

ones.append(cnt)
if state is False:
    ones.append(0)

# print(ones)

n_ones = len(ones)
if n_ones == 1:
    print(ones[0])
    exit(0)

k = min(k, n_ones)

cum = []
for i, num in enumerate(ones):
    if i == 0:
        cum.append(num)
        continue
    cum.append(num + cum[-1])

# print(cum)

max_score = 0
for i in range((n_ones - (2*k - 1)) // 2):
    score = (cum[2 * i + 2 * k] - cum[2 * i - 1]) if i > 0 else cum[2 * i + 2 * k]
    if max_score < score:
        max_score = score

print(max_score)

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(b)

print(max(b))

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

li = []
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:
    li.append(before)
    size.append(cnt)
    cnt = 1

li.append(now)
size.append(cnt)

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

max_sum = 0

if END:
  pass
else:
  for i in range(len(size)):
    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:]
    c = 1
  if s[i-1:i+1] == '11':
    c += 1
  if s[i-1:i+1] == '01':
    ss += [c]
    if len(ss) > tr:
      ss = ss[1:]
    c = 1
  if s[i] == '1':
    if len(tt) == tr:
      m = sum(ss) + sum(tt)
      if mm < m:
        mm = m
if s[n-1] == '0':
  ss += [c]
else:
  tt += [c]
  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 = 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):
            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):
            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 = str(input())

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

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
s=list(input())
st=[]
en=[]
if s[0]=="1":
    st.append(0)
for i in range(1,n):
    if s[i-1]=="0" and s[i]=="1":
        st.append(i)
    if s[i-1]=="1" and s[i]=="0":
        en.append(i)
if s[n-1]=="1":
    en.append(n)
ans=0
if len(en)+1<=k:
    print(n)
    exit()
if len(en)<=k and s[0]=="1":
    print(n)
    exit()
if len(en)<=k and s[n-1]=="1":
    print(n)
    exit()
if len(en)-1<=k and s[n-1]=="1" and s[0]=="1":
    print(n)
    exit()
    
if len(en)<=k:
    k=len(en)
for i in range(len(en)-k):
    ans=max(ans,en[i+k]-st[i])
if s[0]=="0":
    ans=max(ans,en[k-1])
if s[n-1]=="0":
    ans=max(ans,n-st[len(st)-k])
print(ans,st,en)

Submission status: Wrong Answer

Solution code:
from itertools import groupby

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

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

if num[0] == "1":
  x = K*2+1
else:
  x = K*2

ans = 0
for i in range(len(cot)-x+1):
  ans = max(ans,sum(cot[i:i+x]))
if ans == 0:
  ans = len(S)
print(ans)

Submission status: Wrong Answer

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

runlength_one = []
runlength_zero = []
counter = 0
prev = '1'
for s in S:
    if s == prev:
        counter += 1
    else:
        if s == '1':
            runlength_zero.append(counter)
        else:
            runlength_one.append(counter)
        counter = 1
        prev = s
if S[-1] == '1':
    runlength_one.append(counter)
else:
    runlength_zero.append(counter)


max = 0
for i in range(len(runlength_one)):
    if i + K + 1 > len(runlength_one):
        break
    length = sum(runlength_one[i:i+K+1]) + sum(runlength_zero[i:i+K])
    max = length if max < length else max

if max == 0:
    max = sum(runlength_one) + sum(runlength_zero)

print(str(max))

Submission status: Wrong Answer

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

C = []
c = 1
s = S[0]

for i in range(1, len(S)):
    if S[i] == S[i-1]:
        c += 1
    else:
        C.append((c, s))
        c = 1
        s = '1' if s == '0' else '0'
C.append((c, s))

ans = 0
for i in range(len(C)):
    flg = False
    count = 0
    if (len(C) - i) < (K // 2) - 1:
        ans = max(ans, sum(map(lambda x: x[0], C[i:])))
        break
    for j in range(len(C) - i):
        if C[i+j][1] == '0':
            count += 1
            if count > K:
                ans = max(ans, sum(map(lambda x: x[0], C[i: i+j])))
                flg = True
                break
    if not flg:
        ans = max(ans, sum(map(lambda x: x[0], C[i:])))
print(ans, len(S))

Submission status: Wrong Answer

Solution code:
import itertools

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


lr = []

i = 0
while True:
    if i >= N:
        break

    if S[i] == '0':
        j = i
        while True:
            if j >= N:
                lr.append([i, j-1])
                break
            if S[j] == '0':
                j += 1
            else:
                lr.append([i, j-1])
                i = j
                break
    i += 1

if len(lr) == 0:
    print(len(S))
    import sys
    sys.exit()

ans = 0

for orders in itertools.permutations(lr, r=K):
    listS = list(S)
    for v in orders:
        for i in range(v[0], v[1]+1):
            listS[i] = '1'
    # pprint(listS)
    # pprint("".join(listS))
    # pprint("".join(listS).split("0"))

    s = "".join(listS)
    a = s.split('0')
    lens = [len(x) for x in a]
    # pprint(max(lens))
    ans = max(ans, max(lens))

print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N, K = map(int, 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)

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
	for i in range(1, len(A)-2*K, 2):
		old_sum_of_two = sum(A[i:i+2])
		sum_of_two = sum(A[i+2*K:i+2*K+2])
		val += sum_of_two - old_sum_of_two
		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):
		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:
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 cnt == k + 1:                               
        total -= len(ar[j-2*k]) + len(ar[j-2*k-1]) 
    m = max(m, total)                              
    j += 1    
          
print m

Submission status: Wrong Answer

Solution code:
import itertools
 
_, k = map(int, input().split())
S = input()
blocks = [(key, len(list(v))) for key, v in itertools.groupby(S)]
res = 0
i = 0
if blocks[i][0] == '0':
  res = max(res, sum([b[1] for b in blocks[i:i+2*k]]))
  i += 1

crr = sum([b[1] for b in blocks[i:i+2*k+1]])
res = max(res, crr)
while i+2*k + 2 < len(blocks):
  i += 2
  crr = crr + blocks[i+2*k-1][1] + blocks[i+2*k][1]
  crr = crr - blocks[i-1][1] - blocks[i-2][1]
  res = max(res, crr)
print(res)

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

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
                    r += 1
                continue


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

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
S=input()
if S[-1]=="0":
    S+="1"
else:
    S+="0"
List=[]
ini=S[0]
now=0
zeronum=0
for i in range(N+1):
    if S[i]==ini:
        now+=1
        continue
    else:
        if ini=="1":
            List.append(now)
        else:
            List.append(now*(-1))
            zeronum+=1
        ini=S[i]
        now=1
if List[0]<0:
    List.insert(0,0)

sum=0
SUM=0

if zeronum<=K:
    print()
    quit()
N=len(List)
for i in range(2*K+1):
    if i%2==1:
        sum+=List[i]*(-1)
    else:
        sum+=List[i]
SUM=max(sum,SUM)

for i in range(6):
    List.append(0)
ind=0
for i in range(K+1,N//2+3):

    sum+=-List[ind]+List[ind+1]-List[2*i-1]+List[2*i]
    ind+=2
    SUM=max(sum,SUM)
print(SUM)