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):
def Is_ToPut(p,w,k):
    count =1
    temp =p
    for i in w:
        if temp >= i:
            temp -=i
        elif i > p :
            return 0
        else:     
            count +=1
            temp =p-i
    if count <= k:return 1
    else:return 0

n,k = list(map(int,input().split(' ')))
w =[]
for _ in range(n):
    w.append(int(input()))
ans =100000*10000
left = 0
right = 100000*10000
while left < right:
    p = (left+right)//2
    if Is_ToPut(p,w,k):
        if ans > p:ans =p
        right=p
    else:
        left = p +1
print(ans)

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

ma = sum(wlist) + 1
mi = max(wlist) - 1
while (ma - mi > 1):
    mid = (ma + mi)//2
    truck = [0for i in range(k)] 
    i = 0
    gobigger = 0
    for w in wlist:
        if(truck[i] + w <= mid):
            truck[i] = truck[i] + w
        else:
            i = i+1
            if (i == k):
                gobigger = 1
                break
            truck[i] = truck[i] + w
    if (gobigger == 1):
        mi = mid
    else:
        ma = mid
    
print(ma)

Solution (Python):
n, k = map(int, input().split())
W = [int(input()) for _ in range(n)]
def can_load(W, k, q, n):
    i = 0
    for _ in range(k):
        s = 0
        while i < n and s + W[i] <= q:
            s += W[i]
            i += 1
    if i == n:
        return True
    else:
        return False


hi = 10 ** 9  
lo = 0  
while hi - lo > 1:
    mid = (hi + lo) // 2
    if can_load(W, k, mid, n):
        hi = mid
    else:
        lo = mid
print(hi)

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

w = []

for i in range(n):
    w.append(int(input()))
   
def carriable(P, n, k, w):
    j = 0
    for i in range(k):
        sum = 0
        while j < n and sum + w[j] <= P:
            sum += w[j]
            j += 1
    return j

def binary_search(p_left, p_right, n, k, w):
    if p_left == p_right:
        return p_left
    else:
        p_center = (p_left + p_right)//2
        
        
        if carriable(p_center, n, k, w) >= n:
            return binary_search(p_left, p_center, n, k, w)
        else:
            return binary_search(p_center + 1, p_right, n, k, w)

p_left = 0
p_right = sum(w)
print(binary_search(p_left, p_right, n, k, w))

Solution (Python):
def allocate(n,w,k,p):
	t=0
	c=1
	for i in range(n):
		if t + w[i] > p:
			t=0
			c+=1
			if c > k:
				return -1
		t+=w[i]
	return 0

n,k=map(int,input().split())
w=[0]*n
m=(1<<30)*-1
s=0
for i in range(n):
	w[i]=int(input())
	m=max(m,w[i])
	s+=w[i]

i=m
j=s
while (i<=j):
	guess = int(i+(j-i)/2)
	if allocate(n,w,k,guess) < 0:
		i = guess + 1
	else:
		j = guess - 1
print(i)

Solution (Python):
def is_leq_S(A, lim, K):
    S = [0] * K

    ai = 0
    si = 0

    while ai < len(A):
        if S[si] + A[ai] <= lim:
            S[si] += A[ai]
            ai += 1
        else:
            si += 1

            if si == K:
                return False
    
    return True


def main():
    N, K = [int(_) for _ in input().split()] 
    A = []
    
    for _ in range(N):
        A.append(int(input()))

    l = 0
    r = 100000 * 100000

    while l + 1 < r:
        m = (l + r) // 2

        if is_leq_S(A, m, K):
            S = m
            r = m
        else:
            l = m

    print(S)
    

main()

Solution (Python):
def can_load(num_baggages, baggages, num_truck, max_load):
    bag_no = 0

    for i in range(num_truck):
        load_cap = max_load
        while load_cap > 0:
            if bag_no >= num_baggages:
                
                return True
            if load_cap >= baggages[bag_no]:
                load_cap -= baggages[bag_no]
                bag_no += 1
            else:
                break
    
    return bag_no >= num_baggages

def main():
    num_baggages, num_trucks = map(int, input().split())
    baggages = [int(input()) for x in range(num_baggages)]

    start = max(sum(baggages) // num_trucks, min(baggages))
    end = sum(baggages)

    while start < end:
        mid = (start + end) // 2
        if can_load(num_baggages, baggages, num_trucks, mid):
            end = mid
        else:
            start = mid + 1

    print(start)
    return


main()

Solution (Python):
import sys
def can_load(num_baggages, baggages, num_truck, max_load):
    bag_no = 0

    for i in range(num_truck):
        load_cap = max_load
        while load_cap > 0:
            if bag_no >= num_baggages:
                
                return True
            if load_cap >= baggages[bag_no]:
                load_cap -= baggages[bag_no]
                bag_no += 1
            else:
                break
    
    return bag_no >= num_baggages

def main():
    num_baggages, num_trucks = map(int, input().split())
    baggages = list(map(int, sys.stdin.read().splitlines()))

    start = max(sum(baggages) // num_trucks, min(baggages))
    end = sum(baggages)

    while start < end:
        mid = (start + end) // 2
        if can_load(num_baggages, baggages, num_trucks, mid):
            end = mid
        else:
            start = mid + 1

    print(start)
    return


main()

Solution (Python):
import sys
def can_load(num_baggages, baggages, num_truck, max_load):
    bag_no = 0

    for i in range(num_truck):
        load_cap = max_load
        while load_cap > 0:
            if bag_no >= num_baggages:
                
                return True
            if load_cap >= baggages[bag_no]:
                load_cap -= baggages[bag_no]
                bag_no += 1
            else:
                break
    
    return bag_no >= num_baggages

def main():
    num_baggages, num_trucks = map(int, input().split())
    baggages = tuple(map(int, sys.stdin.read().splitlines()))

    start = max(sum(baggages) // num_trucks, min(baggages))
    end = sum(baggages)

    while start < end:
        mid = (start + end) // 2
        if can_load(num_baggages, baggages, num_trucks, mid):
            end = mid
        else:
            start = mid + 1

    print(start)
    return


main()

Solution (Python):
n, k = map(int, input().split())
l = list(int(input()) for i in range(n))

left = max(l)-1; right = sum(l)

while left+1 < right:  
    mid = (left + right) // 2
    cnt = 1; cur = 0  
    for a in l:
        if mid < cur + a:
            cur = a
            cnt += 1
        else:
            cur += a

    if cnt <= k:
        right = mid
    else:
        left = mid

print(right)

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

def solve():
    left   = 0
    right  = 10**5 * 10**4
    middle = 0
    while(right-left > 1):
        middle = (left+right)//2
        v = check(middle)
        if(v >= len(n_list)):
            right = middle
        else:
            left = middle
    return right

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

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

def solve():
    left   = 0
    right  = 10**5 * 10**4
    middle = 0
    while right - left > 1:
        middle = (left+right)//2
        temp_ans = check(middle)
        if temp_ans >= n:
            right = middle
        else:
            left  = middle
    return right

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

Solution (Python):
import math
from typing import List

buggage_num, truck_num = map(int, input().split())
buggage_list = [int(input()) for i in range(buggage_num)]


def able_to_load_buggage(max_weight: int,
                         truck_num: int, buggage_list: List[int]) -> bool:
    i = 0
    truck = [0 for i in range(truck_num + 1)]
    for buggage in buggage_list:
        if truck[i] + buggage <= max_weight:
            truck[i] += buggage
        else:
            i += 1
            truck[i] += buggage
        if i > (truck_num - 1):
            return False
    for t in truck:
        if t > max_weight:
            return False
    else:
        return True







left = 0
right = 100000 * 10000 + 1
i = 0
while left < right:
    i += 1
    mid = math.floor((left + right) / 2)
    result = able_to_load_buggage(mid, truck_num, buggage_list)
    
    if result:
        right = mid
    else:
        left = mid + 1

print(left)

Solution (Python):
n, k = list(map(int, input().split()))


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
    while right - left > 1:
        mid = (left + right) // 2
        v = check(mid)
        if v >= n:
            right = mid
        else:
            left = mid
    return right


T = []
for _ in range(n):
    T.append(int(input()))
ans = solve()
print(ans)

Solution (Python):
n,k=map(int,raw_input().split())
w=[input() for _ in range(n)]
l=max(w)
r=sum(w)
while l<r:
    m=(l+r)/2
    s=c=0
    for i in w:
        s+=i
        if s>m:s=i;c+=1
    if c<k:r=m
    else:l=m+1
print l

Solution (Python):
import math, sys

def loadable(w, n, k, p, wsum):
    wait = 0
    for i in range(n):
        wait += w[i]
        wsum -= w[i]
        if wait > p:
            k -= 1
            wait = w[i]
            if k == 0 or p < wsum / k:
                return False
    return True

n, k = map(int, sys.stdin.readline().split())
w = [int(sys.stdin.readline()) for _ in range(n)]

wsum = sum(w)

p = max(int(math.ceil(wsum / k)), max(w))
pre_f = p -1
pre_s = None

while True:
    if loadable(w, n, k, p, wsum):
        pre_s = p
    else:
        pre_f = p

    if pre_s != None and pre_s - pre_f == 1:
            break
    
    if pre_s:
        p = int((pre_s + pre_f) / 2)
    else:
        p = math.ceil(p * 1.01)
    
print(pre_s)

Solution (Python):
n, _k = map(int, raw_input().split())
w = [int(raw_input()) for i in xrange(n)]

l, r = max(w), sum(w)
while r > l:
    P = (r + l) / 2
    c = P
    k = _k - 1
    for wi in w:
        if c >= wi: c -= wi
        else:
            c = P - wi
            k -= 1
            if k < 0:
                break
    if k >= 0:
        r = P
    else:
        l = P + 1
print l

Solution (Python):
n, k = [ int( val ) for val in raw_input( ).split( " " ) ]
w = []
maxW = sumW = 0
for i in range( n ):
	num = int( raw_input( ) )
	sumW += num
	w.append( num )
	if maxW < num:
		maxW = num

minP = 0
if 1 == k:
	minP = sumW
elif n == k:
	minP = maxW
else:
	left = maxW
	right = 100000*10000
	while left <= right:
		middle = ( left+right )//2
		truckCnt = i = loadings = 0
		while i < n:
			loadings += w[i]
			if middle < loadings:
				truckCnt += 1
				if k < truckCnt+1:
					break
				loadings = w[i]
			i += 1

		if truckCnt+1 <= k:
			minP = middle
		
		if k < truckCnt+1:
			left = middle + 1
		else:
			right = middle - 1

print( minP )

Solution (Python):
n, k = [ int( val ) for val in input( ).split( " " ) ]
w = []
maxW = sumW = 0
for i in range( n ):
	num = int( input( ) )
	sumW += num
	w.append( num )
	if maxW < num:
		maxW = num

minP = 0
if 1 == k:
	minP = sumW
elif n == k:
	minP = maxW
else:
	left = maxW
	right = 100000*10000
	while left <= right:
		middle = ( left+right )//2
		truckCnt = i = loadings = 0
		while i < n:
			loadings += w[i]
			if middle < loadings:
				truckCnt += 1
				if k < truckCnt+1:
					break
				loadings = w[i]
			i += 1

		if truckCnt+1 <= k:
			minP = middle
		
		if k < truckCnt+1:
			left = middle + 1
		else:
			right = middle - 1

print( minP )

Solution (Python):
n, k = [ int( val ) for val in raw_input( ).split( " " ) ]
w = [ int( raw_input( ) ) for val in range( n ) ]
sumW = sum( w )
maxW = max( w )

minP = 0
if 1 == k:
	minP = sumW
elif n == k:
	minP = maxW
else:
	left = maxW
	right = 100000*10000
	while left <= right:
		middle = ( left+right )//2
		truckCnt = i = loadings = 0
		while i < n:
			loadings += w[i]
			if middle < loadings:
				truckCnt += 1
				if k < truckCnt+1:
					break
				loadings = w[i]
			i += 1

		if truckCnt+1 <= k:
			minP = middle
		
		if k < truckCnt+1:
			left = middle + 1
		else:
			right = middle - 1

print( minP )

Solution (Python):
n, k = [ int( val ) for val in input( ).split( " " ) ]
w = [ int( input( ) ) for val in range( n ) ]
sumW = sum( w )
maxW = max( w )

minP = 0
if 1 == k:
	minP = sumW
elif n == k:
	minP = maxW
else:
	left = maxW
	right = 100000*10000
	while left <= right:
		middle = ( left+right )//2
		truckCnt = i = loadings = 0
		while i < n:
			loadings += w[i]
			if middle < loadings:
				truckCnt += 1
				if k < truckCnt+1:
					break
				loadings = w[i]
			i += 1

		if truckCnt+1 <= k:
			minP = middle
		
		if k < truckCnt+1:
			left = middle + 1
		else:
			right = middle - 1

print( minP )

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

while l < r:
    m = (l + r) // 2
    stack = car = 0
    for i in w:
        stack += i
        if stack > m:
            stack = i
            car += 1
    if car < k:
        r = m
    else:
        l = m + 1
print(l)

Solution (Python):
import sys

n, k = map(int, input().split())
w = [int(sys.stdin.readline()[:-1]) for i in range(n)]
l = max(w)
r = sum(w)

while l < r:
    m = (l + r) // 2
    stack = car = 0
    for i in w:
        stack += i
        if stack > m:
            stack = i
            car += 1
    if car < k:
        r = m
    else:
        l = m + 1
print(l)

Solution (Python):
import sys

n, k = map(int, input().split())
w = [int(sys.stdin.readline()) for i in range(n)]
l = max(w)
r = sum(w)

while l < r:
    m = (l + r) // 2
    stack = car = 0
    for i in w:
        stack += i
        if stack > m:
            stack = i
            car += 1
    if car < k:
        r = m
    else:
        l = m + 1
print(l)

Solution (Python):
def maxStack(k, p, W):
    nw = 0
    count = 0
    t = 1
    for x in W:
        if x > p:
            return count
        if nw + x <= p:
            nw += x
            count += 1
        else:
            if t == k:
                return count
            else:
                t += 1
                nw = x
                count += 1
    return count


n, k = map(int, raw_input().split())
W = [int(raw_input()) for i in xrange(n)]

left = 1
right = sum(W)
while left < right:
    mid = (left + right) / 2
    v = maxStack(k, mid, W)
    if v >= n:
        right = mid
    else:
        left = mid + 1
print left

Solution (Python):
def carriable_baggage(baggage, baggage_num, truck_capacity, truck_num):
    """Return the number of baggae which trucks can carry."""
    laden_baggage = 0
    for i in range(truck_num):
        laden_weight = 0
        while laden_weight + baggage[laden_baggage] <= truck_capacity:
            laden_weight += baggage[laden_baggage]
            laden_baggage += 1
            if laden_baggage == baggage_num:
                return baggage_num
    return laden_baggage

def min_capacity(baggage, baggage_num, truck_num):
    """Return the minimum value of maximum load capacity of trucks."""
    left = 0
    right = 10000 * baggage_num
    while right - left > 1:
        mid = (left + right) // 2
        v = carriable_baggage(baggage, baggage_num, mid, truck_num)
        if v >= baggage_num:
            right = mid;
        else:
            left = mid
    return right


import sys

n, k = map(int, sys.stdin.readline().split())

T = list(map(int, sys.stdin))

P = min_capacity(T, n, k)

print(P)

Solution (Python):
def carriable_baggage(baggage, baggage_num, truck_capacity, truck_num):
    """Return the number of baggae which trucks can carry."""
    laden_baggage = 0
    for i in range(truck_num):
        laden_weight = 0
        while laden_weight + baggage[laden_baggage] <= truck_capacity:
            laden_weight += baggage[laden_baggage]
            laden_baggage += 1
            if laden_baggage == baggage_num:
                return baggage_num
    return laden_baggage

def min_capacity(baggage, baggage_num, truck_num):
    """Return the minimum value of maximum load capacity of trucks."""
    heaviest_baggage = max(baggage)
    left = 0
    right = heaviest_baggage * baggage_num
    while right - left > 1:
        mid = (left + right) // 2
        v = carriable_baggage(baggage, baggage_num, mid, truck_num)
        if v >= baggage_num:
            right = mid;
        else:
            left = mid
    return right


import sys

n, k = map(int, sys.stdin.readline().split())

T = list(map(int, sys.stdin))

P = min_capacity(T, n, k)

print(P)

Solution (Python):
def carriable_baggage(baggage, baggage_num, truck_capacity, truck_num):
    """Return the number of baggae which trucks can carry."""
    laden_baggage = 0
    for i in range(truck_num):
        laden_weight = 0
        while laden_weight + baggage[laden_baggage] <= truck_capacity:
            laden_weight += baggage[laden_baggage]
            laden_baggage += 1
            if laden_baggage == baggage_num:
                return baggage_num
    return laden_baggage

def min_capacity(baggage, baggage_num, truck_num):
    """Return the minimum value of maximum load capacity of trucks."""
    left = 0
    right = max(baggage) * baggage_num
    while right - left > 1:
        mid = (left + right) // 2
        v = carriable_baggage(baggage, baggage_num, mid, truck_num)
        if v >= baggage_num:
            right = mid;
        else:
            left = mid
    return right


import sys

n, k = map(int, sys.stdin.readline().split())

T = list(map(int, sys.stdin))

P = min_capacity(T, n, k)

print(P)

Solution (Python):
def carriable_baggage(baggage, baggage_num, truck_capacity, truck_num):
    """Return the number of baggae which trucks can carry."""
    laden_baggage = 0
    for i in range(truck_num):
        laden_weight = 0
        while laden_weight + baggage[laden_baggage] <= truck_capacity:
            laden_weight += baggage[laden_baggage]
            laden_baggage += 1
            if laden_baggage == baggage_num:
                return baggage_num
    return laden_baggage

def min_capacity(baggage, baggage_num, truck_num):
    """Return the minimum value of maximum load capacity of trucks."""
    left = 0
    right = 10000 * baggage_num
    while right - left > 1:
        mid = (left + right) // 2
        v = carriable_baggage(baggage, baggage_num, mid, truck_num)
        if v >= baggage_num:
            right = mid;
        else:
            left = mid
    return right


import sys

n, k = map(int, sys.stdin.readline().split())

T = list(map(int, sys.stdin))

P = min_capacity(T, n, k)

print(P)

Solution (Python):
def carriable_baggage(baggage, baggage_num, truck_capacity, truck_num):
    """Return the number of baggae which trucks can carry."""
    laden_baggage = 0
    for i in range(truck_num):
        laden_weight = 0
        while laden_weight + baggage[laden_baggage] <= truck_capacity:
            laden_weight += baggage[laden_baggage]
            laden_baggage += 1
            if laden_baggage == baggage_num:
                return baggage_num
    return laden_baggage

def min_capacity(baggage, baggage_num, truck_num):
    """Return the minimum value of maximum load capacity of trucks."""
    left = 0
    right = sum(baggage)
    while right - left > 1:
        mid = (left + right) // 2
        v = carriable_baggage(baggage, baggage_num, mid, truck_num)
        if v >= baggage_num:
            right = mid;
        else:
            left = mid
    return right


import sys

n, k = map(int, sys.stdin.readline().split())

T = list(map(int, sys.stdin))

P = min_capacity(T, n, k)

print(P)

Solution (Python):
def carriable_baggage(baggage, baggage_num, truck_capacity, truck_num):
    """Return the number of baggae which trucks can carry."""
    laden_baggage = 0
    for i in range(truck_num):
        laden_weight = 0
        while laden_weight + baggage[laden_baggage] <= truck_capacity:
            laden_weight += baggage[laden_baggage]
            laden_baggage += 1
            if laden_baggage == baggage_num:
                return baggage_num
    return laden_baggage

def min_capacity(baggage, baggage_num, truck_num):
    """Return the minimum value of maximum load capacity of trucks."""
    left = max(baggage) - 1
    right = sum(baggage)
    while right - left > 1:
        mid = (left + right) // 2
        v = carriable_baggage(baggage, baggage_num, mid, truck_num)
        if v >= baggage_num:
            right = mid
        else:
            left = mid
    return right


import sys

n, k = map(int, sys.stdin.readline().split())

T = list(map(int, sys.stdin))

P = min_capacity(T, n, k)

print(P)

Solution (Python):
def carriable_baggage(baggage, baggage_num, truck_capacity, truck_num):
    """Return the number of baggae which trucks can carry."""
    laden_baggage = 0
    for i in range(truck_num):
        laden_weight = 0
        while laden_weight + baggage[laden_baggage] <= truck_capacity:
            laden_weight += baggage[laden_baggage]
            laden_baggage += 1
            if laden_baggage == baggage_num:
                return baggage_num
    return laden_baggage

def min_capacity(baggage, baggage_num, truck_num):
    """Return the minimum value of maximum load capacity of trucks."""
    left = 0
    right = 1000000000
    while right - left > 1:
        mid = (left + right) // 2
        v = carriable_baggage(baggage, baggage_num, mid, truck_num)
        if v >= baggage_num:
            right = mid
        else:
            left = mid
    return right


import sys

n, k = map(int, sys.stdin.readline().split())

T = list(map(int, sys.stdin))

P = min_capacity(T, n, k)

print(P)

Solution (Python):


def is_carriable(w_lim, trucks, loads):
    """ Check whether w_lim(maximum carrying capacity) is possible or not """
    i = 0
    while trucks > 0:
        w = 0
        while i < len(loads):
            if w + loads[i] > w_lim:
                trucks -= 1
                break
            w += loads[i]
            i += 1
        if i == len(loads):
            return True
    return False

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

left = max(loads)
right = sum(loads) + 1

while right - left > 1:
    mid = (left + right) // 2
    if is_carriable(mid, trucks, loads):
        ans = mid
        right = mid
    else:
        left = mid

if is_carriable(max(loads), trucks, loads):
    ans = max(loads)

print(ans)

Solution (Python):


def is_carriable(w_lim, trucks, loads):
    """ Check whether w_lim(maximum carrying capacity) is possible or not """
    i = 0
    while trucks > 0:
        w = 0
        while i < len(loads):
            if w + loads[i] > w_lim:
                trucks -= 1
                break
            w += loads[i]
            i += 1
        if i == len(loads):
            return True
    return False

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

left = max(loads)
right = sum(loads) + 1

while right - left > 1:
    mid = (left + right) // 2
    if is_carriable(mid, trucks, loads):
        ans = mid
        right = mid
    else:
        left = mid

if is_carriable(max(loads), trucks, loads):
    ans = max(loads)

print(ans)

Solution (Python):


def is_carriable(w_lim, trucks, loads):
    """ Check whether w_lim(maximum carrying capacity) is possible or not """
    i = w = 0
    while i < len(loads):
        if w + loads[i] > w_lim:
            trucks -= 1
            w = 0
        else:
            w += loads[i]
            i += 1

        if trucks == 0:
            return False
    return True

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

left = max(loads)
right = sum(loads) + 1

while right - left > 1:
    mid = (left + right) // 2
    if is_carriable(mid, trucks, loads):
        ans = mid
        right = mid
    else:
        left = mid

if is_carriable(max(loads), trucks, loads):
    ans = max(loads)

print(ans)

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


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

lis=[]
a = input()
(nn,kk)=list(map(int,a.split()))
for i in range(nn):
    lis.append(int(input()))
ans = solve(nn,kk,lis)
print(ans)

Solution (Python):
MAX = 100000

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

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

n, k = map(int, raw_input().split())
T = []

for i in range(n):
    T.append(input())

ans = solve()

print ans

Solution (Python):
def check_p(p):
    global k, ws
    ct, nt = 0, 1
    for w in ws:
        if ct + w <= p:
            ct += w
        else:
            nt += 1
            if nt > k:
                return False
            ct = w
    return True


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

ws = [int(input()) for _ in range(n)]
l, r, p = max(ws), sum(ws), 0

while l < r:
    p = (l + r) // 2
    if check_p(p):
        r = p
    else:
        l = p = p + 1

print(p)

Solution (Python):
def check_p(p):
    global k, ws
    lt, nt = 0, 1  
    for w in ws:
        lt += w
        if lt > p:
            nt += 1
            if nt > k:
                return False
            lt = w
    return True


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

ws = [int(input()) for _ in range(n)]
l, r, p = max(ws), sum(ws), 0

while l < r:
    p = (l + r) // 2
    if check_p(p):
        r = p
    else:
        l = p = p + 1

print(p)

Solution (Python):
def check(T, p, n, k):
    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

n, k = [int(x) for x in input().split()]
T = []
for i in range(n):
    T.append(int(input()))

left, right = 0, 100000 * 10000

while(right - left > 1):
    mid = int((left + right) / 2)
    if (check(T, mid, n, k) >= n):
        right = mid
    else:
        left = mid

print(right)

Solution (Python):
n, k = map(int, raw_input().split())
W = [input() for _ in xrange(n)]

MAX = 100000
T = [0] * MAX

def check(p):
    i = 0
    for j in xrange(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 = int((right + left) / 2)
        v = check(mid)
        if v >= n:
            right = mid
        else:
            left = mid

    return right

def main():
    ans = solve()
    print ans
    return 0

main()

Solution (Python):
n, k = map(int, raw_input().split())
W = [input() for _ in xrange(n)]

def check(p):
    i = 0
    for j in xrange(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 = int((right + left) / 2)
        v = check(mid)
        if v >= n:
            right = mid
        else:
            left = mid

    return right

def main():
    ans = solve()
    print ans
    return 0

main()

Solution (Python):

n, k = map(int,raw_input().split())
ws = []
for _ in xrange(n):
  ws.append(int(raw_input()))

def possible_put_p(p):
  
  current = 1
  weight = 0
  for w in ws:
    if weight + w <= p:
      weight += w
    else:
      current += 1
      if current > k:
        return False
      weight = w

  if current <= k:
    return True
  else:
    return False

low = max(ws)
high = sum(ws)
while(high - low > 0):
  mid = (low + high) / 2
  if(possible_put_p(mid)):
    high = mid
  else:
    low = mid + 1

print high

Solution (Python):
n, k = map(int, input().split())
w = []
for _ in range(n):
    w.append(int(input()))
imax = 100000 * 1000000
imin = 1
while imin < imax:
    imid = (imin + imax) // 2
    track_num = 0
    current = 0
    for i in range(n):
        if w[i] > imid:
            track_num = k
            break
        elif w[i] + current > imid:
            track_num += 1
            current = w[i]
        else:
            current += w[i]
    if track_num < k:
        imax = imid
    else:
        imin = imid + 1
print(imax)

Solution (Python):
import sys
import math

def check(k, w, P):
    i = 0
    for t in range(k):
        p = 0
        while p + w[i] <= P:
            p += w[i]
            i += 1
            if i == len(w):
                return True
    return False

def allocate(k, w):
    t = [0] * k
    le = 0
    ri = 100000 * 10000

    while ri - le > 1:
        P = (ri + le) / 2
        if check(k, w, P):
            ri = P
        else:
            le = P
    return ri

if __name__ == "__main__":
    w = sys.stdin.readlines()
    k = int(w[0].split()[1])
    del w[0]
    w = [int(wi) for wi in w]
    print allocate(k, w)

Solution (Python):
import sys
'''
	??\?????????????????????
'''
_allinput = []
for inp in sys.stdin:
	_allinput += inp.strip().split(" ")
def _input():
	for put in _allinput:
		yield put
_obj = _input();
def __input():
	return _obj.__next__()
def nextInt():
	return int(__input())

def check(lst,L):
	tot = 1
	curSum = 0
	for i in lst:
		if curSum + i <= L:
			curSum += i
		else:
			curSum = i
			tot += 1
	return tot

def solve(lst,m):
	l,r = max(lst), 100000 * 10000
	while l != r:
		mid = int((l+r)/2)
		if check(lst,mid) <= m:
			r = mid
		else:
			l = mid + 1
	return l


n,m = nextInt(),nextInt()
if m > 0 or n > 0:
	lst = [nextInt() for i in range(0,n)]
	print(solve(lst,m))