PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#CT0000073. Phân lô hàng (Cargo Lot Partitioning)

Bài tập này chưa được xếp vào lộ trình.

Phân lô hàng (Cargo Lot Partitioning)

Cargo Lot Partitioning

Version: Phuoc Hung OJ Extended

Problem Statement

A warehouse contains nn parcels arranged in their original order from 11 to nn. Parcel ii has weight aia_i.

The warehouse manager must divide all parcels into at most kk lots. Each lot must be nonempty and contain a contiguous block of parcels. Every parcel belongs to exactly one lot, and the original order cannot be changed.

The weight of a lot is the sum of the weights of its parcels. For any partition, consider the weight of its heaviest lot.

Task: Minimize that heaviest-lot weight over all valid partitions.

Input

  • The first line contains two positive integers nn and kk.
  • The second line contains exactly nn positive integers a1,a2,…,ana_1,a_2,\ldots,a_n, the parcel weights in order.

Output

Print one integer: the smallest achievable weight of the heaviest lot.

Subtasks

  • Subtask 1 (25 points): 1≤k≤n≤181\le k\le n\le 18; 1≤ai≤1091\le a_i\le 10^9 for all 1≤i≤n1\le i\le n.
  • Subtask 2 (35 points): 1≤k≤n≤20001\le k\le n\le 2000; 1≤ai≤1091\le a_i\le 10^9 for all 1≤i≤n1\le i\le n.
  • Subtask 3 (40 points): 1≤k≤n≤1051\le k\le n\le 10^5; 1≤ai≤1091\le a_i\le 10^9 for all 1≤i≤n1\le i\le n.

Examples

Example 1

Input

5 3
7 2 5 10 8

Output

14

Explanation

Partitioning into [7, 2, 5], [10], [8] yields lot weights 14,10,814,10,8, with maximum 1414. A maximum below 1414 is impossible: with a limit of 1313, the parcel of weight 55 cannot join the first pair of weights 7,27,2 or the following parcel of weight 1010, so at least four lots would be needed.

Example 2

Input

4 2
2 2 2 2

Output

4

Explanation

Split [2, 2, 2, 2] into [2, 2] and [2, 2]. Both lots weigh 44. As the total weight is 88, at most two lots cannot both weigh less than 44.

Example 3

Input

3 1
5 1 4

Output

10

Explanation

With k=1k=1, all parcels of weights 5,1,45,1,4 must belong to the sole lot, giving weight 5+1+4=105+1+4=10.