Pagini recente » Istoria paginii utilizator/voxbig | Monitorul de evaluare | Atasamentele paginii bkt2_oct2011 | Monitorul de evaluare | Cod sursa (job #3160147)
#include <fstream>
using namespace std;
ifstream fin("rucsac.in");
ofstream fout("rucsac.out");
struct t_object{
int weight,profit;
}obj[1000];
const int MAX_N=5005;
const int MAX_G=10005;
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;
t_object obiect={curr_greutate, curr_profit};
obj[i]=obiect;
}
int memo[MAX_P][MAX_G]={0};
fout<<dp_profit(n,greutate,obj,memo);
return 0;
}