Pagini recente » Cod sursa (job #2880370) | Cod sursa (job #2061616) | Cod sursa (job #1751560) | Cod sursa (job #411356) | Cod sursa (job #2717314)
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
ifstream f("rucsac.in");
ofstream g("rucsac.out");
struct obiect {
int g, val;
};
int n, gmax, row = 1;
vector <obiect> v;
int a[3][5005];
int otherRow(int l) {
return 3 - l;
}
void rucsac() {
for(int i = 1; i <= n; ++i, row = otherRow(row))
for(int j = 1; j <= gmax; ++j)
if(v[row].g > j)
a[row][j] = a[otherRow(row)][j];
else a[row][j] = max(a[otherRow(row)][j], a[otherRow(row)][j - v[i].g] + v[i].val);
}
int main()
{
f >> n >> gmax;
v = vector <obiect> (n + 1);
for(int i = 1; i <= n; ++i)
f >> v[i].g >> v[i].val;
rucsac();
g << a[otherRow(row)][gmax];
}