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 check_numbers(cargo_weight_list, number_of_all_cargo, number_of_tracks, maximum_weight):
    """check the numbers that are loadable in tracks
    
    Args:
        cargo_weight_list: cargo weight list
        number_of_tracks: the number of tracks
        
    Returns:
        the number of cargos that are loadable in tracks
    """
    counter = 0
    number_of_loaded_cargo = 0

    while counter < number_of_tracks:
        current_track_weight = 0

        while current_track_weight + cargo_weight_list[number_of_loaded_cargo] <= maximum_weight:
            current_track_weight += cargo_weight_list[number_of_loaded_cargo]
            number_of_loaded_cargo += 1
            if number_of_loaded_cargo == number_of_all_cargo:
                return number_of_all_cargo

        counter += 1

    return number_of_loaded_cargo


def find_the_minimum_of_maximum_weihgt(cargo_weight_list, number_of_all_cargo, number_of_tracks):
    """find the minimum of maximum weight s.t all of the cargos can be loaded into tracks.
    (binary search is used. the number of loadable cargos monotonicaly increases as the maximum weight increases)


    Args:
        cargo_weight_list: cargo weight list
        numbef of all cargo: the number of all cargos
        number of tracks: the number of tracks

    Returns:
        minumim number of maximum track weight that are needed to load all of the cargos
    """
    left = max(cargo_weight_list)
    right = sum(cargo_weight_list)
    while (right - left) > 0:
        middle = (right + left) / 2
        the_number_of_loadable_cagos = check_numbers(cargo_weight_list, number_of_all_cargo, number_of_tracks, middle)
        if the_number_of_loadable_cagos >= number_of_all_cargo:
            right = middle
        else:
            left = middle + 1

    return right

def main():
    number_of_all_cargo, number_of_tracks = [int(x) for x in raw_input().split(' ')]
    cargo_weight_list = [int(raw_input()) for _ in xrange(number_of_all_cargo)]
    
    print find_the_minimum_of_maximum_weihgt(cargo_weight_list, number_of_all_cargo, number_of_tracks)


if __name__ == '__main__':
    main()

Solution (Python):



def check_numbers(cargo_weight_list, number_of_all_cargo, number_of_tracks, maximum_weight):
    """check the numbers that are loadable in tracks
    
    Args:
        cargo_weight_list: cargo weight list
        number_of_tracks: the number of tracks
        
    Returns:
        the number of cargos that are loadable in tracks
    """
    counter = 0
    number_of_loaded_cargo = 0

    while counter < number_of_tracks:
        current_track_weight = 0

        while current_track_weight + cargo_weight_list[number_of_loaded_cargo] <= maximum_weight:
            current_track_weight += cargo_weight_list[number_of_loaded_cargo]
            number_of_loaded_cargo += 1
            if number_of_loaded_cargo == number_of_all_cargo:
                return number_of_all_cargo

        counter += 1

    return number_of_loaded_cargo


def find_the_minimum_of_maximum_weihgt(cargo_weight_list, number_of_all_cargo, number_of_tracks):
    """find the minimum of maximum weight s.t all of the cargos can be loaded into tracks.
    (binary search is used. the number of loadable cargos monotonicaly increases as the maximum weight increases)


    Args:
        cargo_weight_list: cargo weight list
        numbef of all cargo: the number of all cargos
        number of tracks: the number of tracks

    Returns:
        minumim number of maximum track weight that are needed to load all of the cargos
    """
    left = max(cargo_weight_list)
    right = sum(cargo_weight_list)
    while (right - left) > 0:
        middle = (right + left) / 2
        the_number_of_loadable_cagos = check_numbers(cargo_weight_list, number_of_all_cargo, number_of_tracks, middle)
        if the_number_of_loadable_cagos >= number_of_all_cargo:
            right = middle
        else:
            left = middle + 1

    return right

def main():
    number_of_all_cargo, number_of_tracks = [int(x) for x in raw_input().split(' ')]
    cargo_weight_list = [int(raw_input()) for _ in xrange(number_of_all_cargo)]
    
    print find_the_minimum_of_maximum_weihgt(cargo_weight_list, number_of_all_cargo, number_of_tracks)


if __name__ == '__main__':
    main()

Solution (Python):


import sys
import os


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


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

def check(P):
    
    weight_index = 0
    for i in range(k):
        current_weight = 0
        while current_weight + W[weight_index] <= P:
            
            current_weight += W[weight_index]
            weight_index += 1
            if weight_index == len(W):
                
                return len(W)
    return weight_index

P_min = 1
P_max = 100000 * 10000
P_mid = None
while True:
    P_mid = (P_min + P_max) // 2
    loaded_num = check(P_mid)

    if loaded_num < n:
        
        P_min = P_mid
    else:
        
        P_max = P_mid

    if P_max - P_min <= 1:
        break


if check(P_min) == n:
    print(P_min)
else:
    print(P_max)

Solution (Python):


import sys
import os


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


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

def check(P):
    
    weight_index = 0
    for i in range(k):
        current_weight = 0
        while current_weight + W[weight_index] <= P:
            
            current_weight += W[weight_index]
            weight_index += 1
            if weight_index == len(W):
                
                return len(W)
    return weight_index

P_min = 1
P_max = sum(W)
P_mid = None
while True:
    P_mid = (P_min + P_max) // 2
    loaded_num = check(P_mid)

    if loaded_num < n:
        
        P_min = P_mid
    else:
        
        P_max = P_mid

    if P_max - P_min <= 1:
        break


if check(P_min) == n:
    print(P_min)
else:
    print(P_max)

Solution (Python):


import sys
import os


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


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

def check(P):
    
    weight_index = 0
    for i in range(k):
        current_weight = 0
        while current_weight + W[weight_index] <= P:
            
            current_weight += W[weight_index]
            weight_index += 1
            if weight_index == len(W):
                
                return len(W)
    return weight_index

P_min = 1
P_max = sum(W)
P_mid = None
while True:
    P_mid = (P_min + P_max) // 2
    loaded_num = check(P_mid)

    if loaded_num < n:
        
        P_min = P_mid + 1
    else:
        
        P_max = P_mid

    if P_max == P_min:
        break


print(P_max)

Solution (Python):
def check(k, p, w_list):
    i_w = 0
    for i_k in range(k):
        sum_w = 0
        while True:
            if i_w >= len(w_list):
                return len(w_list)
            if sum_w + w_list[i_w] > p:
                break
            sum_w += w_list[i_w]
            i_w += 1
    return i_w

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

for _ in range(n):
    w = int(input())
    w_list.append(w)

left = 0
right = 100000 * 10000
mid = None

while left < right:
    mid = (left + right) // 2
    v = check(k, mid, w_list)
    if v >= n:
        right = mid
    else:
        left = mid + 1

print(right)

Solution (Python):
def allocation():
    n, cars = map(int, input().split())
    weight = [int(input()) for i in range(n)]

    left = max(weight)
    right = sum(weight)

    while left < right:
        mid = (left + right) // 2

        if check(weight, cars, mid):
            right = mid
        else:
            left = mid + 1

    print(left)

def check(weight, cars, capacity):
    t = 0
    c = 1
    for w in weight:
        t += w
        if t > capacity:
            t = w
            c += 1

    if c <= cars:
        return True
    else:
        return False

if __name__ == '__main__':
    allocation()

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



def howmany(P):
 ans = 0
 truck = 1
 i = 0
 while truck <= k and i <= n-1:
  rp = P
  while rp >= WL[i]:
   ans += 1
   rp -= WL[i]
   i += 1
   if i == n: break
  truck += 1
 return ans

low = 0
high = 100000 * 100000 
while low != high:
 mid = (low + high) // 2
 if howmany(mid) == n:
  high = mid
 else:
  low = mid + 1
print(str(low))

Solution (Python):



load_num, track_num = list(map(int, input().split(" ")))

loads = list()
for _ in range(load_num):
    loads.append(int(input()))

P = 10000 * 100000

def isMeet(p):
    i = 0

    for _ in range(track_num): 
        sum = 0 
        while (sum + loads[i] <= p):
            sum += loads[i]
            i += 1
            if i == load_num:
                return True
    return False


def binary_search(P):
    left = 0 
    right = P
    while right - left > 0:
        mid = int((left+right)/2)
        if isMeet(mid):
            right = mid
        else:
            left = mid + 1
    return right

print(binary_search(P))

Solution (Python):
import math
n, k = map(int, input().split())
a = [int(input()) for _ in [None]*n]

lb = max(a)
ub = lb * n

while True:
    mid = (ub+lb) // 2
    remain = k - 1
    cw = 0
    for w in a:
        cw += w
        if cw > mid:
            remain -= 1
            cw = w
        if remain < 0:
            break
    if remain < 0:
        if lb == mid:
            print(lb+1)
            break
        lb = mid
    else:
        if lb == mid:
            print(lb)
            break
        ub = mid

Solution (Python):
import sys

global n, k, T


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


def cal_ans():
    left = 0
    right = sum(T)
    mid = (right-left) // 2
    while right - left > 1:
        mid = left + (right-left)//2
        if cal_v(mid) >= n:
            right = mid
        else:
            left = mid
    return right

if __name__ == "__main__":
    n, k = map(int, sys.stdin.readline().split())
    T = list(map(int, sys.stdin.readlines()))
    ans = cal_ans()
    print(ans)

Solution (Python):
def check(P,w,n):
    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

s = input().split(" ")
n = int(s[0])
k = int(s[1])
w = []
for i in range(n):
    a = int(input())
    w.append(a)

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

Solution (Python):
def main():
	n, n_truck = map(int, input().split())
	w_list = []

	for _ in range(n):
		w_list.append(int(input()))

	print(b_search(w_list, n_truck))
 
def b_search(w_list, n_truck):
	left = max(w_list)
	right = sum(w_list)
	while left < right:
		mid = int((left+right) / 2)
		if check(w_list, n_truck, mid):
			right = mid
		else:
			mid += 1
			left = mid

	return mid


def check(w_list, n_truck, p):
	truck_count = 1
	tmp_truck = 0

	for w in w_list:
		tmp_truck += w
		if tmp_truck > p:
			truck_count += 1
			tmp_truck = w
		if truck_count > n_truck:
			return False

	return True






if __name__ == '__main__':
    main()

Solution (Python):
from collections import deque

def isPa(w,k,p):
    tr = 0
    c = 0
    d = deque(w)
    while(d):
        tmp = d.popleft()
        if(tmp > p):
            return False
        
        if( tr + tmp > p):
            tr = tmp
            c += 1
        else:
            tr += tmp
        if c+1>k:
            return False
    return True

def MaxInP(w,k,p):
    maxinp = 0
    tmp = 0
    d = deque(w)
    while(d):
        tmp += d.popleft();
        if(tmp > p):
            if(tmp > maxinp):
                maxinp = tmp
            tmp = 0
    return maxinp

n,k = [int(x) for x in input().split()]
w = deque()
for i in range(n):
    w.append(int(input()))
    
mean = int(sum(w)/k)
maxinp = MaxInP(w,k,mean)

if k == 1:
    print(mean)
else:
    minP = mean
    maxP = maxinp
    while(True):
        m = int ((minP + maxP)/2)
        if ( isPa(w,k,m) ):
            maxP = m
        else:
            minP = m
        if (minP+1 == maxP or minP == maxP):
            print(maxP)
            break

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


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




def can(p_org,k):
	p = p_org
	w = w_org[::-1]
	while k and w:
		ww = w[-1]
		if ww <= p:
			p -= ww
			w.pop()
		else:
			p = p_org
			k -= 1
		if p == 0:
			p = p_org
			k -= 1
		
	return not(w)


left,right = max(w_org)-1,sum(w_org)+1
roop = 0
while left+1 < right:
	roop += 1
	middle = (left+right) // 2
	if can(middle,k):
		right = middle
	else:
		left = middle
print(right)

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

w = []

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

def f(p, k, w):
        tmpk = 0
        tmpw = 0
        cnt = 0

        for i in w:
                if tmpk < k - 1:
                        if tmpw + i <= p:
                                tmpw += i
                                cnt += 1
                        else:
                                if i <= p:
                                        tmpk += 1
                                        tmpw = i
                                        cnt += 1
                                else:
                                        break
                else:
                        if tmpw + i <= p:
                                tmpw += i
                                cnt += 1
                        else:
                                break
        return cnt

    
pmin = 1
pmax = sum(w)

while pmin < pmax:
        mid = (pmin + pmax) // 2
        tmp = f(mid, k , w)

        if tmp < n:
                pmin = mid + 1
        else:
                pmax = mid

print(pmin)

Solution (Python):


"""
Allocation

????????????????????? wi(i=0,1,...n???1) ??? n ??????????????????
????????????????????¢????????????????????????????????????
????????????????????? k ??°?????????????????????????????????
???????????????????????£?¶??????? 0 ?????\??????????????????????????¨?????§???????????????
???????????????????????????????????????????????§????????? P ????¶????????????????????????????
?????§????????? P ?????????????????????????????§??±?????§??????

n???k???wi ???????????????????????§????????????????????????????????????????????????
?????§????????? P ???????°????????±???????????????°????????????????????????????????????

"""
import sys


def kenzan(P, w, n, k):

    t = 1 
    s = 0 
    st = 0
    for i in range(n):
        x = s + w[i]

        if x > P: 
            t += 1
            st += s
            s = w[i]

            if t > k: 

                return st
        else:
            s = x


    return 0

def main():
    """ ????????? """
    n,k = list(map(int,input().split()))
    istr = sys.stdin.read()
    wi = list(map(int,istr.splitlines()))

    
    P = 0
    total = 0
    min = 100000
    for w in wi:
        if P < w:
            P = w
        if min > w:
            min = w
        total += w
    na = int(total / n) 
    if total % n > 0:
        na += 1
    ka = int(total / k) 
    if total % k > 0:
        ka += 1
    

    if P < na:
        P = na
    if P < ka:
        P = ka
    z = 0
    while P <= total:
        st = kenzan(P, wi, n, k)

        if st == 0:
            a = P - z
            b = P
            break
        z = int((total-st)/k)
        if z < 1:
            z = 1
        P += z
    
    while b - a > 0:
        x = int((b - a) / 2)
        if x < 1:
            a = b
        P = x + a
        st = kenzan(P, wi, n, k)

        if st == 0:
            b = P
        else:
            a = P

    print(P)


if __name__ == '__main__':
    main()

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):
n, k = map(int, input().split())
w = [int(input()) for i in range(n)]

def f(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 = 100000 * 10000

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

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

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

max_weight = sum(a)
left = 0
right = max_weight
ans = 0
while left < right:
    mid = int((left + right) / 2)
    if calcNum(mid) >= n:
        right = mid
    elif calcNum(mid) < n:
        left = mid + 1
print right

Solution (Python):
n,k = map(int, input().split())
lag = []
for l in range(n):
    lag.append(int(input()))
w_max = 100000*100000
w_min = 0
while w_min < w_max:
    w_mid = (w_max + w_min) // 2
    tracks = 0
    current = 0
    for i in range(n):
        if lag[i] > w_mid:
            tracks = k
            break
        elif lag[i] + current > w_mid:
            tracks += 1
            current = lag[i]
        else:
            current += lag[i]
    if tracks < k:
        w_max = w_mid
    else:
        w_min = w_mid + 1
print(w_max)

Solution (Python):

N, k = [int(n) for n in input().split()]
W = []

def search(W, k, P):
    tmp = 0
    track = 1
    for w in W:
        if tmp + w <= P:
            tmp += w
        else:
            track += 1
            tmp = w
    if track > k:
        return False
    else:
        return True         
        

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

left = max(sum(W) // N, max(W))
right = sum(W)

while right - left > 1 :
    mid = (left + right)//2
    if search(W, k, mid):
        right = mid
    else:
        left = mid
    
if search(W, k, left):
    print(left)
else:
    print(right)

Solution (Python):

import sys, math, os


PYDEV = os.environ.get('PYDEV')
if PYDEV=="True":
    sys.stdin = open("sample-input2.txt", "rt")


def check(n, k, W, p):
    idx = 0
    for i in range(k):
        S = 0
        while W[idx] + S <= p:
            S += W[idx]
            idx += 1
            if idx == n:
                return True
    return False

    
n, k = [int(_) for _ in input().split()]
W = [int(input()) for _ in range(n)]
left, right = 0, 10**16
while right - left > 1:
    mid = (left + right) // 2
    if check(n, k, W, mid):
        right = mid
    else:
        left = mid
print(right)

Solution (Python):


def ca(n,k,w,b):
    cn=0
    m=0
    l=0
    
    while l <n and cn<=k:
        d=0
        
        while d<b and l<n:
          
            d+=w[l]
            l+=1
          
        if d>b:
    
            l-=1
        cn+=1
 


    
    if cn>k:
        return 1
    else:
        return -1
  
def bs(n,k,w,Mx,U):
    while Mx+1<U:
        t=U-Mx
        t//=2
        t+=Mx
 
        if ca(n,k,w,t)==-1:
            U=t
        else:
            Mx=t
  
    if ca(n,k,w,Mx)==-1:
        return Mx
    else:
        return U
n,k=(int(i) for i in input().split())
w=[int(input())]

Mx=w[0]
for i in range(1,n):
    w.append(int(input()))
    if w[i]>Mx:
        Mx=w[i]
a=n//k
if n%k!=0:
    a+=1
U=a*Mx

Mx=bs(n,k,w,Mx,U)
print(Mx)

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

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

l, r = 0, 10000 * n
while r - l > 1:
    m = (l + r) // 2
    if check(m):
        r = m
    else:
        l = m

print(r)

Solution (Python):


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

weight=[int(input()) for i in range(n)]
def binary_search(start,end):
    left = start
    right = end
    middle = 0
    while left < right:
        middle = (left + right)//2
        if simulate(middle,weight,k):
            right = middle
        else:
            left = middle + 1
    return left
 
def simulate(prb,wei,num):
    count = 1
    tmp = 0
    for j in wei:
        tmp += j
        if tmp > prb:
            tmp = j
            count += 1
            if count > num:
                return False
    return True
 
print(binary_search(max(weight), sum(weight)))

Solution (Python):
n, k = map(int, raw_input().split())
w = [int(raw_input()) for _ in xrange(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 True
    return False


def solver():
    left, right = 0, 100000 * 10000

    while left < right:
        mid = int((left + right) / 2)

        if check(mid):
            right = mid
        else:
            left = mid + 1
    
    return right


print solver()

Solution (Python):


def p(k,w,maxP):
    count = 1
    tmpWeight = 0
    for x in w:
        if tmpWeight+x <= maxP:
            tmpWeight += x
        else:
            count += 1
            if count > k :
                return False
            tmpWeight = x
    

    return True




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


w = []

for _ in range(n):
    w.append((int)(input()))
rangeMax = sum(w)
rangeMin = max(w)
rangeMid = 0
while rangeMin < rangeMax:
    rangeMid = (rangeMax + rangeMin)//2
    '''print(p(k,w,rangeMid))
    print(p(k,w,rangeMid+1))
    print("---")
    '''
    if p(k,w,rangeMid) == True:
        rangeMax = rangeMid
    else:
        rangeMid += 1
        rangeMin = rangeMid

print(rangeMid)

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):

def countPakages(P, k, W):
    i, n = 0, len(W)
    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 ALDS1_4D():
    n_limit, k_limit, wi_limit = 100000, 100000, 10000
    n, k= map(int, input().split())
    W = [int(input()) for i in range(n)]
    Pleft, Pright = 0, n_limit * wi_limit
    while Pright - Pleft > 1:
        Pmid = (Pleft+Pright)//2
        v = countPakages(Pmid, k, W)
        if v < n:
            Pleft = Pmid
        else:
            Pright = Pmid

    return Pright
if __name__ == '__main__':
    print(ALDS1_4D())

Solution (Python):
n, k = map(int, input().split())
klist = [0 for _ in range(k)]
w = [int(input()) for _ in range(n)]
def check(h, wl, knum):
    cnt = 1
    tmpw = 0
    for w in wl:
        tmpw += w
        if tmpw > h:
            cnt += 1
            tmpw = w
            if cnt > knum:
                return False
    return True
left = max(w)
right = sum(w)
while left  < right:
    mid = (right + left)//2
    if check(mid, w, k):
        right = mid
    else:
        mid += 1
        left = mid
print(mid)

Solution (Python):

import sys


def allcation(truck, baggage, limit):
    t = 1
    cap = 0
    for weight in baggage:
        if weight <= limit:
            if cap + weight <= limit:
                cap += weight
            else:
                cap = weight
                t += 1

            if t > truck:
                return False
        else:
            return False
    return True


def bin_search(truck, baggage):
    small = 0
    big = 10000 * 100000
    minimum = 0
    while small < big:
        mid = (small + big) >> 1
        if allcation(truck, baggage, mid):
            big = mid
            minimum = mid
        else:
            small = mid + 1

    return minimum


if __name__ == "__main__":
    n = [int(val) for val in input().split()]
    lst = [int(val) for val in sys.stdin.read().splitlines()]
    print(bin_search(n[1], lst))

Solution (Python):



nk=input().split()

n=int(nk[0])
k=int(nk[1])

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

def ok(m):
    load=0
    track=1
    
    for i in range(n):
        load+=weight[i]
        if load==m:
            load=0
            if i!=n-1:
                track+=1
        elif load>m:
            if weight[i]>m:
                return False
            else:
                load=weight[i]
                track+=1
        else:
            continue

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

low=0
high=sum(weight)
while low+1<high:


    mid=(low+high)//2
    if ok(mid):
        high=mid
    else:
        low=mid


print(high)

Solution (Python):
n, k = map(int, raw_input().split())
w = []
max = 0
sum = 0
for i in xrange(n):
	w_i = int(raw_input())
	if w_i > max:
		max = w_i
	sum += w_i
	w.append(w_i)

def can_put(w, p, k):
	weight = 0
	cnt = 1
	for w_i in w:
		if weight + w_i <= p:
			weight += w_i
		else:
			cnt += 1
			if cnt > k:
				return 0
			else:
				weight = w_i
	return 1

low = max
high = sum

while low < high:
	mid = (low + high) / 2
	
	if can_put(w, mid, k) == 1:
		
		high = mid
	else:
		
		low = mid + 1
	
print high

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

for i in range(n):
    w_in.append(int(input()))


def alloc(p):
    load = 0
    track = 1
    for w in w_in:
        if w > p:
            return False
        if load + w <= p:
           load += w
        else:
            load = w
            track += 1
            if track > k:
                return False
    return True


def binsearch():
    upper = sum(w_in)
    lower = max(w_in)
    while(upper != lower):
        mid = int((upper + lower) / 2)
        if alloc(mid):
            upper = mid
        else:
            lower = mid + 1
    return upper
print(binsearch())

Solution (Python):



nk=input().split()

n=int(nk[0])
k=int(nk[1])

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

def ok(m):
    load=0
    track=1
    
    for i in range(n):
        load+=weight[i]
        if load==m:
            load=0
            if i!=n-1:
                track+=1
        elif load>m:
            if weight[i]>m:
                return False
            else:
                load=weight[i]
                track+=1
        else:
            continue

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

low=0
high=sum(weight)
while low+1<high:


    mid=(low+high)//2
    if ok(mid):
        high=mid
    else:
        low=mid



print(high)

Solution (Python):
import sys
readline = sys.stdin.readline
n, k = map(int, input().split())
v = [int(readline()) for _ in range(n)]
lo = max(v) - 1
hi = int(1e10)
while hi - lo > 1:
    p = P = (lo + hi) // 2
    m = 1
    for i in range(n):
        if v[i] > p:
            p = P
            m += 1
        p -= v[i]
    if m <= k:
        hi = P
    else:
        lo = P
print(hi)

Solution (Python):
import sys
readline = sys.stdin.readline
n, k = map(int, input().split())
v = tuple(int(readline()) for _ in range(n))
lo = max(v) - 1
hi = int(1e10)
while hi - lo > 1:
    p = P = (lo + hi) // 2
    m = 1
    for i in range(n):
        if v[i] > p:
            p = P
            m += 1
        p -= v[i]
    if m <= k:
        hi = P
    else:
        lo = P
print(hi)

Solution (Python):
import sys
readline = sys.stdin.readline
n, k = map(int, readline().split())
v = [int(readline()) for _ in range(n)]
lo = max(v) - 1
hi = sum(v)
while hi - lo > 1:
    p = P = (lo + hi) // 2
    m = 1
    for i in range(n):
        if v[i] > p:
            p = P
            m += 1
        p -= v[i]
    if m <= k:
        hi = P
    else:
        lo = P
print(hi)

Solution (Python):
import sys
readline = sys.stdin.readline
N, K = map(int, input().split())
W = tuple(int(readline()) for _ in range(N))
ma = max(W)
def check(x):
    if x < ma:
        return False
    use = 1
    rest = x
    for w in W:
        if rest >= w:
            rest -= w
        else:
            rest = x - w
            use += 1
    return use <= K
l = 0
r = sum(W)
while r - l > 1:
    m = (r + l) // 2
    if check(m):
        r = m
    else:
        l = m
print(r)

Solution (Python):
import sys
readline = sys.stdin.readline
N, K = map(int, input().split())
W = tuple(map(int, sys.stdin.read().splitlines()))
ma = max(W)
def check(x):
    if x < ma:
        return False
    use = 1
    rest = x
    for w in W:
        if rest >= w:
            rest -= w
        else:
            rest = x - w
            use += 1
    return use <= K
l = 0
r = sum(W)
while r - l > 1:
    m = (r + l) // 2
    if check(m):
        r = m
    else:
        l = m
print(r)

Solution (Python):
import sys
readline = sys.stdin.readline
N, K = map(int, input().split())
W = tuple(map(int, sys.stdin.read().splitlines()))
ma = max(W)
def check(x):
    if x < ma:
        return False
    use = 1
    rest = x
    for w in W:
        if rest >= w:
            rest -= w
        else:
            rest = x - w
            use += 1
    return use <= K
l = 0
r = sum(W)
while r - l > 1:
    m = (r + l) // 2
    if check(m):
        r = m
    else:
        l = m
print(r)

Solution (Python):
import sys
readline = sys.stdin.readline
N, K = map(int, input().split())
W = tuple(map(int, sys.stdin.readlines()))
ma = max(W)
def check(x):
    if x < ma:
        return False
    use = 1
    rest = x
    for w in W:
        if rest >= w:
            rest -= w
        else:
            rest = x - w
            use += 1
    return use <= K
l = 0
r = sum(W)
while r - l > 1:
    m = (r + l) // 2
    if check(m):
        r = m
    else:
        l = m
print(r)