Mahamba wants to participate in International Olympiad in Informatics. In order to get a place in a national team, he must solve a simple programming task. The problem is that Mahamba has no idea how to solve it. So, as always, he asked you, the greatest programmer in Lalalandia, to help him!
You must divide the sequence A of length N into K consecutive segments so that each element is in only one segment and the sum of minimums of segments is maximum (see sample explanation for more details).
Input format
The first line contains N and K - the length of the sequence and the number of needed fragments respectively. The second line contains N numbers.
Output format
The only line of output should contain the answer to the problem.
Constraints
\(1 <= N <= 10^5\)
\(1 <= K <= min(N, 500)\)
\(0 <= A_i <= 10^9\) for each \(1 <= i <= N\)
P.S.: I discourage people to actually cheat on their exams. Use only your brain! =)
5 3 1 2 3 5 4
10
We can divide the array into blocks with ranges (1, 3), (4, 4) and (5, 5), thus the answer is min(1, 2, 3) + min(5) + min(4) = 10
Please login to use the editor
You need to be logged in to access the code editor
Loading...
Please wait while we load the editor
Login to unlock the editorial
Please login to use the editor
You need to be logged in to access the code editor
Loading...
Please wait while we load the editor