Pagini recente » Cod sursa (job #2517752) | Cod sursa (job #474052) | Cod sursa (job #1715420) | Cod sursa (job #1226729) | Cod sursa (job #1667635)
#include <fstream>
#include <vector>
using namespace std;
ifstream fin("rucsac.in");
ofstream fout("rucsac.out");
int maxim(const int x, const int y)
{
if(x>y)
return x;
return y;
}
struct obiect
{
int w,p;
};
vector<obiect>obiecte;
int profit[10001];
int n,g;
void citire()
{
int i;
fin>>n>>g;
obiecte.resize(n+2);
//profit.resize(g+2);
for(i=1;i<=n;i++)
{
fin>>obiecte[i].w>>obiecte[i].p;
}
}
void dinamica()
{
int i,j;
for(i=1;i<=n;i++)
{
int greutate=obiecte[i].w;
int profit_curent=obiecte[i].p;
if(obiecte[i].w<=g)
{
for(j=g;greutate<=j;j--)
{
profit[j]=maxim(profit[j],profit[j-greutate]+profit_curent);
}
for(j;j>0;j--)
{
profit[j]=maxim(profit[j],profit[j-1]);
}
}
}
}
int main()
{
int i;
citire();
dinamica();
fout<<profit[g];
return 0;
}