Cod sursa(job #2525240)

Utilizator caesar2001Stoica Alexandru caesar2001 Data 16 ianuarie 2020 22:04:44
Problema Ghiozdan Scor 54
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.01 kb
#include <bits/stdc++.h>

using namespace std;

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

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

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

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

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

        i -= (dp[i].fr * dp[i].el);
    }*/

    return 0;
}