Cod sursa(job #1947370)
| Utilizator | Data | 30 martie 2017 21:58:45 | |
|---|---|---|---|
| Problema | Problema rucsacului | Scor | 0 |
| Compilator | cpp | Status | done |
| Runda | Arhiva educationala | Marime | 0.68 kb |
#include <iostream>
#include <fstream>
using namespace std;
int max(int x, int y)
{
if(x > y)
{
return x;
}
else
{
return y;
}
}
int main()
{
ifstream fin("tst.in");
ofstream fout("tst.out");
int n,G,g[5002],p[5002],a[100000];
fin >> n >> G;
for(int i = 1; i <= n; i++)
{
fin >> g[i] >> p[i];
}
for(int i = 0; i <= G; i++)
{
a[i] = 0;
}
for(int i = 1; i <= n; i++)
for(int j = G; j >= 0; j--)
{
if( j - g[i] >= 0)
{
a[j] = max(a[j], a[j-g[i]]+p[i]);
}
}
fout << a[G];
return 0;
}
