GastonFontenla

UVa: 624 - CD

May 27th, 2016
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.88 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. #define GB(m, x) ((m) &  (1<<(x)))
  4. #define SB(m, x) ((m) | (1<<(x)))
  5. #define CB(m, x) ((m) &= ~(1<<(x)))
  6.  
  7. using namespace std;
  8.  
  9. int bitSol;
  10. int n;
  11. int a[21];
  12. int maxDur;
  13. int minDif;
  14.  
  15. void solve(int bitMask, int i, int p)
  16. {
  17.     if(p > maxDur)
  18.         return;
  19.  
  20.     if(p <= maxDur && p > minDif)
  21.         minDif = p, bitSol = bitMask;
  22.  
  23.     if(i < n)
  24.     {
  25.         solve(SB(bitMask, i), i+1, p+a[i]);
  26.         solve(bitMask, i+1, p);
  27.     }
  28. }
  29.  
  30.  
  31. int main()
  32. {
  33.     while(cin >> maxDur)
  34.     {
  35.         cin >> n;
  36.         for(int i=0; i<n; i++)
  37.             cin >> a[i];
  38.         minDif = 0;
  39.         bitSol = 0;
  40.         solve(0, 0, 0);
  41.  
  42.         for(int i=0; i<n; i++)
  43.         {
  44.             if(GB(bitSol, i))
  45.             {
  46.                 cout << a[i] << " ";
  47.             }
  48.         }
  49.         cout << "sum:" << minDif << endl;
  50.     }
  51.     return 0;
  52. }
Advertisement
Add Comment
Please, Sign In to add comment