Pagini recente » Cod sursa (job #2702003) | Cod sursa (job #529445) | Cod sursa (job #1026751) | Valorificarea spiritului competititv | Cod sursa (job #490249)
Cod sursa(job #490249)
#include <fstream>
using namespace std;
const char InFile[]="energii.in";
const char OutFile[]="energii.out";
const int MaxN=5005;
const int MAX=1<<30;
ifstream fin(InFile);
ofstream fout(OutFile);
int W,CGi,EGi,G,sol,marked[MaxN],cost[MaxN];
int main()
{
fin>>G>>W;
cost[0]=0;
marked[0]=1;
for(register int i=0;i<G;++i)
{
fin>>EGi>>CGi;
for(register int j=MaxN-1;j>=0;--j)
{
if(marked[j]==1)
{
int pos=j+EGi;
if(j+EGi>W)
{
pos=W;
}
if(marked[pos]==1)
{
cost[pos]=min(cost[pos],cost[j]+CGi);
}
else
{
marked[pos]=1;
cost[pos]=CGi+cost[j];
}
}
}
}
fin.close();
if(cost[W]==MAX)
{
cost[W]=-1;
}
fout<<cost[W];
fout.close();
return 0;
}