Pagini recente » Cod sursa (job #1177264) | Cod sursa (job #412044) | Cod sursa (job #347726) | Cod sursa (job #106487) | Cod sursa (job #612824)
Cod sursa(job #612824)
#include <stdio.h>
typedef unsigned int ui_t;
struct object {
ui_t weight, value;
} objects[5001];
ui_t objnr, sacwg, cost[5001][10001];
ui_t maxcost(ui_t a, ui_t b)
{
return a > b ? a : b;
}
void process()
{
for (ui_t i = 1; i <= objnr; i++) {
for (ui_t j = 1; j <= sacwg; j++) {
if (objects[i].weight > j) {
cost[i][j] = cost[i - 1][j];
} else {
cost[i][j] = maxcost(cost[i - 1][j], cost[i - 1][j - objects[i].weight] + objects[i].value);
}
}
}
}
int main()
{
freopen("rucsac.in", "r", stdin);
freopen("rucsac.out", "w", stdout);
scanf("%u %u", &objnr, &sacwg);
for (ui_t i = 1; i <= objnr; i++) {
scanf("%u %u", &(objects[i].weight), &(objects[i].value));
}
process();
printf("%u", cost[objnr][sacwg]);
fclose(stdin);
fclose(stdout);
return 0;
}