Pagini recente » Cod sursa (job #898288) | Cod sursa (job #1885357) | Cod sursa (job #34139) | Cod sursa (job #1519363) | Cod sursa (job #1853506)
#include <iostream>
#include <fstream>
#include <algorithm>
using namespace std;
ifstream in("rucsac.in");
ofstream out("rucsac.out");
/*
prof[j] = profitul maxim care poate fi obtinut cu obiecte care au suma greutatilor j (1<= j <= k, k = capacitate)
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
0 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
--------------------------------------------------------------
g[i]
0 1 2 3 4 5 6
x 3 3 1 1 2 1
--------------------------------------------------------------
p[i]
0 1 2 3 4 5 6
x 7 4 2 9 4 5
--------------------------------------------------------------
k = 10
*/
int main()
{
int N, k, i, j, p[5002], g[5002], prof[10004];
in>>N>>k;
for(i = 1; i<=N; i++)
{
in>>g[i]>>p[i];
}
for(j = 0; j<=k; j++)
prof[j] = 0;
for(int i=1; i<=N; i++)
{
/*
for(j = 0; j<=k; j++)
{
cout<<prof[j]<<' ';
}
cout<<endl<<endl;
*/
for(j = k-g[i]; j>=0; j--)
{
if( prof[j] + p[i] > prof[j+g[i]] )
prof[j+g[i]] = prof[j] + p[i];
}
}
while(prof[k] == -1)
k--;
out<<prof[k];
return 0;
}