Pagini recente » Cod sursa (job #3041119) | Cod sursa (job #2456288) | Cod sursa (job #1629339) | Cod sursa (job #428835) | Cod sursa (job #2430799)
#include <fstream>
#include <vector>
#define change(A) (A%2==1)?0:1
using namespace std;
ifstream fin("ghiozdan.in");
ofstream fout("ghiozdan.out");
struct cont
{
int value;
vector<int> items;
};
int n,g;
int greutati[20010];
cont help[2][75000];
int main()
{
int maximum = 0;
vector<int> temp;
fin>>n>>g;
for(int i = 1;i<=n;i++)
fin>>greutati[i];
for(int i = 1;i<=n;i++)
{
for(int j = 0;j<=g;j++)
{
if(greutati[i]<=j)
{
if(help[change(i)][j].value>help[change(i)][j-greutati[i]].value+greutati[i])
{
help[i%2][j]=help[change(i)][j];
}
else
{
help[i%2][j] = help[change(i)][j-greutati[i]];
help[i%2][j].items.push_back(greutati[i]);
help[i%2][j].value+=greutati[i];
}
}
else
{
help[i%2][j] = help[change(i)][j];
}
if(maximum<help[i%2][j].value)
maximum = help[i%2][j].value,temp = help[i%2][j].items;
else if(maximum == help[i%2][j].value)
{
if(temp.size()>help[i%2][j].items.size())
temp = help[i%2][j].items;
}
}
}
int res = help[n%2][g].value;
fout<<maximum<<" "<<temp.size()<<'\n';
for(int i = 0;i<temp.size();i++)
fout<<temp[i]<<'\n';
return 0;
}