Pagini recente » Cod sursa (job #3157150) | Cod sursa (job #3292490) | Cod sursa (job #2296807) | Cod sursa (job #2935963) | Cod sursa (job #1865895)
#include <fstream>
using namespace std;
ifstream in("rucsac.in");
ofstream out("rucsac.out");
const int smax=10000;
const int inf=1e9;
const int nmax=5000;
int nr[nmax+1],co[nmax+1],costper[smax+1];
int main()
{
int s,n;
in>>n>>s;
for(int i=1; i<=s; i++)
{
costper[i]=-inf;
}
costper[0]=0;
for(int i=1; i<=n; i++)
{
in>>nr[i]>>co[i];
}
for(int i=1; i<=n; i++)
{
for(int j=s-nr[i]; j>=0; j--)
{
if(co[i]+costper[j]>costper[j+nr[i]])
costper[j+nr[i]]=co[i]+costper[j];
}
}
int max=-1;
for(int i=0; i<=s; i++)
{
if(costper[i]>max)
max=costper[i];
}
out<<max;
return 0;
}