Cod sursa(job #2837285)

Utilizator cdenisCovei Denis cdenis Data 22 ianuarie 2022 01:21:53
Problema Energii Scor 5
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.9 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,1e9);

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)
                cost[j]=min(cost[j],v[i].cost);
            else if(j>v[i].en)
                cost[j]=min(cost[j-v[i].en]+v[i].cost,cost[j]);
    waf=1e9;
    for(int j=w;j<=wmax;j++)
        if(cost[j]<waf)
            waf=cost[j];
    fout << waf;
	return 0;
}