Cod sursa(job #2185205)

Utilizator TonuMihaelaTonu Mihaela TonuMihaela Data 24 martie 2018 13:50:58
Problema Ghiozdan Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.85 kb
#include <fstream>
#include <algorithm>
 
#define NMAX 75007
 
using namespace std;
 
ifstream in("ghiozdan.in");
ofstream out("ghiozdan.out");
 
int Ap[NMAX], a[NMAX], b[NMAX];
int G, n, x;
 
int main (){
    in >> n >> G;
    for(int i = 1; i <= n; ++i){
        in >> x;
        Ap[x]++;
    }
    a[0] = 1;
    int Max = 0;
    for(int i = 200; i >= 1 ; i--)
        if(Ap[i])
            for(int j = G - i ; j >= 0 ; j--)
                if(a[j])
                    for(int k = 1; k <= Ap[i] && j + k*i <= G && !a[j + k*i]; ++k){
                        a[j + k * i] = a[j] + k;
                        b[j + k * i] = i;
                        Max = max(Max, j + k * i);
                    }
    out << Max << " " << a[Max] - 1 << "\n";
    for(int i = Max ; i > 0 ; i -= b[i])
        out << b[i] << "\n";
    return 0;
}