Pagini recente » Cod sursa (job #1174914) | Cod sursa (job #2425496) | Cod sursa (job #2204491) | Cod sursa (job #2372866) | Cod sursa (job #3160145)
#include <fstream>
using namespace std;
ifstream fin("rucsac.in");
ofstream fout("rucsac.out");
const int max_n=5001;
const int max_g=10001;
struct t_object
{
int weight;
int profit;
}obj[max_n];
int dp_profit(int idx,int capacitate,t_object obj[],int memo[][max_g])
{
if(idx==0)
return 0;
if(capacitate==0)
return 0;
if(memo[idx][capacitate]!=0)
return memo[idx][capacitate];
t_object curr_obj=obj[idx];
if(curr_obj.weight>capacitate)
{
memo[idx][capacitate]=dp_profit(idx-1,capacitate,obj,memo);
return memo[idx][capacitate];
}
int profit1=dp_profit(idx-1,capacitate,obj,memo);
int profit2=curr_obj.profit+dp_profit(idx-1,capacitate-curr_obj.weight,obj,memo);
return max(profit1,profit2);
}
int main()
{
int n,greutate;
fin>>n>>greutate;
for(int i=1;i<=n;i++)
{
int curr_greutate;
int curr_profit;
fin>>curr_greutate>>curr_profit;
t_object obiect={curr_greutate,curr_profit};
obj[i]=obiect;
}
int memo[max_n][max_g]={0};
fout<<dp_profit(n,greutate,obj,memo);
return 0;
}