Task
Anna really likes tulips. She has tulips in her garden, numbered from to . The beautiness of tulip is .
She wants to create (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.
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 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 , .
- a line containing the integers .
Output data
The output file must contain a single line with an integer , the maximum total beautiness of the bouquets.
Constraints and clarifications
- .
- .
- for each .
# | Points | Constraints |
---|---|---|
1 | 0 | Examples. |
2 | 12 | |
3 | 25 | |
4 | 25 | |
5 | 38 | No additional limitations. |
Example 1
stdin
5 2
3 4 1 5 2
stdout
4
Explanation
In the first sample case is an optimal way to distribute the flowers into bouquets.
The total beautiness is .
Example 2
stdin
6 4
4 2 6 1 3 5
stdout
14
Explanation
In the second sample case is an optimal way to distribute the flowers into bouquets.
The total beautiness is .