Cod sursa(job #2690610)

Utilizator AndreiBOTOBotocan Andrei AndreiBOTO Data 24 decembrie 2020 21:27:42
Problema Energii Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.8 kb
#include <fstream>
#include <cmath>
#include <algorithm>
using namespace std;
ifstream fin("energii.in");
ofstream fout("energii.out");
const int NMAX=10005;
long long e[NMAX];
long long c[NMAX];
long long d[NMAX];
int main()
{
    long long g, w,i,r,j;
    fin >> g >> w;
    for ( i = 1; i <= g; i++)
        fin >> e[i] >> c[i];
    for ( i = 1; i <= w; i++)
        d[i] =1000000 ;
    r = 1000000;
    for ( j = 1; j <= g; j++)
        {
        for ( i = w - 1; i >= 0; i--)
        {
            if (d[i] != 1000000 && i + e[j] >= w)
                r = min(r, d[i] + c[j]);
            else if (d[i] != 1000000)
                d[i + e[j]] = min(d[i + e[j]], d[i] + c[j]);
        }
    }
    if (r != 1000000)
        fout << r;
    else
        fout << -1;
    return 0;
}