Cod sursa(job #679759)

Utilizator R.A.RFMI Romila Remus Arthur R.A.R Data 13 februarie 2012 18:09:22
Problema Ghiozdan Scor 64
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.67 kb
#include <fstream>
#define nmax 75004

using namespace std;

ifstream in("ghiozdan.in");
ofstream out("ghiozdan.out");

int Ob[nmax];
int T[nmax];
int N,G,i,j,x,gmax;

int main()
{
    in>>N>>G;
    Ob[0]=1;
    for(i=1;i<nmax-2;i++)Ob[i]=nmax+1;
    for(i=1;i<=N;i++)
    {
        in>>x;
        for(j=gmax;j>=0;j--)
            if(j+x<=G&&Ob[j]!=nmax+1)
            {
                if(Ob[j+x]>Ob[j]+1)
                    Ob[j+x]=Ob[j]+1,T[j+x]=x;
            }
        gmax+=x;if(gmax>G)gmax=G;
    }
    while(Ob[G]==nmax+1)G--;
    out<<G<<' '<<Ob[G]-1<<'\n';
    while(G)
    {
        out<<T[G]<<'\n';
        G-=T[G];
    }
    return 0;
}