Advertisement
Guest User

Untitled

a guest
Dec 13th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.66 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<algorithm>
  3. typedef struct data
  4. {
  5. int value;
  6. int weight;
  7. }data;
  8. bool acompare(data lhs, data rhs)
  9. {
  10. return (double)lhs.value/lhs.weight > (double)rhs.value/rhs.weight;
  11. }
  12.  
  13. data arr[100];
  14. int main()
  15. {
  16. int n,i;
  17. int k,tp=0;
  18. scanf("%d",&n);
  19.  
  20. for(i=0;i<n;i++)
  21. {
  22. scanf("%d", &arr[i].weight);
  23. scanf("%d", &arr[i].value);
  24. }
  25. sort(arr, arr+n, acompare);
  26.  
  27. scanf("%d",&k);
  28.  
  29. for(i=0;i<n;i++)
  30. {
  31. while(k!=0)
  32. {
  33. if(arr[i].weight<=k)
  34. {
  35. tp+=arr[i].value;
  36. k-=arr[i].weight;
  37. }
  38. else
  39. {
  40. tp+=(arr[i].value/arr[i].weight)*k;
  41. break;
  42. }
  43. }
  44. }
  45. printf("%d\n",tp);
  46.  
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement