Cod sursa(job #1613712)

Utilizator theodor1289Theodor Amariucai theodor1289 Data 25 februarie 2016 16:29:21
Problema Ghiozdan Scor 30
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.15 kb
#include <fstream>
#include <algorithm>
using namespace std;
ifstream fin("ghiozdan.in");
ofstream fout("ghiozdan.out");
short n;
int gt;
int gr[20001];
struct {
short nrpoz;
short deunde;
} ghiozdan[75001];
int rez[20001];

int main()
{
    fin>>n>>gt;
    for(int i=1;i<=n;i++)
    fin>>gr[i];

    for(int i=1;i<=gt;i++)
        ghiozdan[i].nrpoz=30000;

    sort(gr+1, gr+1+n);

    for(int k=n; k>=1; k--)
    {
        for(int i=gt; i>= gr[k]; i--)
        {
            if(ghiozdan[i-gr[k]].nrpoz!=30000)
            if(ghiozdan[i-gr[k]].nrpoz+1<ghiozdan[i].nrpoz)
            {
                ghiozdan[i].nrpoz=ghiozdan[i-gr[k]].nrpoz+1;
                ghiozdan[i].deunde=i-gr[k];
                break;
            }
        }
    }

    int i;
    for(i=gt;i>=0;i--)
        if(ghiozdan[i].nrpoz!=30000)
        break;

    int aux=i, s=0, ctr=0;
    while(aux>0)
    {
        s+=aux-ghiozdan[aux].deunde;
        rez[++ctr]=aux-ghiozdan[aux].deunde;
        aux=ghiozdan[aux].deunde;
    }
    s+=aux;
    fout<<s<<' '<<ctr<<'\n';

    for(int i=1;i<=ctr;i++)
        fout<<rez[i]<<'\n';

    return 0;
}