Pagini recente » Cod sursa (job #2345814) | Cod sursa (job #2158364) | Cod sursa (job #975466) | Cod sursa (job #1714698) | Cod sursa (job #2118085)
#include <fstream>
using namespace std;
ifstream in("rucsac.in");
ofstream out("rucsac.out");
inline int max(int a, int b) {
return a < b ? b : a;
}
struct Object {
int weight, profit;
};
const int MAX_WEIGHT = 1e4, MAX_ITEMS = 5e3;
int dp[2][MAX_WEIGHT + 2];
int items, totalWeight;
Object a[MAX_ITEMS + 2];
int main() {
in >> items >> totalWeight;
for (int index = 1; index <= items; ++index) {
in >> a[index].weight >> a[index].profit;
}
for (int index = 1; index <= items; ++index) {
dp[index % 2][a[index].weight] = a[index].profit;
for (int w = a[index].weight + 1; w <= totalWeight; ++w) {
dp[index % 2][w] = max(dp[(index - 1) % 2][w], dp[(index - 1) % 2][w - a[index].weight] + a[index].profit);
}
}
out << dp[items % 2][totalWeight] << "\n";
return 0;
}