Cod sursa(job #1477756)

Utilizator eu3neuomManghiuc Teodor-Florin eu3neuom Data 26 august 2015 21:06:38
Problema Ghiozdan Scor 10
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.19 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 = 20005;
const int GMax = 75005;
const int INF = 1e9;

int n, g;
int v[NMax], go[GMax];

vector < int > sol;
bitset < GMax > viz;

void solve(int x){
    for(int i = g - x; i >= 0; i--){
        if(viz[i]){
            if(viz[i + x]){
                if(go[i + x] > i && i != 0){
                    go[i + x] = i;
                }
            } else {
                viz[i + x] = 1;
                go[i + x] = i;
            }
        }
    }
}

int main(){
    int ans;
    fin >> n >> g;
    viz[0] = true;
    for(int i = 1; i <= n; i++){
        fin >> v[i];
        solve(v[i]);
    }
    for(int i = g; i > 0; i--){
        if(viz[i]){
            ans = i;
            while(i > 0){
                sol.push_back(i - go[i]);
                i = go[i];
            }
        }
    }
    fout << ans << " " << sol.size() << "\n";
    sort(sol.begin(), sol.end());
    for(int i = 0; i < sol.size(); i++){
        fout << sol[i] << "\n";
    }
    return 0;
}