Pagini recente » Cod sursa (job #842880) | Cod sursa (job #876112) | Cod sursa (job #1376943) | Cod sursa (job #1895552) | Cod sursa (job #3149603)
#include <iostream>
#include <fstream>
#include <vector>
#include <climits>
#include <algorithm>
using namespace std;
int main() {
// Open input and output files
ifstream fin("energii.in");
ofstream fout("energii.out");
int G, W;
fin >> G >> W;
// Initialize dp array where dp[i] will store the minimum cost to produce i units of energy.
// Initialize with INT_MAX as it's impossible to produce any energy without generators.
vector<int> dp(W + 1, INT_MAX);
// It costs 0 to produce 0 energy
dp[0] = 0;
// Read energy and cost for each generator
for (int i = 0; i < G; ++i) {
int E, C;
fin >> E >> C;
// Update the dp array
for (int w = W; w >= 0; --w) {
// If dp[w] is INT_MAX, we can't produce w energy, so skip
if (dp[w] == INT_MAX) continue;
// Otherwise, update dp for new energy level
int new_w = w + E;
if (new_w <= W) {
dp[new_w] = min(dp[new_w], dp[w] + C);
}
}
}
// If it's possible to produce W energy, output the cost. Otherwise, output -1
if (dp[W] == INT_MAX) {
fout << "-1\n";
} else {
fout << dp[W] << "\n";
}
// Close files
fin.close();
fout.close();
return 0;
}