Ship Packages In D Days

0

0 votes
Problem

There are n packages that must be shipped from one port to another within d days.

The ith package has a weight of w[i]. Each day, we load the ship with packages (in order given by weights array). We can not load more weight than the maximum weight capacity of the ship.

Return the least weight capacity of the ship that will result in all the packages being shipped within d days.

 

Constraints

1 <= d <= w.length <= 105
1 <= w[i] <= 500

Input

First line contains two integers n and d - number of packages and number of days. 

Second line contains n space separated integers w1,w2,.....,w- weight of ith packeage.

Output

 Print least weight capacity.

Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

1st day: 1, 2, 3, 4, 5

2nd day: 6, 7

3rd day: 8

4th day: 9

5th day: 10

So minimum needed capacity of ship to transport all packages within 5 days is from day 1, 1 + 2 + 3 + 4 + 5 = 15.

Arrangement {1, 2, 3, 4}, {5, 6}, {7, 8}, {9}, {10} also give same minimum capacity = 7 + 8 = 15. 

Note that the cargo must be shipped in the order given, so using a ship of capacity 14 and splitting the packages into parts like (2, 3, 4, 5), (1, 6, 7), (8), (9), (10) is not allowed.

Editor Image

?