Pagini recente » Cod sursa (job #520422) | Cod sursa (job #2343342) | Cod sursa (job #1842281) | Cod sursa (job #1780110) | Cod sursa (job #3131383)
#include <iostream>
#include <fstream>
using namespace std;
ifstream in("rucsac.in");
ofstream out("rucsac.out");
struct obiect
{
int g, v;
};
int main()
{
int n, G;
in>>n>>G;
obiect ob[n+1];
for(int i=1; i<=n; i++)
in>>ob[i].g>>ob[i].v;
int mat[n+1][G+1];
for(int i=0; i<=n; i++)
mat[i][0]=0;
for(int j=0; j<=G; j++)
mat[0][j]=0;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=G;j++)
{
if(j-ob[i].g<0)
mat[i][j]=0;
else
{
mat[i][j]=max(mat[i-1][j], mat[i-1][j-ob[i].g]+ob[i].v);
}
}
}
out<<mat[n][G];
return 0;
}