Pagini recente » Cod sursa (job #2114688) | Cod sursa (job #920425)
Cod sursa(job #920425)
#include <fstream>
#include <algorithm>
using namespace std;
ifstream in("rucsac.in");
ofstream out("rucsac.out");
const int N=10001;
const int M=5001;
int g[N], p[M], profit[M];
int main()
{
int n, k, i, j;
in>>n>>k;
for (i=1; i<=n; i++)
in>>g[i]>>p[i];
for (i=1; i<=n; i++){
for(j=k-g[i]; j>0; j--)
if (profit[j]!=0 && profit[j]+p[i]>profit[j+g[i]])
if (g[i]<=k && p[i]>profit[g[i]])
profit[g[i]]=p[i];
}
sort (&profit[1], &profit[k+1]);
out<<profit[k];
return 0;
}