Pagini recente » Cod sursa (job #2325930) | Cod sursa (job #2623060) | Cod sursa (job #1117615) | Cod sursa (job #2646692) | Cod sursa (job #1601955)
#include <iostream>
#include <fstream>
#include <utility>
#include <vector>
#define NMAX 5005
#define GMAX 10005
using namespace std;
vector<pair<int, int> > Ob;
vector<vector<int> > D;
int nrO, G, a, b, pMax;
int main()
{
freopen("rucsac.in", "rt", stdin);
freopen("rucsac.out", "wt", stdout);
scanf("%d%d", &nrO, &G);
D.assign(nrO+1, vector<int>(G+2, 0)); //creaza nrO+1 linii
Ob.push_back(make_pair(0, 0));
for(int i=1; i<=nrO; ++i){
scanf("%d%d", &a, &b);
Ob.push_back(make_pair(a, b));
}
for(int o=1; o<=nrO; ++o)
for( int g=0; g<=G; ++g){
D[o][g] = D[o-1][g];
if(Ob[o].first <= g)
D[o][g] = max(D[o][g], (D[o-1][g - Ob[o].first] + Ob[o].second) );
}
for(int i=0; i<=G; ++i)
pMax = max(pMax, D[nrO][i]);
cout<<pMax<<'\n';
}