Cod sursa(job #937936)

Utilizator mvcl3Marian Iacob mvcl3 Data 11 aprilie 2013 13:10:03
Problema Ghiozdan Scor 42
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.98 kb
#include <fstream>
#define NMAX 20009
#define GMAX 75009
using namespace std;

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

int N, S, G[GMAX];
struct obj {
    int c;
    int nr;
}   DP[GMAX * 200];

inline void read_Data() {

    f >> N >> S;
    for(int i = 1; i <= N; ++i) f >> G[i];
}

inline void solve() {

    for(int i = 1; i <= N; ++i)
        for(int j = S; j >= 0; --j)
            if(DP[j].c + G[i] > DP[j + G[i]].c)
                DP[j + G[i]].c = DP[j].c + G[i],
                DP[j + G[i]].nr = DP[j].nr + 1;
            else
            if(DP[j].c + G[i] == DP[j + G[i]].c)
                if(DP[j].nr < DP[j + G[i]].nr)
                    DP[j + G[i]].nr = DP[j].nr + 1;
}

int main() {

    read_Data();
    solve();

    int sol = 0, nr = 0;
    for(int i = 1; i <= S; ++i)
        if(DP[i].c > sol)
            sol = DP[i].c,
            nr =  DP[i].nr;

    g << sol << ' ' << nr << '\n';

    g.close();
    return 0;
}