Cod sursa(job #1477762)

Utilizator eu3neuomManghiuc Teodor-Florin eu3neuom Data 26 august 2015 21:25:13
Problema Ghiozdan Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.06 kb
#include <iostream>
#include <fstream>
#include <bitset>
#include <vector>
#include <algorithm>

using namespace std;

ifstream fin("ghiozdan.in");
ofstream fout("ghiozdan.out");

const int NMax = 205;
const int GMax = 75005;
const int INF = 1e9;

int v[NMax], go[GMax], D[GMax];

int main(){
    int n, g, x;
    fin >> n >> g;
    for(int i = 1; i <= n; i++){
        fin >> x;
        v[x]++;
    }
    D[0] = 1;
    for(int i = NMax - 1; i > 0; i--){
        if(v[i]){
            for(int j = g - i; j >= 0; j--){
                if(D[j]){
                    for(int k = 1; k <= v[i] && j + i * k <= g && !D[j + i * k]; k++){
                        D[j + i * k] = D[j] + k;
                        go[j + i * k] = i;
                    }
                }
            }
        }
    }
    for(int i = g; i >= 0; i--){
        if(D[i]){
            fout << i << " " << D[i] - 1 << "\n";
            while(go[i]){
                fout << go[i] << "\n";
                i -= go[i];
            }
        }
    }
    return 0;
}