Pagini recente » Cod sursa (job #2682246) | Cod sursa (job #1797838) | Cod sursa (job #1058940) | Cod sursa (job #604852) | Cod sursa (job #2837278)
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
ifstream fin("energii.in");
ofstream fout("energii.out");
const int MAX=1005;
const int wMAX=20005;
int g,w,wtotal,wmax,waf;
struct T
{
int en,cost;
};
vector < T > v(MAX);
vector < int > cost(wMAX,-1);
int main()
{
fin >> g >> w;
for(int i=1;i<=g;i++)
{
fin >> v[i].en >> v[i].cost;
wtotal+=v[i].en;
wmax=max(wmax,v[i].en);
}
if(wtotal<w)
{
fout << -1;
return 0;
}
for(int i=1;i<=g;i++)
for(int j=wmax;j>=0;j--)
if(j>=v[i].en)
{
if(cost[j]!=-1)
cost[j]=min(cost[j-v[i].en]+v[i].cost,cost[j]);
else if(j==v[i].en)
cost[j]=v[i].cost;
}
waf=1e9;
for(int j=w;j<=wmax;j++)
if(cost[j]!=-1 && cost[j]<waf)
waf=cost[j];
fout << waf;
return 0;
}