Pagini recente » Cod sursa (job #2095471) | Cod sursa (job #3288653) | Cod sursa (job #2456389) | Cod sursa (job #3191499) | Cod sursa (job #2382047)
#include <fstream>
using namespace std;
ifstream fin("rucsac.in");
ofstream fout("rucsac.out");
int G, n, g[101], v[101], w[101], x[101], d[101], p;
int main()
{
fin>>n>>G;
for (int i=1;i<=n;i++) fin>>g[i];
for (int i=1;i<=n;i++) fin>>w[i];
x[0]=1;
d[0]=0;
v[0]=0;
p=0;
int maxi=0;
for (int i=1;i<=n;i++)
{
for (int j=maxi;j>=0;j--)
if (x[j] && j+g[i]<=G && v[j+g[i]]<v[j]+w[i])
{
v[j+g[i]]=v[j]+w[i];
x[j+g[i]]=i;
if (v[j+g[i]]>v[p]) p=j+g[i];
d[j+g[i]]=j;
if (j+g[i]>maxi) maxi=j+g[i];
}
/*for (int k=0;k<=G;k++) fout<<x[k]<<" ";
fout<<'\n';
for (int k=0;k<=G;k++) fout<<v[k]<<" ";
fout<<'\n';
fout<<p<<'\n';*/
}
fout<<v[p]<<'\n';
/*while (p)
{ fout<<x[p]<<" "<<v[x[p]]<<'\n';
p=d[p];
}
fout<<'\n';
for (int i=0;i<=G;i++) fout<<x[i]<<" ";
fout<<'\n';
for (int i=0;i<=G;i++) fout<<v[i]<<" ";
fout<<'\n';
for (int i=0;i<=G;i++) fout<<d[i]<<" ";
fout<<'\n';*/
return 0;
}