Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <algorithm>
- using namespace std;
- void CheckSumm(vector<int>& ans, vector<int> &val, int needed_summ, int cur_summ, vector<int>& taken, int i) {
- if (i == val.size()){
- return;
- }
- CheckSumm(ans, val, needed_summ, cur_summ, taken, i + 1);
- if (cur_summ + val[i] < needed_summ) {
- taken.push_back(val[i]);
- CheckSumm(ans, val, needed_summ, cur_summ + val[i], taken, i + 1);
- taken.pop_back();
- } else if (cur_summ + val[i] == needed_summ) {
- if (ans.size() > taken.size() + 1){
- taken.push_back(val[i]);
- ans = vector(taken);
- }
- return;
- } else {
- return;
- }
- if (cur_summ + 2 * val[i] < needed_summ) {
- taken.push_back(val[i]);
- taken.push_back(val[i]);
- CheckSumm(ans, val, needed_summ, cur_summ + 2 * val[i], taken, i + 1);
- taken.pop_back();
- taken.pop_back();
- } else if (cur_summ + 2 * val[i] == needed_summ) {
- if (ans.size() > taken.size() + 2){
- taken.push_back(val[i]);
- taken.push_back(val[i]);
- ans = vector(taken);
- }
- return;
- } else {
- return;
- }
- }
- int main() {
- int n, m;
- cin >> n >> m;
- vector<int> val;
- int total_summ = 0;
- for (int i = 0; i < m; ++i) {
- int x;
- cin >> x;
- total_summ += x;
- val.emplace_back(x);
- }
- std::sort(val.begin(), val.end());
- if (total_summ * 2 < n) {
- cout << -1;
- return 0;
- }
- vector<int> ans(1000, 0);
- vector<int> taken;
- CheckSumm(ans, val, n, 0, taken, 0);
- if (ans.size() == 1000){
- cout << 0;
- } else {
- cout << ans.size() << '\n';
- for (auto el: ans){
- cout << el << " ";
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement