Cod sursa(job #2859907)

Utilizator dobreraduDobre Radu Fabian dobreradu Data 2 martie 2022 09:48:04
Problema Energii Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.71 kb

#include <fstream>

using namespace std;

const int N = 1000;
const int K = 5001;

int n, k, e[N], c[N], cost[K];

int main()
{
    ifstream in("energi.in");
    ofstream out("energii.out");
    in >> n >> k;
    for (int i = 0; i < n; i++)
        in >> e[i] >> c[i];
    for (int j = 1; j <= k; j++)
        cost[j] = -1;
    cost[0] = 0;
    for (int i = 0; i < n; i++){
        for (int j = k - 1; j >= 0; j--){
            if (cost[j] != -1){
                int energia = min(k, j + e[i]);
                if (cost[energia] == -1 || cost[j] + c[i] < cost[energia])
                    cost[energia] = cost[j] + c[i];
            }
        }
    }
    out << cost[k];
    return 0;
}