Cod sursa(job #3167896)

Utilizator dragoncrackCandidatu Mario Luca dragoncrack Data 11 noiembrie 2023 11:22:24
Problema Energii Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.21 kb
#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;
}