Pagini recente » Cod sursa (job #2080275) | Cod sursa (job #2221019) | Cod sursa (job #2070351) | Cod sursa (job #1780592) | Cod sursa (job #972416)
Cod sursa(job #972416)
#include<fstream>
#include<iostream>
using namespace std;
int n,g,w[5010],p[5010],a[5010][10010];
int main()
{
ifstream in("rucsac.in");
ofstream out("rucsac.out");
in>>n>>g;
for(int i=1;i<=n;i++)
{
in>>w[i]>>p[i];
}
in.close();
for(int i=1;i<=n;i++)
{
for(int j=1;j<=g;j++)
{
if(j-w[i] >= 0)
{
a[i][j]=max(a[i-1][j], a[i-1][j-w[i]]+p[i]);
}
else
a[i][j]=a[i-1][j];
}
}
out<<a[n][g];
return 0;
}