Cod sursa(job #365233)

Utilizator popoiu.georgeGeorge Popoiu popoiu.george Data 18 noiembrie 2009 10:51:37
Problema Energii Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.98 kb
#include<fstream>
#include<iostream>
#define inf "energii.in"
#define outf "energii.out"
#define Max 1001
#define MaxW 5001
using namespace std;

fstream f(inf,ios::in),g(outf,ios::out);

int G,W;
int E[Max],C[Max];
int Cost[Max][MaxW];

void Citire()
{
f>>G>>W;
for(int i=1;i<=G;i++)
    {
    f>>E[i]>>C[i];
    }
}

int main()
{
Citire();
for(int i=1;i<=G;i++)
    for(int j=1;j<=W;j++)Cost[i][j]=-1;
for(int i=1;i<=W;i++){Cost[0][i]=0;Cost[i][0]=0;}
for(int i=1;i<=G;i++)
    {
    for(int j=1;j<=W;j++)
        {
        if(E[i]<=j)
            {
            if( C[i]+Cost[i-1][j-E[i]] > Cost[i-1][j]/* && Cost[i-1][j-E[i]]!=-1 && Cost[i-1][j]!=-1*/ )
                {
                Cost[i][j]=C[i]+Cost[i-1][j-E[i]];
                }
            else //if(Cost[i-1][j]!=-1)
                {
                Cost[i][j]=Cost[i-1][j];
                }
            }
        }
    }
if(Cost[G][W])g<<Cost[G][W];
else g<<"-1";
f.close();
g.close();
return 0;
}