Pagini recente » Cod sursa (job #1712292) | Cod sursa (job #371863) | Cod sursa (job #2906344) | Cod sursa (job #2592234) | Cod sursa (job #2468042)
#include <fstream>
using namespace std;
ifstream in("rucsac.in");
ofstream out("rucsac.out");
const int MAX=5005;
int g,n,p[MAX],w[MAX],profit[MAX];
int main()
{
in>>n>>g;
for(int i=1;i<=n;i++)
in>>w[i]>>p[i];
profit[0]=0;
for(int j=1;j<=g;j++)
profit[j]=-1;
for(int i=0;i<n;i++){
for(int j=g-w[i];j>=0;j--){
if(profit[j]!=-1){
if(p[i]+profit[j]>profit[j+w[i]]){
profit[j+w[i]]=p[i]+profit[j];
}
}
}
}
int maxim=0;
for(int j=0;j<=g;j++){
if(maxim<profit[j])
maxim=profit[j];
}
out<<maxim;
return 0;
}