Cod sursa(job #683315)

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

#define NMAX 20010
#define GMAX 75010

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

string i2s(int x, int y)
{
    stringstream ss;
    for (int i=0; i<x; i++)
        ss << '\n' << y;
    return ss.str();
}

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