Cod sursa(job #2060459)

Utilizator Alex18maiAlex Enache Alex18mai Data 8 noiembrie 2017 11:58:05
Problema Ghiozdan Scor 60
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.34 kb
//#include <iostream>
#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];
vector < vector <pair<int , int>> > elem(100100);


int main() {

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

    /*freopen ("input" , "r" , stdin);
    freopen ("output" , "w" , stdout);*/

    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=1; i<=200; i++){
        for (int j=g; j>=0; j--){
            for (int k=1; k<=fv[i]; k++){
                if (j + k * i > g){
                    break;
                }
                if (G[j + k * i] > G[j] + k){
                    G[j + k * i] = G[j] + k;
                    elem[j + k * i] = elem[j];
                    elem[j + k * i].push_back({i , k});
                }
            }
        }
    }

    for (int i=g; i>=0; i--){
        if (G[i] != inf){
            cout<<i<<" "<<G[i]<<'\n';
            for (auto &x : elem[i]){
                for (int i=1; i<=x.second; i++){
                    cout<<x.first<<'\n';
                }
            }
            break;
        }
    }

    return 0;
}