pasholnahuy

МОНЕТЫЫЫЫЫЫЫЫЫ

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