Cod sursa(job #1087487)

Utilizator Corina1997Todoran Ana-Corina Corina1997 Data 19 ianuarie 2014 14:51:33
Problema Ghiozdan Scor 12
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.9 kb
#include <fstream>
#include <vector>
using namespace std;

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

int n, S;
const int INF = 0x3f3f3f3f;
vector<int> a, g, t;

void Path( int i );

int main()
{
    is >> n >> S;
    a = vector<int>(n+1);
    g = vector<int>(S+1, INF);
    t = vector<int>(S+1);
    for ( int i = 1; i <= n; i++ )
        is >> a[i];
    g[0] = 0;
    for ( int i = 1; i <= n; i++ )
        for ( int j = S; j >= 0; j-- )
            if ( g[j] != INF && (g[j + a[i]] > g[j] + 1) )
                g[j + a[i]] = g[j] + 1;
    for ( int i = S; i >= 0; i-- )
        if ( g[i] != INF )
        {
            os << i << ' ' << g[i] << '\n';
            //Path( i );
            break;
        }
    is.close();
    os.close();
    return 0;
}

void Path( int i )
{
    if ( i == 0 )
        return;
    Path( t[i] );
    os << i - t[i] << '\n';
}