Cod sursa(job #2241720)

Utilizator mihailescu_eduardMihailescu Eduard-Florin mihailescu_eduard Data 16 septembrie 2018 20:15:03
Problema Ghiozdan Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.41 kb
#include <cstdio>

FILE *fin = freopen("ghiozdan.in","r",stdin); FILE *fout = freopen("ghiozdan.out","w",stdout);

static const int kMaxG = 75000 + 1;
static const int kMaxOb = 200 + 1;

/* ------- DATA --------*/
int n, g;
int count[kMaxOb];

int dp[kMaxG], substract[kMaxG];
/* ------- DATA --------*/

void ReadInput()
{
    scanf("%d%d",&n,&g);
    for(int i= 1; i<= n; ++i)
    {
        int x; scanf("%d",&x);
        count[x]++;
    }
}

void SolveDp()
{
    dp[0] = 1;

    for(int i = kMaxOb - 1; i > 0; i--)
    {
        if(count[i] > 0)
        {
            for(int j = g - i; j>= 0; j--)
            {
                if(dp[j] != 0)
                {
                    for(int x = 1; x <= count[i] && j + x * i <= g && dp[j + x * i] == 0; x++)
                    {
                        dp[j + x * i] = dp[j] + x;
                        substract[j + x * i] = i;
                    }
                }
            }
        }
    }
}

void PrintOutput()
{
    for(int i = g ; i >= 0; i--)
    {
        if(dp[i] !=0)
        {
            printf("%d %d\n",i, dp[i] - 1);
            int x =i;
            while(x > 0)
            {
                printf("%d\n", substract[x]);
                x -= substract[x];
            }
            return ;
        }
    }
}


int main()
{
    ReadInput();
    SolveDp();
    PrintOutput();

    return 0;
}