Pagini recente » Cod sursa (job #376749) | Cod sursa (job #1014183) | Cod sursa (job #1648375) | Cod sursa (job #168688) | Cod sursa (job #2471851)
#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[5005];
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<=w; i++) profit[i] = INF;
for (int i=1; i<=n; i++)
{
for (int j=w; 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;
}