fork download
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. void solve() {
  6.  
  7. int n;
  8.  
  9. cin >> n;
  10.  
  11. vector<long long> nums(n + 1);
  12. vector<long long> count(30, 0);
  13.  
  14. for(int i = 1; i <= n; i++) {
  15.  
  16. cin >> nums[i];
  17.  
  18. for(int j = 0; j < 30; j++) {
  19.  
  20. count[j] = count[j] + ((nums[i] >> j) & 1);
  21.  
  22. }
  23.  
  24. }
  25.  
  26. long long ans = 0;
  27.  
  28. for(int i = 1; i <= n; i++) {
  29.  
  30. long long sum = 0;
  31.  
  32. for(int j = 0; j < 30; j++) {
  33.  
  34. bool flags = ((nums[i] >> j) & 1);
  35.  
  36. if(flags) {
  37.  
  38. sum = sum + (1 << j) * (n - count[j]);
  39.  
  40. }
  41.  
  42. else {
  43.  
  44. sum = sum + (1 << j) * count[j];
  45.  
  46. }
  47.  
  48. }
  49.  
  50. ans = max(ans, sum);
  51.  
  52. }
  53.  
  54. cout << ans << endl;
  55.  
  56. }
  57.  
  58. int main() {
  59.  
  60. ios_base::sync_with_stdio(false);
  61. cin.tie(0);
  62.  
  63. int t;
  64.  
  65. cin >> t;
  66.  
  67. while(t--) {
  68.  
  69. solve();
  70.  
  71. }
  72.  
  73. return 0;
  74.  
  75. }
Success #stdin #stdout 0.01s 5272KB
stdin
5
3
18 18 18
5
1 2 4 8 16
5
8 13 4 5 15
6
625 676 729 784 841 900
1
1
stdout
0
79
37
1555
0