Cod sursa(job #683296)

Utilizator ion_calimanUAIC Ion Caliman ion_caliman Data 20 februarie 2012 13:43:48
Problema Ghiozdan Scor 20
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.1 kb
#include <fstream>
#include <string>
#include <sstream>
using namespace std;
ifstream f("ghiozdan.in");
ofstream g("ghiozdan.out");

#define NMAX 20010
#define GMAX 75010

short D[2][GMAX], B[2][GMAX];
string S[2][GMAX];
int N, G;

string i2s(int x)
{
    stringstream ss;
    string s;
    ss << x;
    ss >> s;
    return s;
}

int main()
{
    int i, j, x, l=0;
    f >> N >> G;
    for (i=1; i<=N; i++, l=1-l){
        f >> x;
        for (j=0; j<=G; j++){
            if (x<=j){
                if (D[l][j-x]+x>D[l][j] || (D[l][j-x]+x==D[l][j] && B[l][j-x]+1<B[l][j])){
                    D[1-l][j]=D[l][j-x]+x;
                    B[1-l][j]=B[l][j-x]+1;
                    S[1-l][j]=S[l][j-x]+"\n"+i2s(x);
                } else {
                    D[1-l][j]=D[l][j];
                    B[1-l][j]=B[l][j];
                    S[1-l][j]=S[l][j];
                }
            } else {
                D[1-l][j]=D[l][j];
                B[1-l][j]=B[l][j];
                S[1-l][j]=S[l][j];
            }
        }
    }
    g << D[l][G] << ' ' << B[l][G] << S[l][G];
}