Pagini recente » Cod sursa (job #1200002) | Cod sursa (job #2766691) | Cod sursa (job #541410) | Cod sursa (job #654154) | Cod sursa (job #2199889)
#include <algorithm>
#include <fstream>
#include <utility>
#include <vector>
#define INF 0xf0f0f0f0
int main() {
std::ifstream in("energii.in");
std::ofstream out("energii.out");
unsigned g, w;
in >> g >> w;
std::vector<std::pair<long, long>> generators;
generators.reserve(g);
for (unsigned i = 0; i < g; i++) {
long energy, cost;
in >> energy >> cost;
generators.push_back(std::make_pair(energy, cost));
}
std::vector<std::vector<long>> dp;
dp.reserve(g + 1);
for (unsigned i = 0; i <= g; i++) {
dp.push_back(std::vector<long>());
dp[i].reserve(w);
for (unsigned j = 0; j < w; j++) {
dp[i].push_back(INF);
}
}
for (unsigned i = 1; i <= g; i++) {
for (unsigned j = 0; j < w; j++) {
long previousMax = dp[i - 1][j];
long currentCost = generators[i - 1].second;
long subEnergy = j + 1 - generators[i - 1].first;
long subCost = subEnergy <= 0 ? 0 : dp[i - 1][subEnergy];
dp[i][j] = std::min(previousMax, currentCost + subCost);
}
}
if (dp[g][w - 1] == INF) out << -1 << "\n";
else out << dp[g][w - 1] << "\n";
return 0;
}