Cod sursa(job #2268104)

Utilizator petru.ciocirlanPetru Ciocirlan petru.ciocirlan Data 24 octombrie 2018 14:58:18
Problema Ghiozdan Scor 56
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.93 kb
#include <cstdio>
#include <cstdlib>

#define FILE_IN "ghiozdan.in"
#define FILE_OUT "ghiozdan.out"

const int INF = 0x3f3f3f3f;
const int MAXG = 75000 + 16;
int Rucsac[MAXG], Prev[MAXG];
int N, G;

int main()
{
    freopen(FILE_IN, "r", stdin);
    freopen(FILE_OUT, "w", stdout);

    scanf("%i %i", &N, &G);

    Rucsac[0] = 0;
    for(int i = 1; i <= G; ++i)
        Rucsac[i] = INF;

    while(N--)
    {
        int w;
        scanf("%i", &w);
        for(int i = G - w; i >= 0; --i)
            if(Rucsac[i + w] > Rucsac[i] + 1)
        {
            Rucsac[i + w] = Rucsac[i] + 1;
            if(!Prev[i + w])
                Prev[i + w] = w;
        }
    }

    int most = G;
    while(Rucsac[most] == INF) most--;

    printf("%i %i\n", most, Rucsac[most]);

    while(most)
    {
        printf("%i\n", Prev[most]);
        most -= Prev[most];
        // gresit :/
    }

    return 0;
}