Cod sursa(job #1250126)
Utilizator | Data | 27 octombrie 2014 20:23:19 | |
---|---|---|---|
Problema | Problema rucsacului | Scor | 10 |
Compilator | cpp | Status | done |
Runda | Arhiva educationala | Marime | 0.66 kb |
#include <fstream>
#define Wmax 10010
using namespace std;
int N, G, Solution, DP[Wmax];
int main() {
int i, j, weight, profit;
ifstream in("rucsac.in");
ofstream out("rucsac.out");
in >> N >> G;
DP[0] = 1;
for(i = 1; i <= N; i++) {
in >> weight >> profit;
for(j = Wmax - 1; j >= 0; j--)
if(DP[j] && DP[j + weight] < DP[j] + profit)
DP[j + weight] = DP[j] + profit;
}
for(i = 0; i <= G; i++)
if(DP[i] > Solution)
Solution = DP[i];
out << (Solution - 1) << '\n';
in.close();
out.close();
return 0;
}