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:
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":
        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

    ends.append(n - 1)

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


main()

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 - 1

    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:
            for i in range(times):
                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:
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":
        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

    ends.append(n - 1)
    print("starts: {}".format(starts))
    print("ends: {}".format(ends))

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


main()

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+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+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(i) for i in input().split())
S = list(str(input()))

L0 = []
L1 = []

pre = -1
now = -1
count = 1

for i in range(1,len(S)):
    pre = int(S[i-1])
    now = int(S[i])
    
    if pre != now:
        if pre == 1:
            L1.append(count)
        else:
            L0.append(count)
        count = 1
        continue
    
    count += 1
        
    
if now  == 1:
    L1.append(count)
else:
    L0.append(count)


if int(S[0]) == 0 and int(S[-1]) == 0:
    L1.append(0)
    L1.insert(0,0)

if int(S[0]) == 1 and int(S[-1]) == 0:
    L1.append(0)

if int(S[0]) == 0 and int(S[-1]) == 1:
    L1.insert(0,0)        


print(L1)
print(L0)


maxi = 0


for i in range(len(L1)-1):
    sum1 = sum(L1[i:i+K+1])
    sum0 = sum(L0[i:i+K])
    sum_ = sum0 + sum1
    print(sum_)
    if maxi < sum_ :
        maxi = sum_
        
if maxi == 0:
    maxi = 1
    
print(maxi)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
s = input()
c = [int(s[i]) for i in range(n)]
count = []
cost = 0
if c[0] == 0:
  cost += -1
else:
  cost += 1
for i in range(1,n):
  if c[i] == c[i-1]:
    cost += -1 if cost < 0 else 1
  else:
    count.append(cost)
    cost = -1 if cost > 0 else 1
count.append(cost)
i = 0
M = 0
l = 0
r = 0
for j in range(len(count)):
  if count[j] < 0:
    M += -count[j]
    i += 1
  else:
    M += count[j]
  if i == k:
    if j < len(count)-2:
      M += count[j+1]
      r = j + 1
    else:
      r = j
    break
tmp = M
if r < len(count) - 2:
  if count[0] < 0:
    tmp += count[0]
    l += 1
    r += 1
    tmp += -count[r]
    if r+1 < k:
      tmp += count[r + 1]
      r += 1
    M = max(M,tmp)
  while r + 1 < len(count) -1:
    tmp -= count[l]
    tmp += count[l+1]
    l += 2
    r += 1
    tmp += -count[r]
    if r+1 < len(count):
      tmp += count[r + 1]
      r += 1

    M = max(M,tmp)
print(M)

Submission status: Wrong Answer

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

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

number_list = []
num = 1

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

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


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

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

if max_num == 0:
    max_num = number_list[0]
if len(ruiseki) >= 5 and max_num < ruiseki[-1] - ruiseki[-5]:
    max_num = ruiseki[-1] - ruiseki[-5]

print (max_num)

Submission status: Wrong Answer

Solution code:
w = input().split()
N = int(w[0])
K = int(w[1])
S = input()
 
jk_p = []
joutai = S[0]
kaishi = 0
for i in range(len(S)-1):
    if joutai!=S[i+1]:
        w = [joutai, kaishi, i+1-kaishi]
        jk_p.append(w)
        joutai = S[i+1]
        kaishi = i+1
    if i == len(S)-2:
        w = [joutai, kaishi, len(S) - kaishi]
        jk_p.append(w)
 
 
max = 0
k = 0
for i in range(len(jk_p)):
    c = 0
    for j in range(K*2+2):
        if K*2+2 > len(jk_p)-1:
            break
        if jk_p[i][0] == '0':
            if j == K*2+1:
                break
        if i+j > len(jk_p)-1:
            break
        c += jk_p[i+j][2]
    if c > max:
        max = c
print(max)

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=0
    for i in range(N-1):
        if p==K:
            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-1):
        if n[f]==1 and n[f+1]==0:
            last=f
            break
        else:
            pass

    print(n[:last+1].count(1))

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
S = list(map(int, input()))
 
Z = [j for j, s in enumerate(S) if (j > 0 and s == 0 and S[j] != S[j-1]) or (j == 0 and s == 0)] + [N]
Y = [j for j, s in enumerate(S) if (j > 0 and s == 1 and S[j] != S[j-1]) or (j == 0 and s == 1)]
result = 0
if len(Z) <= K:
    print(N)
else:
    for i in range(len(Z)-K):
        if i == 0:
            result = Z[i+K]
        else:
            result = max(result, Z[i+K] - Y[i if len(Y) >= len(Z) else i-1])
    print(result)

Submission status: Wrong Answer

Solution code:
N,K=[int(i) for i in input().split()]
S=input()
cur=0
line=[]
for i in range(len(S)):
	if i==0:
		line.insert(0,1)
	else:
		if S[i-1]!=S[i]:
			line.append(1)
		else:
			line[-1]+=1
c=0
if S[0]=="0":
	for i in range(len(line)):
		if i==0:
			if 2*K<=len(line):
				c=sum(line[i:2*K])
			else:
				c=sum(line[i:])
		elif i==1:
			if i+2*K+1<=len(line):
				c=max(c,c-line[i-1]+line[i+2*K-1]+line[i+2*K])
			else:
				c=max(c,sum(line[i:]))
		
		elif i%2==1:
			if i+2*K+1<=len(line):
				c=max(c,c-line[i-2]-line[i-1]+line[i+2*K-1]+line[i+2*K])
			else:
				c=max(c,sum(line[i:]))
				break
else:
	for i in range(len(line)):
		if i==0:
			if 2*K<=len(line):
				c=sum(line[i:2*K])
			else:
				c=sum(line[i:])
		
		elif i%2==0:
			if i+2*K+1<=len(line):
				c=max(c,c-line[i-2]-line[i-1]+line[i+2*K-1]+line[i+2*K])
			else:
				c=max(c,sum(line[i:]))
				break
print(c)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
s = input()
c = [int(s[i]) for i in range(n)]
count = []
cost = 0
if c[0] == 0:
  cost += -1
else:
  cost += 1
for i in range(1,n):
  if c[i] == c[i-1]:
    cost += -1 if cost < 0 else 1
  else:
    count.append(cost)
    cost = -1 if cost > 0 else 1
count.append(cost)
i = 0
M = 0
l = 0
r = 0
for j in range(len(count)):
  if count[j] < 0:
    M += -count[j]
    i += 1
  else:
    M += count[j]
  if i == k:
    if j < len(count)-2:
      M += count[j+1]
      r = j + 1
    else:
      r = j
    break
tmp = M
if r < len(count) - 2:
  if count[0] < 0:
    tmp += count[0]
    l += 1
    r += 1
    tmp += -count[r]
    if r+1 < k:
      tmp += count[r + 1]
      r += 1
    M = max(M,tmp)
  while r + 1 < len(count) -1:
    tmp -= count[l]
    tmp += count[l+1]
    l += 2
    r += 1
    tmp += -count[r]
    if r+1 < len(count):
      tmp += count[r + 1]
      r += 1
    print("tmp: {}".format(tmp))
    M = max(M,tmp)
print(M)

Submission status: Wrong Answer

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

def Solve(S,K):

    if S.count("0")<=K:
        return len(S)
    if S[0]=="1":
        S="0"+S
    

    LA=[]#立ってる人 = 0
    LB=[]#逆立ちしてる人 = 1
    k=-1
    current="0"
    appendLA=LA.append
    appendLB=LB.append
    for i,s in enumerate(S):
        k+=1
        if s!=current:
            if s=="0":
                appendLA(k)#逆立ちの個数 = 1
                current="0"
                k=0
            else:
                appendLB(k)#立ってる人個数 = 0
                current="1"
                k=0
    
    if current=="1":
        appendLA(k+1)
    else:
        appendLB(k+1)
        appendLA(0)
    mx=0

    if K==0:
        return max(LA)

    sum_LA = list(numpy.cumsum(LA))
    sum_LB=list(numpy.cumsum(LB))
    

    for i in range(len(LA)-K+1):
        if i==0:
            result=sum_LA[K-1]+sum_LB[K-1]
        elif i==1:
            result=sum_LA[i+K-1]+sum_LB[i+K-1]-sum_LB[i-1]
        else:
            result=sum_LA[i+K-1]-sum_LA[i-2]+sum_LB[i+K-1]-sum_LB[i-1]
        mx=max(mx,result)

    return mx

print(Solve(S,K))

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:
            ans = max(ans,nn)
            break
    print(ans)

Submission status: Wrong Answer

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

        if j[2] > maxLen:
            maxLen = j[2]
print(maxLen)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
S = list(map(int, input()))
 
Z = [j for j, s in enumerate(S) if (j > 0 and s == 0 and S[j] != S[j-1]) or (j == 0 and s == 0)] + [N]
Y = [j for j, s in enumerate(S) if (j > 0 and s == 1 and S[j] != S[j-1]) or (j == 0 and s == 1)] + [N]
result = 0
if len(Z) <= K:
    print(N)
else:
    for i in range(len(Z)-K):
        if i == 0:
            result = Z[i+K]
        else:
            result = max(result, Z[i+K] - Y[i if len(Y) >= len(Z) else i-1])
    print(result)

Submission status: Wrong Answer

Solution code:
import itertools

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

s = input()

idx=0
instruction = []
while idx<n:
    if s[idx]=="0":
        start = idx
        while s[idx]=="0":
            if idx == n-1:
                idx = idx+1
                break
            else:
                idx = idx+1
        end = idx-1
        instruction.append((str(start),str(end)))
    else:
        idx=idx+1

inst_combi = list(itertools.combinations(instruction,k))

max_max_line = 0
for i in inst_combi:
    end_state = s
    for j in i:
        start = int(j[0])
        end = int(j[1])
        for x in range(start, end+1):
            end_state=end_state[:x] + '1' + end_state[x+1:]
    idx=0
    max_line = 0
    while idx<n:
        if end_state[idx]=="1":
            line = 0
            while end_state[idx]=="1":
                if idx == n-1:
                    idx = idx+1
                    line = line+1
                    break
                else:
                    idx = idx+1
                    line = line+1
            if line > max_line:
                max_line = line
        else:
            idx=idx+1
    if max_line > max_max_line:
        max_max_line = max_line
print(max_max_line)

Submission status: Wrong Answer

Solution code:
raw = input().split()
n = int(raw[0])
order = int(raw[1])
state = input()

#analyzer
stand = []
back = []
adder = 0
checker = "0"

for a in range(n):
  if state[a] == checker:
    adder += 1
  else:
    if checker == "0":
      stand.append(adder)
      adder = 1
      checker = "1"
    else:
      back.append(adder)
      adder = 1
      checker = "0"  
    if a == n - 1:
      if checker == "0":
        stand.append(adder)
      else:
        back.append(adder) 
back.append(0)
#print(stand)
#print(back)

#Detector 
if len(stand) <= order:
  print(n)
else:
  max_back = 0
  test_back = 0
  for b in range(len(stand) - order):
    test_back = 0
    for c in range(b, b + order):
      test_back = test_back + stand[c]+ back[c]
      #test_back = test_back + back[b]
      if max_back < test_back:
        max_back = test_back
  print(max_back)

Submission status: Wrong Answer

Solution code:
n,k = list(map(int,input().split()))
s = input()
c = [0]
INF = 10**9
ans = -INF

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:
w = input().split()
N = int(w[0])
K = int(w[1])
S = input()
 
jk_p = []
joutai = S[0]
kaishi = 0
for i in range(len(S)-1):
    if joutai!=S[i+1]:
        w = [joutai, kaishi, i+1-kaishi]
        jk_p.append(w)
        joutai = S[i+1]
        kaishi = i+1
    if i == len(S)-2:
        w = [joutai, kaishi, len(S) - kaishi]
        jk_p.append(w)
 
 
max = 0
k = 0
for i in range(len(jk_p)):
    c = 0
    for j in range(K*2+2):
        if K+3 > len(jk_p)-1:
            break
        if jk_p[i][0] == '0':
            if j == K+1:
                break
        if i+j > len(jk_p)-1:
            break
        c += jk_p[i+j][2]
    if c > max:
        max = c
print(max)

Submission status: Wrong Answer

Solution code:
# coding:utf-8

def reverse(mans,l,r):
	for i in range(l,r+1):
		if mans[i]=="0":
			mans[i] = "1"
		elif mans[i]=="1":
			mans[i] = "0"
	return mans

def getOptimumPosition(mans):
	count=0
	start_index = 0;
	end_index = 0;
	max_num = 100000
	flag = 0
	for i in range(len(mans)):
		if mans[i] == "0":
			count = count + 1
			
		elif mans[i] == "1":
			if max_num > count:
				start_index = i-count
				end_index = i-1
				max_num = count
				count = 0
	return start_index,end_index

def getResult(mans):
	count=0
	start_index = 0;
	end_index = 0;
	max_num = 100000
	flag = 0
	for i in range(len(mans)):
		if mans[i] == "1":
			count = count + 1
		elif mans[i] == "0":
			if max_num > count:
				start_index = i-count
				end_index = i-1
				max_num = count
				count = 0
	return end_index - start_index + 1

n, k = map(int, input().split())
mans =  list(input())
start_index = 0
end_index = 0
for count in range(k):
	start_index,end_index = getOptimumPosition(mans)
	mans = reverse(mans,start_index,end_index)
print(getResult(mans))

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
S = input()
a = []
if S[len(S) - 1] == "1":
    S += '0'
else:
    S += '1'
# print(S)
cur = 0
cntdif = 0
while cur != len(S) - 1:
    cntdif += 1
    if S[cur] != S[cur + 1]:
        a.append(cntdif)
        cntdif = 0
    cur += 1
# print(a)

countsum = 0
printmax = 0
m = 0
if k == 1:
    cnv = 2
else:
    cnv = 2 * k + 1
while 1:
    for i in range(cnv):
        if i + m > len(a) - 1:
            printmax = max(printmax, countsum)
            print(printmax)
            exit()
        countsum += a[i + m]
    printmax = max(printmax, countsum)
    countsum = 0
    m += 1

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 ll[0][0] == 1 and ll[-1][0] == 0:
    ll[0][0] = 0
    ll[-1][0] = 1
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+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+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)

print(max(lll))

Submission status: Wrong Answer

Solution code:
N,K = [int(i) for i in input().split()]
S = list(map (int, list(input())))
start = S[0]
row = []
state = [0,start]
for i in S:
    if i == start:
        state[0] += 1
    else:
        row.append(state)
        start = i
        state = [1,i]
row.append(state)
ans1 = 0
for i in range(max(len(row)-K*2,1)):
    if row[i][1] == 1:
        t = sum([row[i][0] for i in range(i,min(K*2+i+1,len(row)))])
        if t> ans1:
            ans1 = t
    else:
        t = sum([row[i][0] for i in range(i,min(K*2+i,len(row)))])
        if t > ans1:
            ans1 = t
print(ans1)

Submission status: Wrong Answer

Solution code:
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
            line[ind] = counter1
            counter1 = 0
            ind+=1
    elif ind%2 ==1:
        if c == 0:
            counter0 += 1
        else:
            counter1 += 1
            line[ind] = counter0
            counter0 = 0
            ind+=1

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

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

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
a=input()
l=len(a)
src=[int(a[i:i+1]) for i in range(l)]
from itertools import groupby
y =[sum(1 for e in it) for _, it in groupby(src, key=lambda x: x==1)]
m=len(y)
if m<=k+1:
  print(int(n))
else:
  c=0
  for i in range(m-2*k+1):
    d=y[i:i+2*k]
    f=sum(d)
    if f>=c:
      c=f
  print(c)

Submission status: Wrong Answer

Solution code:
import sys

# 文字列の読み込み&変換
N, K = input().split()

st = input()
fl = [ int(x) for x in st]

# 1と1の間の長さ
leng = []

bi = 0

ctr = 0

for i in range(int(N)):
    if fl[i] == 1:
        # 1が連続している場合を考える
        if (i-bi) != 1:
            leng.append(i-bi+1)
            bi = i

# 一番最後が0だとうまく数えることができないので
# 工夫する
if fl[-1] == 0:
    leng.append(len(fl)-bi)

max_sum = 0
for i in range(len(leng)-int(K)+1):

    num_sum = 0

    for j in range(int(K)):
        num_sum += leng[i+j]

    if num_sum >= max_sum:
        max_sum = num_sum

print(max_sum)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
S = list(map(int, input()))
 
Z = [j for j, s in enumerate(S) if (j > 0 and s == 0 and S[j] != S[j-1]) or (j == 0 and s == 0)] + [N]
Y = [j for j, s in enumerate(S) if (j > 0 and s == 1 and S[j] != S[j-1]) or (j == 0 and s == 1)] + [N]
result = 0
if len(Z) <= K:
    print(N)
else:
    for i in range(len(Z)-K):
        if i == 0:
            result = Z[i+K]
        else:
            result = max(result, Z[i+K] - Y[i if len(Y) > len(Z) else i-1])
    print(result)

Submission status: Wrong Answer

Solution code:
n,k=list(map(int,input().split()))
s=input()
a=[0]
b=[0]
if s[0]=="0":
    a.append(1)
for i in range(1,n):
    if s[i]=="0" and s[i-1]=="1":
        a.append(i+1)
a.append(n+1)
if len(a)-2>k:
    q=len(a)-k-1
    max=0
    for i in range(q):
        if max<a[k+1+i]-a[i]-1:
            max=a[k+1+i]-a[i]-1
else:max=n
print(max)

Submission status: Wrong Answer

Solution code:
import itertools

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

s = input()

idx=0
instruction = []
while idx<n:
    if s[idx]=="0":
        start = idx
        while s[idx]=="0":
            if idx == n-1:
                idx = idx+1
                break
            else:
                idx = idx+1
        end = idx-1
        instruction.append(str(start)+str(end))
    else:
        idx=idx+1

inst_combi = list(itertools.combinations(instruction,k))

max_max_line = 0
for i in inst_combi:
    end_state = s
    for j in i:
        start = int(j[0])
        end = int(j[1])
        for x in range(start, end+1):
            #end_state[x]=1
            end_state=end_state[:x] + '1' + end_state[x+1:]
    print(end_state)
    idx=0
    max_line = 0
    while idx<n:
        if end_state[idx]=="1":
            line = 0
            while end_state[idx]=="1":
                if idx == n-1:
                    idx = idx+1
                    line = line+1
                    break
                else:
                    idx = idx+1
                    line = line+1
            if line > max_line:
                max_line = line
        else:
            idx=idx+1
    print(max_line)
    if max_line > max_max_line:
        max_max_line = max_line
print(max_max_line)

Submission status: Wrong Answer

Solution code:
n,k=list(map(int,input().split()))
s=input()
a=[0]
b=[0]
if s[0]=="0":
    a.append(1)
for i in range(1,n):
    if s[i]=="0" and s[i-1]=="1":
        a.append(i+1)
a.append(n+1)
if len(a)-2>k:
    q=len(a)-k-1
    max=0
    for i in range(q):
        if max<a[k+1+i]-a[i]-1:
            max=a[k+1+i]-a[i]-1
else:max=n

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
if(not '0' in S):
    ans = len(S)
elif(not '1' in S):
    ans = len(S) 

print(ans)

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=0
    for i in range(N-1):
        if p==K:
            break
        elif n[i]==0 and n[i+1]==1:
            p=p+1
            q=i
        else:
            pass
    for j in range(q+1):
        if n[j]==0:
            n[j]=1
        else:
            pass
    for f in range(N-1):
        if n[f]==1 and n[f+1]==0:
            last=f
            break
        else:
            pass

    print(n[:last+1].count(1))

Submission status: Wrong Answer

Solution code:
n, k = list(map(int, input().split()))
s = input()
begin = s[0]
l = [begin,0]
cur = 1
for i in range(len(s)):
    if s[i] == begin:
        l[cur]+=1
    else:
        if begin == '0':
            begin = '1'
        else: begin = '0'
        l.append(begin)
        l.append(1)
        cur+=2
max = 0
for i in range(1,len(l),2):
    sumall = 0
    if l[i-1] == '0':
        if (i+k*2*2 <= len(l)+1):
            for j in range (k*2):
                sumall+=l[i+j*2]
            if sumall > max: max = sumall
    else:
        if (i+(k*2+1)*2 <= len(l)+1):
            for j in range (k*2+1):
                sumall+=l[i+j*2]
            if sumall > max: max = sumall
    print (i,max)