Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- // #include <atcoder/all>
- using namespace std;
- #define int long long
- #define all(x) (x).begin(), (x).end()
- typedef vector<int> vi;
- typedef vector<vi> vvi;
- typedef vector<pair<int, int>> vpi;
- typedef pair<int, int> pi;
- #define f first
- #define s second
- #define pb push_back
- #define endl "\n"
- #define yes cout << "YES" << endl
- #define no cout << "NO" << endl
- #define init(x, a) memset(x, a, sizeof(x))
- const int mod1 = 1e9 + 7, mod2 = 998244353, INF = 2e18, N = 2e5 + 5, L = 19;
- int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }
- // -----------------------------------------------------------------------------
- void solve()
- {
- int n;
- cin >> n;
- vi a(n);
- int sum = 0;
- vi ones(31), zeros(31);
- for (int i = 0; i < n; i++)
- {
- cin >> a[i];
- sum += a[i];
- }
- for (int b = 0; b < 31; b++)
- {
- int num1 = 0, num2 = 0;
- for (int i = 0; i < n; i++)
- {
- if ((a[i] >> b) & 1)
- {
- num1++;
- }
- else
- {
- num2++;
- }
- }
- ones[b] = num1, zeros[b] = num2;
- }
- // cout << ones[0] + zeros[0] << endl;
- for (int i = 0; i < n; i++)
- {
- int temp = 0;
- for (int b = 0; b < 31; b++)
- {
- if ((a[i] >> b) & 1)
- {
- // zeros to ones
- temp += (1LL << b) * zeros[b];
- }
- else
- {
- // if zero, add the ones
- temp += (1LL << b) * ones[b];
- }
- }
- sum = max(sum, temp);
- }
- cout << sum << endl;
- return;
- }
- signed main()
- {
- // __START__;
- ios_base::sync_with_stdio(false);
- cin.tie(NULL);
- cout.tie(NULL);
- int t = 1;
- // cin >> t;
- while (t--)
- {
- solve();
- }
- // __END__;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment