Pagini recente » Cod sursa (job #2575633) | Cod sursa (job #3137442) | Cod sursa (job #1283606) | Cod sursa (job #3029967) | Cod sursa (job #1276704)
#include<fstream>
#include<algorithm>
using namespace std;
class Object{
public:
int weight;
int profit;
Object(int weight,int profit):profit(profit),weight(weight){}
Object(const Object ©O){
this->weight = copyO.weight;
this->profit = copyO.profit;
}
};
#define NMAX 5000
#define GMAX 10000
Object *objects[NMAX];
int bestSol[GMAX+1];
int n,gMax;
int main(){
ifstream f("rucsac.in");
ofstream g("rucsac.out");
f>>n>>gMax;
int w,p;
for(int i=0; i<n; ++i){
f>>w>>p;
objects[i] = new Object(w,p);
}
for(int i=0; i<n; ++i){
for(int j=gMax; j>=0; --j)
if(objects[i]->weight<= j){ ///daca pot adauga la solutii ob respectiv
bestSol[j] = max(bestSol[j], bestSol[j- objects[i]->weight] + objects[i]->profit);
///cea mai buna solutie ori ramane cea care era deja ori devine
/// solutia in care adaug profitul pt elementul asta unde pot (la greutatea asta - greutatea elementului asta)
}
}
g<<bestSol[gMax];
f.close();g.close();
return 0;
}