Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- typedef long long ll;
- struct item
- {
- double weight;
- double price;
- double unitprice;
- };
- int main()
- {
- #ifndef ONLINE_JUDGE
- freopen("input.txt", "r", stdin);
- freopen("output.txt", "w", stdout);
- #endif
- int n;
- cin >> n;
- item arr[n];
- int capacity;
- cin >> capacity;
- for(int i=0;i<n;i++)
- cin >> arr[i].weight >> arr[i].price;
- for(int i=0;i<3;i++)
- arr[i].unitprice = (arr[i].price/arr[i].weight);
- for(int i=0;i<n-1;i++)
- {
- int key=i;
- for(int j=i+1;j<n;j++)
- if(arr[j].unitprice>arr[key].unitprice)
- key = j;
- swap(arr[key],arr[i]);
- }
- //sort(arr,arr+n,cmp);
- for(int i=0;i<n;i++)
- cout << arr[i].unitprice << endl;
- double bag=0;
- int ind=0;
- while(ind!=n and capacity>0)
- {
- if(arr[ind].weight<=capacity)
- {
- bag+=arr[ind].price;
- capacity-=arr[ind].weight;
- arr[ind].weight=0;
- }
- else
- {
- bag+=(arr[ind].unitprice*capacity);
- capacity=0;
- }
- ind++;
- }
- cout << bag << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment