Pagini recente » Rating Mateescu Vlad (vlad.mateescu) | Cod sursa (job #1179890) | Cod sursa (job #2669554) | Cod sursa (job #1337050) | Cod sursa (job #2367052)
#include <fstream>
#include <cstring>
using namespace std;
const int GMAX = 1005;
const int WMAX = 10005;
const int INF = (1 << 30);
int g, w;
int dp[GMAX][WMAX];
int cost[GMAX], cant[GMAX];
int main()
{
ifstream fin("energii.in");
ofstream fout("energii.out");
fin >> g >> w;
for (int i = 1;i <= g;++i)
fin >> cant[i] >> cost[i];
int sw = w;
w += 1000;
for (int i = 1;i <= g;++i)
for (int j = 1;j <= w;++j)
dp[i][j] = INF;
dp[1][cant[1]] = cost[1];
for (int i = 2;i <= g;++i)
for (int j = 1;j <= w;++j)
if (cant[i] <= j)
dp[i][j] = min(dp[i - 1][j], dp[i - 1][j - cant[i]] + cost[i]);
int ans = INF;
for (int j = sw;j <= w;++j)
ans = min(ans, dp[g][j]);
if (ans != INF)
fout << ans << "\n";
else
fout << -1 << "\n";
fin.close();
fout.close();
return 0;
}