Problem 736
Fractional Knapsack
Medium · 20 pts · Greedy, Sorting
Problem
You have N items, item i with value V[i] and weight W[i], and a knapsack that holds a total weight of at most CAP. You may take any fraction of an item, getting the same fraction of its value. Maximise the total value. Greedy is optimal: sort the items by value per unit of weight, from highest to lowest, take whole items while they fit, and then the needed fraction of the next one. The answer can be a fraction; print it exactly, in lowest terms.
Input
The first line contains N and CAP. Each of the next N lines contains V[i] and W[i].
Output
Print the maximum value as P/Q in lowest terms, or just P when it is a whole number.
Constraints
1 <= N <= 100000; 1 <= CAP <= 10^9; 1 <= V[i], W[i] <= 10^5
Example input
3 50 60 10 100 20 120 30
Example output
240
Explanation
With items (60, 10), (100, 20), (120, 30) and CAP = 50, take the first two whole (value 160, weight 30) and 20 of the 30 units of the third, worth 120 × 20 / 30 = 80. The total is 240.