Cod sursa(job #1575431)
| Utilizator | Data | 21 ianuarie 2016 14:58:40 | |
|---|---|---|---|
| Problema | Problema rucsacului | Scor | 10 |
| Compilator | cpp | Status | done |
| Runda | Arhiva educationala | Marime | 0.94 kb |
#include <iostream>
#include <fstream>
using namespace std;
int mat[10000];
int obj[5000][2];
int main()
{
ifstream i("rucsac.in");
ofstream o("rucsac.out");
int x, y;
i >> x >> y;
int max = 0;
int m = 0;
mat[0] = 1;
for(int a = 0; a < x; a++)
{
i >> obj[a][0] >> obj[a][1];
}
for(int a = 0; a < x; a++)
{
for(int b = max; b >= 0; b--)
{
if(mat[b])
{
mat[b + obj[a][0]] = mat[b] + obj[a][1];
if(b + obj[a][0] > max)
{
max = b + obj[a][0];
}
if(mat[b] + obj[a][1] > m)
{
if(b + obj[a][0] <= y)
{
m = mat[b] + obj[a][1];
}
}
}
}
}
o << m - 1;
return 0;
}
