Cod sursa(job #2193442)

Utilizator Constantin.Dragancea Constantin Constantin. Data 10 aprilie 2018 10:14:05
Problema Ghiozdan Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.71 kb
#include <bits/stdc++.h>
using namespace std;

int n, g, a[210], dp[75005] = {1}, ap[75005];

int main(){
    ifstream cin ("ghiozdan.in");
    ofstream cout ("ghiozdan.out");
    cin >> n >> g;
    for (int i=1, x; i<=n; i++) cin >> x, a[x]++;
    for (int i=200; i; i--){
        if (!a[i]) continue;
        for (int j=g; j>=0; j--){
            if (dp[j]){
                for (int d=1; d<=a[i] && j+d*i <= g && !dp[j + d*i]; d++)
                    dp[j+d*i] = dp[j] + d, ap[j+d*i] = i;
            }
        }
    }
    int j = g;
    while (j && !dp[j]) j--;
    cout << j << " " << dp[j]-1 << "\n";
    while (j>0){
        cout << ap[j] << "\n";
        j -= ap[j];
    }
    return 0;
}