Advertisement
pasholnahuy

Монеты с восстановлением

Jun 15th, 2023
804
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.86 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4.  
  5. using namespace std;
  6.  
  7. void CheckSumm(vector<int>& ans, vector<int> &val, int needed_summ, int cur_summ, vector<int>& taken, int i) {
  8.     if (i == val.size()){
  9.         return;
  10.     }
  11.     CheckSumm(ans, val, needed_summ, cur_summ, taken, i + 1);
  12.     if (cur_summ + val[i] < needed_summ) {
  13.         taken.push_back(val[i]);
  14.         CheckSumm(ans, val, needed_summ, cur_summ + val[i], taken, i + 1);
  15.         taken.pop_back();
  16.     } else if (cur_summ + val[i] == needed_summ) {
  17.         if (ans.size() > taken.size() + 1){
  18.             taken.push_back(val[i]);
  19.             ans = vector(taken);
  20.         }
  21.         return;
  22.     } else {
  23.         return;
  24.     }
  25.     if (cur_summ + 2 * val[i] < needed_summ) {
  26.         taken.push_back(val[i]);
  27.         taken.push_back(val[i]);
  28.         CheckSumm(ans, val, needed_summ, cur_summ + 2 * val[i], taken, i + 1);
  29.         taken.pop_back();
  30.         taken.pop_back();
  31.     } else if (cur_summ + 2 * val[i] == needed_summ) {
  32.         if (ans.size() > taken.size() + 2){
  33.             taken.push_back(val[i]);
  34.             taken.push_back(val[i]);
  35.             ans = vector(taken);
  36.         }
  37.         return;
  38.     } else {
  39.         return;
  40.     }
  41. }
  42.  
  43. int main() {
  44.     int n, m;
  45.     cin >> n >> m;
  46.     vector<int> val;
  47.     int total_summ = 0;
  48.     for (int i = 0; i < m; ++i) {
  49.         int x;
  50.         cin >> x;
  51.         total_summ += x;
  52.         val.emplace_back(x);
  53.     }
  54.     std::sort(val.begin(), val.end());
  55.     if (total_summ * 2 < n) {
  56.         cout << -1;
  57.         return 0;
  58.     }
  59.     vector<int> ans(1000, 0);
  60.     vector<int> taken;
  61.     CheckSumm(ans, val, n, 0, taken, 0);
  62.     if (ans.size() == 1000){
  63.         cout << 0;
  64.     } else {
  65.         cout << ans.size() << '\n';
  66.         for (auto el: ans){
  67.             cout << el << " ";
  68.         }
  69.     }
  70.  
  71.     return 0;
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement