Jailbreak
Time limit2sMemory limit512 MB
Partition L cells into at most G consecutive blocks, minimizing the sum over each cell of its escape power times its block length.
- Level
Hard8 of 10
- Topics
- Dynamic programming, Divide and conquer, Prefix sum
- Solved
- No attempts yet
Problem
A prison has cells in a row. The cells are numbered 1 through , and cell holds one prisoner. The escape power of the prisoner in cell is , a number that measures how well that prisoner can break out.
The ideal way to watch the prison is to put one guard on every cell, so that each guard watches a single prisoner. Because of the budget, this prison can hire at most guards. You want to hire and place the guards so that the escape risk is as small as possible.
Each guard watches cells with consecutive numbers, and every cell is watched by exactly one guard. The escape risk of cell is the escape power multiplied by the number of prisoners watched by the guard in charge of cell . The escape risk of the prison is the sum of over all cells.
Given , , and the escape power of the prisoner in each cell, write a program that finds the minimum escape risk.
Input
The first line contains the size of the prison and the number of guards , separated by a space. (, )
The second line contains separated by spaces. ()
Output
Print the minimum escape risk on the first line.
Hint
In the first example the escape risk is smallest when one guard watches cells 1, 2, 3, another guard watches cells 4, 5, and the remaining guard watches cell 6.
The guard in charge of cells 1, 2, 3 watches three prisoners, so the escape risk of each of those cells is .
The guard in charge of cells 4, 5 watches two prisoners, so cell 4 gives and cell 5 gives .
The guard in charge of cell 6 watches one prisoner, so cell 6 gives . The total is .