Cod sursa(job #2471851)

Utilizator StanCatalinStanCatalin StanCatalin Data 11 octombrie 2019 17:01:36
Problema Energii Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.87 kb
#include <iostream>
#include <fstream>

using namespace std;

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

const int dim = 1005;
const int INF = 100000001;

int n,w,e[dim],cost[dim],profit[5005];

int main()
{
    in >> n >> w;
    long long int s = 0;
    for (int i=1; i<=n; i++)
    {
        in >> e[i] >> cost[i];
        s += e[i];
    }
    if (s < w)
    {
        out << "-1";
        return 0;
    }
    for (int i=1; i<=w; i++) profit[i] = INF;
    for (int i=1; i<=n; i++)
    {
        for (int j=w; j>=0; j--)
        {
            if (j + e[i] > w)
            {
                profit[w] = min(profit[w] , profit[j] + cost[i]);
            }
            else
            {
                profit[j + e[i]] = min(profit[j] + cost[i] , profit[j + e[i]]);
            }
        }
    }
    out << profit[w];
    return 0;
}