#codefestivalchinah. [code_festival_china_h]Dungeon
[code_festival_china_h]Dungeon
Problem
You are playing a video game. In this game there is a dungeon with rooms placed on a straight line. Each room is numbered from to in order. You can move from one room to a neighboring room. You are now at room No.. Your goal is to reach room No. with least damage taken.
The room No. and No. are empty rooms. In each of the room from No. to No., there is an item or a monster. You are given pairs of integers which tells the contents of each room respectively. For each (), the integers , means as following.
- When , there is key on which a booby-trap is set in the room No.. If you pick up that key you take points of damage from the trap. You may choose not to pick up the key, in which case you take no damage.
- When , there is treasure box in the room No.. In the treasure box there is a crystal that increases your DEF parameter by points. You can consume key to open the treasure box and increase your DEF. You may choose not to open the treasure box, in which case you don't consume a key, and keep your DEF parameter as is. Your initial DEF parameter is .
- When , there is a monster in the room No.. You must fight that monster when you first entered that room, and take points of damage. When you visit the room after that first battle, there is no monster in the room, and you take no damage.
You start from the room No.. Calculate the least damage you take to reach the room No.
Note that you can go back to the room you have visited, and the damage taken from the booby-trap can't be decreased by your DEF parameter.
Input
:
- On the first line, you will be given an integer (), which means the number of rooms in the dungeon is .
- On the following lines you will be given two integers (), () separated by space, the information of each room. The -th line tells the contents of the room No.. These information are guaranteed to meet the following conditions.
- The number of each kind of rooms is less than .
- Even if you acquire all the crystals in the dungeon, the damage taken from a monster will not be less than . That means, let the sum of for all which satisfies . For any which satisfies , holds.
Output
Output a single line containing the least damage you take to reach the room No. starting from the room No.. Make sure to insert a line break at the end of the output.
Input Example 1
4
1 2
2 3
0 1
2 2
Output Example 1
4
Optimal move is as following.
- At first go to room No. and take the key. On the way, you take points of damage from the monster in the room No., and point of damage from a booby-trap at the room No..
- Next, go back to room No., open the treasure box, increase your DEF by points. On this way you pass the room No., but you have already beaten the monster so you take no damage.
- Finally go to the goal, room No.. On this way you fight the monster at the room No., but thanks to your points of DEF parameter, you take points of damage.
Input Example 2
4
1 2
2 3
0 4
2 2
Output Example 2
5
This case is similar to the Example 1, but the damage from the booby-trap is large so the optimal move is to ignore the key.
Input Example 3
7
0 4
1 3
2 10
1 6
2 9
0 5
2 10
Output Example 3
21