Pagini recente » Cod sursa (job #3232667) | Cod sursa (job #498991) | Cod sursa (job #2700220) | Cod sursa (job #2957268) | Cod sursa (job #3210209)
#include <iostream>
#include <fstream>
#include <bits/stdc++.h>
using namespace std;
ifstream fin("rucsac.in");
ofstream fout("rucsac.out");
bool comparison(const pair<int,int> &a, const pair<int,int> &b)
{
return ((a.second+a.first)/2)>((b.second+b.first)/2);
}
int main()
{
int n, gmax;
fin>>n>>gmax;
vector<pair<int,int>> v(n); // first e weight, second e profit
for (int i=0; i<n; i++){
fin>>v[i].first>>v[i].second;
}
sort(v.begin(),v.end(),comparison); // sortate dupa medie
int profit=0, greutate=0, i=0;
//for (int i=0; i<n; i++){
// cout<<v[i].first<<" "<<v[i].second<<endl;
// }
while (greutate <= gmax && i < n){
greutate += v[i].first;
if (greutate > gmax)
break;
profit += v[i].second;
i++;
}
fout<<profit;
return 0;
}