Pagini recente » Cod sursa (job #2101400) | Cod sursa (job #781201) | Cod sursa (job #2872943) | Cod sursa (job #556722) | Cod sursa (job #2850365)
#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 = 1; energy <= req_energy; energy++) {
if (energy >= generator.energy) {
cost_min[i][energy] = min(
cost_min[i - 1][energy],
cost_min[i - 1][energy - generator.energy] - generator.cost
);
} else {
cost_min[i][energy] = min(cost_min[i - 1][energy], generator.cost);
}
}
}
return int_max - cost_min[number_of_gen][req_energy];
}
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;
}