Russian Dolls
Time limit3sMemory limit128 MB
Arrange all dolls into nested chains where each doll fits only inside a strictly roomier one to minimize the total cost of leftover empty space.
- Level
Medium7 of 10
- Topics
- Dynamic programming, Sorting, Graph
- Solved
- No attempts yet
Problem
A Russian doll is a wooden souvenir where a smaller doll sits inside a bigger one. Take the whole set apart and look at the dolls one by one. Doll has an outer volume , the volume it occupies in space, and an inner volume , the volume of the empty space inside it. You may put one doll inside another when the outer volume of the first doll is strictly less than the inner volume of the second one. Two dolls that go inside the same doll cannot lie side by side, so they must be nested one inside the other.
Every doll charges for the empty space left in it. You pay for each unit of empty space that belongs directly to doll , that is, the part of its inner space that the doll placed directly inside it does not occupy. A doll with nothing inside it has all of empty. You may arrange the dolls any way the rules allow, and you do not have to nest all of them. Find the smallest total cost you have to pay.
Input
The first line contains an integer (), the number of dolls. The -th of the next lines contains three integers , , (, ), the outer volume, the inner volume, and the cost of one unit of empty space of doll .
Output
Print one integer , the minimum cost you have to pay.