Codeforces
CF Step
Youtube Linkedin Discord Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Back to homepage

Code : XOR to All

#include <bits/stdc++.h>
using namespace std;

long long get_sum_after_xor(int n, vector<int> &sbit, int x) {
    long long sum = 0;
    for (int j = 0; j < 30; j++) {
        if ((1 << j) & x) {
            sum += (1LL << j) * (n - sbit[j]);
        } else {
            sum += (1LL << j) * sbit[j];
        }
    }
    return sum;
}

void solve(vector<int> &a) {
    int n = a.size();
    vector<int> sbit(30 + 1);
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < 30; j++) {
            if ((1 << j) & a[i]) {
                sbit[j]++;
            }
        }
    }

    long long res = accumulate(a.begin(), a.end(), 0LL);
    for (int i = 0; i < n; i++) {
        res = max(res, get_sum_after_xor(n, sbit, a[i]));
    }
    cout << res << endl;
}

int main() {
    int n;
    cin >> n;
    vector<int> a(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }
    solve(a);
    return 0;
}