GastonFontenla

UVa: 10664 - Luggage

May 27th, 2016
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.73 KB | None | 0 0
  1. #include <iostream>
  2. #include <sstream>
  3. #include <stdlib.h>
  4.  
  5. using namespace std;
  6.  
  7. int sum, n, tc, minDif, a[25];
  8.  
  9. void solve(int i, int p)
  10. {
  11.     minDif = min(minDif, abs(sum-p-p));
  12.     if(i < n)
  13.     {
  14.         solve(i+1, p+a[i]);
  15.         solve(i+1, p);
  16.     }
  17. }
  18.  
  19. int main()
  20. {
  21.     cin >> tc;
  22.     string s;
  23.     getline(cin, s);
  24.     while(tc--)
  25.     {
  26.         sum = 0, n = 0, minDif = 999999999;
  27.  
  28.         getline(cin, s);
  29.         stringstream ss;
  30.         ss << s;
  31.  
  32.         while(ss >> a[n])
  33.         {
  34.             sum += a[n];
  35.             n++;
  36.         }
  37.         solve(0, 0);
  38.  
  39.         if(minDif == 0)
  40.             cout << "YES" << endl;
  41.         else
  42.             cout << "NO" << endl;
  43.     }
  44.  
  45.     return 0;
  46. }
Advertisement
Add Comment
Please, Sign In to add comment