Pagini recente » Cod sursa (job #2124709) | Cod sursa (job #1562593) | Cod sursa (job #2317529) | Cod sursa (job #1408158) | Cod sursa (job #732297)
Cod sursa(job #732297)
#include <fstream>
using namespace std;
#define weight first
#define cost second
const int MAXSIZE = 5000;
const int MAXWEIGHT = 10000;
ifstream in("rucsac.in");
ofstream out("rucsac.out");
int objects,capacity;
int best[MAXWEIGHT+1][MAXWEIGHT+1];
pair<int,int> object[MAXSIZE+1];
int main() {
in >> objects >> capacity;
for(int i=1;i<=objects;i++)
in >> object[i].weight >> object[i].cost;
for(int i=1;i<=objects;i++) {
for(int k=0;k<=capacity;k++) {
best[i][k] = best[i-1][k];
if(object[i].weight <= k)
best[i][k] = max(best[i][k],best[i-1][k - object[i].weight] + object[i].cost);
}
}
out << best[objects][capacity] << "\n";
return (0);
}