Cod sursa(job #2973647)

Utilizator 55andreiv55Andrei Voicu 55andreiv55 Data 1 februarie 2023 15:24:45
Problema Energii Scor 5
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.76 kb
#include <fstream>

using namespace std;
const int N = 5001, INF = 1e9;

ifstream in("energii.in");
ofstream out("energii.out");

int sum[N];
int main()
{
    int e, k;
    in >> e >> k;
    for (int i = 1; i <= k; i++)
        sum[i] = INF;
    for (int i = 0; i < e; i++)
    {
        int ei, ci;
        in >> ei >> ci;
        for (int j = e - 1; j >= 0; j--)
        {
            if (sum[j] != INF)
            {
                if (j + ei >= e)
                    sum[e] = min(sum[e], sum[j] + ci);
                else
                    sum[j + ei] = min(sum[j + ei], sum[j + ei] + ci);
            }
        }
    }
    in.close();
    if (sum[e] == INF)
        sum[e] = -1;
    out << sum[e];
    out.close();
    return 0;
}