Pagini recente » Cod sursa (job #1854487) | Cod sursa (job #512942) | Cod sursa (job #73762) | Cod sursa (job #3328668) | Cod sursa (job #487047)
Cod sursa(job #487047)
#include<iostream>
#include<fstream>
#define lim 12000000
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]=lim;
}
for(int i=1;i<g;i++)
{
for(int j=1;j<=w;j++)
{
best2[j]=lim;
if(e[i]<=j)
best2[j]=min(best1[j-e[i]]+cost[i],best1[j]);
else
best2[j]=min(cost[i],best1[j]);
}
for(int i=1;i<=w;i++)
best1[i]=best2[i];
}
if(best1[w]!=lim)
out<<best1[w]<<endl;
else
out<<"-1\n"<<endl;
out.close();
}