Cod sursa(job #3224737)
| Utilizator | Data | 16 aprilie 2024 00:57:33 | |
|---|---|---|---|
| Problema | Problema rucsacului | Scor | 0 |
| Compilator | cpp-64 | Status | done |
| Runda | Arhiva educationala | Marime | 0.62 kb |
#include <iostream>
#include <fstream>
#include <algorithm>
using namespace std;
ifstream fin("rucsac.in");
ofstream fout("rucsac.out");
int n,g;
struct obiecte{
int w,p;
};
bool adevar_greutate(obiecte a,obiecte b)
{
if(a.p == b.p)
return a.w>b.w;
else
return a.p>b.p;
}
int main()
{
int i,s=0;
fin>>n>>g;
obiecte obj[n];
for(i=0;i<n;i++)
fin>>obj[i].w>>obj[i].p;
sort(obj,obj+n,adevar_greutate);
i=0;
while(g-obj[i].w>=0)
{
g=g-obj[i].w;
s=s+obj[i].p;
i++;
}
fout<<s;
return 0;
}
