Cod sursa(job #2525176)

Utilizator caesar2001Stoica Alexandru caesar2001 Data 16 ianuarie 2020 20:59:40
Problema Ghiozdan Scor 20
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.28 kb
#include <bits/stdc++.h>

using namespace std;

const int VALMAX = 200;
const int INF = 1e7;

struct Data {
    int val, el, fr, from;
};

int main() {

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

    int g, n;
    in >> n >> g;
    vector<int> v(1 + VALMAX, 0);
    for(int i = 1; i <= n; i ++) {
        int x;
        in >> x;
        v[x] ++;
    }

    vector<vector<Data>> dp(1 + VALMAX, vector<Data> (g + 1, {INF, 0, 0, 0}));
    dp[0][0].val = 0;
    for(int j = 1; j <= VALMAX; j ++) {
        for(int i = 0; i <= g; i ++)
            dp[j][i] = dp[j - 1][i];
        for(int i = 1; i <= g; i ++)
            for(int cnt = 0; cnt * j <= i && cnt <= v[j]; cnt ++)
                if(dp[j][i].val > dp[j - 1][i - cnt * j].val + cnt)
                    dp[j][i] = {dp[j - 1][i - cnt * j].val + cnt, j, cnt, dp[j - 1][i - cnt * j].el};
    }



    int i = g;
    while(i >= 0 && dp[VALMAX][i].val == INF)
        i --;

    out << i << " " << dp[VALMAX][i].val << "\n";
    int j = VALMAX;
    while(i != 0) {
        for(int it = 1; it <= dp[j][i].fr; it ++)
            out << dp[j][i].el << "\n";

        int newi = i - (dp[j][i].fr * dp[j][i].el);
        j = dp[j][i].from;
        i = newi;
    }

    return 0;
}