Repelling Monsters in the Magic Dungeon
Time limit0.5sMemory limit1024 MB
Bert starts at floor 0 and walks up or down to clear monsters, each costing w per hour until cleared; the task is to minimize total drained power over the walk.
- Level
Medium7 of 10
- Topics
- Greedy, Dynamic programming, Sorting
- Solved
- No attempts yet
Problem
Bert is a guardian of a magic dungeon. The dungeon has floors 1 to above ground (written as positive integers from to ) and floors 1 to below ground (written as negative integers from down to ). Bert's lodge is on floor 0.
Recently, monsters that drain magic power from the dungeon started appearing, so the Magic Association assigned Bert to exterminate them. There are kinds of monsters in the dungeon, numbered 1 to . The -th kind lives on floor and takes units of magic power from the dungeon every hour. Two different kinds of monsters never live on the same floor.
Bert starts on floor 0 and can move up or down one floor per hour. As soon as he arrives on a floor, he uses a powerful spell that instantly removes all monsters on that floor. Bert wants to minimize the total magic power the monsters take.
Input
The first line contains the number of test cases .
The first line of each test case contains . The next lines each contain and , separated by a space.
Output
For each test case, print the answer on its own line.
Constraints
- For all with :
- For all with :