Cod sursa(job #1845655)
| Utilizator | Data | 11 ianuarie 2017 19:17:05 | |
|---|---|---|---|
| Problema | Problema rucsacului | Scor | 100 |
| Compilator | cpp | Status | done |
| Runda | Arhiva educationala | Marime | 0.58 kb |
#include<fstream>
using namespace std;
ifstream f("rucsac.in");
ofstream q("rucsac.out");
int p[5001],g[5001],prof[10001];
int n,k;
int main()
{
int i,j, maxx = 0;
f>>n>>k;
for(i=1;i<=n;i++)
f>>g[i]>>p[i];
for(i=1;i<=k;i++)
prof[i]=-1;
prof[0]=0;
for(i=1;i<=n;i++)
for(j=k-g[i];j>=0;j--)
{
if(prof[j]!=-1)
if(prof[j] + p[i] > prof[j+g[i]])
prof[j+g[i]] = prof[j] + p[i];
}
for(i=1;i<=k;i++)
if(prof[i] > maxx)
maxx = prof[i];
q<<maxx;
return 0;
}
