CCO '08 P5 - Candy

View as PDF

Submit solution

Points: 7 (partial)
Time limit: 2.75s
Memory limit: 512M

Problem type
Canadian Computing Competition: 2008 Stage 2, Day 2, Problem 2

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 Specification

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

Output Specification

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

Sample Input

3 5
3 3
1 2
3 100

Sample Output


Explanation for Sample Output

Your friend takes two of the 100-calorie candies, for a total of 200 calories. You keep the remaining candies, which have 126 calories.


You may assume that 50\% of the test cases will have at 1 \le N, k_i, c_i \le 100. All test cases will have 1 \le N \le 100, 1 \le k_i \le 500 and 1 \le c_i \le 200.


There are no comments at the moment.