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 heapq
import sys

import numpy as np

reatInt = lambda: int(sys.stdin.readline())
reatIntN = lambda: [int(v) for v in sys.stdin.readline().split(" ")]


n, k = reatIntN()
ss = sys.stdin.readline().strip()


class Node:
    def __init__(self, p, n, v):
        self.prev = p
        self.next = n
        self.value = v


r = Node(None, None, None)
r.next = r
r.prev = r


def append(n, value):
    nn = Node(n, n.next, value)
    n.next.prev = nn
    n.next = nn


def remove(n):
    pn = n.prev
    nn = n.next
    nn.prev = pn
    pn.next = nn
    n.next = None
    n.prev = None
    n.value = None



i = 0
while i < len(ss):
    b = i
    c = ss[i]
    while (i < len(ss)) and (c == ss[i]):
        i += 1
    append(r.prev, (int(c), b - i, i))


def _calc(p):
    v = 0
    if (p is not r) and (p.value[0] == 0):
        v = p.value[1]
        if p.next is not r:
            v += p.next.value[1]
        if p.prev is not r:
            v += p.prev.value[1]
    return v


p = r.next
pq = []
while p is not r:
    d = _calc(p)
    if 0 < d:
        heapq.heappush(pq, (-d, p.value[2], p))
    p = p.next


for i in range(k):
    if len(pq) == 0:
        break

    d, _, p = heapq.heappop(pq)
    if (p.value is not None) and (p.value[0] == 0):
        if p.next is not r:
            remove(p.next)
        if p.prev is not r:
            remove(p.prev)
        p.value = (1, -d)

        dp = _calc(p.prev)
        if 0 < dp:
            heapq.heappush(pq, (-dp, p.prev.value[2], p.prev))
        dn = _calc(p.next)
        if 0 < dn:
            heapq.heappush(pq, (-dn, p.next.value[2], p.next))

ret = 0
p = r.next
while p is not r:
    if p.value[0] == 1:
        ret = max(ret, p.value[1])
    p = p.next
print(ret)

Submission status: Wrong Answer

Solution code:
def cul(lst, m):
    r = 0
    for i in lst:
        r  += i if i < m else m
    return r

n, k = map(int,input().split())
s = input()
val = []

a = 1
for i in range(1, len(s)):
    if s[i-1] == s[i]:
        a += 1
    else:
        val.append(a)
        a = 1
val.append(a)
ans = []
now = int(s[0])
for i in range(len(s)-k):
    ins = 0
    j = 1 if now == 0 else 2
    while True: 
        j += 1   
        if now == 0:
            ins += 1
        if ins == k:
            lst = val[i:i+j] 
            ans.append(cul(lst, n))
            break
        now = 1 if now == 0 else 0

print(max(ans) if not len(ans) == 0 else 1)

Submission status: Wrong Answer

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

STAND = '0'
REVERSE = '1'
NONE = '2'

prev = NONE
size = 0
groups = []
for s in S:
    if s == prev:
        size += 1
    else:
        if prev == STAND:
            groups.append(-size)
        elif prev == REVERSE:
            groups.append(size)
        prev = s
        size = 1
if prev == STAND:
    groups.append(-size)
elif prev == REVERSE:
    groups.append(size)
max_size = 2 * K
ans = 0
cul_sum = []
total = 0
for g in groups:
    total += g if g > 0 else -g
    cul_sum.append(total)
if groups[0] < 0:
    ans = cul_sum[min(max_size-1, len(cul_sum)-1)]
    offset = 1
else:
    ans = cul_sum[min(len(cul_sum)-1, max_size)]
    offset = 2
for i in range(offset, len(cul_sum)-max_size, 2):
    ans = max(ans, cul_sum[i+max_size]-cul_sum[i-1])
print(ans)

Submission status: Wrong Answer

Solution code:
a = []

n,k = map(int, input().split())
for x in range(k+1):
  a.append(0)
q = input()

p = 0
c = 0
mx = 0

for i in range(n-1):
  p = p+1
  c = c+1  
  if q[i] == q[i+1]:
    lalala = 1

  elif (int(q[i]) == 1) & (int(q[i+1]) == 0) :
    p = p-a[0]
    if p > mx:
      mx = p
    
  elif (int(q[i]) == 0) & (int(q[i+1]) == 1):
    a.pop(0)
    a.append(c)
    c = 0

if int(q[n-1]) == 1:
  if p+1> mx:
    mx = p +1 - a[0]
else:
  if p+1 - a[0] -a[1] > mx:
    mx = p - -a[1] +1
  
  


print(mx)

Submission status: Wrong Answer

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

people = []
prev = s[0]
neg = False
if prev == "0":
	neg = True

count = 1

for i in range(1, len(s)):
	if prev != s[i]:
		people.append(count)
		count = 0
		prev = s[i]
	count += 1
people.append(count)


if len(people)/2 <= k:
	print(n)
	quit()


max_people, start, prep = 0, 0, 0
if neg == False:
	max_people = sum(people[:(k*2)+1])
	start = 2
	prep = max_people - people[0] - people[1]
else:
	max_people = sum(people[:k*2])
	start = 1
	prep = max_people - people[0]

scan = (k*2)+1
i = 0
for i in range(start, len(people)-(k*2)+1, 2):
	try:
		cur_sum = prep + people[i+scan-1] + people[i+scan-2]
	except IndexError:
		cur_sum = prep + people[-1]
	max_people = max(max_people, cur_sum)
	prep = cur_sum - people[i] - people[i-1]


print(max_people)

Submission status: Wrong Answer

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

people = []
prev = s[0]
neg = False
if prev == "0":
	neg = True

count = 1

for i in range(1, len(s)):
	if prev != s[i]:
		people.append(count)
		count = 0
		prev = s[i]
	count += 1
people.append(count)

print(people)

if len(people)/2 <= k:
	print(n)
	quit()


max_people, start, prep = 0, 0, 0
if neg == False:
	max_people = sum(people[:(k*2)+1])
	start = 2
	prep = max_people - people[0] - people[1]
else:
	max_people = sum(people[:k*2])
	start = 1
	prep = max_people - people[0]

scan = (k*2)+1
i = 0
for i in range(start, len(people)-(k*2)+1, 2):
	try:
		cur_sum = prep + people[i+scan-1] + people[i+scan-2]
	except IndexError:
		cur_sum = prep + people[-1]
	max_people = max(max_people, cur_sum)
	prep = cur_sum - people[i] - people[i-1]


print(max_people)

Submission status: Wrong Answer

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

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

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

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

Submission status: Wrong Answer

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

if "0" not in S:
	print len(S)
	quit()

L=[]
zero_kukan_cnt=0
for i,x in enumerate(S):
	if i==0:
		cnt=1
		if x=="0": zero_kukan_cnt+=1
	else:
		if prev==x:
			cnt+=1
		else:
			L.append(cnt)
			cnt=1
			if x=="0": zero_kukan_cnt+=1
	prev=x
else:
	L.append(cnt)	


if zero_kukan_cnt==K:
	print len(S)
	quit()



LSUM=[0]
for x in L:
	LSUM.append(LSUM[-1]+x)


l=len(LSUM)


if S[0]=="0":
	h=2*K		
else:
	h=2*K+1


ans=float("-inf")
for i in range(l-h):
	ans=max(ans, LSUM[i+h]-LSUM[i])

print ans

Submission status: Wrong Answer

Solution code:
#d=["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"]
#import numpy as np
#import math
#n=int(input())
#li=input().split()
#n=int(input())
#l=len(S)

lis=list(map(int,input().split()))
S=input()

ans=0
a=[] #0のリスト
b=[] #1のリスト
i,flag=0,0
while i<lis[0]:
    j=1
    if S[i]=="0":
        while flag==0 and i+j<lis[0]:
            if S[i+j]=="0":
                j+=1
            elif S[i+j]=="1":
                flag=1
        a.append(j)
        i+=j
    else:
        while flag==0 and i+j<lis[0]:
            if S[i+j]=="1":
                j+=1
            elif S[i+j]=="0":
                flag=1
        b.append(j)
        i+=j
    flag=0

A=len(a)
B=len(b)

if A<=lis[1]:
    ans=lis[0]
else:
    if A>B:
        for i in range(A-lis[1]+1):
            if i!=A-lis[1]:
                tmp=sum(a[i:lis[1]+i])+sum(b[i:lis[1]+i])
                if ans<tmp:
                    ans=tmp
            else:
                tmp=sum(a[i:lis[1]+i])+sum(b[i:lis[1]+i-1])
                if ans<tmp:
                    ans=tmp
    elif A<B:
        for i in range(A-lis[1]+1):
            tmp=sum(a[i:lis[1]+i])+sum(b[i:lis[1]+i+1])
            if ans<tmp:
                ans=tmp
    else:
        for i in range(A-lis[1]+1):
            tmp=sum(a[i:lis[1]+i-1])+sum(b[i:lis[1]+i-1])
            if ans<tmp:
                ans=tmp

print(ans)

Submission status: Wrong Answer

Solution code:
import sys,re,copy
input = sys.stdin.readline

N,K=[int(x) for x in input().strip().split()]

f=1
cnt=0
S=[]
r=0
for s in input().strip():
    s=int(s)
    if s==f:
        cnt+=1
    else:
        r+=cnt
        S.append(r)
        cnt=1
        f=1-f
else:
    r+=cnt
    S.append(r)
if len(S)%2==0:
    S.append(r)

Max=0
i=0
if len(S)==1:
    Max=S[0]
else:
    while True:
        r=i+2*K
        if r>=len(S):
            break
        cnt=S[r]-S[max(i-1,0)]
        if cnt > Max:
            Max = cnt
        i+=2

print(Max)

Submission status: Wrong Answer

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

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

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

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

Submission status: Wrong Answer

Solution code:
a = []

n,k = map(int, input().split())
for x in range(k+1):
  a.append(0)
q = input()

p = 0
c = 0
mx = 0

for i in range(n-1):
  p = p+1
  c = c+1  
  if q[i] == q[i+1]:
    lalala = 1

  elif (int(q[i]) == 1) & (int(q[i+1]) == 0) :
    
    p = p-a[0]
    if p > mx:
      mx = p
    
  elif (int(q[i]) == 0) & (int(q[i+1]) == 1):
    a.append(c)
    a.pop(0)
    c = 0

if int(q[n-1]) == 1:
  if p+1> mx:
    mx = p +1 - a[0]
else:
  if p+1 - a[0] > mx:
    mx = p - a[0] +1
  
  


print(mx)

Submission status: Wrong Answer

Solution code:
import sys,re,copy
input = sys.stdin.readline

N,K=[int(x) for x in input().strip().split()]

f=1
cnt=0
S=[]
r=0
for s in input().strip():
    s=int(s)
    if s==f:
        cnt+=1
    else:
        r+=cnt
        S.append(r)
        cnt=1
        f=1-f
else:
    r+=cnt
    S.append(r)
if len(S)%2==0:
    S.append(r)

Max=0
i=0
while True:
    r=i+2*K
    if r>=len(S):
        break
    cnt=S[r]-S[max(i-1,0)]
    if cnt > Max:
        Max = cnt
    i+=2

print(Max)

Submission status: Wrong Answer

Solution code:
a = []

n,k = map(int, input().split())
for x in range(k+1):
  a.append(0)
q = input()

p = 0
c = 0
mx = 0

for i in range(n-1):
  p = p+1
  c = c+1  
  if q[i] == q[i+1]:
    lalala = 1

  elif (int(q[i]) == 1) & (int(q[i+1]) == 0) :
    
    p = p-a[0]
    if p > mx:
      mx = p
    
  elif (int(q[i]) == 0) & (int(q[i+1]) == 1):
    a.append(c)
    a.pop(0)
    c = 0
  print(a,c,p)

if int(q[n-1]) == 1:
  if p+1> mx:
    mx = p +1 - a[0]
else:
  if p+1 - a[0] > mx:
    mx = p - a[0] +1
  
  


print(mx)

Submission status: Wrong Answer

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

wl = []
wc = 0
prevs = None
for i, _s in enumerate(s):
  if prevs is None:
    wl.append(i)
    prevs = _s
    if _s == '0':
      wc += 1
  elif prevs != _s:
    wl.append(i)
    prevs = _s
    if _s == '0':
      wc += 1
wl.append(n)
print(wl, wc)
if k >= wc:
  print(n)
elif s[0] == '0':
  if s[-1] == '0':
    maxl = max(wl[2*k] - wl[0], wl[-1] - wl[-2*k-1])
    for l, r in zip(wl[1::2], wl[2*k+2:-1:2]):
      if r - l > maxl:
        maxl = r - l
  else:
    maxl = wl[2*k] - wl[0]
    for l, r in zip(wl[1::2], wl[2*k+2::2]):
      if r - l > maxl:
        maxl = r - l
  print(maxl)
else:
  if s[-1] == '0':
    maxl = wl[-1] - wl[-2*k-1]
    for l, r in zip(wl[::2], wl[2*k+1:-1:2]):
      if r - l > maxl:
        maxl = r - l
  else:
    maxl = 0
    for l, r in zip(wl[::2], wl[2*k+1::2]):
      if r - l > maxl:
        maxl = r - l
  print(maxl)

Submission status: Wrong Answer

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

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

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

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

Submission status: Wrong Answer

Solution code:
import sys

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


def nexti(n):
    while S[n] == '1':
        n += 1
    while S[n] == '0':
        n += 1
    return n


def nextj(n):
    while S[n] == '0':
        n += 1
        if n == N:
            return N
    while S[n] == '1':
        n += 1
        if n == N:
            return N
    return n


i = 0
j = 0
while S[j] == '1':
    j += 1
    if j == N:
        print(1)
        sys.exit(0)
for _ in range(K):
    j = nextj(j)
    if j == N:
        print(1)
        sys.exit(0)

ans = j - i
while True:
    i = nexti(i)
    j = nextj(j)
    ans = max(ans, j - i)
    if j == N:
        break

print(ans)

Submission status: Wrong Answer

Solution code:
import sys,re,copy
input = sys.stdin.readline

N,K=[int(x) for x in input().strip().split()]

f=1
cnt=0
S=[]
r=0
for s in input().strip():
    s=int(s)
    if s==f:
        cnt+=1
    else:
        r+=cnt
        S.append(r)
        cnt=1
        f=1-f
else:
    r+=cnt
    S.append(r)
if len(S)%2==0:
    S.append(r)

Max=0
i=0
while True:
    r=i+2*K
    if r>=len(S):
        break
    cnt=S[r]-S[i]
    if cnt > Max:
        Max = cnt
    i+=2

print(Max)

Submission status: Wrong Answer

Solution code:
a = []

n,k = map(int, input().split())
for x in range(k+1):
  a.append(0)
q = input()

p = 0
c = 0
mx = 0

for i in range(n-1):
  p = p+1
  c = c+1  
  if q[i] == q[i+1]:
    lalala = 1

  elif (int(q[i]) == 1) & (int(q[i+1]) == 0) :
    
    p = p-a[0]
    if p > mx:
      mx = p
    
  elif (int(q[i]) == 0) & (int(q[i+1]) == 1):
    a.append(c)
    a.pop(0)
    c = 0

if int(q[n-1]) == 1:
  if p+1> mx:
    mx = p +1
else:
  if p+1 - a[0] > mx:
    mx = p - a[0] +1
  
  


print(mx)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
s=input()
s+='e'
arr=[]
arr2=[]
count=1
for i in range(n):
  if s[i]==s[i+1]:
    count+=1
  else:
    arr.append(count)
    arr2.append(s[i])
    count=1
if arr2.count('0')<=k:
  print(n)
else:
  tmp=max([arr[i] for i in range(len(arr))])
  for i in range(len(arr)-k*2):
    if arr2[i]=='1':
      if tmp<sum(arr[i:i+k*2+1]):
        tmp=sum(arr[i:i+k*2+1])
    else:
      if tmp<sum(arr[i:i+k*2]):
        tmp=sum(arr[i:i+k*2])
  print(tmp)

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

#端が0の時は,その端に1が0個あると置けば場合分けを防げる
if S[0] == '0':
    number_list = [0] + number_list
if S[-1] == '0':
    number_list.append(0)

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

max_num = 0

for i in range(0, len(ruiseki), 2):
    if i + K * 2 > len(ruiseki) - 1:
        break
    if i == 0:
        max_num = ruiseki[i + K * 2]
    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]

print (max_num)

Submission status: Wrong Answer

Solution code:
in1 = list(map(int, input().split()))
s = input()
n = in1[0]
k = in1[1]

l = 0
r = 1
c = 0
while r < n and (s[r-1]!='1' or s[r]!='0' or c!=k):
  if r==n-1 or (s[r]=='0' and s[r+1]=='1'):
    c += 1
  r += 1

m = r
while r < n and l < n:
  while s[l]!='0' or s[l+1]!='1': l += 1
  l += 1
  while True:
    r+=1
    if r == n:
      break
    elif s[r-1]=='1' and s[r]=='0':
      break
  m = max(m, r-l)
print(m)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import copy

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

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

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


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

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


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

Submission status: Wrong Answer

Solution code:
import sys
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':
        d = [0] + d
        for x, y, u, t in zip(c, c[K:], d, d[K+1:]):
            ans = max(ans, y-x+t-u)
        print(ans)
    elif S[0] == '1':
        r = d[-1]
        d.append(r)
        for x, y, u, t in zip(c, c[K:], d, d[K+1:]):
            ans = max(ans, y-x+t-u)
        print(ans)
elif len(c) == len(d)+1:
    for x, y, u, t in zip(c, c[K:], 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)
    d = [0] + d
    for x, y, u, t in zip(c, c[K:], d, d[K+1:]):
        ans = max(ans, y-x+t-u)
    print(ans)

Submission status: Wrong Answer

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

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

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

# if "0" not in S:
# 	print(len(S))
# else:
# 	if K >= len(stand): # 全員逆立ちにできる
# 		print(len(S))
# 	else:
# 		if len(stand) > len(reverse):
# 			reverse.append(0)
# 			reverse.insert(0, 0)
# 		if len(stand) == len(reverse) and S[0] == "0":
# 			reverse.insert(0, 0)
# 		if len(stand) == len(reverse) and S[0] == "1":
# 			reverse.append(0)
# 		# print(stand)
# 		# print(reverse)
# 		max_ = 0
# 		for i in range(max(1, 1+len(reverse)-(K+1))):
# 			# print(reverse[i:i+K+1],stand[i:i+K])
# 			tmp = sum(reverse[i:i+K+1]+stand[i:i+K])
# 			if tmp > max_:
# 				max_ = tmp
# 		print(max_)
if N == 1:
    print(1)
elif not "0" in S:
	print(N)
else:
	indexes = []
	tmp = ""
	for i in range(len(S)):
		if S[i] == "0":
			state = "stand"
		else:
			state = "reverse"
		if tmp != state:
			indexes.append(i)
		else: # 状態が前と同じ
			pass
		tmp = state
	indexes.append(N)
	ans = []
	# print(indexes)
	for i in range(len(indexes)):
		# print(i)
		if i == len(indexes)-1:
			break
		if S[indexes[i]] == "0":
			if i+2*K >= len(indexes):
				continue
			ans.append(indexes[i+2*K]-indexes[i])
		else:
			if i+2*K+1 >= len(indexes):
				continue
			ans.append(indexes[i+2*K+1]-indexes[i])
	if ans == []:
		print(len(S))
	else:
		print(max(ans))

Submission status: Wrong Answer

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

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

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

# if "0" not in S:
# 	print(len(S))
# else:
# 	if K >= len(stand): # 全員逆立ちにできる
# 		print(len(S))
# 	else:
# 		if len(stand) > len(reverse):
# 			reverse.append(0)
# 			reverse.insert(0, 0)
# 		if len(stand) == len(reverse) and S[0] == "0":
# 			reverse.insert(0, 0)
# 		if len(stand) == len(reverse) and S[0] == "1":
# 			reverse.append(0)
# 		# print(stand)
# 		# print(reverse)
# 		max_ = 0
# 		for i in range(max(1, 1+len(reverse)-(K+1))):
# 			# print(reverse[i:i+K+1],stand[i:i+K])
# 			tmp = sum(reverse[i:i+K+1]+stand[i:i+K])
# 			if tmp > max_:
# 				max_ = tmp
# 		print(max_)
if N == 1:
    print(1)
if not "0" in S:
	print(N)
else:
	indexes = []
	tmp = ""
	for i in range(len(S)):
		if S[i] == "0":
			state = "stand"
		else:
			state = "reverse"
		if tmp != state:
			indexes.append(i)
		else: # 状態が前と同じ
			pass
		tmp = state
	indexes.append(N)
	ans = []
	# print(indexes)
	for i in range(len(indexes)):
		# print(i)
		if i == len(indexes)-1:
			break
		if S[indexes[i]] == "0":
			if i+2*K >= len(indexes):
				continue
			ans.append(indexes[i+2*K]-indexes[i])
		else:
			if i+2*K+1 >= len(indexes):
				continue
			ans.append(indexes[i+2*K+1]-indexes[i])
	if ans == []:
		print(len(S))
	else:
		print(max(ans))

Submission status: Wrong Answer

Solution code:
n, k=map(int, input().split())
s=list(map(int, list(input())))
if s[0]==0:
    renzoku_list=[0]
else:
    renzoku_list=[]
sum=1
before=s[0]
for i in range(1, n):
    if s[i]==before:
        sum=sum+1
        before=s[i]
    else:
        renzoku_list.append(sum)
        sum=1
        before=s[i]
renzoku_list.append(sum)

ruisekiwa=[0]
for i in range(len(renzoku_list)):
    ruisekiwa.append(renzoku_list[i]+ruisekiwa[i])

result_list=[]
length=len(renzoku_list)

max=0

if s[0]==1 and (length)//2<=k:
    print(n)
else:
    for i in range(len(ruisekiwa)-(2*k+1)):
        if i%2==0:#i=0,2,4,...
            sum=ruisekiwa[i+2*k+1]-ruisekiwa[i]
            if sum>=max:
                max=sum
    print(max)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
s = input()
cur = "1"
sum_ = []
ans = -1
for i, c in enumerate(s):
    if cur != c:
        sum_.append(i)
        if c == "0":
            if len(sum_) >= 1 + k + k:
                ans = max(ans, sum_[-1] - sum_[-1-k-k])
        cur = c
sum_.append(len(s))
b = 0 if s[-1] == "0" else 1
if len(sum_) >= k + k + b:
    ans = max(ans, sum_[-1] - sum_[-k-k-b-1])
else:
    ans = n
print(ans)

Submission status: Wrong Answer

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

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

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

# if "0" not in S:
# 	print(len(S))
# else:
# 	if K >= len(stand): # 全員逆立ちにできる
# 		print(len(S))
# 	else:
# 		if len(stand) > len(reverse):
# 			reverse.append(0)
# 			reverse.insert(0, 0)
# 		if len(stand) == len(reverse) and S[0] == "0":
# 			reverse.insert(0, 0)
# 		if len(stand) == len(reverse) and S[0] == "1":
# 			reverse.append(0)
# 		# print(stand)
# 		# print(reverse)
# 		max_ = 0
# 		for i in range(max(1, 1+len(reverse)-(K+1))):
# 			# print(reverse[i:i+K+1],stand[i:i+K])
# 			tmp = sum(reverse[i:i+K+1]+stand[i:i+K])
# 			if tmp > max_:
# 				max_ = tmp
# 		print(max_)
if N == 1:
    print(1)
else:
	indexes = []
	tmp = ""
	for i in range(len(S)):
		if S[i] == "0":
			state = "stand"
		else:
			state = "reverse"
		if tmp != state:
			indexes.append(i)
		else: # 状態が前と同じ
			pass
		tmp = state
	indexes.append(N)
	ans = []
	# print(indexes)
	for i in range(len(indexes)):
		# print(i)
		if i == len(indexes)-1:
			break
		if S[indexes[i]] == "0":
			if i+2*K >= len(indexes):
				continue
			ans.append(indexes[i+2*K]-indexes[i])
		else:
			if i+2*K+1 >= len(indexes):
				continue
			ans.append(indexes[i+2*K+1]-indexes[i])
	if ans == []:
		print(len(S))
	else:
		print(max(ans))