Mai intai trebuie sa te autentifici.
Cod sursa(job #1383819)
| Utilizator | Data | 10 martie 2015 17:57:46 | |
|---|---|---|---|
| Problema | Problema rucsacului | Scor | 100 |
| Compilator | cpp | Status | done |
| Runda | Arhiva educationala | Marime | 0.8 kb |
#include <fstream>
#include <cstring>
#define MINF 0x80808080
using namespace std;
int g[5010], c[5010];
int d[10010];
int n, G, i, jnext, sol, pmaxim, j;
int main() {
ifstream fin ("rucsac.in");
ofstream fout("rucsac.out");
fin>>n>>G;
for (i=1;i<=n;i++)
fin>>g[i]>>c[i];
memset(d, 0x80, sizeof(d));
// fout<<d[1];
d[0] = 0;
pmaxim = 0;
for (i=1;i<=n;i++) {
for (j=pmaxim; j>=0;j--) {
if (d[j] != MINF) {
jnext = j + g[i];
if (jnext <= G) {
d[jnext] = max(d[jnext], d[j] + c[i]);
sol = max(sol, d[jnext]);
pmaxim = max(pmaxim, jnext);
}
}
}
}
fout<<sol;
return 0;
}
