Pagini recente » Cod sursa (job #6275) | Cod sursa (job #595824) | Cod sursa (job #1530135) | Cod sursa (job #1925012) | Cod sursa (job #2850372)
#include <iostream>
#include <vector>
#include <limits>
#include <fstream>
using namespace std;
struct Generator {
const int energy;
const int cost;
};
int solve(const int req_energy, const vector<Generator> &generators) {
const auto number_of_gen = generators.size();
constexpr auto int_max = numeric_limits<int>::max();
vector<vector<int>> cost_min(number_of_gen + 1, vector<int>(req_energy + 1, int_max));
for (int i = 1; i <= generators.size(); ++i) {
const auto generator = generators[i - 1];
for (int energy = req_energy; energy >= 0; --energy) {
if (energy + generator.energy <= req_energy) {
cost_min[i][energy] = min(
cost_min[i - 1][energy],
cost_min[i - 1][energy + generator.energy] - generator.cost
);
} else {
cost_min[number_of_gen][energy] = min(cost_min[number_of_gen][energy], generator.cost);
}
}
}
return cost_min[number_of_gen][1];
}
int main() {
ifstream in("energii.in");
ifstream::sync_with_stdio(false);
int G{};
int W{};
in >> G >> W;
vector<Generator> generators{};
for (int i = 0; i < G; i++) {
int e{}, c{};
in >> e >> c;
generators.push_back({e, c});
}
ofstream out("energii.out");
out << solve(W, generators);
return 0;
}