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:
import copy

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

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

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


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

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


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

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
S = input()
SL = [int(i) for i in S]
keepcount = "A"
count = 0
ans = [0]
SL.append("B")
for i in SL:
    if keepcount == "A":
        keepcount = i
        count = 1
    elif keepcount == i:
        count += 1
        keepcount = i
    else:
        ans.append(count)
        count = 1
        keepcount = i


LA = len(ans)
ANS = []
value = 0
if K == 1:
    for i in range(LA-(2*K)):
        for j in range(i,i+2*K):
            value += ans[j]
        ANS.append(value)
        value = 0
else:
    for i in range(LA-(2*K)):
        for j in range(i,i+2*K+1):
            value += ans[j]
        ANS.append(value)
        value = 0

if len(ANS) != 0:
    print(max(ANS))
else:
    if S == "1":
        print(1)
    else:
        print(0)

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
S=input()
flag = 0
a=[0]*N
if S[0]=='0':
  a[0]=1
  flag = 1

for i in range(1,N):
  if S[i]=='0':
    if not flag:
      a[i]=1
    flag = 1
  else:
    flag = 0
  a[i]+=a[i-1]

r=0
ret=0
count=0
for l in range(N):
  while 1:
    if r==N:
      break
    if l==0:
      count = a[r]
    else:
      count = a[r]-a[l-1]
    if count <=K:
      ret=max(ret,r-l+1)
      r+=1
    else:
      break
print(ret)

Submission status: Wrong Answer

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

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

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

    times = len(block_size_list) - K * 2

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

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

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

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

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

Submission status: Wrong Answer

Solution code:




N,K=map(int,input().split())
S=input()
l=[]
count=0
if S[0]=="0":
    now=0
    l+=[0]
    a=0
else:
    now=1
    a=1
for i in range(len(S)):
    if int(S[i])==now:
        count+=1
    else:
        l+=[count]
        count=1
        now=int(S[i])
l+=[count]
ans=sum(l[:2*K+1])
s=sum(l[:2*K+1])
if len(l)-2*K>0:
    for i in range(2*K+2,len(l),2):
        s+=l[i]+l[i-1]-l[i-2*K-1]-l[i-2*K-2]
        ans=max(ans,s)
else:
    ans=sum(l)



if len(l)==1 and a==1:
    print(l[0])
else:
    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 K == 0:
	if S[0] == "0":
		val = A[1]
		currentMax = val
		for i in range(1, len(A)-2*K, 2):
			val = A[i]
			if val > currentMax:
				currentMax = val
		print(currentMax)
	else:
		val = A[0]
		currentMax = val
		for i in range(0, len(A)-2*K, 2):
			val = A[i]
			if val > currentMax:
				currentMax = val
		print(currentMax)


else:
	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:
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)
print(ll)
if ll[0][0] == 0:
    if ll[-1][0] == 0:
        for i in range(2*k):
            co += ll[i][1]
        lll.append(co)
        print(co)
        co -= ll[0][1]
        for i in range(ppp-k):
            if ppp-k-1 == i and i == 0:
                co += ll[-1][1]
                lll.append(co)
            elif ppp-k-1 == i:
                co += ll[-1][1] - (ll[i*2+1][1] + ll[i*2][1])
                lll.append(co)
            elif i == 0:
                co += ll[i*2+1+2*k][1] + ll[i*2+2*k][1]
                lll.append(co)
            else:
                co += ll[i*2+1+2*k][1] + ll[i*2+2*k][1] - (ll[i*2+1][1] + ll[i*2][1])
                lll.append(co)

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

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
s = tuple(input())
st_char_list = [0]
ans = 0

for i in range(n-1):
    if s[i] != s[i+1]:
        st_char_list.append(i+1)

if s[0] == '0':
    st_char_list.insert(0,0)

if s[-1] == '0':
    st_char_list.append(n)

stride = k*2+1
if len(st_char_list)-1 < stride:
    stride = len(st_char_list)-1

for i in range(0,len(st_char_list)-stride,2):
    ans = max(ans,st_char_list[i+stride]-st_char_list[i])

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=N
    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):
        if n[f]!=1 :
            last=f
            break
        else:
            pass
    print(n[:last+1].count(1))

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 = []
    s0 = S[0]
    sc = 0
    for i, s in enumerate(S):
        if s == s0:
            sc += 1
        else:
            data.append((s0, sc))
            s0 = s
            sc = 1
    data.append((s0, sc))
    return data

def main(ss):
    N, K = sp2nn(ss[0])
    S = ss[1]
    data = analyze(S)
    cmax = 0
    
    for i in range(len(data)):
        c = 0
        j0 = i
        if data[i][0] == '0':
            j0 = i+2
        else:
            j0 = i+3
        for j in range(i, min(len(data), j0+(K-1)*2)):
            c += data[j][1]
        if cmax < c:
            cmax = c
    
    print(cmax)

main(ii2ss(2))

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().rstrip().split())
S = input().rstrip()
index = {}
now = 0
for i in range(N):
    if now > N-1:
        break
    if S[now] == "1":
        count = 1
        for j in range(1,N):
            if now+j > N-1:
                break
            if S[now+j] == "1":
                count += 1  
            else:
                break
        index[now] = count
        now += count
    else:
        now += 1
one = list(index)
one.insert(0, 0)
maximum = 0
for i in range(len(one)-K):
    tmp = one[i+K] - one[i] + index[one[i+K]]
    if tmp > maximum:
        maximum = tmp
print(maximum)

Submission status: Wrong Answer

Solution code:

from itertools import groupby
import sys
stdin = sys.stdin
 
sys.setrecursionlimit(10**5) 
 
def ii(): return int(stdin.readline())
def li(): return map(int, stdin.readline().split())
N, K = tuple(li())
S = stdin.readline()
# use window
l, r = 0, 0
cnt = 0
r_iter = groupby(S)
l_iter = groupby(S)
r_next = next(r_iter)
l_next = next(l_iter)
while r < N:
    # print(l, r)
    if r_next[0] == '1' and K >= 0:
        r += len(list(r_next[1]))
        r_next = next(r_iter)
        cnt = max(cnt, r - l)
        K -= 1
        continue
    elif K >=0:
        if r_next[0] == "0":
            pass
        r += len(list(r_next[1]))
        r_next = next(r_iter)
        continue
    if l_next[0] == "0":
        K += 1
    elif l_next[0] == "1":
        pass
    l += len(list(l_next[1]))
    l_next = next(l_iter)
if K > 0:
    cnt = max(cnt, r-l)
print(cnt)

Submission status: Wrong Answer

Solution code:
n,k = [int(i) for i in input().split()]
s = input()

if s[0] == "1":
    retu = "whiteblack"
else:
    retu = "blackwhite"


count = 0
pre_person = s[0]
groups = []
for i in range(n):
    person = s[i]

    if person != pre_person:
        groups.append(count)
        count = 0
        pre_person = person
    count += 1
groups.append(count)


maxscore1 = 0
if retu == "whiteblack":
    score1 = 0
    blackgroups = len(groups)//2
    if k < blackgroups:
        start_index = 1
        score1 = groups[start_index-1]
        for j in range(k):
            score1 += groups[start_index+2*j]
            if start_index + 2*j+1 < len(groups):
                score1 += groups[start_index+2*j+1]
        maxscore1 = max(score1, maxscore1)
        for i in range(blackgroups - k):
            score1 -= groups[start_index + 2*i - 1]
            score1 -= groups[start_index + 2*i]
            score1 += groups[start_index + 2*k +2*i]
            if start_index+2*k+1+2*i < len(groups):
                score1 += groups[start_index + 2 * k +1 + 2 * i]
            maxscore1 = max(score1, maxscore1)
    else:
        maxscore1 = n

else:
    score1 = 0
    blackgroups = len(groups) - len(groups)//2
    if k < blackgroups:
        start_index = 0
        score1 = 0
        for j in range(k):
            score1 += groups[start_index+2*j]
            if start_index + 2*j+1 < len(groups):
                score1 += groups[start_index+2*j+1]

        maxscore1 = max(score1, maxscore1)
        for i in range(blackgroups - k):
            if i >1:
                score1 -= groups[start_index + 2*i - 1]
            score1 -= groups[start_index + 2*i]
            score1 += groups[start_index + 2*k +2*i]
            if start_index+2*k+1+2*i < len(groups):
                score1 += groups[start_index + 2 * k + 1 + 2 * i]

            maxscore1 = max(score1, maxscore1)
    else:
        maxscore1 = n

print(maxscore1)

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]) 
            if j < 2*k + 1:
                total -= len(ar[j-2*k-1])
            cnt = k
    m = max(m, total)
    j += 1

print m

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):
        if n[f]!=1 :
            last=f
            break
        else:
            pass

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

Submission status: Wrong Answer

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

mans = [];
seq = 0;
now = S[0];
if(now == "0"):
    mans.append(0);

for i in S:
    if(i == now):
        seq += 1;
    else:
        mans.append(seq);
        seq = 1;
        if(now=="1"):
            now="0"
        else:
            now="1"

if(seq!=0):
    mans.append(seq);
    if(now=="1"):
        now="0"
    else:
        now="1"
if(now=="0"):
    mans.append(0);

mans.append(0);

shakutori = [0]
shakunum = 0;

for i in mans:
    shakunum += i;
    shakutori.append(shakunum);

print(shakutori);

if(len(shakutori) <= 2*K+1):
    print(shakutori[-1]);
else:
    answer = 0;
    for i in range(1,len(shakutori),2):
        if(len(shakutori) <= i+2*K):
            break;
        else:
            if(shakutori[i+2*K]-shakutori[i-1] > answer):
                answer = shakutori[i+2*K]-shakutori[i-1];
    print(answer);

Submission status: Wrong Answer

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

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

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

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

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

    # print(index)
    # print(count)

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

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

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

Submission status: Wrong Answer

Solution code:
N,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 :
            last=f
            break
        else:
            pass

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

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())#0~K-1回目と考える。
s = input() #0~N-1人目と考える。


#output

first_pos = int(s[0:1]) #0人目が○なら1, ×なら0

nowpos = first_pos #今チェックしている立ち方
nowposcount = 0 #1にすると0人目を考えるときにバグる
Accum = [0] * (N+1) #累積和 取り出し方は[まとまり個数目] 要素数はまとまりの個数+1
nowRuisekiCount = 1 #何番目のまとまりを見ているか 0~まとまり個数-1
zeroRenzoku = 1

for i in range(len(s)): #0~N-1人目につき、全てをチェックして累積和のリストを作る
    if int(s[i:i+1]) == nowpos: #今チェックしている立ち方と同じ(左の人と同じ)なら
        nowposcount += 1 #左のまとまりの個数を1増やす
        Accum[nowRuisekiCount] = Accum[nowRuisekiCount-1] + nowposcount #まとまりの数字数を入れる
    else:
        nowpos = abs(nowpos - 1)
        nowposcount = 1 #nowposcountは1に戻る
        nowRuisekiCount += 1 #次のまとまりに移ったことになる
        Accum[nowRuisekiCount] = Accum[nowRuisekiCount-1] + nowposcount #まとまりの文字数を入れる(1を入れる)

matomariCount = nowRuisekiCount #まとまりの数

#累積和を出す
if matomariCount < (K*2+1): #K*2+1よりもまとまり数が少なければ、全部逆立ちさせられる
    print(sum(Accum))
else:
    #一番最初の合計文字数
    if first_pos == 1:
        maxaccum = Accum[1+(K*2+1)]-Accum[i]
    else: #最初のまとまりが0なら
        maxaccum = Accum[1+(K*2)]-Accum[i]

    #2個目のまとまりからも同様に見ていく
    for i in range(2, matomariCount+1, 1):
        if i+(K*2+1) > matomariCount+1:
            break
        if maxaccum < Accum[i+(K*2+1)]-Accum[i]:
            maxaccum = Accum[i+(K*2+1)]-Accum[i]
    print(maxaccum)

Submission status: Wrong Answer

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

c = [0]
import itertools
for _, g in itertools.groupby(s):
  ci = len(list(g))
  c.append(c[-1] + ci)

c = c[1:] + [0]
ans = 1
c = [0] + c
if s[0] == "1":
  for i in range(1, len(c), 2):
    if i+2*k > len(c)-1:
      ans = max(ans, c[-2]-c[i-1])
    else:
      ans = max(ans, c[i+2*k]-c[i-1])
else:
  for i in range(0, len(c), 2):
    if i+2*k > len(c)-1:
      ans = max(ans, c[-2]-c[i-1])
    else:
      ans = max(ans, c[i+2*k]-c[i-1])

print(ans)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python
N, K = map(int, input().split())
S = input()
D = []
tmp0 = -1
tmp1 = 1
for i in range(1,N):
    if S[i] == '0' and S[i-1] == '0':
        tmp0 -= 1
    elif S[i] == '1' and S[i-1] == '1':
        tmp1 += 1
    elif S[i] == '1' and S[i-1] == '0':
        D.append(tmp0)
        tmp0 = -1
    else:
        D.append(tmp1)
        tmp1 = 1
if S[N-1] == '0':
    if S[N-2] == '0':
        D.append(tmp0)
    else:
        D.append(-1)
else:
    if S[N-2] == '0':
        D.append(1)
    else:
        D.append(tmp1)
M = 1
for k in range(K):
    if not len(D) == 1:
        for i,d in enumerate(D):
            if i == 0:
                if abs(D[0]) + abs(D[1]) > M:
                    M = abs(D[0]) + abs(D[1])
                    index = 1
            elif i == len(D) - 1:
                if abs(D[len(D)-2]) + abs(D[len(D)-1]) > M:
                    M = abs(D[len(D)-2]) + abs(D[len(D)-1])
                    index = len(D)-1
            elif d < 0:
                if D[i-1]-d+D[i+1] > M:
                    M = D[i-1]-d+D[i+1]
                    index = i
        E = []
        for i,d in enumerate(D):
            if i == index - 1:
                E.append(M)
            elif i == index or i == index + 1:
                pass
            else:
                E.append(d)
        index = 0
        M = 0
        D = E[:]
    else:
        break

print(max(D))

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
#偶数番目+が１の人たち
lengths = [patterns[0]] + [0] * (len(patterns) - 1)
outs = [0] * (len(patterns))
width = 2*k + 1
if width > len(patterns):
  print(len(s))
else:
  for i in range(1,len(lengths)):
    # print(i, len(patterns), len(lengths), len(outs))
    lengths[i] = lengths[i-1] + patterns[i]
    if width <= i and i % 2 == 0:
       outs[i] = lengths[i] - lengths[i - width]

  print(max(outs))

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
s = input()
s0 = []
s1 = []
str_s = s[0]
str_num = 0
for i in range(n-1):
    if str_s != s[i+1] and str_s == "0":
        s0.append(i+1 - str_num)
        str_num = i+1
        str_s = "1"
    elif str_s != s[i+1] and str_s == "1":
        s1.append(i+1 - str_num)
        str_num = i+1
        str_s = "0"
if str_s == "0":
    s0.append(n - str_num)
else:
    s1.append(n - str_num)
if len(s0) >= len(s1):
    s1.append(0)
if len(s0) >= len(s1):
    s1.insert(0, 0)
sum_0 = sum(s0[0:k])
sum_1 = sum(s1[0:k+1])
ans = sum_0 + sum_1
for i in range(len(s0)-k):
    sum_0 = sum_0 - s0[i] + s0[i+k]
    sum_1 = sum_1 - s1[i] + s1[i+k+1]
    if(ans < (sum_0 + sum_1)):
        ans = sum_0 + sum_1
print(ans)

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

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)        




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
    if maxi < sum_ :
        maxi = sum_
        
if maxi == 0:
    maxi = 1
    
print(maxi)

Submission status: Wrong Answer

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

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

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

ll = [l[i+N2]-l[i] for i in range(len(l)-N2)]

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

Submission status: Wrong Answer

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

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

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

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

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

    # print(index)
    # print(count)

    cumsum = [0] + np.cumsum(count).tolist()
    c_len = len(cumsum)
    win = min(2 * K + 1, c_len - 1)
    # print(cumsum)
    # print(win)

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

Submission status: Wrong Answer

Solution code:
n, 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
#print(patterns)
#偶数番目+が１の人たち
lengths = [patterns[0]] + [0] * (len(patterns) - 1)
outs = [0] * (len(patterns))
width = 2*k + 1
for i in range(1,len(lengths)):
  # print(i, len(patterns), len(lengths), len(outs))
  lengths[i] = lengths[i-1] + patterns[i]
  if width <= i and i % 2 == 0:
     outs[i] = lengths[i] - lengths[i - width]
#print(lengths)
#print(outs)
print(max(outs))

Submission status: Wrong Answer

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

# abc124_d

# input
n, k = [int(s) for s in input().split()]
S = input()
if n<100: exit(0)
# get ans
node = [len(s) for s in S.split('0')]
node = [s for i, s in enumerate(node) if not (s==0 and i!=0 and i!=len(node)-1)]
arch = [len(s) for s in S.split('1')]
arch = list(filter(int, arch))
k = min(k, len(arch))
sls = [(i,i+k) for i in range(len(arch)-k+1)]
m = (sum(arch[i:j])+sum(node[i:j+1]) for i, j in sls)
ans = max(m)
# output
# print(node)
# print(arch)
print(ans)

Submission status: Wrong Answer

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

array=[]

for i in range(len(S)):
    if i == 0:
        cnt = 1
    else:
        if S[i-1] == S[i]:
            cnt += 1
        else:
            if S[i] != "0":
                cnt = cnt * -1
            array.append(cnt)
            cnt = 1

if S[i] == "0":
    cnt = cnt * -1
array.append(cnt)

#print "array:",array

if len(array) == 1:
    print 1
    exit()

for i in range(K):
    m_num = 0
    idx = 0
#        for j,n in enumerate(array):
    for j in range(len(array)):
        val = 0
        if j == 0:
            val = (array[j] * -1) + array[j + 1]
        elif j == (len(array) -1):
            val = array[j-1] + (array[j] * -1)
        else:
            val = array[j-1] + (array[j] * -1) + array[j+1]
#        print "j,val:", j, val
        if m_num < val:
            m_num = val
            idx = j

#    print "idx,m_num:", idx, m_num
    if idx == 0:
        array[idx] = m_num
        array.pop(idx + 1)
    elif idx == (len(array) -1):
        array[idx -1] = m_num
        array.pop(idx)
    else:
        array[idx -1] = m_num
        array.pop(idx + 1)
        array.pop(idx)

#    print "tmp_array:",array, idx, m_num

#print max(array), array
print max(array)

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)):
  r = j
  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)