Pagini recente » Cod sursa (job #1006653) | Cod sursa (job #1029550) | Cod sursa (job #3197325) | Cod sursa (job #1078331) | Cod sursa (job #2798932)
#include <bits/stdc++.h>
using namespace std;
ifstream f("ghiozdan.in");
ofstream g("ghiozdan.out");
int frec[100000],n,gh,x,solutie[250000],dp[200000],k;
int main()
{
f>>n>>gh;
for(int i=1; i<=n; i++)
{
f>>x;
frec[x]++;
}
dp[0]=1;
for(int i=200; i>=1; i--)
{
for(int j=gh; j>=0; j--)
{
if(dp[j])
{
for(k=1; k*i+j<=gh&&k<=frec[i]; k++)
{
if(dp[k*i+j])
{
break;
}
dp[k*i+j]=dp[j]+k;
solutie[i*k+j]=i;
}
}
}
}
while(!dp[gh])
gh=gh-1;
g<<gh<<" "<<dp[gh]-1<<" "<<endl;
while(gh)
{
g<<solutie[gh]<<endl;
gh=gh-solutie[gh];
}
}