Cod sursa(job #1790822)

Utilizator CiurezAndreiCiurez Marius-Andrei CiurezAndrei Data 28 octombrie 2016 18:58:16
Problema Ghiozdan Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.33 kb
#include <bits/stdc++.h>

#define INF 200000000

using namespace std;

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

int D[135010], N, G, fr[250], x, T[135010];
int solutionG, nr;

inline void way(int x)
{
    if(T[x] == 0)
    {
        return;
    }
    fout << T[x] << '\n';
    if(T[x] > 0)
    {
        way(x - T[x]);
    }
}

int main()
{
    fin >> N >> G;

    for(int i = 1; i <= G; i ++)
    {
        D[i] = INF;
    }

    for(int i = 1; i <= N; i ++)
    {
        fin >> x;
        fr[x] ++;
    }

    for(int i = 200; i >= 1; i --)
    {
        if(fr[i] == 0)
        {
            continue;
        }

        for(int j = G; j >= 0; j --)
        {
            if(D[j] != INF)
            {
                for(int k = 1; k <= fr[i] && j + i * k <= G; k ++)
                {
                    if(D[j + i * k] == INF)
                    {
                        D[j + i * k] = D[j] + k;
                        T[j + i * k] = i;
                        solutionG = max(j + i * k, solutionG);
                    }
                    else
                    {
                        break;
                    }
                }
            }
        }
    }

    fout << solutionG << " " << D[solutionG] << '\n';
    way(solutionG);

    return 0;
}