fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4. #include <algorithm>
  5. #include <cmath>
  6. #include <stdio.h>
  7. #include <string.h>
  8. #include <iomanip>
  9. #include <map>
  10.  
  11. using namespace std;
  12. #define all(v) (v.begin()), (v.end())
  13.  
  14. void fast_io()
  15. {
  16. ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
  17. }
  18.  
  19.  
  20. bool expression(int n, int arr[], long long fsum, long long sum = 0, int i = 0){
  21. if (i == n){
  22. return sum == fsum;
  23. }
  24. return expression(n, arr, fsum, sum+arr[i], i+1) || expression(n, arr, fsum, sum-arr[i], i+1);
  25. }
  26.  
  27. int main()
  28. {
  29. fast_io();
  30. int n = 0, sum = 0;
  31. cin >> n >> sum;
  32. int arr[n] = {0};
  33. for (int i = 0; i < n; i++){
  34. cin >> arr[i];
  35. }
  36. cout << (expression(n, arr, sum) == 1? "YES" : "NO") << endl;
  37. }
Success #stdin #stdout 0.01s 5284KB
stdin
5 5
1 2 3 4 5
stdout
YES