problems / 128

Log Shipping Windows

medium binary-search greedy arrays 1000 ms · 256 MB

Log Shipping Windows

A log shipper on an edge server has buffered n log chunks, numbered 1 to n in the order they were written. The i-th chunk is a_i bytes long. The central log store only accepts uploads during scheduled upload windows, and tonight there are D of them.

Chunks must arrive in order, and a chunk cannot be split across windows. So each window uploads one contiguous run of chunks: the first window sends chunks 1 to j_1, the next sends chunks j_1 + 1 to j_2, and so on. A window may also be left unused. Every window is configured with the same byte budget B, and the total size of the chunks sent in a single window must not exceed B.

Find the smallest budget B (in bytes) that lets all n chunks be shipped within the D windows.

Input

The first line contains two integers n and D (1 ≤ D ≤ n ≤ 2·10^5) — the number of chunks and the number of upload windows.

The second line contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^9) — the chunk sizes in bytes, in shipping order.

Output

Print a single integer: the minimum byte budget B per window such that all chunks can be shipped, in order, using at most D windows.

Note that the answer can exceed 2^32, so use 64-bit integers.

Notes

In the first example, a budget of 18 works: the first window sends chunks of sizes 7, 2, 5 (14 bytes) and the second sends 10, 8 (18 bytes). No split into two contiguous runs keeps both runs at 17 bytes or less.

In the second example the total is 22 bytes over 3 windows, yet a budget of 9 is not enough: each 9-byte chunk then needs a window to itself, which forces the runs 1 | 9 | 1+1+1 | 9 — four windows. With a budget of 10 the windows send 1+9, 1+1+1 and 9.

Example 1

Input

5 2
7 2 5 10 8

Output

18

Example 2

Input

6 3
1 9 1 1 1 9

Output

10

Hints · reveal one at a time if you're stuck

Hint 1

If a budget B is enough, is every larger budget enough too? That monotonicity suggests searching over B rather than over the splits directly. What range of budgets could possibly be the answer?

Hint 2

For a fixed budget, you can check the number of windows needed with a single left-to-right pass that fills each window as far as the budget allows. Chunks cannot be split, so a window must hold at least one chunk.

Hint 3

Binary search the budget with that check. The bounds are the largest single chunk and the total of all chunks, and sums can exceed 32 bits.

report an issue Prefer your own editor? sooks prep 128 (CLI)
Sign in to run code

Run your code on the examples, or submit it against every test.