Pagini recente » Cod sursa (job #676651) | Cod sursa (job #2162744) | Cod sursa (job #2745652) | Cod sursa (job #2855888) | Cod sursa (job #2760083)
#include <fstream>
using namespace std;
ifstream f("energii.in");
ofstream g("energii.out");
const int N = 1000, inf = N * N * 10 + 1;
int n, w, e[N], c[N], costMin[5 * N];
int main(){
f >> n >> w;
for(int i = 0; i < n; i++)
f >> e[i] >> c[i];
f.close();
for(int j = 1; j <= w; j++)
costMin[j] = inf;
for(int i = 0; i < n; i++){
for(int j = w - 1; j >= 0; j--){
if(costMin[j] != inf){
if(j + e[i] >= w)
costMin[w] = min(costMin[w], costMin[j] + c[i]);
else
costMin[j + e[i]] = min(costMin[j + e[i]], costMin[j] + c[i]);
}
}
}
if(costMin[w] == inf)
g << -1;
else
g << costMin[w];
g.close();
}