Cod sursa(job #2509704)

Utilizator IoanaDraganescuIoana Draganescu IoanaDraganescu Data 14 decembrie 2019 16:16:02
Problema Economie Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.84 kb
#include <iostream>
#include <fstream>
#include <algorithm>
#include <queue>

using namespace std;

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

const int Nmax = 1000, Vmax = 50000;

queue <int> q;
int n, nr, v[Nmax + 5];
bool b[Vmax + 5];

void Read(){
    fin >> n;
    for (int i = 1; i <= n; i++)
        fin >> v[i];
}

void Solve(){
    sort(v + 1, v + n + 1);
    b[0] = 1;
    for (int i = 1; i <= n; i++)
        if (!b[v[i]]){
            nr++;
            q.push(v[i]);
            for (int j = 0; j + v[i] <= Vmax; j++)
                if (b[j])
                    b[j + v[i]] = 1;
        }
}

void Print(){
    fout << nr << '\n';
    while (!q.empty()){
        fout << q.front() << '\n';;
        q.pop();
    }
}

int main(){
    Read();
    Solve();
    Print();
    return 0;
}