Ahmed_Negm

Untitled

Mar 17th, 2022
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.86 KB | None | 0 0
  1. #include<iostream>
  2. #include<cmath>
  3. #include<iomanip>
  4. #include<algorithm>
  5. #include<cstdlib>
  6. #include<cstring>
  7. #include<vector>
  8.  
  9. #define ll long long
  10. #define sz(x) int(x.size())
  11. #define all(x) x.begin(),x.end()
  12. using namespace std;
  13.  
  14. void Fast_IO(){
  15.     ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
  16.     #ifndef ONLINE_JUDGE
  17.         freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout);
  18.     #endif
  19. }
  20.  
  21.  ll n,target;
  22.  ll arr[25];
  23.  
  24.  bool is_valid(ll sum , ll idx = 0){
  25.      if(idx == n) return sum == target;
  26.     return is_valid(sum+arr[idx],idx+1) || is_valid(sum - arr[idx],idx+1);
  27.  
  28.  }
  29.  
  30.  
  31. void solve(){
  32.   cin>>n>>target;
  33.   for(int i =0; i<n; i++) cin>>arr[i];
  34.     cout<<(is_valid(0,0)? "YES\n":"NO\n");
  35.  
  36.  
  37. }
  38.  
  39. int main(){
  40.     Fast_IO();
  41. int t =1;
  42. //cin>>t;
  43. while(t--){
  44. solve();
  45. }
  46. return 0;
  47. }  
Advertisement
Add Comment
Please, Sign In to add comment