Cod sursa(job #2837278)

Utilizator cdenisCovei Denis cdenis Data 22 ianuarie 2022 00:59:59
Problema Energii Scor 5
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.97 kb
#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

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

const int MAX=1005;
const int wMAX=20005;
int g,w,wtotal,wmax,waf;
struct T
{
    int en,cost;
};
vector < T > v(MAX);
vector < int > cost(wMAX,-1);

int main()
{
    fin >> g >> w;
    for(int i=1;i<=g;i++)
    {
        fin >> v[i].en >> v[i].cost;
        wtotal+=v[i].en;
        wmax=max(wmax,v[i].en);
    }
    if(wtotal<w)
    {
        fout << -1;
        return 0;
    }
    for(int i=1;i<=g;i++)
        for(int j=wmax;j>=0;j--)
            if(j>=v[i].en)
            {
                if(cost[j]!=-1)
                    cost[j]=min(cost[j-v[i].en]+v[i].cost,cost[j]);
                else if(j==v[i].en)
                    cost[j]=v[i].cost;
            }
    waf=1e9;
    for(int j=w;j<=wmax;j++)
        if(cost[j]!=-1 && cost[j]<waf)
            waf=cost[j];
    fout << waf;
	return 0;
}