Pagini recente » Cod sursa (job #1318380) | Cod sursa (job #791073) | Cod sursa (job #2474136) | Cod sursa (job #2386105) | Cod sursa (job #3210213)
#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);
//return a.first<b.first;
if (a.first <= b.first && a.second >= b.second)
return true;
else if (a.first >= b.first && a.second >= b.second)
return true;
return false;
}
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);
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;
// cout<<v[i].first<<" "<<v[i].second<<endl;
i++;
}
fout<<profit;
return 0;
}