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

#BS0000254. Chia mảng để tối thiểu hóa tổng đoạn lớn nhất (Split Array Largest Sum)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Chia mảng để tối thiểu hóa tổng đoạn lớn nhất (Split Array Largest Sum)

Split Array Largest Sum

Source: LeetCode

Version: Phuoc Hung OJ Extended

Problem Statement

Given nn nonnegative integers a1,a2,…,ana_1, a_2,\ldots,a_n, choose k−1k-1 cuts between elements to obtain exactly kk nonempty contiguous blocks. Minimize the largest block sum. Output the optimal value, not the cuts.

Input

The first line contains n,kn,k: the number of elements and blocks.

The second line contains exactly nn integers a1,a2,…,ana_1,a_2,\ldots,a_n, representing the array elements.

Output

Print the smallest possible largest block sum.

Subtasks

  • Subtask 1 (20 points): 1≤n≤121\le n\le 12; 0≤ai≤500\le a_i\le 50 for every 1≤i≤n1\le i\le n; 1≤k≤min⁡(50,n)1\le k\le \min(50,n).
  • Subtask 2 (30 points): 1≤n≤10001\le n\le 1000; 0≤ai≤1060\le a_i\le 10^6 for every 1≤i≤n1\le i\le n; 1≤k≤min⁡(50,n)1\le k\le \min(50,n); a1=a2=⋯=ana_1=a_2=\cdots=a_n.
  • Subtask 3 (50 points): 1≤n≤10001\le n\le 1000; 0≤ai≤1060\le a_i\le 10^6 for every 1≤i≤n1\le i\le n; 1≤k≤min⁡(50,n)1\le k\le \min(50,n).

Examples

Input

5 2
7 2 5 10 8

Output

18

Explanation

The four possible cuts give sum pairs (7,25)(7,25), (9,23)(9,23), (14,18)(14,18) and (24,8)(24,8). Their maxima are 25, 23, 18 and 24. Cutting after the third element achieves 18.