Pagini recente » Cod sursa (job #300527) | Cod sursa (job #7443) | Cod sursa (job #2736318) | Cod sursa (job #405573) | Cod sursa (job #3218238)
#include <iostream>
#include <fstream>
using namespace std;
ifstream fin("ghiozdan.in");
ofstream fout("ghiozdan.out");
int obj[205], dp[75005];
pair<int, int> conn[75005];
int main()
{
int n, g, x, maxi = 0;
fin>>n>>g;
for(int i=0;i<n;i++){
fin>>x;
obj[x]++; // increase the objects appearance
maxi = max(x, maxi);
}
for(int i=maxi;i;i--) // walk through the objects
if(obj[i])
for(int j=g;j;j--) // walk through the needed total weights
if(!dp[j])
for(int k=1;k<=obj[i] && j-i*k>=0;k++) // walk through the number of appearances of the object
if(dp[j-i*k] || j-i*k==0) { // is there any object to fill up the weight difference?
dp[j] = k + dp[j-i*k]; // add the number of objects needed to fill the weight
// k ( for the current object ) and dp[j-i*k] for the last object
conn[j] = {i, k};
break;
}
int pos = g;
while(!dp[pos])
pos--;
fout<<pos<<' '<<dp[pos]<<'\n';
while (pos){
for (int i=1;i<=conn[pos].second;i++)
fout<<conn[pos].first<<'\n';
pos -= conn[pos].first * conn[pos].second;
}
return 0;
}