Cod sursa(job #3199080)

Utilizator pauseZamfir Horia pause Data 31 ianuarie 2024 17:31:42
Problema Energii Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.89 kb
#include <fstream>

using namespace std;

const int N = 1000;
const int W = 5001;

int c[N], e[N], cost[W+1];

int main()
{
    ifstream in("energii.in");
    ofstream out("energii.out");
    int n, w;
    in >> n >> w;
    for (int i = 0; i < n; i++)
    {
        in >> e[i] >> c[i];
    }

    cost[0] = 0;
    for (int j = 1; j <= w; j++)
    {
        cost[j] = 1e9;
    }
    for (int i = 0; i < n; i++)
    {
        for (int j = w - 1; j >= 0; j--)
        {
            if (cost[j] != 1e9)
            {
                int j_obtinut = j + e[i];
                j_obtinut = min(j_obtinut, w);
                cost[j_obtinut] = min(cost[j_obtinut], cost[j] + c[i]);
            }
        }
    }
    if (cost[w] == INF)
    {
        out << "-1\n";
    }
    else
    {
        out << cost[w] << "\n";
    }
    in.close();
    out.close();
    return 0;
}