fork(1) download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. struct Items {
  5. int weight;
  6. int value;
  7. double value_per_weight;
  8. };
  9.  
  10. bool cmpValuePerWeight(Items a, Items b) {
  11. return a.value_per_weight > b.value_per_weight;
  12. }
  13.  
  14. int main(void) {
  15. Items item[100];
  16. int n, c;
  17. cin >> n;
  18.  
  19. for (int i = 0; i < n; i++) {
  20. cin >> item[i].weight >> item[i].value;
  21. item[i].value_per_weight = (double)item[i].value / item[i].weight;
  22. }
  23.  
  24. cin >> c;
  25.  
  26. sort(item, item + n, cmpValuePerWeight);
  27.  
  28. double max_value = 0;
  29. for (int i = 0; i < n; i++) {
  30. if (c >= item[i].weight) {
  31. max_value += item[i].value;
  32. c -= item[i].weight;
  33. } else {
  34. max_value += item[i].value_per_weight * c;
  35. break;
  36. }
  37. }
  38.  
  39. cout << fixed << setprecision(2) << max_value << endl;
  40.  
  41. return 0;
  42. }
Success #stdin #stdout 0.01s 5332KB
stdin
4
210
15
3 15
27
5
stdout
0.00