Cod sursa(job #690001)

Utilizator ion_calimanUAIC Ion Caliman ion_caliman Data 25 februarie 2012 01:04:51
Problema Energii Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.55 kb
#include <fstream>
using namespace std;
ifstream f("energii.in");
ofstream g("energii.out");

int N, E;
int S[5002];
int P[5002];

int main()
{
    int x,p,i,j;
    f >> N >> E;
    S[0] = 1;
    for (i=0; i<N; i++){
        f >> x >> p;
        for (j=E; j>=0; j--){
            if (S[j]){
                if (j+x>=E && (!S[E] || P[j]+p<P[E])) S[E] = 1, P[E] = P[j] + p; else
                if (j+x<E && (!S[j+x] || P[j]+p<P[j+x])) S[j+x] = 1, P[j+x] = P[j] + p;
            }
        }
    }
    if (!S[E]) g << -1; else g << P[E];
}