FCANDY - Candy (Again)

You and a friend have a big bag of candy. You want to keep slim and trim, and so you would like to equalize the candy which you are sharing with your friend in terms of calorie count. That is, your task is to divide the candies into two groups such that the number of calories in each group is as close together as possible.

Input

The first line of input contains the number of different kinds of candy you have in your bag of candy N (1 ≤ N ≤ 100). On the following N lines, there are pairs of numbers describing each type of candy. The candy description is of the form ki ci where ki is the number of that particular type of candy contained in the bag and ci is the calorie count for each piece of that type of candy. You may assume that 1 ≤ ki ≤ 500 and 1 ≤ ci ≤ 200.

Output

Your output is one integer which is the minimum difference of calories between friends

Example

Input:
4
3 5
3 3
1 2
3 100

Output:
74

Added by:JaceTheMindSculptor
Date:2009-04-08
Time limit:1s-2.397s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: C99 ERL JS-RHINO
Resource:Canadian Computing Competition 2008 Stage 2 Question E

hide comments
2009-06-04 22:00:43 madhav
Top down approach is giving TLE??
2009-04-26 19:13:50 JaceTheMindSculptor
Really? Could you give me a link?
2009-04-25 13:42:30 Jelle van den Hooff
This problem was also used in the dutch olympiad in informatics
© Spoj.com. All Rights Reserved. Spoj uses Sphere Engine™ © by Sphere Research Labs.