Pagini recente » Cod sursa (job #2867845) | Cod sursa (job #2911841) | Rating Gorgan Razvan-Florin (GRazvan) | Cod sursa (job #2476838) | Cod sursa (job #1853456)
#include <iostream>
#include <fstream>
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[5000], g[5000], prof[10000];
in>>N>>k;
for(i = 1; i<=N; i++)
in>>g[i]>>p[i];
/*
for(j = 1; j<=N; j++)
{
cout<<p[j]<<' ';
}
cout<<endl<<endl;
for(j = 1; j<=N; j++)
{
cout<<g[j]<<' ';
}
cout<<endl<<endl;
*/
for(j = 0; j<=k+20; j++)
prof[j] = -1;
prof[0] = 0;
for(int i=1; i<=N; i++)
{
for(j = k-g[i]; j>=0; j--)
{
if(prof[j] != -1 && prof[j] + p[i] > prof[j+g[i]] )
prof[j+g[i]] = prof[j] + p[i];
}
}
while(prof[k] == -1)
k--;
cout<<prof[k];
return 0;
}