#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 N+2N + 2 rooms placed on a straight line. Each room is numbered from 00 to N+1N+1 in order. You can move from one room to a neighboring room. You are now at room No.00. Your goal is to reach room No.(N+1)(N + 1) with least damage taken.

The room No.00 and No.(N+1)(N + 1) are empty rooms. In each of the room from No.11 to No.NN, there is an item or a monster. You are given NN pairs of integers which tells the contents of each room respectively. For each ii (1leqileqN1 \\leq i \\leq N), the integers AiA_i, BiB_i means as following.

  • When Ai=0A_i = 0, there is 11 key on which a booby-trap is set in the room No.ii. If you pick up that key you take BiB_i points of damage from the trap. You may choose not to pick up the key, in which case you take no damage.
  • When Ai=1A_i = 1, there is 11 treasure box in the room No.ii. In the treasure box there is a crystal that increases your DEF parameter by BiB_i points. You can consume 11 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 00.
  • When Ai=2A_i = 2, there is a monster in the room No.ii. You must fight that monster when you first entered that room, and take Bi(rmplayersDEFatthattime)B_i - (\\rm{player's\\ DEF\\ at\\ that\\ time}) 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.00. Calculate the least damage you take to reach the room No.(N+1)(N + 1)

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

NN A1A_1 B1B_1 A2A_2 B2B_2 : ANA_N BNB_N

  • On the first line, you will be given an integer NN (1leqNleq9001 \\leq N \\leq 900), which means the number of rooms in the dungeon is N+2N + 2.
  • On the following NN lines you will be given two integers AiA_i (0leqAileq20 \\leq A_i \\leq 2), BiB_i (0leqBileq1090 \\leq B_i \\leq 10^9) separated by space, the information of each room. The ii-th line tells the contents of the room No.ii. These information are guaranteed to meet the following conditions.
    • The number of each kind of rooms is less than 300300.
    • Even if you acquire all the crystals in the dungeon, the damage taken from a monster will not be less than 00. That means, let XX the sum of BiB_i for all ii which satisfies Ai=1A_i = 1. For any jj which satisfies Aj=2A_j = 2, BjgeqXB_j \\geq X holds.

Output

Output a single line containing the least damage you take to reach the room No.(N+1)(N + 1) starting from the room No.00. 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.33 and take the key. On the way, you take 33 points of damage from the monster in the room No.22, and 11 point of damage from a booby-trap at the room No.33.
  • Next, go back to room No.11, open the treasure box, increase your DEF by 22 points. On this way you pass the room No.22, but you have already beaten the monster so you take no damage.
  • Finally go to the goal, room No.55. On this way you fight the monster at the room No.44, but thanks to your 22 points of DEF parameter, you take 00 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