Pagini recente » Cod sursa (job #1976441) | Rating Raul Rusu (raulrusu99) | Cod sursa (job #3283834) | Cod sursa (job #2435198) | Cod sursa (job #3221815)
#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;
ifstream fin("ghiozdan.in");
ofstream fout("ghiozdan.out");
int n,g;
const int smax = 75000;
const int INF = 1e9;
int d[smax + 5];
pair<int,int> t[smax + 5];
int val[205];
int main()
{
fin>>n>>g;
for(int i=1; i<=n; i++)
{
int x;
fin>>x;
val[x]++;
}
for(int i = 200; i>=1; i--)
if(val[i])
for(int j = g; j>0; j--)
if(!d[j])
for(int ap=1; ap<=val[i]; ap++)
{
if(j-ap*i<0)
break;
if(d[j-ap*i] ||j-ap*i==0 )
{
d[j]=d[j-ap*i] + ap;
t[j]= {i,ap};
break;
}
}
int ind = g;
while(!d[ind])
ind--;
fout<<ind<<' '<<d[ind]<<'\n';
while(ind!=0)
{
for(int j = 1; j<=t[ind].second; j++)
fout<<t[ind].first<<'\n';
ind -= t[ind].first * t[ind].second;
}
}