Cod sursa(job #2060671)

Utilizator Alex18maiAlex Enache Alex18mai Data 8 noiembrie 2017 16:37:49
Problema Ghiozdan Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.24 kb
#include <fstream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <queue>
#include <map>

using namespace std;

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

int fv[205];
int G[100100];
int last[100100];

int main() {

    ios::sync_with_stdio(false);
    cin.tie(NULL);

    int n , g;
    cin>>n>>g;

    for (int i=1; i<=n; i++){
        int a;
        cin>>a;
        fv[a] ++;
    }

    const int inf = 2e9;

    for (int i=1; i<=g; i++){
        G[i] = inf;
    }

    for (int i=200; i>=1; i--){
        if (fv[i] == 0){
            continue;
        }
        for (int j=g; j>=0; j--){
            if (G[j] == inf){
                continue;
            }
            for (int k=1; k<=fv[i]; k++){
                int go = j + k * i;
                if (go > g || G[go] != inf){
                    break;
                }
                G[go] = G[j] + k;
                last[go] = i;
            }
        }
    }

    for (int i=g; i>=0; i--){
        if (G[i] != inf){
            cout<<i<<" "<<G[i]<<'\n';
            int point = i;
            while (last[point] != 0){
                cout<<last[point]<<'\n';
                point = point - last[point];
            }
            break;
        }
    }

    return 0;
}