Cod sursa(job #2990392)

Utilizator raresgherasaRares Gherasa raresgherasa Data 7 martie 2023 20:55:56
Problema Economie Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.65 kb
#include <bits/stdc++.h>

using namespace std;

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

const int N_MAX = 5e4 + 5;

int n;
int a[N_MAX], dp[N_MAX];
vector<int>used, v;

int main(){
   ios_base::sync_with_stdio(false);

   fin >> n;
   for (int i = 1; i <= n; i++){
      fin >> a[i];
   }
   sort(a + 1, a + n + 1);
   dp[0] = true;
   for (int i = 1; i <= n; i++){
      if (!dp[a[i]]){
         used.push_back(a[i]);
         for (int j = 0; j < N_MAX; j++){
            dp[j] |= dp[j - a[i]];
         }
      }
   }
   fout << (int)used.size() << '\n';
   for (int x : used){
      fout << x << '\n';
   }
}