Cod sursa(job #1503733)

Utilizator elevenstrArina Raileanu elevenstr Data 16 octombrie 2015 20:32:26
Problema Energii Scor 45
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.84 kb
#include <iostream>
#include <fstream>
using namespace std;
ifstream in("energii.in");
ofstream out("energii.out");
#define INF 20000
#define maxi(a,b) (a>b?a:b)
#define mini(a,b) (a>b?b:a)
int e[1003],c[1003];
int mat[1003][5002];
int main()
{
    int g,w,su=0,i,j;
    in>>g>>w;
    for(i=1; i<=g; i++)
    {
        in>>e[i]>>c[i];
        su+=e[i];
    }
    if(su<w)out<<-1;
    else
    {
        for(i=1; i<=g; i++)
            for(j=1; j<=su; j++)
                mat[i][j]=INF;
        for(i=1; i<=e[1]; i++)
            mat[1][i]=c[1];

        for(i=2; i<=g; i++)
            for(j=1; j<=su; j++)
            {
                if(j-e[i]>0)mat[i][j]=mini(mat[i-1][j],mat[i-1][j-e[i]]+c[i]);
                else mat[i][j]=mini(c[i],mat[i-1][j]);
            }


        out<<mat[g][w];
    }

    return 0;
}