Guest User

Untitled

a guest
Jan 20th, 2014
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.46 KB | None | 0 0
  1. #include <list>
  2. #include <map>
  3. #include <set>
  4. #include <deque>
  5. #include <stack>
  6. #include <queue>
  7. #include <algorithm>
  8. #include <sstream>
  9. #include <iostream>
  10. #include <iomanip>
  11. #include <cstdio>
  12. #include <cmath>
  13. #include <cstdlib>
  14. #include <memory.h>
  15. #include <ctime>
  16. #include <bitset>
  17.  
  18. using namespace std;
  19.  
  20. #define ABS(a) ((a>0)?a:-(a))
  21. #define MIN(a,b) ((a<b)?(a):(b))
  22. #define MAX(a,b) ((a<b)?(b):(a))
  23. #define FOR(i,a,n) for (int i=(a);i<(n);++i)
  24. #define FI(i,n) for (int i=0; i<(n); ++i)
  25. #define pnt pair <int, int>
  26. #define mp make_pair
  27. #define PI 3.14159265358979
  28. #define MEMS(a,b) memset(a,b,sizeof(a))
  29. #define LL long long
  30. #define U unsigned
  31.  
  32. int was[110][200100];
  33. int n,t;
  34. int a[110];
  35. void dfs(int p, int sum)
  36. {
  37.     if (was[p][sum]==t)
  38.         return;
  39.     was[p][sum]=t;
  40.     if (p==n)
  41.         return;
  42.     dfs(p+1,sum+a[p]);
  43.     dfs(p+1,sum);
  44. }
  45.  
  46. int main()
  47. {
  48. #ifdef Fcdkbear
  49.     freopen("in.txt","r",stdin);
  50.     //freopen("out.txt","w",stdout);
  51.     double beg=clock();
  52. #endif
  53.  
  54.     int num;
  55.     scanf("%d",&num);
  56.     for (t=1; t<=num; ++t)
  57.     {
  58.         int A;
  59.         scanf("%d%d",&n,&A);
  60.         int sum=0;
  61.         FOR(i,0,n)
  62.         {
  63.             scanf("%d",&a[i]);
  64.             sum+=a[i];
  65.         }
  66.         dfs(0,0);
  67.         bool is=false;
  68.         FOR(i,A/2,sum+1)
  69.             if ((was[n][i]==t) && (i+i>=A))
  70.             {
  71.                 is=true;
  72.                 printf("%d\n",i);
  73.                 break;
  74.             }
  75.         if (!is)
  76.             printf("-1\n");
  77.     }
  78.  
  79. #ifdef Fcdkbear
  80.     double end=clock();
  81.     fprintf(stderr,"*** Total time = %.3lf ***\n",(end-beg)/CLOCKS_PER_SEC);
  82. #endif
  83. }
Advertisement
Add Comment
Please, Sign In to add comment