Pagini recente » Cod sursa (job #747426) | Cod sursa (job #2661924) | Cod sursa (job #2623029) | Cod sursa (job #2952010) | Cod sursa (job #2443893)
#include <fstream>
#include <cstdio>
#define INF 2000000000
#define DIM 210
#define DIMG 80000
#define DIMBUFF 5000000
using namespace std;
FILE *fin = fopen ("ghiozdan.in","r");
FILE *fout = fopen ("ghiozdan.out","w");
int f[DIM],d[DIMG],ant[DIMG];
int g,n,i,j,t,maxi,x,pos,mx;
char buff[DIMBUFF];
inline int get_nr(){
while(!(buff[pos] >= '0' && buff[pos] <= '9'))
pos++;
int nr = 0;
while(buff[pos] >= '0' && buff[pos] <= '9'){
nr = nr*10 + buff[pos] - '0';
pos++;
}
return nr;
}
int main (){
fread (buff,1,DIMBUFF,fin);
n = get_nr(), g = get_nr();
for (i=1;i<=n;i++){
x = get_nr();
f[x]++;
mx = max (mx,x);
}
/// d[i][j] - nr min de obiecte pt a obtine greutatea j folosind\
for (j=0;j<=g;j++)
d[j] = INF;
d[0] = 0;
for (i=mx;i;i--){
if (!f[i]) /// nu are sens sa il mai iau in calcul daca nu apare
continue;
for (j=g;j>=0;j--){
if (d[j] != INF){
for (t=1;t<=f[i];t++){
if (j+i*t > g)
break;
if (d[j+i*t] != INF)
continue;
maxi = max (maxi,j+i*t);
d[j+i*t] = d[j]+t;
ant[j+i*t] = i;
}}}}
fprintf(fout,"%d %d\n",maxi,d[maxi]);
x = maxi;
while (x){
fprintf(fout,"%d\n",ant[x]);
x -= ant[x];
}
return 0;
}