Cod sursa(job #3221814)

Utilizator Robert_MitriRobert Mitri Robert_Mitri Data 8 aprilie 2024 09:58:46
Problema Ghiozdan Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.15 kb
#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;
    }


}