Pagini recente » Cod sursa (job #1917637) | Cod sursa (job #2758469) | Cod sursa (job #148508) | Cod sursa (job #1253007) | Cod sursa (job #2276534)
#include <fstream>
#include <cmath>
using namespace std;
ifstream fin("energii.in");
ofstream fout("energii.out");
const int DM = 5e4+ 5;
const int MI = 9e8;
int w,g;
int dp[DM][DM];
struct generator
{
int energie; int cost;
} gn[DM];
int main()
{
fin >> g >> w;
for(int i = 1; i <= g; i++)
fin >> gn[i].energie >> gn[i].cost;
for(int i = 0; i <= g; i++)
for(int j = 0; j <= w; j++)
dp[i][j] = MI;
for(int i = 1; i <= g; i++)
for(int j = 0; j <= w; j++)
{
dp[i][j] = dp[i - 1][j];
if(gn[i].energie >= j) dp[i][j] = gn[i].cost;
else dp[i][j] = min(dp[i][j], (dp[i - 1][j - gn[i].energie] + gn[i].cost));
}
if(dp[g][w] == MI) fout << "-1";
else fout << dp[g][w];
fin.close();
fout.close();
return 0;
}