Pagini recente » Cod sursa (job #2944115) | Cod sursa (job #1525522) | Cod sursa (job #1345182) | Cod sursa (job #953294) | Cod sursa (job #2941942)
#include <fstream>
using namespace std;
int p[5001], g[5001];
int profit[10001];
int main()
{
ifstream fin("rucsac.in");
ofstream fout("rucsac.out");
int n, k;
fin >> n >> k;
for(int i = 0; i < n; i++)
fin >> g[i] >> p[i];
profit[0] = 0;
for(int j = 1; j <= k; j++)
profit[j] = -1;
for(int i = 0; i < n; i++){
for(int j = k - g[i]; j >= 0; j--){
if(profit[j] != -1)
profit[j + g[i]] = max(profit[j + g[i]], profit[j] + p[i]);
}
}
int maxim = -1;
for(int i = 1; i <= k; i++)
maxim = max(maxim, profit[i]);
fout << maxim;
return 0;
}