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

#GD0000234. Vận chuyển hàng trong D ngày (Capacity To Ship Packages Within D Days)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

2 lộ trình

Vận chuyển hàng trong D ngày (Capacity To Ship Packages Within D Days)

Capacity To Ship Packages Within D Days

Source: LeetCode

Version: Phuoc Hung OJ Extended

Problem Statement

A conveyor belt contains nn packages in a fixed order, with weights wiw_i. Ship all packages in at most dd days using a ship of constant daily capacity CC.

Each day's cargo must consist of a consecutive prefix of the packages still waiting; its total weight must not exceed CC. Packages cannot be reordered or split.

Find the minimum integer capacity CC to finish on time.

Input

  • The first line contains n,dn,d.
  • The next line contains the nn positive package weights w1,…,wnw_1,\ldots,w_n in conveyor order.

Output

Print the smallest possible ship capacity.

Subtasks

  • Subtask 1 (20 points): 1≤n≤201\le n\le 20; 1≤d≤n1\le d\le n; 1≤wi≤5001\le w_i\le 500.
  • Subtask 2 (30 points): 1≤n≤20001\le n\le 2000; 1≤d≤n1\le d\le n; 1≤wi≤5001\le w_i\le 500.
  • Subtask 3 (50 points): 1≤n≤500001\le n\le 50000; 1≤d≤n1\le d\le n; 1≤wi≤5001\le w_i\le 500.

Examples

Input

5 4
1 2 3 1 1

Output

3

Explanation

With capacity 33, ship [1,2][1,2], then [3][3], then [1,1][1,1], using only three of the four available days. Capacity 22 cannot carry the package of weight 33, so the minimum is 33.