Problem:
You are given $n$ packages of $w_i$ kg from a belt conveyor in order ($i = 0, 1, ... n-1$). You should load all packages onto $k$ trucks which have the common maximum load $P$. Each truck can load consecutive packages (more than or equals to zero) from the belt conveyor unless the total weights of the packages in the sequence does not exceed the maximum load $P$. Write a program which reads $n$, $k$ and $w_i$, and reports the minimum value of the maximum load $P$ to load all packages from the belt conveyor. Input In the first line, two integers $n$ and $k$ are given separated by a space character. In the following $n$ lines, $w_i$ are given respectively. Output Print the minimum value of $P$ in a line. Constraints  $1 \leq n \leq 100,000$  $1 \leq k \leq 100,000$  $1 \leq w_i \leq 10,000$  Sample Input 1 5 3
8
1
7
3
9 Sample Output 1 10 If the first truck loads two packages of $\{8, 1\}$, the second truck loads two packages of $\{7, 3\}$ and the third truck loads a package of $\{9\}$, then the minimum value of the maximum load $P$ shall be 10. Sample Input 2 4 2
1
2
2
6 Sample Output 2 6 If the first truck loads three packages of $\{1, 2, 2\}$ and the second truck loads a package of $\{6\}$, then the minimum value of the maximum load $P$ shall be 6.

Solution (Python):
N, K = map(int, input().split())
lst = [int(input()) for i in range(N)]

def num_luggage(P, K, lst):
    weight = 0
    n_track = 1
    for i, e in enumerate(lst):
        if (e > P):
            return i
        if (weight + e <= P):
            weight += e
        else:
            n_track += 1
            weight = e
        if (n_track == K+1):
            return i
    return len(lst)

left, right = 0, 10000 * N
i_break = 0
while right - left > 1:
    mid = (left+right)//2
    if N <= num_luggage(mid, K, lst):
        right = mid  
    else:  
        left = mid

print(right)

Solution (Python):
n,k = map(int,input().split())
a = [int(input()) for s in range(n)]
pmax = sum(a)
pmin = pmax//k-1

def check(ptest):
    tk = 1
    p = 0
    global a,k,n
    i = 0
    while i < n:
        p += a[i]
        if p <= ptest:
            i += 1
        else:
            tk += 1
            p = 0
        if tk > k:
            return False
    return True

while (pmax-pmin) > 1:
    pmid = (pmax+pmin)//2
    if check(pmid):
        pmax = pmid
    else:
        pmin = pmid
print(pmax)

Solution (Python):
def p_is_smaller(p):
    total = 0
    number_of_track = 1
    for w in W:
        total += w
        if total > p:
            number_of_track += 1
            if number_of_track > k:
                return 1
            total = w
    return 0

if __name__ == "__main__":
    n, k = map(int, input().split())
    W = [int(input()) for _ in range(n)]
    left, right = max(W), sum(W)
    while right > left:
        mid = (left + right) // 2
        if p_is_smaller(mid):
            left = mid + 1
        else:
            right = mid
    print(right)

Solution (Python):
N, K = map(int, input().split())
W = []
for _ in range(N):
  W.append(int(input()))
    
def carriable(p):
  if p < max(W):
    return False
  
  k_count = 0
  current_weight = 0
  for w in W:
    if current_weight + w <= p:
      current_weight += w
    else:
      if k_count + 1 < K:
        k_count += 1
        current_weight = w
      else:
        return False
      
  return True

def solve():
  min_p = 0
  max_p = sum(W)
  current_p = (max_p + min_p) // 2
  
  while True:    
    if min_p == current_p:
      return max_p
    
    if carriable(current_p):
      max_p = current_p
    else:
      min_p = current_p
    current_p = (max_p + min_p) // 2
    
print(solve())

Solution (Python):


n,k=map(int,input().split())
w=[int(input()) for i in range(n)]

def binarySearch(left,right):
    mid=0
    while right>left:
        mid=int(left+(right-left)//2)
        if check(mid,w,k):
            right=mid
        else:
            left=mid+1
    return left

def check(p,w,k):
    k_tmp=1
    w_tmp=0
    for w_i in w:
        w_tmp+=w_i
        if w_tmp>p:
            w_tmp=w_i
            k_tmp+=1
            if k_tmp>k:
                return False
    return True

print(binarySearch(max(w),sum(w)))

Solution (Python):
import sys
n, k = map(int, input().split())
T = [int(input()) for i in range(n)]
def c(mid):
	global k,T,n
	i=0
	for j in range(k):
		s=0
		while s+T[i]<=mid:
			s+=T[i]
			i+=1
			if(i == n):
				return n
	return i
def Allocation():
	l=0
	r=sys.maxsize
	while r-l>1:
		mid=(l+r)//2
		v=c(mid)
		if (v>=n):
			r=mid
		else:
			l=mid
	return r
x=Allocation()
print(x)

Solution (Python):
n,k=map(int, input("").split(" "))
w=[]
for i in range(n):
    w.append(int(input("")))
s=sum(w)
m=max(w)
while s>m:
    a=0
    b=0
    tmp=int((s+m)/2)
    for i in w:
        a+=i
        if a>tmp:
            a=i
            b+=1
    if b>=k:
        m=tmp+1
    else:
        s=tmp
print(m)

Solution (Python):
n,k=map(int,input().split())
T = [int(input()) for i in range(n)]

def check(P):
    global k, T, n

    i = 0
    for j in range(k):
        s = 0
        while s + T[i] <= P:
            s += T[i]
            i += 1
            if (i == n):
                return n
	
    return i
def find():
    
    left = 0
    right = 100000 * 10000

    while right - left > 1:
        mid = (left + right) // 2
        v = check(mid)
        if (v >= n):
            right = mid
        else:
            left = mid

    return right


print(find())

Solution (Python):
n, k = (int(x) for x in input().split())

w = []
w = [int(input()) for i in range(n)]

def f(x):
    count = 0
    for i in range(k):
        s = 0
        while s + w[count] <= x:
            s += w[count]
            count += 1
            if count == n:
                return n
    return count

left = 0
right = 100000 * 100000

while right - left > 1:
    mid = (left + right) // 2
    v = f(mid)
    if v >= n:
        right = mid
    else:
        left = mid

print(right)

Solution (Python):
def chkp(P):
    global n, k, w
    i = 0
    for j in range(k):
        s = 0
        while s + w[i] <= P:
            s += w[i]
            i += 1
            if(i == n):
                return n
    return i

def binary_search():
    l = 0
    r = 100000 * 10000
    while r - l > 1:
        c = (l+r) // 2
        v =  chkp(c)
        if(v >= n):
            r = c
        else:
            l = c
    return r

n, k = map(int, input().split())
w = [int(input()) for i in range(n)]
res = binary_search()
print(int(res))

Solution (Python):
def main():
    def check(p):
        
        i = 0
        
        for _ in range(k):
            
            s = 0
            while s + w[i] <= p:
                
                
                s += w[i]
                i += 1
                if i == n:
                    return n
        return i

    def solve():
        left, right = 0, 100000 * 10000 + 1
        while left < right:
            mid = (left + right) // 2
            if n <= check(mid):
                right = mid
            else:
                left = mid + 1
        return left

    n, k = [int(i) for i in input().split()]
    w = [int(input()) for _ in range(n)]
    ans = solve()
    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
N, K = map(int, input().split())
W = []

for i in range(N):
    W.append(int(input()))

def f(N, K, W, p):
    cnt = 0
    k = 1
    c_k = 0
    for w in W:
        if c_k + w <= p:
            c_k = c_k + w
            cnt = cnt + 1
        elif k < K and w <= p:
            k = k + 1
            c_k = w
            cnt = cnt + 1
        elif k < K and w > p:
            pass
        else:
            break
    return cnt

start = 0
end = 1000000000
while start != end:
    center = (start + end)//2
    if f(N, K, W, center) < N:
        start = center + 1
    else:
        end = center

print(start)

Solution (Python):
import math
n, k = map(int, input().split())
w = [int(input()) for i in range(n)]


def check(P): 
    i = 0
    for j in range(k):
        s = 0
        while (s + w[i]) <= P:
            s += w[i]
            i += 1
            if (i==n):
                return n
    return i







        
def solve():
    left = 0
    right = 100000*10000
    while (right - left) > 1:
        mid = (left + right) / 2
        v = check(mid)
        if (v >= n):   
            right = mid
        else:
            left = mid
            
    return right

ans = math.floor(solve())
print(ans)

Solution (Python):
import sys

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

lower_p = 0
w_list = []
for _ in range(n):
    w = int(sys.stdin.readline())
    lower_p = w if w > lower_p else lower_p
    w_list.append(w)
    
def get_possible_n(p):
    """最大積載重量を p としたときに、所与のトラック台数 k で処理可能な荷物の数

    所与の荷物数 n のうち、いくつ処理可能かを返す。荷物の重量と順序に依存し、
    （弱い）単調増加関数である。これが返す最大値は n である。
    """
    pos_n = 0
    for _ in range(k):
        acc_w = 0
        while pos_n < n:
            if acc_w + w_list[pos_n] <= p:
                acc_w += w_list[pos_n]
                pos_n += 1
            else:
                break
        else:
            break
    return pos_n


upper_p = lower_p * n // k + lower_p

def b_search(lower_p, upper_p):
    while lower_p < upper_p:
        mid_p = (lower_p + upper_p) // 2
        if get_possible_n(mid_p) < n:
            lower_p = mid_p + 1
        else:
            upper_p = mid_p
    return lower_p

print(b_search(lower_p, upper_p))

Solution (Python):
n, k  = map(int, input().split())
w = []
def check(ptest):
    tk=1
    p=0
    global k
    global w
    global n
    i=0
    
    while i<n:
        p+=w[i]
        if p<=ptest:
            i+=1
        else:
            tk+=1
            p=0
            
        if tk>k:
            return 0
            
    
    return 1
for i in range(n):
    w.append(int(input()))
pmax = sum(w)
pmin = pmax//k - 1


while (pmax - pmin) > 1:
    pmid = (pmax + pmin)//2
    if check(pmid) == 1:
        pmax = pmid
    else:
        pmin = pmid
print(pmax)

Solution (Python):
def check(P):
    global k,T,n
    
    i = 0
    for j in range(k):
        s = 0
        while s + T[i] <= P:
            s += T[i]
            i += 1
            if(i == n):
                return n
    return i
    
def solve():
    left = 0
    right = 100000 * 10000
    
    while right - left > 1:
        mid = (left + right)//2
        v = check(mid)
        if(v >= n):
            right = mid
        else:
            left = mid
    return right
    
if __name__ == '__main__':
    n,k = map(int,input().split())
    T = [int(input()) for i in range(n)]
    
    ans = solve()
    print(ans)

Solution (Python):
def check(list, p, k):
    i = 0
    for j in range(k):
        sum = 0
        while sum+list[i] <= p:
            sum += list[i]
            i += 1
            if i == len(list):
                return i


    return i

nk = list(map(int, input().split()))
n = nk[0]
k = nk[1]

list = []
for i in range(n):
    list.append(int(input()))

max_num = 100000 * 10000


left = 0
right = max_num
while left+1 < right:
    mid = int((left + right) /2)
    if check(list, mid, k) >= len(list):
        right = mid
    else:
        left = mid

print(right)

Solution (Python):
N, K = map(int,input().split())
w = [int(input()) for i in range(N)]

def check(p):
    track = 0
    tmp_w = 0
    for i in range(N):
        if w[i] > p:
            return False
        else:
            if (tmp_w + w[i]) > p:
                track += 1
                tmp_w = w[i]
            else:
                tmp_w += w[i]
    if track + 1 > K:
        return False
    else:
        return True

left = 0
right = 2000000000
while((left + 1) < right):
    mid = (left + right) // 2
    track = check(mid)
    if check(mid):
        right = mid
    else:
        left = mid 
        
print(right)

Solution (Python):
def min_item_num(weights, num_item, num_vehicle, capacity):
    item_idx = 0
    for _ in range(num_vehicle):
        load = 0
        while load + weights[item_idx] <= capacity:
            load += weights[item_idx]
            item_idx += 1
            if item_idx == num_item:
                return num_item
    return item_idx


def main():
    num_item, num_vehicle = list(map(int, input().split(" ")))
    weights = [int(input()) for _ in range(num_item)]
    left = 0
    right = 10000 * 100000
    while right - left > 1:
        center = (left + right) // 2
        min_item = min_item_num(weights, num_item, num_vehicle, center)
        if num_item <= min_item:
            right = center
        else:  
            left = center
    print(right)


if __name__ == "__main__":
    main()

Solution (Python):
def check(P):
    i = 0
    for j in range(k):
        s = 0
        while s + T[i] <= P:
            s += T[i]
            i += 1
            if i == n: return n
    return i

def solve():
    left, right = 0, 100000*10000
    while right - left > 1:
        mid = (left + right) / 2
        v = check(mid)
        if v >= n: right = mid
        else: left = mid
    return right

if __name__ == '__main__':
    T = []
    n, k = [int(x) for x in input().split()]
    for i in range(n): T.append(int(input()))    
    print(int(solve()))

Solution (Python):




def check(P):
    i = 0
    for j in range(k):
        s = 0
        while s + T[i] <= P:
            s += T[i]
            i += 1
            if i == n:
                return n
    return i
    
def solve():
    left = 0
    right = 100000 * 10000
    mid = 0
    while right - left > 1:
        mid = (left + right) // 2
        v = check(mid)
        if v >= n:
            right = mid
        else:
            left = mid
    return right
    
tmp = list(map(int, input().split())) 
n = tmp[0]
k = tmp[1]
T = []
for i in range(n):
    T.append(int(input()))
ans = solve()
print(ans)

Solution (Python):
n, k, *W = map(int, open(0).read().split())
left, right = 1000000000, max(W) - 1


def check(M):
    L = [W[0]]
    for i in range(1, n):
        if L[-1] + W[i] <= M:
            L[-1] += W[i]
        else:
            L.append(W[i])
    return len(L) <= k


while left - right > 1:
    m = (left + right) // 2
    if check(m):
        left = m
    else:
        right = m
print(left)

Solution (Python):
n,k=map(int,input().split())
l=0
r=10**9+1
ww=[]
for i in range(n):
	ww.append(int(input()))
while l<r:
	mid=(r+l)//2
	cnt=0
	tr=1
	for i in range(n):
		w=ww[i]
		if w>mid or tr>k:
			tr=10**18
			l=mid+1
			break
		if w+cnt<=mid:
			cnt+=w
		else:
			cnt=w
			tr+=1
	if tr<=k:
		r=mid
	else:
		l=mid+1
print(l)

Solution (Python):
import sys
def slove(goods, carnum, P):
    tot_good = len(goods)
    cur_good = 0
    for _ in range(carnum):
        load = 0
        while cur_good < tot_good and goods[cur_good] <= P -load:
            load += goods[cur_good]
            cur_good += 1
    if cur_good == tot_good:
        return True
    else:
        return False

if __name__ == '__main__':

    datas = input().split(' ')
    good_num = int(datas[0])
    car_num = int(datas[1])
    goods = []
    for _ in range(good_num):
        goods.append(int(input()))

    left = 0
    right = sys.maxsize
    while left < right:
        mid = (left + right) // 2
        if slove(goods, car_num, mid):
            
            right = mid
        else:
            left = mid + 1
    print(right)

Solution (Python):



from collections import Counter,deque


from sys import stdin,setrecursionlimit







setrecursionlimit(10**6)
input=stdin.readline

n,k=map(int,input().split())
w=[int(input().rstrip()) for _ in range(n)]
l,r=0,10**9
while l<r:
    m=(l+r)//2
    loop=0
    ch=False
    for i in range(k):
        noww=0
        while loop<n:
            if noww+w[loop]<=m:
                noww+=w[loop]
                loop+=1
            else: break
        else:
            ch=True
    if ch: r=m
    else: l=m+1
    
print(l)

Solution (Python):

n, k = map(int, input().split())
pkt = list()
for i in range(n):
    pkt.append(int(input()))
lenpkt = len(pkt)

def maxPkt(payload):
    maxPnum = 0
    for i in range(k):
        tempPL = 0
        while tempPL + pkt[maxPnum] <= payload:
            tempPL += pkt[maxPnum]
            maxPnum += 1
            if maxPnum == lenpkt:
                return True
    return False

lft = 0
mid = 0
rgt = 100000 * 10000

while lft < rgt:
    mid = (lft + rgt) // 2
    if maxPkt(mid):
        rgt = mid
    else:
        lft = mid + 1

print(rgt)

Solution (Python):
def track_number_needed(p, n_track):
    global W
    n_luggage = 0
    for j in range(n_track):
        weight_sum = 0
        while (weight_sum + W[n_luggage] <= p):
            weight_sum += W[n_luggage]
            n_luggage += 1
            if n_luggage == len(W):
                return n
    return n_luggage


def binary_search():
    global n
    global k
    left = 0
    right = 100000 * 10000
    while(right - left > 1):
        mid = (left + right) // 2
        n_luggage = track_number_needed(mid, k)
        if n_luggage >= n:
            right = mid
        else:
            left = mid
    return right
            


if __name__ == "__main__":
    n, k = map(int, input().split())
    W = [int(input()) for i in range(n)]
    print(binary_search())

Solution (Python):
n, k = (int(i) for i in input().split())
w = [int(input()) for i in range(n)]

total_weght = sum(w)

def check(p):
    track = [0 for i in range(k)]
    target = 0

    for i in range(k):
        while (track[i] + w[target]) <= p:
            track[i] += w[target]
            target += 1
            if target not in range(n):
                break
        if target not in range(n):
            break
    
    return(target)

def solve(n):
    left = 0
    right = 100000 * 100000
    while left < right:
        
        mid = (left + right) // 2
        if n == check(mid):
            right = mid
        else:
            left = mid + 1

    return(right)

if __name__ == "__main__":
    ans = solve(n)
    print(ans)

Solution (Python):
n,k=map(int,input().split())
w=[0]*n
for i in range(n):
    w[i] = int(input())

def hantei(w,a,k):
    tumu=a
    daime=1
    for ww in w:
        if ww<=tumu:
            tumu-=ww
        else:        
            tumu=a
            daime+=1
            if ww<=tumu:
                tumu-=ww
            else:
                return False
    if daime >k:
        return False
    else:
        return True


right= max(w)*n +1
left = min(w)

while left<right:
    mid  = (left+right)//2
    if hantei(w,mid,k):
        right=mid
    else:
        left=mid+1
print(left)

Solution (Python):
n, k = map(int, input().split())
W = [int(input()) for i in range(n)]

left = max(W)-1; right = sum(W)
while left+1 < right:
    mid = (left + right) // 2
    cnt = 1; cur = 0
    for w in W:
        if mid < cur + w:
            cur = w
            cnt += 1
        else:
            cur += w
    if cnt <= k:
        right = mid
    else:
        left = mid
print(right)

Solution (Python):
def check(p, n, w, k):
	temp = 0
	for i in range(n):
		if w[i] > p:
			return False
		if temp + w[i] > p:
			temp = 0
			k -= 1
			if k < 0:
				return False
		temp += w[i]
	if k == 0:
		return False
	else:
		return True

def main():
	n, k = map(int,input().split())
	w = []
	for _ in range(n):
		w += [int(input())]
	pmin = 0
	pmax = 10**10
	while True:
		if pmin >= pmax:
			print(pmin)
			break
		if check(int((pmin+pmax)/2),n,w,k):
			pmax = int((pmin+pmax)/2)
		else:
			pmin = int((pmin+pmax)/2)+1

main()

Solution (Python):
n, k = map(int, input().split())
wList = []
for i in range(n):
    w = int(input())
    wList.append(w)
minP = max(wList)-1
maxP = sum(wList)
def countD(mid, wList):
    curr = 0
    count = 0
    for w in wList:
        if curr+w > mid:
            count+=1
            curr = w
        else:
            curr+=w
    if curr > 0:
        count+=1
    return count
while maxP - minP > 1:
    mid = (minP + maxP)//2
    days = countD(mid, wList)
    if days <= k:
        maxP = mid
    else:
        minP = mid
print(maxP)

Solution (Python):






























def f(P):
    i = 0
    for k in range(K):
        current_weight = 0
        while current_weight + W[i] <= P:
            current_weight += W[i]
            i += 1
            if i == N:
                return N

    return i


def solve_binary():
    left = 0
    right = max(W)  
    right = 100000 * 10000

    while (right - left) > 1:  
        mid = (left + right) // 2
        mid_v = f(mid)

        if mid_v >= N:
            right = mid
        elif mid_v < N:
            left = mid

    return right


def solve_linear():
    p = 1
    while True:
        v = f(p)
        

        if v == N:
            
            return p

        p = p + 1


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

W = [int(input()) for _ in range(N)]



print(solve_binary())

Solution (Python):
from collections import deque
def check(n, k, w_master, p):
    w = w_master.copy()
    for _ in range(k):
        weight_rem = p 
        while (len(w) > 0) and (weight_rem >= w[0]):
            weight_rem -= w.popleft()
    
        if len(w) == 0:
            return True

    return False

def main(n, k, w_master):
    amin=0
    amax= 10_000 *  100_000

    while amin+1!=amax:
        amid=(amin+amax)//2
        if(check(n, k, w_master, amid)):
            amax=amid
        else:
            amin=amid

    return amax 

if __name__ == "__main__":
    
    n, k = map(int, input().split())
    w_master = deque()
    for _ in range(n):
        w_master.append(int(input()))

    print(main(n, k, w_master))

Solution (Python):
n,k = map(int,input().split())
w = [int(input()) for i in range(n)]

def check(p):
    i = 0
    for j in range(k):
        s = 0
        while s + w[i] <= p:
            s += w[i]
            i += 1
            if i == n:
                return n
    return i

left = 0
right = (10**5)*(10**4)
while right - left > 1:
    mid = (left+right)//2
    v = check(mid)
    if v >= n:
        right = mid
    else:
        left = mid
print(right)

Solution (Python):
import heapq
from collections import deque
from enum import Enum
import sys
import math
from _heapq import heappush, heappop

BIG_NUM = 2000000000
MOD = 1000000007
EPS = 0.000000001


N,K = map(int,input().split())
W = []
for _ in range(N):
    W.append(int(input()))


def is_OK(P):
    track_index = 0
    w_index = 0
    while w_index < N and track_index < K:
        tmp_sum = 0
        while  w_index < N and tmp_sum+W[w_index] <= P:
            tmp_sum += W[w_index]
            w_index += 1
        track_index += 1
    return w_index == N

L = 0
R = 100000*100000
mid = (L+R)//2
ans = R

while L <= R:
    if is_OK(mid):
        ans = mid
        R = mid-1
    else:
        L = mid+1
    mid = (L+R)//2

print("%d"%ans)

Solution (Python):
n,k = map(int,input().split())
s = []
for i in range(n):
    s.append(int(input()))

def f(P):
    v = 0
    for j in range(k):
        a = 0
        while(a+s[v]<=P):
            a += s[v]
            v+=1
            if v==n:
                return v
    return v

l = 0
r = 10**9
while l!=r:
    mid = (l+r)//2
    c = f(mid)
    if c>=n:
        r = mid
    else:
        l = mid+1   
print(r)

Solution (Python):
n, k = map(int, input().split())
w = [int(input()) for _ in range(n)]
s = max(w)-1
w.append(10**13)
t = sum(w)

while s+1 < t:
	m = (s + t) // 2
	x = 0
	c = 0
	for i in range(n+1):
		c += w[i]
		if c > m:
			x += 1
			c = w[i]
		elif c == m:
			if i != n-1:
				x += 1
				c = 0
	if x > k:
		s = m
	else:
		t = m

print(t)

Solution (Python):
def judge_1(arr, p, k):
    index = 0
    n = len(arr)
    for i in range(k):
        weight_sum = 0
        while weight_sum + arr[index] <= p:
            weight_sum += arr[index]
            index += 1
            if index >= n:
                return True
    return index >= n


if __name__ == '__main__':
    n, k = map(int, input().split())
    weight = []
    l, r = 0, 0
    for _ in range(n):
        tmp = int(input())
        weight.append(tmp)
        r += tmp
        l = max(l, tmp)
    ans = r
    while l <= r:
        mid = (l + r) // 2
        if judge_1(weight, mid, k):
            ans = mid
            r = mid - 1
        else:
            l = mid + 1
    print(ans)

Solution (Python):
n,k=map(int,input().split())
A=[int(input()) for i in range(n)]

def track_num(n):
    cnt,track=0,1
    for i in A:
        if cnt+i>n:
            track +=1
            cnt=i
        else:
            cnt +=i
    return track

def Binaryserch():
    left,right=max(A),sum(A)+1
    ans=0
    while left<right:
        mid=(left+right)//2
        track=track_num(mid)
        if track<=k:
            ans=mid
            right=mid
        elif track>k:
            left=mid+1
    return ans

print(Binaryserch())

Solution (Python):
n,k = map(int,input().split())
w = []
for i in range(0,n):
	w.append(int(input()))

def check(p):
	i = 0
	for j in range(0,k):
		sm = 0
		while sm + w[i] <= p:
			sm = sm + w[i]
			i = i + 1
			if i == n:
				return n
	return i


def solve():
	r = 1000000000
	l = 0
	while r-l > 1:
		mid = int((r+l)/2)
		v = check(mid)
		if(v >= n):
			r = mid
		else:
			l = mid
	return r

print(solve())

Solution (Python):


def check(mid):
	global k, T, n

	i = 0
	for j in range(k):
		s = 0
		while s + T[i] <= mid:
			s += T[i]
			i += 1
			if (i == n):
				return n	
	return i

n, k = map(int, input().split())
T = [int(input()) for i in range(n)]
left = 0
right = 100000 * 10000

while right - left > 1:
	mid = (left + right) // 2
	v = check(mid)
	if (v >= n):
		right = mid
	else:
		left = mid

print(right)

Solution (Python):
n,k=map(int,input().split())
w=[int(input()) for i in range(n)]
left=max(w)-1;right=sum(w)
mid=0
while left+1<right:
    mid=(left+right)//2
    sum=0;count=1
    for wi in w:
        if wi+sum>mid:
            sum=wi
            count+=1
        else:
            sum+=wi
    if count<=k:
        right=mid
    else:
        left=mid

print(right)

Solution (Python):
n, k = map(int, input().split())
T = [int(input()) for i in range(n)]

def update(P):

    i=0
    for j in range(k):
        s=0
        while s + T[i] <= P:
            s += T[i]
            i += 1
            if(i == n):
                return n

    return i

left = 0
right = 1e+9

while right - left > 1:
    mid = int((left+right)/2)

    v = update(mid)
    if(v >= n):
        right = mid
    else:
            left = mid

print(right)

Solution (Python):
import math

n,k = (int(x) for x in input().split())
w = list()

for i in range(0, n):
    w.append(int(input()))

def check(P):
    i = 0
    for j in range(0, k):
        summ = 0
        while(summ + w[i] <= P):
            summ += w[i]
            i += 1
            if(i == n): 
                return n

    return i 

def Binary_Search():
    left = 0
    right = 100000*100000
    while(right - left > 1):
        mid = math.floor((left + right) / 2)
        if(check(mid) >= n): 
            right = mid
        else:
            left = mid

    return right

ans = Binary_Search()

print(ans)

Solution (Python):
def check(w,k,mid):
  weight=mid
  remains=k
  flag=True
  for i in range(len(w)):
    if weight>=w[i]:
      weight-=w[i]
    else:
      remains-=1
      if remains==0:
        flag=False
        break
      weight=mid
      weight-=w[i]

  return flag



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

for i in range(n):
  a=int(input())
  w.append(a)

left=max(w)
right=sum(w)
mid=0

while(1):
  mid=int((left+right)/2)
  
  if check(w,k,mid):
    right=mid
  else:
    left=mid+1

  if left>=right:
    print(left)
    break

Solution (Python):
MAX=100000
def Allocate(A, p, k, n):
    count=1
    s=0     
    index=0
    while index<n:
        if s+A[index]<=p:
            s+=A[index]
            index+=1
        else:
            count+=1
            s=0
        if count>k:
            return False

    return True
         
    
n, k=map(int,input().split())
A=[int(input()) for i in range(n)]
left=0
right=MAX*MAX
while left+1<right:
    mid=left+(right-left)//2
    if Allocate(A,mid,k,n):
        right=mid
    else:
        left=mid
print(right)

Solution (Python):
n,k = map(int,input().split())
w = []

for loop in range(n):
    w.append(int(input()))

left = max(w)
right = 100000 * 10000
mid = (left+right)//2
ans = right

def check(P):
    tmp_sum = 0
    cnt = 1
    for loop in range(n):
        if tmp_sum + w[loop] <= P:
            tmp_sum += w[loop]
        else:
            tmp_sum = w[loop]
            cnt += 1
            if cnt > k:
                return False
    return True

while left <= right:
    if check(mid):
        ans = mid
        right = mid - 1
    else:
        left = mid + 1
    mid = (left+right)//2

print(ans)

Solution (Python):
def main():
    N, K = (int(i) for i in input().split())
    w = [int(input()) for i in range(N)]
    S = 0
    for i in range(N):
        S += w[i]

    def is_ok(P):
        w_i = 0
        for i in range(K):
            cur = 0
            while w_i < N :
                if cur + w[w_i] <= P:
                    cur += w[w_i]
                    w_i += 1
                else:
                    break
        if w_i == N:
            return True
        else:
            return False

    def binary_search_meguru():
        left = -1
        right = S + 1
        while right - left > 1:
            mid = left + ((right - left) // 2)
            if is_ok(mid): 
                right = mid
            else: 
                left = mid
        return right
    
    print(binary_search_meguru())


if __name__ == '__main__':
    main()

Solution (Python):
def can(l, k, p):
    s, a = 0, 0
    for li in l:
        if li > p: return False
        s += li

        if s > p:
            s = li
            k -= 1
        if k <= 0: return False
        
    return True

def bi(array, k, l, r):
    m = (l + r) // 2
    if l > r:
        return max(l, r)
    b = can(array, k, m)
    if b:
        return bi(array, k, l, m-1)
    else:
        return bi(array, k, m+1, r)
        
    
n, k = map(int, input().split())
l = [0] * n
for i in range(n):
    l[i] = int(input())
    
p = sum(l)
print(bi(l, k, 0, p))

Solution (Python):
n, k = map(int, (input().split()))
w_list = [int(input()) for _ in range(n)]
left = max(w_list)-1
right = sum(w_list)

while (right-left) > 1:
    mid = (left + right)//2
    count = 1
    c = 0
    for w in w_list:
        if mid < c + w:
            ｃ = w
            count += 1
        else:
            c += w
    if count <= k:
        right = mid
    else:
        left = mid
print(right)

Solution (Python):
def check(P):
    lis = [[] for i in range(k)]
    su = [0 for i in range(k)]
    
    su.append(-10000)
    cur = 0
    for i, bag in enumerate(baggage):
        
        
        while su[cur] + bag > P:
            cur += 1
        if cur == k:
            return False
        else:
            lis[cur].append(bag)
            su[cur] += bag
    
    return True

def main():
    min_P = 1000000000
    left = 1
    right = 1000000000
    mid = (left + right)//2
    while  left < right:
        '''if (left + right) % 2 == 0:
            mid = (left + right)//2
        else:
            mid = (left + right)//2 + 1
        
        
        if check(mid):
            right = mid
            if min_P > mid:
                min_P = mid
        else:
            left = mid + 1
        mid = (left + right)//2
    
    print(min_P)
if __name__ == '__main__':
    n, k = map(int, input().rstrip().split(" "))
    
    baggage = [int(input()) for i in range(n)]
    
    
    main()