Cod sursa(job #1597184)

Utilizator mihai.constantinConstantin Mihai mihai.constantin Data 11 februarie 2016 19:28:25
Problema Ghiozdan Scor 42
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.93 kb
#include <iostream>
#include <fstream>
using namespace std;

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

const int dmax = 20002;
const int G_max = 75002;

int g[dmax];

int d[G_max];
int nr[G_max];

int ans_1;

int N, G;

int main()
{
    int i, j;

    in >> N >> G;

    for(i = 1; i <= N; i++) in >> g[i];

    for(i = 1; i <= G; i++) d[i] = -1;
    for(i = 1; i <= G; i++) nr[i] = dmax;

    d[0] = 0;

    for(i = 1; i <= N; i++)
        for(j = G; j >= g[i]; j--)
            if(d[j - g[i]] != -1)
            {
                if( d[j] < d[j - g[i]] + g[i] )
                    d[j] = d[j - g[i]] + g[i];

                nr[j] = min(nr[j], nr[j - g[i]] + 1);
            }

    /*
    for(i = 0; i <= G; i++) cout << d[i] << " ";
    cout << '\n';
    */

    for(i = G; i >= 0; i--)
        if(d[i] != -1)
        {
            ans_1 = d[i];
            break;
        }

    out << ans_1 << " " << nr[ans_1];

    return 0;
}