Pagini recente » Cod sursa (job #1136674) | Cod sursa (job #2345756) | Clasamentul arhivei educationale | Cod sursa (job #2192198) | Cod sursa (job #3167896)
#include <fstream>
#include <climits>
#define DIM 1005
#define maxEnergyRequirement 5001
#define maxGeneratorPower 10001
using namespace std;
ifstream fin("energii.in");
ofstream fout("energii.out");
int G, requirement;
int energy[DIM], cost[DIM];
int costForEnergy[maxEnergyRequirement + maxGeneratorPower];
int sol = -1;
int main()
{
for (int i = 1; i < maxEnergyRequirement + maxGeneratorPower; i++) {
costForEnergy[i] = INT_MAX;
}
fin >> G >> requirement;
for (int i = 1; i <= G; i++) {
fin >> energy[i] >> cost[i];
}
for (int i = 1; i <= G; i++) {
for (int j = requirement; j >= 0; j--) {
if (costForEnergy[j] != INT_MAX) {
costForEnergy[j + energy[i]] = min(costForEnergy[j + energy[i]], costForEnergy[j] + cost[i]);
if (j + energy[i] >= requirement)
{
if (sol == -1)
sol = costForEnergy[j + energy[i]];
else
sol = min(sol, costForEnergy[j + energy[i]]);
}
}
}
costForEnergy[energy[i]] = min(costForEnergy[energy[i]], cost[i]);
}
fout << sol;
}