Problem:


  MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }});

  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.



Sample input:
5 3
8
1
7
3
9


Sample output:
10

Submission status: Accepted

Solution code:
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()

Submission status: Accepted

Solution code:
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

Submission status: Accepted

Solution code:
n,k = [int(s) for s in input().split()]
#n,k = [int(s) for s in data.pop().split()]
#n,k = 100000,100000
w_org = []
[w_org.append(int(input())) for i in range(n)]
#[w_org.append(int(data.pop())) for i in range(n)]
#w_org = [1 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
		#print(ww,k,p)
	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)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Allocation

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

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

"""
import sys

# ??????????????\????????????
def kenzan(P, w, n, k):
#    print("P:{} kosu:{} track:{}".format(P,n,k))
    t = 1 # ????????????
    s = 0 # ?????????
    st = 0
    for i in range(n):
        x = s + w[i]
#        print("max:{} nimotu[{}]:{} + sekisai:{} = {} track:{}".format(P,i,w[i],s,x,t))
        if x > P: # ????????????
            t += 1
            st += s
            s = w[i]
#            print("next track:{} sekisai:{}".format(t,s))
            if t > k: # ??°??°????????????
#                print("\ttrack:{}/{} P:{} sekisai:{}".format(t,k,P,st))
                return st
        else:
            s = x

#    print("\ttrack:{}/{} P:{} sekisai:{}".format(t,k,P,st))
    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
    # ?????? P (??????????????§??????????????????????????????????±?????????????
#    print("kosu:{} MAX:{} MIN:{} na:{} ka:{} total:{}".format(n, P, min, na, ka, total))
    if P < na:
        P = na
    if P < ka:
        P = ka
    z = 0
    while P <= total:
        st = kenzan(P, wi, n, k)
#        print("??????????????°:{} ??????????????°??°:{} ?????????????????§??????:{} ????????????????????????:{} ??????????????????:{} ??????:{}".format(n, k, P, total, st,z))
        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)
#        print("P:{} MAX:{} MIN:{} sekisai:{}".format(P, b, a, st))
        if st == 0:
            b = P
        else:
            a = P

    print(P)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
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)

Submission status: Accepted

Solution code:
# Aizu Problem ALDS_1_4_D: Allocation
import sys, math, os

# read input:
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)

Submission status: Accepted

Solution code:
# your code goes here
#allocation
def ca(n,k,w,b):
    cn=0
    m=0
    l=0
    #print(k)
    while l <n and cn<=k:
        d=0
        #print(l)
        while d<b and l<n:
          #  print(l)
            d+=w[l]
            l+=1
          #  print(l)
        if d>b:
    #        d-=w[l]
            l-=1
        cn+=1
 #       print(l)
#        print(n)
#        print(cn)
    #    print(k)
    if cn>k:
        return 1
    else:
        return -1
  #  print(d)
def bs(n,k,w,Mx,U):
    while Mx+1<U:
        t=U-Mx
        t//=2
        t+=Mx
 #       print(t)
        if ca(n,k,w,t)==-1:
            U=t
        else:
            Mx=t
  #      print(Mx)
    if ca(n,k,w,Mx)==-1:
        return Mx
    else:
        return U
n,k=(int(i) for i in input().split())
w=[int(input())]
#print(w)
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
#print(w)
Mx=bs(n,k,w,Mx,U)
print(Mx)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# coding: utf-8
# Here your code !
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)))

Submission status: Accepted

Solution code:
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()

Submission status: Accepted

Solution code:


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)

Submission status: Accepted

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

Submission status: Accepted

Solution code:

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# -*- coding:utf-8 -*-
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))

Submission status: Accepted

Solution code:



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
    # sum all the weight and count the track number we need
    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:
# low<mid<high
#not ok(l) and ok(h)
    mid=(low+high)//2
    if ok(mid):
        high=mid
    else:
        low=mid


print(high)

Submission status: Accepted

Solution code:
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
	#print high, mid, low
	if can_put(w, mid, k) == 1:
		#print("*1")
		high = mid
	else:
		#print("*0")
		low = mid + 1
	#print high, mid, low
print high

Submission status: Accepted

Solution code:
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())

Submission status: Accepted

Solution code:



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
    # sum all the weight and count the track number we need
    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:
# low<mid<high
#not ok(l) and ok(h)
    mid=(low+high)//2
    if ok(mid):
        high=mid
    else:
        low=mid



print(high)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
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
            if use > K:
                return False
    return True
l = 0
r = sum(W)
while r - l > 1:
    m = (r + l) // 2
    if check(m):
        r = m
    else:
        l = m
print(r)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-


if __name__ == '__main__':

    n, k = [int(s) for s in input().split(" ")]
    W = [int(input()) for _ 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 = 100000 * 10000

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

    print(right)

Submission status: Accepted

Solution code:
setting = input().split();
package_count = int(setting[0]);
truck_count = int(setting[1]);
packages = [];
for i in range(package_count):
    packages.append(int(input()));

def allocation():
    max_p = sum(packages);
    left = 0;
    right = max_p;
    while left < right:
        mid = (left + right) // 2;
        load = calculate_load(mid);
        if load >= package_count:
            right = mid;
        else:
            left = mid + 1;
    return right;

def calculate_load(p):
    i = 0;
    j = 0;
    current_truck_p = p;
    while i < package_count and j < truck_count:
        if packages[i] <= current_truck_p:
            current_truck_p -= packages[i];
            i += 1;
        else:
            j += 1;
            current_truck_p = p;
    return i;

print(allocation());

Submission status: Accepted

Solution code:
def decode():
    [n, k] = [int(x) for x in input().split(" ")]
    ws = []
    for _ in range(n):
        ws.append(int(input()))

    return n, k, ws


def check(k, ws, p):
    sum_w = ws[0]
    tr = 1

    for w in ws[1:]:
        if sum_w + w <= p:
            sum_w += w
        else:
            sum_w = w
            tr += 1

            if tr > k:
                return False

    return True


if __name__ == '__main__':
    n, k, ws = decode()

    lo = max(ws)
    hi = sum(ws)

    while lo <= hi:
        p = (lo + hi) // 2

        if check(k, ws, p):
            hi = p - 1
            ans = p
        else:
            lo = p + 1

    print(ans)

Submission status: Accepted

Solution code:
def imas(hoge_l, p, track):
    s_weights = 0
    now_track = 1
    for val in hoge_l:
        s_weights += val
        if s_weights > p:
            s_weights = val
            now_track += 1
        if now_track > track:
            return False
    return True

num, track = [int(x) for x in input().split()]
hoge_l = list()
for _ in range(num):
    hoge_l.append(int(input()))
left = 0
right = 100000 * 10000

while left < right :
    mid = (right + left) // 2
    if imas(hoge_l, mid, track):
        right = mid 
    else:
        left = mid + 1
        
max_num = max(hoge_l)
if max_num > right:
    right = max_num
print (right)

Submission status: Accepted

Solution code:
def check(P):
 i=0
 for _ in[0]*k:
  s=0
  while s+w[i]<=P:
   s+=w[i];i+=1
   if i==n: return n
 return i
n,k=map(int,input().split())
w=[int(input())for _ in[0]*n]
l=0;r=n*100000
while r-l>1:
 m=(l+r)//2
 if check(m)>=n:r=m
 else:l=m
print(r)

Submission status: Accepted

Solution code:
import sys
def f(P):
 i=0
 for _ in[0]*k:
  s=0
  while s+w[i]<=P:
   s+=w[i];i+=1
   if i==n: return n
 return i
n,k=map(int,input().split())
w=list(map(int,sys.stdin.read().splitlines()))
l=0;r=n*100000
while r-l>1:
 m=(l+r)//2
 if f(m)>=n:r=m
 else:l=m
print(r)

Submission status: Accepted

Solution code:
import sys
def f(P):
 i=0
 for _ in[0]*k:
  s=0
  while s+w[i]<=P:
   s+=w[i];i+=1
   if i==n: return n
 return i
n,k=map(int,input().split())
w=list(map(int,sys.stdin.read().splitlines()))
l=0;r=sum(w)
while r-l>1:
 m=(l+r)//2
 if f(m)>=n:r=m
 else:l=m
print(r)

Submission status: Accepted

Solution code:
import sys
def f(P):
 i=0
 for _ in[0]*k:
  s=0
  while s+w[i]<=P:
   s+=w[i];i+=1
   if i==n: return n
 return i
n,k=map(int,input().split())
w=list(map(int,sys.stdin.readlines()))
l=0;r=sum(w)
while r-l>1:
 m=(l+r)//2
 if f(m)>=n:r=m
 else:l=m
print(r)

Submission status: Accepted

Solution code:
import sys
n,k=map(int,input().split())
w=list(map(int,sys.stdin.readlines()))
def f():
 c=t=0
 for j in w:
  t+=j
  if t>m:
   t=j
   c+=1
   if c>=k:return 0
 return 1
l,r=max(w),sum(w)
while l<r:
 m=(l+r)//2
 if f():r=m
 else:l=m+1
print(r)

Submission status: Accepted

Solution code:
import sys
n,k=map(int,input().split())
w=list(map(int,sys.stdin.readlines()))
def f():
 i=0
 for _ in[0]*k:
  s=0
  while s+w[i]<=m:
   s+=w[i];i+=1
   if i==n:return n
 return i
l,r=max(w),sum(w)
while l<r:
 m=(l+r)//2
 if f()>=n:r=m
 else:l=m+1
print(r)

Submission status: Accepted

Solution code:
from collections import deque

nk = input().split(' ')
n = int(nk[0])
k = int(nk[1])
w = []
for i in range(0, n):
  w.append(int(input()))

# 1台あたりの積載量がvのとき、荷物の配列aを積めるか返す
def allocatable(a, v):
  b = deque(a)
  currentCar = 0
  wSum = 0
  while currentCar < k:
    weight = b.popleft()
    if wSum + weight <= v:
      wSum += weight
    else:
      # 積み切れなければbに戻して次の車へ
      b.appendleft(weight)
      currentCar += 1
      wSum = 0
    # bに要素が残っていなければ積み切った => True
    if not b:
      return True
  # 最後の車まで来て残っていればFalse
  return not b

# 最小の積載量Pを求める
def search(a):
  l = 0
  r = 1000000000
  while l + 1 < r:
    i = int((l + r) / 2)
    if allocatable(a, i):
      r = i
    else:
      l = i
  return r

print(search(w))

Submission status: Accepted

Solution code:
[n, k] = map(int, input().split(' '))
wi = list(map(int, [input() for i in range(n)]))

def isAllocatable(p):
  kw, ki = 0, 1
  for w in wi:
    if w > p: return False
    kw += w
    if kw <= p: continue
    ki += 1
    if k < ki: return False
    kw = w
  return True

l = 0
r = sum(wi)
while l + 1 < r:
  p = int((l + r) / 2)
  if isAllocatable(p): r = p
  else: l = p
print(r)

Submission status: Accepted

Solution code:
n, k = map(int, input().split(' '))
wi = list(map(int, [input() for i in range(n)]))

def isAllocatable(p):
  kw, ki = 0, 1
  for w in wi:
    if w > p: return False
    kw += w
    if kw <= p: continue
    kw = w
    ki += 1
    if k < ki: return False
  return True

l, r = 0, sum(wi)
while l + 1 < r:
  p = int((l + r) / 2)
  if isAllocatable(p): r = p
  else: l = p

print(r)