CodeEff/ECCO huggingface dataset:
# Original solution
import bisect

n,q = list(map(int, input().split()))
aList = list(map(int, input().split()))

sumList=[]
sep2SumList=[]
for i in range(n):
    if i==0:
        sumList.append(aList[i])
    else:
        sumList.append(sumList[-1]+aList[i])
    if n%2==0:
        if i%2==1:
            if i==1:
                sep2SumList.append(aList[i])
            else:
                sep2SumList.append(sep2SumList[-1]+aList[i])
    else:
        if i%2==0:
            if i==0:
                sep2SumList.append(aList[i])
            else:
                sep2SumList.append(sep2SumList[-1]+aList[i])

                
sakaime=[]
anskazu=(n+1)//2
for i in range(anskazu):
    sakaime.append((aList[n-(i+1+1)]+aList[n-((i+1+1)*2-1)])//2)
sakaime.reverse()
sakaime=sakaime[1:]

def kotae(x):
    bisect.bisect_left(sakaime,x)
    num=len(sakaime)+1-bisect.bisect_left(sakaime,x)
    if num==len(sakaime)+1:
        ans=sumList[-1]-sumList[-1-num]
    else:
        ans=sumList[-1]-sumList[-1-num]+sep2SumList[(n-num*2+1)//2-1]
    return ans

for i in range(q):
  x=int(eval(input()))
  print((kotae(x)))

# Optimized solution
import bisect

n,q = list(map(int, input().split()))
aList = list(map(int, input().split()))

sumList=[]
sep2SumList=[]
for i in range(n):
    if i==0:
        sumList.append(aList[i])
    else:
        sumList.append(sumList[-1]+aList[i])
    if n%2==0:
        if i%2==1:
            if i==1:
                sep2SumList.append(aList[i])
            else:
                sep2SumList.append(sep2SumList[-1]+aList[i])
    else:
        if i%2==0:
            if i==0:
                sep2SumList.append(aList[i])
            else:
                sep2SumList.append(sep2SumList[-1]+aList[i])

                
sakaime=[]
anskazu=(n+1)//2
for i in range(anskazu):
    sakaime.append((aList[n-(i+1+1)]+aList[n-((i+1+1)*2-1)])//2)
sakaime.reverse()
sakaime=sakaime[1:]

def kotae(x):
    bisect.bisect_left(sakaime,x)
    num=len(sakaime)+1-bisect.bisect_left(sakaime,x)
    if num==len(sakaime)+1:
        ans=sumList[-1]-sumList[-1-num]
    else:
        ans=sumList[-1]-sumList[-1-num]+sep2SumList[(n-num*2+1)//2-1] #+1することで奇数を吸収
    return ans

for i in range(q):
  x=int(eval(input()))
  print((kotae(x)))
# Original solution
import itertools
def inpl(): return [int(i) for i in input().split()]
def f(y):
    return abs(x-A[y])
N, Q = inpl()
A = inpl()
SA = list(itertools.accumulate(A))
if N%2 :
    B = [v for i, v in enumerate(A) if not i%2]
    B = [0] + list(itertools.accumulate(B))
else:
    B = [v for i, v in enumerate(A) if i%2]
    B = [0] + list(itertools.accumulate(B))
    
for _ in range(Q):
    x = int(eval(input()))
    lo = 1
    hi = -(-N//2) + 1
    while hi - lo >1:
        mid = (lo+hi)//2
        if mid == 1:
            continue
        if max(f(N-mid-1), f(N-2*mid+1)) <= f(N-mid):
            lo = mid
        else:
            hi = mid    
    print((SA[N-1] - SA[N-lo-1] + B[-((2*lo-N)//2)]))
   

# Optimized solution
import itertools
import sys
def input(): return sys.stdin.readline()
def inpl(): return [int(i) for i in input().split()]
def f(y):
    return abs(x-A[y])
N, Q = inpl()
A = inpl()
SA = list(itertools.accumulate(A))
if N%2 :
    B = [v for i, v in enumerate(A) if not i%2]
    B = [0] + list(itertools.accumulate(B))
else:
    B = [v for i, v in enumerate(A) if i%2]
    B = [0] + list(itertools.accumulate(B))
    
for _ in range(Q):
    x = int(eval(input()))
    lo = 1
    hi = -(-N//2) + 1
    while hi - lo >1:
        mid = (lo+hi)//2
        if mid == 1:
            continue
        if max(f(N-mid-1), f(N-2*mid+1)) <= f(N-mid):
            lo = mid
        else:
            hi = mid    
    print((SA[N-1] - SA[N-lo-1] + B[-((2*lo-N)//2)]))
# Original solution
import bisect

n,q = list(map(int,input().split()))
A = list(map(int,input().split()))

dic = {}

w = [A[0]]
for i in range(1,n):
    w.append(w[-1]+A[i])
w1 = [A[0],0]
w2 = [0,A[1]]
for i in range(2,n):
    if i%2 == 0:
        w1.append(w1[-2]+A[i])
        w2.append(0)
    else:
        w2.append(w2[-2]+A[i])
        w1.append(0)

def solve(x):
    if x in dic:
        print((dic[x]))
        return 
    if x >= A[-1]:
        count = max(w1[-1],w2[-1])
        dic[x] = count
        print(count)
        return
    l = 0
    r = 10**10
    while r > l+1:
        m = (r+l)//2
        li = bisect.bisect_left(A,x-m)
        ri = bisect.bisect_right(A,x+m)
        if n-ri == ri-li:
            count = w[-1]-w[ri-1]
            li -= 1
            if li>= 0:
                if li%2 == 0:
                    count += w1[li]
                else:
                    count += w2[li]
            dic[x] = count
            print(count)
            return 0
        if n-ri == ri-li+1:
            count = w[-1]-w[ri-1]
            li -= 2
            if li>= 0:
                if li%2 == 0:
                    count += w1[li]
                else:
                    count += w2[li]
            dic[x] = count
            print(count)
            return 0
        if A[ri-1] == x+m:
            ri2 = ri-1
            if n-ri2 == ri2-li:
                count = w[-1]-w[ri2-1]
                li -= 1
                if li>= 0:
                    if li%2 == 0:
                        count += w1[li]
                    else:
                        count += w2[li]
                dic[x] = count
                print(count)
                return 0
            elif n-ri2 == ri2-li+1:
                count = w[-1]-w[ri2-1]
                li -= 2
                if li>= 0:
                    if li%2 == 0:
                        count += w1[li]
                    else:
                        count += w2[li]
                dic[x] = count
                print(count)
                return 0
        if 2*ri > n+li:
            r = m
        else:
            l = m
    
    count = max(w1[-1],w2[-1])
    dic[x] = count
    print(count)
    return

for _ in range(q):
    x = int(eval(input()))
    solve(x)



# Optimized solution
n,q = list(map(int,input().split()))

A = list(map(int,input().split()))

w = [A[0]]
for i in range(1,n):
    w.append(w[-1]+A[i])

w1 = [0]*n
for i in range(n):
    if (n-1-i)%2 == 0:
        w1[i] = A[i]
    if i > 0:
        w1[i] += w1[i-1]

def solve(x):
    l = 0
    r = n+1
    while r-l > 1:
        m = (r+l)//2
        if A[n-m] <= x:
            r = m
        else:
            if 2*m-1 > n:
                r = m
            else:
                if A[n-m]-x >= x-A[n-2*m+1]:
                    l = m
                else:
                    r = m
    ans = w[-1]-w[n-l-1]
    if n-2*l-1 >= 0:
        ans += w1[n-2*l-1]
    print(ans)

for _ in range(q):
    solve(int(eval(input())))
# Original solution
n, q = list(map(int, input().split()))
a = list(map(int, input().split()))
x = [int(eval(input())) for _ in range(q)]
sort_x = sorted(x)
ans = {}
if n%2== 1:
    small_position = 0
    border_position = int((n-1)/2)
    sum_all = sum(a[border_position:])
    a.append(1000000000)
    a.append(1000000000)
    for x_i in sort_x:
        for compare in a[border_position:]:
            if x_i - a[small_position] > compare-x_i:
                sum_all = sum_all + a[small_position] - compare
                small_position += 2
                border_position += 1
                ans[x_i] = sum_all
            else:
                ans[x_i] = sum_all
                break

else:
    small_position = 1
    border_position = int(n/2)
    sum_all = sum(a[border_position:])
    a.append(1000000000)
    a.append(1000000000)
    for x_i in sort_x:
        for compare in a[border_position:]:
            if x_i - a[small_position] > compare-x_i:
                sum_all = sum_all + a[small_position] - compare
                small_position += 2
                border_position += 1
                ans[x_i] = sum_all
            else:
                ans[x_i] = sum_all
                break

for x_i in x:
    print((ans[x_i]))


# Optimized solution
n, q = list(map(int, input().split()))
a = list(map(int, input().split()))
x = [int(eval(input())) for _ in range(q)]
sort_x = sorted(x)
ans = {}
if n%2== 1:
    small_position = 0
    border_position = int((n-1)/2)
else:
    small_position = 1
    border_position = int(n/2)
sum_all = sum(a[border_position:])
a.append(10000000000)
a.append(10000000000)
x_index = 0
flag = False
for compare in a[border_position:]:
    compare_x = sort_x[x_index]
    small_a = a[small_position]
    while 2*sort_x[x_index] - small_a <= compare:
        ans[sort_x[x_index]] = sum_all
        x_index += 1
        if x_index == q:
            flag = True
            break
    sum_all = sum_all + small_a - compare
    small_position += 2
    if flag:
        break

for x_i in x:
    print((ans[x_i]))
# Original solution
import sys
input = lambda : sys.stdin.readline().rstrip()
sys.setrecursionlimit(max(1000, 10**9))
write = lambda x: sys.stdout.write(x+"\n")


from bisect import bisect_left as bl, bisect_right as br
n,q = list(map(int, input().split()))
a = list(map(int, input().split()))
a.sort()

def sub(x, i):
    # i番目を取れるかどうか
    v = a[i]
    target = x - (v-x)
    num = i - bl(a, target-0.1)
    return num+1 >= n - i

anss = [None] * q
cumsum1 = [0] * n
cumsum0 = [0] * n
for i in range(n):
    if i%2:
        cumsum1[i] = cumsum1[i-1] + a[i]
        cumsum0[i] = cumsum0[i-1]
    else:
        cumsum0[i] = cumsum0[i-1] + a[i]
        cumsum1[i] = cumsum1[i-1]
        
for j in range(q):
    x = int(eval(input()))
    l = 0
    r = n-1
    while l<r-1:
        m = (r+l)//2
        if sub(x, m):
            r = m
        else:
            l = m
    # rまではxより大きくて取れる
#     print("r",  r)
    ans = sum(a[r:])
    ii = r-2 - (n-r-1)
    if ii>=0:
        if ii%2:
            ans += cumsum1[ii]
        else:
            ans += cumsum0[ii]
    anss[j] = ans
write("\n".join(map(str, anss)))

# Optimized solution
import sys
input = lambda : sys.stdin.readline().rstrip()
sys.setrecursionlimit(max(1000, 10**9))
write = lambda x: sys.stdout.write(x+"\n")


from bisect import bisect_left as bl, bisect_right as br
n,q = list(map(int, input().split()))
a = list(map(int, input().split()))
a.sort()

def sub(x, i):
    # i番目を取れるかどうか
    v = a[i]
    target = x - (v-x)
    num = i - bl(a, target-0.1)
    return num+1 >= n - i

anss = [None] * q
cumsum1 = [0] * n
cumsum0 = [0] * n
cumsum = [0] * n
for i in range(n):
    if i%2:
        cumsum1[i] = cumsum1[i-1] + a[i]
        cumsum0[i] = cumsum0[i-1]
    else:
        cumsum0[i] = cumsum0[i-1] + a[i]
        cumsum1[i] = cumsum1[i-1]
    cumsum[i] = cumsum[i-1] + a[i]

for j in range(q):
    x = int(eval(input()))
    l = 0
    r = n-1
    while l<r-1:
        m = (r+l)//2
        if sub(x, m):
            r = m
        else:
            l = m
    # rまではxより大きくて取れる
#     ans = sum(a[r:])
    ans = cumsum[-1] - cumsum[r-1]
    ii = r-2 - (n-r-1)
    if ii>=0:
        if ii%2:
            ans += cumsum1[ii]
        else:
            ans += cumsum0[ii]
    anss[j] = ans
write("\n".join(map(str, anss)))
# Original solution
import sys
input = lambda : sys.stdin.readline().rstrip()
sys.setrecursionlimit(max(1000, 10**9))
write = lambda x: sys.stdout.write(x+"\n")


from bisect import bisect_left as bl, bisect_right as br
n,q = list(map(int, input().split()))
a = list(map(int, input().split()))
a.sort()

def sub(x, i):
    # i番目を取れるかどうか
    v = a[i]
    target = x - (v-x)
    num = i - bl(a, target-0.1)
    return num+1 >= n - i

anss = [None] * q
cumsum1 = [0] * n
cumsum0 = [0] * n
cumsum = [0] * n
for i in range(n):
    if i%2:
        cumsum1[i] = cumsum1[i-1] + a[i]
        cumsum0[i] = cumsum0[i-1]
    else:
        cumsum0[i] = cumsum0[i-1] + a[i]
        cumsum1[i] = cumsum1[i-1]
    cumsum[i] = cumsum[i-1] + a[i]

for j in range(q):
    x = int(eval(input()))
    l = 0
    r = n-1
    while l<r-1:
        m = (r+l)//2
        if sub(x, m):
            r = m
        else:
            l = m
    # rまではxより大きくて取れる
#     ans = sum(a[r:])
    ans = cumsum[-1] - cumsum[r-1]
    ii = r-2 - (n-r-1)
    if ii>=0:
        if ii%2:
            ans += cumsum1[ii]
        else:
            ans += cumsum0[ii]
    anss[j] = ans
write("\n".join(map(str, anss)))

# Optimized solution
import sys
input = lambda : sys.stdin.readline().rstrip()
sys.setrecursionlimit(max(1000, 10**9))
write = lambda x: sys.stdout.write(x+"\n")


from bisect import bisect_left as bl, bisect_right as br
n,q = list(map(int, input().split()))
a = list(map(int, input().split()))
a.sort()

def sub(x, i):
    # i番目を取れるかどうか
    v = a[i]
    target = x - (v-x)
    num = i - bl(a, target)
    return num+1 >= n - i

anss = [None] * q
cumsum1 = [0] * n
cumsum0 = [0] * n
cumsum = [0] * n
for i in range(n):
    if i%2:
        cumsum1[i] = cumsum1[i-1] + a[i]
        cumsum0[i] = cumsum0[i-1]
    else:
        cumsum0[i] = cumsum0[i-1] + a[i]
        cumsum1[i] = cumsum1[i-1]
    cumsum[i] = cumsum[i-1] + a[i]

for j in range(q):
    x = int(eval(input()))
    l = 0
    r = n-1
    while l<r-1:
        m = (r+l)//2
        if sub(x, m):
            r = m
        else:
            l = m
    # rまではxより大きくて取れる
#     ans = sum(a[r:])
    ans = cumsum[-1] - cumsum[r-1]
    ii = r-2 - (n-r-1)
    if ii>=0:
        if ii%2:
            ans += cumsum1[ii]
        else:
            ans += cumsum0[ii]
    anss[j] = ans
write("\n".join(map(str, anss)))
# Original solution
from math import ceil
N, Q = list(map(int, input().split()))
A = [int(a) for a in input().split()]
AltTake = [0 for a in range(N)]
GetAll = [0 for a in range(N)]
AltTake[0], AltTake[1] = A[0], A[1]
GetAll[-1] = A[-1]
BlockList = [0 for i in range(ceil(N/2))]
maxBlock = ceil(N/2)

for i in range(2, N):
    AltTake[i] = A[i] + AltTake[i-2]
for i in reversed(list(range(N-1))):
    GetAll[i] = GetAll[i+1] + A[i]
for i in range(ceil(N/2) - 1):
        BlockList[-i-1] = (A[-i-2] + A[-2*i - 3]) // 2 + 1

def BSearch(x, L):
    left, right = 0, len(L)
    while right - left > 1:
        mid = (right + left)//2
        if L[mid] > x:
            right = mid
        else:
            left = mid
    return left

for _ in range(Q):
    block = maxBlock - BSearch(int(eval(input())), BlockList)
    if block == maxBlock: print((GetAll[-block]))
    else: print((GetAll[-block] + AltTake[N - block * 2 -1]))

# Optimized solution
import sys
from bisect import bisect_left

def solve():
    input = sys.stdin.readline
    N, Q = map(int, input().split())
    A = [int(a) for a in input().split()]
    X = [int(input()) for _ in range(Q)]
    cont = [0] * N
    cont[N-1] = A[N-1]
    for i in reversed(range(N-1)): cont[i] = cont[i+1] + A[i]
    change = [0] * N
    change[0] = A[0]
    change[1] = A[1]
    for i in range(2, N): change[i] = change[i-2] + A[i]
    upperBound = [-1] * N
    for i in range((N + 1)// 2): #上からi+1個連続でとれるxの最大値
        target = A[N-i-1]
        lower = A[(N-i-1) - i]
        upperBound[N-i-1] = (target + lower) // 2
    upperBound[N-1] = 10 ** 10
    Ans = [0] * Q
    for i, x in enumerate(X):
        pos = bisect_left(upperBound, x)
        Ans[i] += cont[pos] #連続で取れる数
        if N - (N - pos) * 2 - 1 >= 0: Ans[i] += change[N - (N - pos) * 2 - 1]
    print(*Ans, sep="\n")

    return 0

if __name__ == "__main__":
    solve()
# Original solution
import copy
 
N,Q = list(map(int,input().split()))
A = list(map(int,input().split()))
X = [0]*Q
 
for i in range(Q):
  X[i] = int(eval(input()))
  #初期値
  j= -1
  Takahashi = 0
  Aoki = 0
  B = copy.deepcopy(A)
  flag =0
  
  for k in range(N):
    if flag == 0: #高橋くんの行動
      Takahashi += B[-1]
      del B[-1]
      flag = 1
    
    elif flag ==1: #青木くんの行動
      
      #j-1番目のカード値B[j-1] <= 青木くんが決めた整数X[i] < j番目のカード値B[j]
      #となるようにjの値を決める
      #ただしX[i]がいずれのBの要素よりも大きい場合はi=N-k-1とする
      if j == -1:
        for l in range(N-k):
          if X[i] < B[l]:
            j = l 
            break
        if j == -1:
          j = N-k-1
          
      if len(B) == j:
         j = len(B)-1

          
      #j=0（青木くんが決めた整数は残りカードのいずれよりも小さい）の場合、B[0]を取る
      if j == 0:
        Aoki +=B[0]
        del B[0]
        flag =0
      #j=0（青木くんが決めた整数は残りカードのいずれよりも小さい）の場合、B[0]を取る
      #j番目のカードB[j]を取る     
      elif abs(X[i]-B[j]) < abs(X[i]-B[j-1]):
        Aoki += B[j]
        del B[j]
        flag =0
        if len(B) == j:
           j=j-1
      #j番目のカードB[j-1]を取る   
      else:
        Aoki += B[j-1]
        del B[j-1]
        flag = 0
        j = j-1
  print(Takahashi)

# Optimized solution
n,q = list(map(int,input().split()))
a = list(map(int,input().split()))
query = [int(eval(input())) for i in range(q)]

a = sorted(a)
ruiseki = [0]*(n+1)
for i in range(n):
    ruiseki[i+1] = ruiseki[i] + a[i]
    
ruiseki2 = [0]*(n+1)
if n % 2 == 1:
    for i in range(n):
        if i % 2 == 0:
            ruiseki2[i+1] = ruiseki2[i] + a[i]
        else:
            ruiseki2[i+1] = ruiseki2[i]

if n % 2 == 0:
    for i in range(n):
        if i % 2 == 1:
            ruiseki2[i+1] = ruiseki2[i] + a[i]
        else:
            ruiseki2[i+1] = ruiseki2[i]

def solve(mid, num):
    r = -((-mid) // 2)
    l = mid
    r_pos = n - r
    l_pos = n - l
    if abs(a[l_pos] - num) <= abs(a[r_pos] - num):
        return True
    else:
        return False

for i in range(q):
    num = query[i]
    ok = 1
    ng = n + 1 
    while abs(ok - ng) > 1:
        mid = (ok + ng) // 2
        if solve(mid, num):
            ok = mid
        else:
            ng = mid
    ans = 0
    ans += ruiseki[n] - ruiseki[n-(ok+1)//2]
    nokori = n - ok
    ans += ruiseki2[nokori]
    print(ans)
# Original solution
read = input
n,k = list(map(int, read().split()))
a = read()
a = '1' + a + '1'
# -1 if use 0, -1 if use len(a) -1
pstart = 0
pcnt = 0
ptype = '1'
b = []
# (start, end, cnt)
for tp in enumerate(a):
    i, c = tp
    if ptype == c:
        pcnt = pcnt + 1
    else :
        b.append((pstart, i-1, pcnt))
        pstart = i
        ptype = c
        pcnt = 1
b.append((pstart, i, pcnt))
# print(b)
if k >= len(b)//2:
    print((sum([tp[2] for tp in b]) - 2))
else :
    start = 0
    end = k * 2
    ans = 0
    while(end <= len(b) - 1):
        tmp = sum([tp[2] for tp in b[start:(end+1)]])
        if b[start][0] == 0:
            tmp = tmp - 1
        if b[end][1] == len(a)-1:
            tmp = tmp -1
#         print(start, end ,tmp)
        ans = max(ans, tmp)
        start = start + 2
        end = end + 2
    print(ans)

# Optimized solution
read = input
n,k = list(map(int, read().split()))
a = read()
a = '1' + a + '1'
# -1 if use 0, -1 if use len(a) -1
pstart = 0
pcnt = 0
ptype = '1'
b = []
# (start, end, cnt)
for tp in enumerate(a):
    i, c = tp
    if ptype == c:
        pcnt = pcnt + 1
    else :
        b.append((pstart, i-1, pcnt))
        pstart = i
        ptype = c
        pcnt = 1
b.append((pstart, i, pcnt))
# print(b)
if k >= len(b)//2:
    print((sum([tp[2] for tp in b]) - 2))
else :
    start = 0
    end = k * 2
    ans = 0
    S = [0 for i in range(len(b)+10)]
    S[0] = b[0][2]
    for i in range(1, len(b)):
        S[i] = S[i-1] + b[i][2]
    while(end <= len(b) - 1):
        tmp = S[end] - (S[start - 1] if start - 1>=0 else 0)
        if b[start][0] == 0:
            tmp = tmp - 1
        if b[end][1] == len(a)-1:
            tmp = tmp -1
#         print(start, end ,tmp)
        ans = max(ans, tmp)
        start = start + 2
        end = end + 2
    print(ans)
# Original solution
import sys
input=sys.stdin.readline

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

s.strip()
p = [0]
for i in range(n):
    if s[i] != s[p[-1]]:
        p.append(i)
p.append(n)

X = []
for i in range(len(p)-1):
    if s[p[i]] == "1":
        li = i
        ri = i + 2 * k + 1
        if ri >= len(p)-1:
            ri = len(p)-1
        x = p[ri] - p[li]
    else:
        li = i-1
        if li < 0:
            li = 0
        ri = i + 2 * (k-1) + 1
        if ri >= len(p)-1:
            x = n - p[li]
        else:
            x = p[ri] - p[li] + 1
    X.append(x)
print((max(X)))


# Optimized solution
import sys
input=sys.stdin.readline

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

s.strip()
p = [0]
for i in range(n):
    if s[i] != s[p[-1]]:
        p.append(i)

X = []
for i in range(len(p)):
    if s[p[i]] == "1":
        li = i
        ri = i + 2 * k + 1
    else:
        li = i-1
        if li < 0:
            li = 0
        ri = i + 2 * k
    if ri > len(p)-1:
        x = n - p[li]
    else:
        x = p[ri] - p[li]
    X.append(x)
print((max(X)))
# Original solution
#!/usr/bin/env python3
import sys
try:
    from typing import List
except ImportError:
    pass


def solve(N: int, K: int, S: str):
    d = []  # type: List[int]
    i = 0
    while True:
        r = 0
        while i < N and S[i] == "1":
            r += 1
            i += 1
        d.append(r)
        if i >= N:
            break
        s = 0
        while i < N and S[i] == "0":
            s += 1
            i += 1
        d.append(s)
    c = 2 * K + 1
    if c > len(d):
        print((sum(d)))
        return
    print((max(sum(d[dl:dl + c]) for dl in range(0, len(d) - c + 1, 2))))


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    K = int(next(tokens))  # type: int
    S = next(tokens)  # type: str
    solve(N, K, S)


if __name__ == '__main__':
    main()


# Optimized solution
#!/usr/bin/env python3
import sys
try:
    from typing import List
except ImportError:
    pass


def solve(N: int, K: int, S: str):
    d = []  # type: List[int]
    i = 0
    while True:
        r = 0
        while i < N and S[i] == "1":
            r += 1
            i += 1
        d.append(r)
        if i >= N:
            break
        s = 0
        while i < N and S[i] == "0":
            s += 1
            i += 1
        d.append(s)
    c = 2 * K + 1
    if c > len(d):
        print((sum(d)))
        return
    dl = 0
    acc = sum(d[:c])
    ans = acc
    while dl + c + 2 <= len(d):
        acc += d[dl + c] + d[dl + c + 1] - d[dl] - d[dl + 1]
        ans = max(ans, acc)
        dl += 2
    print(ans)


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    K = int(next(tokens))  # type: int
    S = next(tokens)  # type: str
    solve(N, K, S)


if __name__ == '__main__':
    main()
# Original solution
import sys,re,copy
input = sys.stdin.readline

N,K=[int(x) for x in input().strip().split()]
S=input().strip()
Z=[len(x) for x in re.split(r'1+',S)]
O=[len(x) for x in re.split(r'0+',S)]

m=0
i=0
if S[0]=='1':
    Z.pop(0)
l1=len(O)
l0=len(Z)
while True:
    c=sum(O[i:i+K+1])+sum(Z[i:i+K])
    if c > m:
        m = c
    i+=1
    if i+K>l1 or i+K>l0:
        break

print(m)

# Optimized solution
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:
        if i >=len(S):
            break
        r=i+2*K
        cnt=S[min(r,len(S)-1)]-S[max(i-1,0)]
        if cnt > Max:
            Max = cnt
        i+=2

print(Max)