fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. bool check(long long n)
  4. {
  5. int count=0,max=0;
  6. while(true)
  7. {
  8. if(n%2==0)
  9. {
  10. if(count>max)
  11. max=count;
  12. count=0;
  13. }
  14. else
  15. count++;
  16. n/=2;
  17. if(n==0)
  18. {
  19. if(count>max)
  20. max=count;
  21. break;
  22. }
  23. }
  24. //checks if longest consecutive set bits is of length 1
  25. if(max==1)
  26. return true;
  27. else
  28. return false;
  29. }
  30. int main()
  31. {
  32. int t;
  33. cin>>t;
  34. while(t--)
  35. {
  36. int monsters;
  37. cin>>monsters;
  38. int level[monsters];
  39. for(int i=0;i<monsters;i++)
  40. cin>>level[i];
  41. long long n=pow(2,monsters),max=LLONG_MIN;
  42. for(long long i=0;i<n;i++)
  43. {
  44. long long sum=0;
  45. if(check(i))
  46. {
  47. long long temp=i;
  48. for(int j=0;j<monsters;j++)
  49. {
  50. if(i&1<<j)
  51. sum+=level[j];
  52. }
  53. }
  54. if(sum>max)
  55. max=sum;
  56. }
  57. cout<<max<<endl;
  58. }
  59. return 0;
  60. }
Success #stdin #stdout 0s 15232KB
stdin
1
5
1 2 3 4 5
stdout
9