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(int &ans, vector<int> &val, int needed_summ, int cur_summ, int count, int i) {
- if (i == val.size()){
- return;
- }
- CheckSumm(ans, val, needed_summ, cur_summ, count, i + 1);
- if (cur_summ + val[i] < needed_summ) {
- CheckSumm(ans, val, needed_summ, cur_summ + val[i], count + 1, i + 1);
- } else if (cur_summ + val[i] == needed_summ) {
- ans = min(ans, count + 1);
- return;
- } else {
- return;
- }
- if (cur_summ + 2 * val[i] < needed_summ) {
- CheckSumm(ans, val, needed_summ, cur_summ + 2 * val[i], count + 2, i + 1);
- } else if (cur_summ + 2 * val[i] == needed_summ) {
- ans = min(ans, count + 2);
- return;
- } else {
- return;
- }
- }
- int main() {
- int n, m;
- cin >> n >> m;
- vector<int> val;
- std::sort(val.begin(), val.end());
- std::reverse(val.begin(), val.end());
- int total_summ = 0;
- for (int i = 0; i < m; ++i) {
- int x;
- cin >> x;
- total_summ += x;
- val.emplace_back(x);
- }
- if (total_summ * 2 < n) {
- cout << -1;
- return 0;
- }
- int ans = 100;
- CheckSumm(ans, val, n, 0, 0, 0);
- if (ans == 100) {
- cout << 0;
- } else {
- cout << ans;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement