Pagini recente » Cod sursa (job #1437318) | Cod sursa (job #2130433) | Cod sursa (job #386558) | Cod sursa (job #2666852) | Cod sursa (job #487028)
Cod sursa(job #487028)
#include<iostream>
#include<fstream>
using namespace std;
const int nmax=1005;
inline int min(int a,int b)
{
return a<=b ? a:b;
}
int main()
{
int g,w,best1[5002],best2[5002];
ifstream in("energii.in");
ofstream out("energii.out");
in>>g>>w;
int e[nmax],cost[nmax];
for(int i=0;i<g;i++)
in>>e[i]>>cost[i];
in.close();
best1[0]=0;
for(int i=1;i<=w;i++)
{
if(e[0]>=i)
best1[i]=cost[0];
else
best1[i]=-1;
}
for(int i=1;i<g;i++)
{
for(int j=1;j<=w;j++)
{
if(e[i]<=j)
best2[j]=min(best1[j-e[i]]+cost[i],best1[j]);
else
{
if(best1[j]!=-1)
best2[j]=min(cost[i],best1[j]);
else
best2[j]=cost[i];
}
}
for(int i=0;i<=w;i++)
best1[i]=best2[i];
}
out<<best1[w]<<endl;
out.close();
}