matheus_monteiro

Troco - 70 pontos - bitmask

Aug 20th, 2021 (edited)
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.55 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. const int MAX = 22;
  4.  
  5. int v, n;
  6. int coin[MAX];
  7. vector<int> sums;
  8.  
  9. int32_t main() {
  10.  
  11.     ios_base::sync_with_stdio(false);
  12.     cin.tie(nullptr);
  13.  
  14.     cin >> v >> n;
  15.  
  16.     for(int i = 0; i < n; i++)
  17.         cin >> coin[i];
  18.  
  19.     for(int i = 0; i < (1 << n); i++) {
  20.         int s = 0;
  21.         for(int j = 0; j < n; j++)
  22.             if(i & (1 << j))
  23.                 s += coin[j];
  24.         sums.push_back(s);
  25.     }
  26.  
  27.     sort(sums.begin(), sums.end());
  28.  
  29.     if(binary_search(sums.begin(), sums.end(), v))
  30.         cout << "S\n";
  31.     else
  32.         cout << "N\n";
  33.  
  34.     return 0;
  35. }
Add Comment
Please, Sign In to add comment