Tulip Bouquets

Time limit: 1.5s Memory limit: 512MB Input: Output:

Task

Anna really likes tulips. She has NN tulips in her garden, numbered from 00 to N1N-1. The beautiness of tulip ii is TiT_i.
She wants to create KK (non-empty) bouquets from the tulips. To do that, she starts to walk from the first tulip towards the last.
At each flower, she can either

  • insert it into the current bouquet, or
  • finish the current bouquet and start a new one. The current tulip is added to the new bouquet.

Tulips are indeed beautiful.\text{Tulips are indeed beautiful.}

The beautiness of a bouquet is the minimum of the beautinesses of the tulips in it.
She wants to maximize the sum of the beautinesses of the KK bouquets by partitioning the tulips optimally.
Your task is to calculate this maximum value!

Input data

The input file consists of:

  • a line containing integers NN, KK.
  • a line containing the NN integers T0,,TN1T_{0}, \, \ldots, \, T_{N-1}.

Output data

The output file must contain a single line with an integer MM, the maximum total beautiness of the bouquets.

Constraints and clarifications

  • 1KN100 0001 \le K \le N \le 100 \ 000.
  • 1NK51071 \le N \cdot K \le 5 \cdot 10^7.
  • 0Ti1090 \le T_i \le 10^9 for each i=0N1i=0\ldots N-1.
# Points Constraints
1 0 Examples.
2 12 K2K \le 2
3 25 N16N \leq 16
4 25 N500N \leq 500
5 38 No additional limitations.

Example 1

stdin

5 2
3 4 1 5 2

stdout

4

Explanation

In the first sample case 341523\: 4\: |\: 1\: 5\: 2 is an optimal way to distribute the flowers into 22 bouquets.
The total beautiness is 3+1=43+1=4.

Example 2

stdin

6 4
4 2 6 1 3 5

stdout

14

Explanation

In the second sample case 4261354\: 2\: |\: 6\: |\: 1\: 3\: |\: 5 is an optimal way to distribute the flowers into 44 bouquets.
The total beautiness is 2+6+1+5=142+6+1+5=14.

Log in or sign up to be able to send submissions!