Pagini recente » Cod sursa (job #2587141) | Cod sursa (job #707368) | Cod sursa (job #75663) | Cod sursa (job #2370430) | Cod sursa (job #3167498)
#include <fstream>
#define DIM 5005
#define maxG 10000
using namespace std;
ifstream fin("rucsac.in");
ofstream fout("rucsac.out");
int n, G;
int w[DIM], p[DIM];
int profitForWeight[maxG + 1];
int sol = 0;
int main()
{
fin >> n >> G;
for (int i = 1; i <= n; i++) {
fin >> w[i] >> p[i];
}
for (int i = 1; i <= n; i++) {
for (int j = G; j > 0; j--) {
if (profitForWeight[j] != 0 && w[i] + j <= G) {
profitForWeight[j + w[i]] = max(profitForWeight[j + w[i]], profitForWeight[j] + p[i]);
sol = max(sol, profitForWeight[j + w[i]]);
}
}
profitForWeight[w[i]] = max(profitForWeight[w[i]], p[i]);
}
fout << sol;
}