Pagini recente » Cod sursa (job #1636012) | Cod sursa (job #1624084) | Cod sursa (job #577759) | Cod sursa (job #409105) | Cod sursa (job #1496063)
#include <bits/stdc++.h>
using namespace std;
const int Wmax = 5000;
const int Inf = 0x3F3F3F3F;
int minCost[Wmax + 1];
int main()
{
ifstream in("energii.in");
ofstream out("energii.out");
int n, w;
int energy, cost;
memset(minCost, Inf, sizeof(minCost));
in >> n >> w;
minCost[0] = 0;
for ( int i = 0; i < n; i++ )
{
in >> energy >> cost;
for ( int j = w - 1; j >= 0; j-- )
{
if ( minCost[j] != Inf )
{
int newEnergy = j + energy;
if ( newEnergy > w )
newEnergy = w;
if ( minCost[newEnergy] > minCost[j] + cost )
minCost[newEnergy] = minCost[j] + cost;
}
}
}
in.close();
out << ( minCost[w] == Inf ? -1 : minCost[w] ) << '\n';
out.close();
return 0;
}