Pagini recente » Cod sursa (job #994334) | Cod sursa (job #605588) | Cod sursa (job #2733256) | Cod sursa (job #2442630) | Cod sursa (job #1862881)
#include <fstream>
#include <algorithm>
using namespace std;
ifstream in("rucsac.in"); ofstream out("rucsac.out");
struct object
{
int w, p;
};
object a[5001];
int n, wmax, pmax, wcrt;
bool cmp(object a, object b)
{
return (a.w<=b.w && a.p>=b.p);
}
int main()
{
in>>n>>wmax;
for (int i=1; i<=n; ++i)
in>>a[i].w>>a[i].p;
sort(a+1, a+1+n, cmp);
for (int i=1; i<=n && wcrt<g; ++i)
if (wcrt+a[i].w<=wmax)
pmax+=a[i].p; wcrt+=a[i].w;
out<<pmax;
}