Pagini recente » Cod sursa (job #1226704) | Cod sursa (job #849346) | Cod sursa (job #329661) | Cod sursa (job #1555673) | Cod sursa (job #2837273)
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
ifstream fin("energii.in");
ofstream fout("energii.out");
const int MAX=1005;
const int wMAX=20005;
int g,w,wtotal,wmax,waf,mat[MAX][wMAX];
struct T
{
int en,cost;
};
vector < T > v(MAX);
int main()
{
fin >> g >> w;
for(int i=1;i<=g;i++)
{
fin >> v[i].en >> v[i].cost;
wtotal+=v[i].en;
wmax=max(v[i].en,wmax);
}
if(wtotal<w)
{
fout << -1;
return 0;
}
wmax=max(wmax,w);
for(int j=0;j<=wmax;j++)
mat[0][j]=-1;
for(int i=1;i<=g;i++)
for(int j=0;j<=wmax;j++)
if(j>=v[i].en)
{
if(j==v[i].en)
mat[i][j]=v[i].cost;
else if(mat[i-1][j-v[i].en]!=-1)
mat[i][j]=min(mat[i-1][j-v[i].en]+v[i].cost,mat[i-1][j]);
else
mat[i][j]=-1;
}
else
mat[i][j]=mat[i-1][j];
for(int i=0;i<=g;i++)
{
for(int j=0;j<=wmax;j++)
fout << mat[i][j] << " ";
fout << '\n';
}
waf=1e9;
for(int j=w;j<=wmax;j++)
if(mat[g][j]!=-1 && mat[g][j]<waf)
waf=mat[g][j];
fout << waf;
return 0;
}