Pagini recente » Cod sursa (job #2396828) | Cod sursa (job #882765) | Cod sursa (job #610070) | Cod sursa (job #1715187) | Cod sursa (job #1591137)
#include <iostream>
#include <fstream>
#include <stdlib.h>
using namespace std;
ifstream fin("rucsac.in");
ofstream fout("rucsac.out");
int main()
{
int n, g, i, j;
int **profit;
int prf, grt;
fin >> n >> g;
profit =(int**) calloc(n, sizeof(int *));
profit[0] =(int*) calloc(g, sizeof(int));
for(i = 0; i < n; i++){
fin >> grt >> prf;
if(i == 0 && grt <= g)
profit[0][grt-1] = prf;
else{
profit[i] =(int*) calloc(g, sizeof(int));
for(j = 0 ; j < g; j++)
{
if( profit[i][j] == 0)
profit[i][j] = profit[i-1][j];
if(profit[i-1][j] != 0 && j+grt <=g && profit[i-1][j+grt] < profit[i-1][j] + prf)
profit[i][j+grt] = profit[i-1][j] + prf;
}
if(profit[i][grt-1] < prf)
profit[i][grt-1] = prf;
free(profit[i-1]);
}
}
fin.close();
fout<< profit[n-1][g-1];
free(profit[n-1]);
free(profit);
fout.close();
return 0;
}