Fan migration

Assign every family to a house inside its own supporter block so the total compensation paid to families moving into cheaper houses is minimal.

Medium5GraphNo attempts yetTime limit1sMemory limit256 MB

Problem

Fans of two football teams, MC and MF, live in the town of M. The houses of the town stand along a single street and are numbered 11 through NN. One family lives in each house.

The mayor is tired of the fights between the two sets of fans, so he has issued an edict that sends them to opposite ends of the street. The KK families who support MC must all move into houses 11 through KK, and the MM families who support MF must all move into houses NM+1N - M + 1 through NN. Every family that supports neither team moves into a house numbered K+1K + 1 through NMN - M. Inside a block you may decide freely which family takes which house.

The mayor's assistants draw up a migration plan that meets those requirements. Every house has a price. A family that moves into a house cheaper than the one it used to live in receives compensation equal to the full price of its original house. A family that moves into a house of equal or higher price receives nothing. No family moves more than once.

Write a program that finds the smallest possible total compensation.

Input

The first line contains NN, the number of houses in the town. (2N3002 \le N \le 300)

Each of the next NN lines contains two integers, describing house 11 through house NN in order. The first integer is the price of the house. The second integer tells which team the family living there supports: 00 for neither team, 11 for MC and 22 for MF. At least one family in the town supports each of the two teams. Prices are positive integers no greater than 10001000.

Output

Print the smallest possible total compensation.

Hint

The first sample can be solved this way. The family in house 33 moves to house 22 and receives 33, the family in house 22 moves to house 11 and receives 22, and the family in house 11 moves to house 33 and receives nothing.

The second sample can be solved this way. The families in houses 11 and 44 swap houses and neither is compensated, and the family in house 33 swaps with the family in house 55 for a compensation of 66.