We need to solve fractional knapsack: maximize sum of (value_i * fraction_i) given sum of weight_i * fraction_i ≤ W, fractions in [0,1] per item. This is classic: sort items by value per unit weight (v_i/w_i) descending, then take full items from highest density until remaining capacity less than next item's weight, then take fractional part of that item.
Constraints: N up to 1e5, W up to 1e9, v_i, w_i up to 1e9. Straightforward O(N log N) works.
But output requires absolute error ≤ 1e-6. Use double for total value.
Edge Cases: If total weight sum of all items ≤ W, we can take everything, total value sum of v_i. So compute sum_w and sum_v, if sum_w ≤ W output sum_v.
Otherwise, sort by density.
But careful: Density = v_i/w_i, which is a double. Sorting by double may have ties, but any order works.
Potential issues: large numbers up to 1e9, v_i and w_i multiplication to compute fractional value: v_i * (used_weight / w_i) = v_i * used_weight / w_i. Doing using double: v_i, used_weight, w_i all up to 1e9, product up to 1e18, fits in double with 53-bit mantissa (~9e15 exact integer, but up to 1e18 may lose some integer precision but we need error ≤1e-6 relative, so double is fine.
Alternative: use long double for extra precision but not needed.
Implement:
-
Read N, W.
-
Arrays of pairs (value, weight). Or compute density as double = (double)v_i / w_i.
-
Sort descending by density. Use indices or vector of struct with density.
-
Loop through sorted items:
if W_remaining >= weight_i:
total_value += value_i
W_remaining -= weight_i
else:
double fraction = (double)W_remaining / weight_i
total_value += value_i * fraction
break
-
Print total_value with appropriate precision: e.g., cout << fixed << setprecision(10) << total_value; or printf("%.10lf\n", total_value).
But sample 2: output "210.90909091". Let's check: densities: 60/13 ≈ 4.61538, 100/23≈4.34783, 120/33≈3.63636. So take first item full: 60, rem cap 50-13=37. Next item: weight 23, value 100, take full: total 160, rem 14. Next item weight 33, can take fraction 14/33 ≈0.424242, value add 120*14/33 ≈ 50.90909091. Sum=210.90909091. Good.
Thus solution is trivial.
But is there any twist? Problem statement says "You can break some items if you want." So yes fractional knapsack.
Check constraints: N up to 1e5, so O(N log N) fine.
Potential pitfalls: using integer division incorrectly. Use double.
So solution.
Let's write answer in C++.
But maybe they expect output with many decimals? Use setprecision(10) or more.
Thus final answer.
We'll output with high precision: e.g., printf("%.10lf\n", ans) or cout << fixed << setprecision(10) << ans; That's good.
Now code.
Implementation details:
struct Item {
long long v, w;
double density;
bool operator<(const Item& other) const { return density > other.density; }
};
Or sort using lambda: sort(items.begin(), items.end(), [](const Item& a, const Item& b){ return (double)a.v/a.w > (double)b.v/b.w; });
But computing density each time in comparator may be slow but okay for 1e5. Use precomputed double.
But careful: Sorting with double comparator may cause issues if NaN or something, not the case.
Better: use a pair of density and index, or use stable sort.
Now read:
long long total_weight = 0;
for each item:
cin >> v_i >> w_i;
total_weight += w_i;
items.push_back(Item{v_i, w_i, (double)v_i/w_i});
If total_weight <= W: total_value = sum of v_i. Output that.
Else: sort items descending by density.
Then iterate.
Be careful: sum of v_i could be up to 1e14 (1e5 * 1e9), but double can represent up to 1e308 with 15-17 decimal digits, so fine.
Now done.
But there is nuance: The problem says "The output must not contain an error greater than 10^{-6}." So relative or absolute? They likely mean absolute error, but relative is implied? Use high precision anyway.
Now final solution.
Let's produce final code.
Wait: This is a typical fractional knapsack. Could there be any trick? Possibly with large W (1e9) and items up to 1e9, sorting okay.
One thing: sometimes fractional knapsack can be solved by selecting highest density, but the algorithm of taking full items until capacity is less than next weight works only if items are divisible. That's exactly correct.
Thus answer.
But maybe they want to test with edge case where density ties? Works.
Potential integer overflow: when computing total_weight sum, use long long.
Now code.
I'll produce final answer in C++.
Implementation:
#include <bits/stdc++.h>
using namespace std;
int main() {
int N;
long long W;
cin >> N >> W;
vector v(N), w(N);
long long total_w = 0;
double total_v_if_full = 0; // optional sum for early exit
vector<pair<double, int>> items; // density, index
for (int i=0; i<N; ++i) {
cin >> v[i] >> w[i];
total_w += w[i];
total_v_if_full += v[i];
items.emplace_back((double)v[i]/w[i], i);
}
if (total_w <= W) {
cout << fixed << setprecision(10) << total_v_if_full << endl;
return 0;
}
sort(items.begin(), items.end(), greater<pair<double, int>>()); // descending by density
double ans = 0;
long long rem = W;
for (auto& p : items) {
int i = p.second;
if (rem >= w[i]) {
ans += v[i];
rem -= w[i];
} else {
double fraction = (double)rem / w[i];
ans += v[i] * fraction;
break;
}
}
cout << fixed << setprecision(10) << ans << endl;
return 0;
}
But careful: using greater<pair<double,int>> sorts by first then second. That's fine.
Alternatively, sort with lambda: [](pair<double,int> a, pair<double,int> b) { return a.first > b.first; }
Works.
Now test with sample.
Sample 1:
3 50
60 10
100 20
120 30
total_w=60>50
densities: 6,5,4. Sorted: 6 (index0),5(index1),4(index2)
rem=50:
i0 w=10, rem>=10 -> ans=60, rem=40
i1 w=20, rem>=20 -> ans=160, rem=20
i2 w=30, rem<30 -> fraction=20/30=0.6667, ans+=120*0.6667=80 => total=240. Output 240.0000000000, but sample output "240". Acceptable.
Sample2: as computed 210.9090909091, output setprecision(10) gives 210.9090909091? Let's compute: 14/33 = 0.4242424242, 120 * 14/33 = (120*14)/33 = 1680/33 = 50.9090909091? Actually 120 * 14/33 = (120/33)*14? Let's compute: 120/33 = 3.63636363636, times 14 = 50.9090909090. Sum = 60+100+50.9090909090 = 210.9090909090. So with setprecision(10) we output 210.9090909090? But sample output is "210.90909091". It's okay.
Sample3: 1 100, 100000 100000, total_w=100000 <=100? Actually W=100, total_w=100000 >100. So we take fraction: 100/100000=0.001, value = 100000*0.001=100. output 100. So correct.
Thus solution works.
But wait: The problem statement: "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." So they want maximum total value. So fractional knapsack.
Thus answer.
Now maybe there is nuance about integer division? Not.
Hence final answer.
Given the platform requires output within error, using double and setprecision(10) fine.
Now produce final message with explanation and code.
But the user might want an answer as ChatGPT; they asked "You have N items ... Find the maximum total value..." So they likely expect solution.
Given the instruction: We should provide final answer, maybe with code.
We'll output the solution in C++.
Thus final. ```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int N;
long long W;
cin >> N >> W;
vector<long long> v(N), w(N);
long long total_weight = 0;
double total_value_if_all = 0;
for (int i = 0; i < N; ++i) {
cin >> v[i] >> w[i];
total_weight += w[i];
total_value_if_all += v[i];
}
// If all items fit, take everything.
if (total_weight <= W) {
cout << fixed << setprecision(10) << total_value_if_all << "\n";
return 0;
}
// Sort items by value per unit weight (density) descending.
vector<pair<double, int>> items; // (density, index)
items.reserve(N);
for (int i = 0; i < N; ++i) {
items.emplace_back(static_cast<double>(v[i]) / w[i], i);
}
sort(items.begin(), items.end(),
[](const pair<double, int>& a, const pair<double, int>& b) {
return a.first > b.first;
});
double ans = 0;
long long rem = W;
for (auto& p : items) {
int i = p.second;
if (rem >= w[i]) {
ans += v[i];
rem -= w[i];
} else {
double fraction = static_cast<double>(rem) / w[i];
ans += v[i] * fraction;
break;
}
}
cout << fixed << setprecision(10) << ans << "\n";
return 0;
}