Cod sursa(job #2514358)

Utilizator ApostolIlieDanielApostol Daniel ApostolIlieDaniel Data 25 decembrie 2019 15:38:45
Problema Ghiozdan Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1 kb
#include <bits/stdc++.h>

using namespace std;

const int MX = 200, G = 75000;

int dp[1 + G], f[1 + MX], lst[1 + G];

int main () {
    freopen ("ghiozdan.in", "r", stdin);
    freopen ("ghiozdan.out", "w", stdout);
    ios::sync_with_stdio (false);
    cin.tie (0); cout.tie (0);

    int n, g;
    cin >> n >> g;
    for (int i = 1; i <= n; i++) {
        int x;
        cin >> x;
        f[x]++;
    }

    dp[0] = 1;
    for (int i = MX; i > 0; i--) {
        for (int j = g; j >= 0; j--) {
            if (!dp[j]) continue;
            for (int d = 1; d * i + j <= g && d <= f[i]; d++) {
                if (dp[j + d * i])
                    break;
                dp[j + d * i] = dp[j] + d;
                lst[j + d * i] = i;
            }
        }
    }

    int best = g;
    while (!dp[best])
        best--;
    cout << best << " " << dp[best] - 1 << "\n";
    while (best) {
        cout << lst[best] << "\n";
        best -= lst[best];
    }
    return 0;
}