Pagini recente » Cod sursa (job #3255110) | Cod sursa (job #2710489) | Cod sursa (job #769266) | Cod sursa (job #2096744) | Cod sursa (job #3183055)
#include <fstream>
using namespace std;
ifstream fin("rucsac.in");
ofstream fout("rucsac.out");
struct object{
int greutate;
int profit;
};
const int max_n=10001;
const int max_g=10001;
int dp[max_g]; // valori initializate cu 0
int main()
{
int nr_elem,gmax;
object objs[max_n];
fin>>nr_elem>>gmax;
for(int i=1;i<=nr_elem;i++)
{
fin>>objs[i].greutate>>objs[i].profit;
}
for(int i=1;i<=nr_elem;i++)
for(int j=gmax;j>=objs[i].greutate;j--)
{
dp[j]=max(dp[j],dp[j-objs[i].greutate]+objs[i].profit);
}
fout<<dp[gmax];
return 0;
}