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

#BS0000322. Chia thanh sô cô la (Divide Chocolate)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Chia thanh sô cô la (Divide Chocolate)

Divide Chocolate

Source: LeetCode

Version: Phuoc Hung OJ Extended

Problem Statement

A chocolate bar has n consecutive chunks with positive sweetness aia_i. Make exactly k cuts between chunks, yielding k+1 nonempty consecutive pieces. You receive a piece with the smallest total sweetness. Maximize that minimum.

Input

  • The first line contains integers n,kn,k, the number of chunks and cuts.
  • The second line contains nn integers a1,…,ana_1,\ldots,a_n, sweetness values in bar order.

Output

Print the maximum possible minimum piece sweetness.

Subtasks

  • Subtask 1 (20 points): 1≤n≤121\le n\le12; 0≤k<n0\le k<n; 1≤ai≤201\le a_i\le20.
  • Subtask 2 (30 points): 1≤n≤1041\le n\le10^4; 0≤k<n0\le k<n; 1≤ai≤1051\le a_i\le10^5; a1=⋯=ana_1=\cdots=a_n.
  • Subtask 3 (50 points): 1≤n≤1041\le n\le10^4; 0≤k<n0\le k<n; 1≤ai≤1051\le a_i\le10^5.

Examples

Input 1

9 5
1 2 3 4 5 6 7 8 9

Output 1

6

Explanation

The six pieces (1,2,3), (4,5), (6), (7), (8), (9) have sums 6, 9, 6, 7, 8, 9. A minimum of 7 requires at least four chunks in the first piece. The five remaining chunks must form five separate pieces, including the chunk of sweetness 5, below 7. Thus 6 is optimal.

Input 2

9 8
5 6 7 8 9 1 2 3 4

Output 2

1

Explanation

Eight cuts form nine pieces from nine chunks. Each piece contains one chunk, and the least sweet chunk has value 1. No alternative partition exists.

Input 3

9 2
1 2 2 1 2 2 1 2 2

Output 3

5

Explanation

Split into three pieces (1,2,2), (1,2,2), (1,2,2), each of sum 5. The total 15 is insufficient for three pieces of sum at least 6.