Pagini recente » Cod sursa (job #1238171) | Cod sursa (job #2625808) | Calibrare limite de timp | Cod sursa (job #2572393) | Cod sursa (job #1603021)
#include <iostream>
#include <fstream>
#include <utility>
#include <vector>
using namespace std;
vector<pair<int, int> > Ob;
int nrGen, eNecesara, energ, cost, pMax, cMax, ok;
int main()
{
freopen("energii.in", "rt", stdin);
freopen("energii.out", "wt", stdout);
scanf("%d%d", &nrGen, &eNecesara);
Ob.push_back(make_pair(0, 0));
for(int i=1; i<=nrGen; ++i)
{
scanf("%d\n%d", &energ, &cost);
Ob.push_back(make_pair(cost, energ));
cMax += cost;
}
vector<int>D1(cMax+5), D2(cMax+5);
for(int o=1; o<=nrGen && !ok; ++o)
{
D2.swap(D1);
for( cost=0; cost<=cMax && !ok; ++cost)
{
D2[cost] = D1[cost];
if(Ob[o].first <= cost)
D2[cost] = max(D2[cost], (D1[cost - Ob[o].first] + Ob[o].second) );
if(D2[cost] >= eNecesara)
{
cout<<cost<<'\n';
ok=1;
}
}
}
}