Cod sursa(job #2837275)

Utilizator cdenisCovei Denis cdenis Data 22 ianuarie 2022 00:46:46
Problema Energii Scor 5
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.17 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,mat[MAX][wMAX];
struct T
{
    int en,cost;
};
vector < T > v(MAX);

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