Cod sursa(job #3277426)
| Utilizator | Data | 16 februarie 2025 01:20:51 | |
|---|---|---|---|
| Problema | Problema rucsacului | Scor | 100 |
| Compilator | cpp-64 | Status | done |
| Runda | Arhiva educationala | Marime | 0.56 kb |
#include <iostream>
#include <fstream>
using namespace std;
ifstream f("rucsac.in");
ofstream g("rucsac.out");
const int GMAX = 10001, NMAX = 5000;
struct object
{
int weight, profit;
};
int maxProfit[GMAX];
int main()
{
int n, ans = 0, wMax;
f>>n>>wMax;
for(int i=1; i<=n;i++)
{
object obj;
f>>obj.weight>>obj.profit;
for(int w = wMax; w >= obj.weight; w--)
{
maxProfit[w] = max(maxProfit[w], maxProfit[w - obj.weight] + obj.profit);
ans = max(ans, maxProfit[w]);
}
}
g<<ans;
}
