Fractional Knapsack Problem


提交解答

分數: 1
時間限制: 3.0s
記憶體限制: 256M

作者:
題目代碼
題目類型
允許的語言
C, C++

Fractional Knapsack Problem

You have N items that you want to put them into a knapsack of capacity W. Item i (1 ≤ i ≤ N) has weight wᵢ and value vᵢ for the weight.

When you put some items into the knapsack, the following conditions must be satisfied:

  • The total value of the items is as large as possible.
  • The total weight of the selected items is at most W.
  • You can break some items if you want. If you put w' (0 ≤ w' ≤ wᵢ) of item i, its value becomes vᵢ × w′ / wᵢ.

Find the maximum total value of items in the knapsack.

Input

N  W
v₁ w₁
v₂ w₂
:
vₙ wₙ

The first line consists of the integers N and W. In the following N lines, the value and weight of the i-th item are given.

Output

Print the maximum total value of the items in a line. The output must not contain an error greater than 10⁻⁶.

Constraints

  • 1 \ N \ 10⁵
  • 1 \ W \ 10⁹
  • 1 \ vᵢ \ 10⁹ (1 ≤ i ≤ N)
  • 1 \ wᵢ \ 10⁹ (1 ≤ i ≤ N)

Sample Input 1

3 50
60 10
100 20
120 30

Sample Output 1

240

When you put 10 of item 1, 20 of item 2 and 20 of item 3, the total value is maximized.

Sample Input 2

3 50
60 13
100 23
120 33

Sample Output 2

210.90909091

When you put 13 of item 1, 23 of item 2 and 14 of item 3, the total value is maximized. Note some outputs can be a real number.

Sample Input 3

1 100
100000 100000

Sample Output 3

100

評論

目前沒有評論。