Cod sursa(job #1862884)
| Utilizator | Data | 30 ianuarie 2017 13:23:41 | |
|---|---|---|---|
| Problema | Problema rucsacului | Scor | 0 |
| Compilator | cpp | Status | done |
| Runda | Arhiva educationala | Marime | 0.45 kb |
#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<wmax; ++i)
if (wcrt+a[i].w<=wmax)
pmax+=a[i].p; wcrt+=a[i].w;
out<<pmax;
}
