Advertisement
Carbastan

Rucsac

Dec 13th, 2022
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.79 KB | None | 0 0
  1. #include <iostream>
  2. #include <algorith>
  3.  
  4. using namespace std;
  5.  
  6. //ifstream cin(".in");
  7. //ofstream cout(".out");
  8.  
  9. struct obc
  10. {
  11.     float G, V;
  12. } v[1005];
  13.  
  14. bool cond(obc i, obc j)
  15. {
  16.     if(i.V / i.G > j.V / j.G) return true;
  17.     else return false;
  18. }
  19.  
  20. int main()
  21. {
  22.     int n, GM;
  23.  
  24.     cin >> n >> GM;
  25.  
  26.     for(int i = 1; i <= n; ++i)
  27.     {
  28.         cin >> v[i].G >> v[i].V;
  29.     }
  30.  
  31.     sort(v + 1, v + n + 1, cond);
  32.  
  33.     double S = 0, GR = 0;
  34.  
  35.     for(int i = 1; i <= n and GR <= GM; ++i)
  36.     {
  37.         if(GR + v[i].G <= GM)
  38.         {
  39.             GR += v[i].G;
  40.             S += v[i].V;
  41.         }
  42.         else
  43.         {
  44.             double r = GM - GR, j = v[i].V / v[i].G;
  45.             S += r * j;
  46.             break;
  47.         }
  48.     }
  49.  
  50.     cout << S;
  51.  
  52.     return 0;
  53. }
  54.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement