Cod sursa(job #2060484)

Utilizator Alex18maiAlex Enache Alex18mai Data 8 noiembrie 2017 12:11:48
Problema Ghiozdan Scor 74
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.8 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");

struct go_back{
    int last , val , nr_val , last_val , nr_last_val;
};

int fv[205];
int G[100100];
go_back point[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;
                    point[j + k * i].last = j;
                    point[j + k * i].val = i;
                    point[j + k * i].nr_val = k;
                    point[j + k * i].last_val = point[j].val;
                    point[j + k * i].nr_last_val = point[j].nr_val;

                }
            }
        }
    }

    for (int i=g; i>=0; i--){
        if (G[i] != inf){
            cout<<i<<" "<<G[i]<<'\n';
            int last = i;
            for (int j=1; j<=point[i].nr_val; j++){
                cout<<point[i].val<<'\n';
            }
            while (point[last].last != 0){
                //cout<<"last "<<last<<'\n';
                for (int j=1; j<=point[last].nr_last_val; j++){
                    cout<<point[last].last_val<<'\n';
                }
                last = point[last].last;
            }
            break;
        }
    }

    return 0;
}