Cod sursa(job #2098249)

Utilizator Andrei_CotorAndrei Cotor Andrei_Cotor Data 2 ianuarie 2018 16:40:09
Problema Ghiozdan Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.11 kb
#include<fstream>
#include<queue>
using namespace std;
ifstream fi("ghiozdan.in");
ofstream fo("ghiozdan.out");
int n,g,i,x,Ap[201],G[75001],poz,j,k;
deque<int> S;
int main()
{
    fi>>n>>g;
    for(i=1; i<=n; i++)
    {
        fi>>x;
        Ap[x]++;
    }
    G[0]=1;
    for(i=200; i>=1; i--)
    {
        if(Ap[i])
        {
            for(j=g; j>=0; j--)
            {
                if(G[j])
                {
                    for(k=1; k<=Ap[i] && j+k*i<=g; k++)
                    {
                        poz=j+k*i;
                        //daca avem deja o valoare mai mare pe pozitia poz
                        if(G[poz])
                            break;
                        G[poz]=i;
                    }
                }
            }
        }
    }
    poz=g;
    while(!G[poz])
        poz--;
    fo<<poz<<" ";
    while(poz!=0)
    {
        S.push_back(G[poz]);
        poz-=G[poz];
    }
    fo<<S.size()<<"\n";
    while(!S.empty())
    {
        fo<<S.front()<<"\n";
        S.pop_front();
    }
    fi.close();
    fo.close();
    return 0;
}