Guardians of the Lunatics
Time limit7sMemory limit512 MB
Split a row of L cells into at most G contiguous nonempty blocks, where a block of length k multiplies each member's craziness by k, to minimize the total cost.
- Level
Hard8 of 10
- Topics
- Dynamic programming, Divide and conquer, Prefix sum, Greedy
- Solved
- No attempts yet
Problem
You assign the guards of a prison that holds the most dangerous criminals. The cells stand in one row and are numbered 1 to . Cell holds exactly one lunatic whose craziness level is .
One guard per lunatic would be ideal, but the budget pays for only guards. Decide which lunatics each guard watches so that the total risk of an escape is as small as possible.
Each guard watches a set of adjacent cells. A guard may be left with no cell at all. The risk that the lunatic in cell escapes is the product of the craziness level and the number of lunatics watched by the guard assigned to that cell. Adding up from to gives the total risk .
Given lunatics and guards, find the minimum possible value of .
Input
The first line contains one integer , the number of test cases.
The first line of each test case contains two integers and separated by a space, the number of lunatics and the number of guards. Each of the next lines contains one integer, and the th of them is the craziness level of the lunatic in cell .
Constraints
Output
For each test case, print the minimum possible value of the total risk on its own line.