fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main()
  5. {
  6. int t;
  7. cin>>t;
  8. while(t--)
  9. {
  10. int n;
  11. cin>>n;
  12. int a[n];
  13. for(int i=0;i<n;i++) cin>>a[i];
  14. int maxi = -1;
  15. for(int i=0;i<(1<<n);i++)
  16. {
  17. vector<int> v;
  18. for(int j=0;j<n;j++)
  19. {
  20. if(i&(1<<j)) v.push_back(j);
  21. }
  22. bool thisSubSetCanBeConsidered = true;
  23. for(int j=1;j<v.size();j++)
  24. {
  25. if(v[j]==v[j-1]+1)
  26. {
  27. thisSubSetCanBeConsidered = false;
  28. break;
  29. }
  30. }
  31. int sum = 0;
  32. if(thisSubSetCanBeConsidered)
  33. {
  34. for(int j=0;j<v.size();j++)
  35. sum+=a[v[j]];
  36. maxi = max(maxi,sum);
  37. }
  38. }
  39. cout<<maxi<<endl;
  40. }
  41. }
Success #stdin #stdout 0s 16064KB
stdin
1
5
1 2 3 4 5
stdout
9