Pagini recente » Cod sursa (job #2385400) | Cod sursa (job #3283102) | Cod sursa (job #3129026) | Cod sursa (job #3240442) | Cod sursa (job #3000221)
#include <iostream>
#include <fstream>
using namespace std;
const int MAXN = 5002; // Maximum number of items
const int MAXG = 10002; // Maximum weight of the knapsack
int n, g, d[MAXG], w, p;
int main()
{
ifstream fin("rucsac.in"); // Input stream
ofstream fout("rucsac.out"); // Output stream
fin >> n >> g; // Read number of items and maximum weight of the knapsack
for (int i = 1; i <= n; i++)
{
fin >> w >> p; // Read weight and profit of the current item
for (int j = g; j >= w; j--) // Iterate over the possible knapsack weights
{
d[j] = max(d[j], d[j - w] + p); // Update maximum profit
}
}
fout << d[g]; // Output maximum profit for knapsack of weight g
return 0;
}