Advertisement
El_GEMMY

creating expressions

Mar 26th, 2022
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.51 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #include <ext/pb_ds/assoc_container.hpp>
  3.  
  4. using namespace std;
  5. using namespace __gnu_pbds;
  6.  
  7. typedef long long ll;
  8. typedef unsigned long long ull;
  9. typedef tree<int,null_type,less<>,rb_tree_tag,tree_order_statistics_node_update> indexed_set;
  10.  
  11. #define all(v) v.begin(),v.end()
  12. #define rall(v) v.rbegin(),v.rend()
  13. #define MOD 1000000007
  14. #define PI 3.14159265
  15. #define ceil(a, b) ((a / b) + (a % b ? 1 : 0))
  16. #define imin INT_MIN
  17. #define imax INT_MAX
  18. #define nl '\n'
  19.  
  20. void Start_Crushing() {
  21.     ios::sync_with_stdio(false);
  22.     cin.tie(nullptr);
  23.     cout.tie(nullptr);
  24. #ifndef ONLINE_JUDGE
  25.     freopen("input.txt", "r", stdin);
  26.     freopen("output.txt", "w", stdout);
  27. #endif
  28. }
  29. //vector<int> dx = {0, 0, 1, -1, 1, 1, -1, -1}, dy = {1, -1, 0, 0, 1, -1, 1, -1};
  30. //vector<int> dx = {0, 0, 1, -1}, dy = {1, -1, 0, 0};
  31.  
  32. bool canReach(vector<int>& v, int x, int sum, int idx = 1){
  33.     if(idx >= v.size()) return sum == x;
  34.  
  35.  
  36.     bool first = canReach(v, x, sum + v[idx], idx + 1),
  37.     second = canReach(v, x, sum - v[idx], idx + 1);
  38.  
  39.     return first or second;
  40. }
  41.  
  42. void solve(){
  43.     int n, x; cin >> n >> x;
  44.     vector<int> v(n);
  45.     for(auto& i : v) cin >> i;
  46.  
  47.     bool can = canReach(v, x, v[0]);
  48.     cout << (can ? "YES" : "NO");
  49. }
  50.  
  51. int main(){
  52.     //        freopen("equal.in", "r", stdin);
  53.     Start_Crushing();
  54.     int t = 1;
  55. //    /*is Single Test case?*/ cin >> t;
  56.     while (t--) {
  57.         solve();
  58.         if(!t) break;
  59.         cout << "\n";
  60.     }
  61.  
  62.     return 0;
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement