Pagini recente » Cod sursa (job #1385328) | Cod sursa (job #1765909) | Cod sursa (job #2175380) | Cod sursa (job #3257625) | Cod sursa (job #1265737)
#include <fstream>
using namespace std;
ifstream in("energii.in");
ofstream out("energii.out");
const int gmax = 1000 , wmax = 5000;
int d[wmax + 1][gmax + 1] , e[gmax + 1] , c[gmax + 1];
int g , w;
int main()
{
in >> g >> w;
for(int i = 1 ; i <= g ; i++)
in >> e[i] >> c[i];
for(int i = 1 ; i <= w ; i++)
d[i][0] = -1;
for(int i = 1 ; i <= w ; i++)
for(int j = 1 ; j <= g ; j++)
{
d[i][j] = -1;
if(d[i][j - 1] != -1)
d[i][j] = d[i][j - 1];
if(i >= e[j] && d[i - e[j]][j - 1] != -1 && d[i][j] == -1)
d[i][j] = d[i - e[j]][j - 1] + c[j];
else if(i >= e[j] && d[i - e[j]][j - 1] != -1)
d[i][j] = min(d[i][j] , d[i - e[j]][j - 1] + c[j]);
}
out << d[w][g] << '\n';
return 0;
}