Cod sursa(job #3330293)

Utilizator cont_superscoalaSuperScoala cont_superscoala Data 18 decembrie 2025 16:07:12
Problema Energii Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.04 kb
/*
https://www.infoarena.ro/problema/energii
*/
#include <fstream>

using namespace std;

const int INF = 10015;
const int E = 5000;

struct generator
{
    int e, c;
};

int cost[E+1];

int main()
{
    ifstream in("energii.in");
    ofstream out("energii.out");
    int n, w;
    in >> n >> w;
    for (int j = 1; j <= w + 1; j++)
    {
        cost[j] = INF;
    }
    cost[0] = 0;
    for (int i = 0; i < n; i++)
    {
        generator g;
        in >> g.e >> g.c;
        for (int j = w - 1; j >= 0; j--)
        {
            if (cost[j] != INF)
            {
                int e_prod = j + g.e;
                if (e_prod > w)
                {
                    e_prod = w;
                }
                if (cost[j] + g.c < cost[e_prod])
                {
                    cost[e_prod] = cost[j] + g.c;
                }
            }
        }
    }
    if (cost[w] == INF)
    {
        cost[w] = -1;
    }
    out << cost[w] << "\n";
    in.close();
    out.close();
    return 0;
}