Cod sursa(job #1094709)

Utilizator Corina1997Todoran Ana-Corina Corina1997 Data 29 ianuarie 2014 19:06:16
Problema Ghiozdan Scor 64
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.79 kb
#include <fstream>
#define INF 0x3f3f3f3f
using namespace std;

ifstream is("ghiozdan.in");
ofstream os("ghiozdan.out");
void Scrie(int x);

int n, S, x, a[75001], p[75001], nr;

int main()
{
    is >> n >> S;
    for ( int i = 1; i <= S; ++i )
        a[i] = INF;
    for ( int i = 0; i < n; ++i )
    {
        is >> x;
        for ( int j = S - x; j >= 0; --j )
        {
            if ( a[j + x] > a[j] + 1 )
            {
                a[j+x] = a[j] + 1;
                p[j+x] = j;
            }
        }
    }
    nr = S;
    while ( a[nr] == INF )
        nr--;
    os << nr << ' ' << a[nr] << '\n';
    Scrie(nr);
    is.close();
    os.close();
    return 0;
}
void Scrie(int x)
{
    if ( x == 0 ) return;
    Scrie(p[x]);
    os << x - p[x] << '\n';
}