Pagini recente » Cod sursa (job #139281) | Cod sursa (job #3267803) | Cod sursa (job #168173) | Cod sursa (job #2624254) | Cod sursa (job #365235)
Cod sursa(job #365235)
#include<fstream>
#include<iostream>
#define inf "energii.in"
#define outf "energii.out"
#define Max 1002
#define MaxW 5002
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;
}