Pagini recente » Cod sursa (job #3041261) | Cod sursa (job #1406621) | Cod sursa (job #977503) | Cod sursa (job #2621818) | Cod sursa (job #2471848)
#include <iostream>
#include <fstream>
using namespace std;
ifstream in("energii.in");
ofstream out("energii.out");
const int dim = 1005;
const int INF = 100000001;
int n,w,e[dim],cost[dim],profit[1000005];
int main()
{
in >> n >> w;
long long int s = 0;
for (int i=1; i<=n; i++)
{
in >> e[i] >> cost[i];
s += e[i];
}
if (s < w)
{
out << "-1";
return 0;
}
for (int i=1; i<=s; i++) profit[i] = INF;
for (int i=1; i<=n; i++)
{
for (int j=s-e[i]; j>=0; j--)
{
if (j + e[i] > w)
{
profit[w] = min(profit[w] , profit[j] + cost[i]);
}
else
{
profit[j + e[i]] = min(profit[j] + cost[i] , profit[j + e[i]]);
}
}
}
out << profit[w];
return 0;
}