fork download
  1. #include <iostream>
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4.  
  5. int max_units(vector<int>&req,vector<int>&cost,vector<int>&stock,int budget){
  6. int n=req.size();
  7. int low=0;
  8. int high=1e9;
  9. int ans=0;
  10. while(low<=high){
  11. int mid=(low+high)/2;
  12. long long sum=0;
  13.  
  14. for(int i=0;i<n;i++){
  15. long long need=max(0,req[i]*mid-stock[i]);
  16. sum=sum+need*cost[i];
  17. if(sum>budget){
  18. break;
  19. }
  20. }
  21.  
  22. if(sum<=budget){
  23. ans=mid;
  24. low=mid+1;
  25. }
  26.  
  27. else if(sum>budget){
  28. high=mid-1;
  29. }
  30. }
  31.  
  32. return ans;
  33.  
  34. }
  35.  
  36. int main(){
  37. int n;
  38. cin>>n;
  39. vector<int>req(n);
  40. vector<int>cost(n);
  41. vector<int>stock(n);
  42.  
  43. for(int i=0;i<n;i++){
  44. cin>>req[i];
  45. cin>>cost[i];
  46. cin>>stock[i];
  47. }
  48. int budget;
  49. cin>>budget;
  50.  
  51. int ans=max_units(req,cost,stock,budget);
  52. cout<<"The maximum number of units that are possible from the available constraints are: "<<ans<<endl;
  53. return 0;
  54. }
Success #stdin #stdout 0s 5320KB
stdin
4
2 2 3
2 3 2
3 1 1
1 6 4
30
stdout
The maximum number of units that are possible from the available constraints are: 3