Cod sursa(job #2254458)

Utilizator BGondaGonda George Luis Bogdan BGonda Data 5 octombrie 2018 13:58:49
Problema Energii Scor 5
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.53 kb
#include <fstream>
using namespace std;
ifstream fin("energii.in");
ofstream fout("energii.out");
int  v[5002];
const int inf=0x3f3f3f3f;
int n,G,cost,cant;
int main()
{
    fin>>n>>G;

    for(int i=1;i<=G;i++)
        v[i]=inf;
    v[0]=0;
    for(int i=1;i<=n;i++)
    {
        fin>>cant>>cost;
        for(int j = G-cant; j >= 0; --j)
            if(v[j+cant]>v[j]+cost && v[j]!=inf)
                v[j+cant]=v[j]+cost;
    }
      if(v[G]!=inf)
      fout<<v[G];
      else
        fout<<"-1";
    return 0;
}