Fractional Knapsack
View as PDFYou are packing a bag with weight capacity . There are
items, and the
-th item has
value
and weight
.
You do not have to take an entire item. You may take any fraction of an item, and both its weight
and value scale proportionally. In particular, if you take weight from item
(where
), then you gain value
.
What is the maximum total value you can obtain?
Input
The first line contains two integers and
, the number of items and the knapsack capacity.
The next lines each contain two integers
and
, the value and weight of an item.
Output
Print the maximum obtainable value as a floating-point number.
Your answer will be accepted if it has absolute or relative error at most .
Constraints
- If the exact answer is written as a reduced fraction
, then both
and
fit in signed 64-bit integers.
Example 1
Input
3 3
8 6
9 4
10 5
Output
6.75
Explanation
The second item has the highest value density: .
With capacity
, take
of that item and gain
.
Example 2
Input
4 8
6 3
15 5
14 7
9 6
Output
21.0
Explanation
Take the first two items completely. Their total weight is , and their total value
is
.
Comments