Pagini recente » Cod sursa (job #2840894) | Cod sursa (job #159751) | Clasament test_penali | Cod sursa (job #1460544) | Cod sursa (job #1000897)
#include <fstream>
#define max(a, b) (a < b) ? b : a
struct Object
{
int w, p;
};
int main()
{
std::ifstream in("rucsac.in");
std::ofstream out("rucsac.out");
int nV, nG;
in >> nV >> nG;
Object *A = new Object[nV + 1];
for(int i = 1; i <= nV; i++)
in >> A[i].w >> A[i].p;
int **B = new int*[2];
for(int i = 0; i < 2; i++)
B[i] = new int[nG + 1];
for(int j = 0; j <= nG; j++)
B[0][j] = 0;
int l = 0;
for(int i = 1; i <= nV; i++, l = 1 - l)
for(int j = 0; j <= nG + 1; j++)
{
B[1 - l][j] = B[l][j];
if(j >= A[i].w)
B[1 - l][j] = max(B[1 - l][j], B[l][j - A[i].w] + A[i].p);
}
out << B[l][nG];
return 0;
}