Cod sursa(job #2342136)

Utilizator ilie0712Botosan Ilie ilie0712 Data 12 februarie 2019 16:52:39
Problema Energii Scor 60
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.78 kb
#include <iostream>

#include <fstream>

using namespace std;

ifstream in("energii.in");
ofstream out("energii.out");

const int N=5002;
int profit[10001],p[1002],c[1002];
int n,maxim=1000000,gr,cmax,s;

int main()

{

    in>>n>>gr;

    for(int i=0; i<n; ++i)
    {
        in>>c[i]>>p[i];
        cmax=max(cmax,c[i]);
        s+=p[i];
    }
    if(s<gr) out<<-1;
    else
    {
        profit[0]=0;
        for(int i=1; i<=gr+cmax; ++i) profit[i]=100000000;

        for(int i=0; i<n; ++i)
        {
            for(int j=gr-1; j>=0; --j)
                if(profit[j]!=100000000 && profit[j]+p[i]<profit[j+c[i]])
                    profit[j+c[i]]=profit[j]+p[i];
        }
        for(int i=gr; i<=gr+cmax; ++i)
            maxim=min(maxim,profit[i]);
        out<<maxim;
    }
    return 0;

}