Cod sursa(job #2254485)

Utilizator BGondaGonda George Luis Bogdan BGonda Data 5 octombrie 2018 14:44:10
Problema Energii Scor 5
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.51 kb
#include <fstream>
using namespace std;
ifstream fin("energii.in");
ofstream fout("energii.out");
const int Inf = 0x3f3f3f3f;
int n, G , cant, cost, v[10001];
 
int main()
{
    fin >> n >> G;
    for(int i=1;i<=G;i++)
        v[i]=Inf;
    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+cant]=v[j]+cost;
    }
 
  if(v[G] == Inf)
     fout << -1;
  else
     fout << v[G];
 
  return 0;
}