Pagini recente » Cod sursa (job #617112) | Cod sursa (job #2586442) | Cod sursa (job #921132) | Cod sursa (job #2911105) | Cod sursa (job #2036574)
#include <fstream>
using namespace std;
const int MaxWgt = 10005;
const int MaxN = 5005;
ifstream cin ("rucsac.in");
ofstream cout ("rucsac.out");
int rucsack[MaxWgt];
class ObjectType {
public:
int wgt, profit;
ObjectType(int _wgt = 0, int _profit = 0) {
wgt = _wgt;
profit = _profit;
}
};
ObjectType objectList[MaxN];
int main() {
int n, wgtCap;
cin >> n >> wgtCap;
for (int i = 1; i <= n; ++i) {
cin >> objectList[i].wgt >> objectList[i].profit;
}
for (int currObject = 1; currObject <= n; ++currObject) {
for (int currWgt = wgtCap; currWgt >= 0; --currWgt) {
int newWgt = currWgt + objectList[currObject].wgt;
if (newWgt > wgtCap) {
continue;
}
rucsack[newWgt] = max(rucsack[newWgt], rucsack[currWgt] + objectList[currObject].profit);
}
}
cout << rucsack[wgtCap] << '\n';
return 0;
}