Cod sursa(job #1073504)

Utilizator crisbodnarCristian Bodnar crisbodnar Data 6 ianuarie 2014 14:40:08
Problema Ghiozdan Scor 18
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.08 kb
#include <iostream>
#include <fstream>
#include <algorithm>
#include <vector>
#include <memory.h>

using namespace std;

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

const int Nmax = 20110;
const int Gmax = 75100;
const int oo = 0x3f3f3f3f;

short N, G, V[Nmax], DP[Gmax], T[Gmax], Uz[Gmax];

void Afisare(int g)
{
    if(g == -1) return;
    Afisare(T[g]);
    fout<<V[Uz[g]]<<'\n';
}

int main()
{
    fin>>N>>G;
    for(int i=1; i<=N; i++)
        fin>>V[i];
    memset(DP, oo, sizeof DP);
    memset(T, -1, sizeof T);
    DP[0] = 0;

    for(int i=1; i<=N; i++)
        for(int j=G - V[i]; j >= 0; j--)
            if(DP[j + V[i]] > DP[j] + 1)
            {
                DP[j + V[i]] = DP[j] + 1;
                T[j + V[i]] = j;
                Uz[j + V[i]] = i;
            }

    int gsol, sol;
    for(int i=G; i >=0 ; i--)
        if(DP[i] != oo)
            {
                gsol = i;
                sol = DP[i];
                break;
            }
    fout<<gsol<<' '<<sol<<'\n';
    //Afisare(gsol);
    return 0;
}