Submission

Status:

P-PPPPP-PP

Subtask/Task Score:

80/100

Score: 80

User: nigger123

Problemset: วิศวกรรมข้อมูล

Language: cpp

Time: 0.002 second

Submitted On: 2025-10-10 12:47:41

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

string to_binary_string(unsigned int value) {
    if (value == 0) return "0";

    string binary_representation;
    while (value > 0) {
        binary_representation += ((value & 1) ? '1' : '0');
        value >>= 1;
    }

    reverse(binary_representation.begin(), binary_representation.end());
    return binary_representation;
}

unsigned long long binary_to_decimal(const string& binary_str) {
    unsigned long long result = 0;
    for (char ch : binary_str) {
        result = result * 2 + (ch - '0');
    }
    return result;
}

int count_ones(int x) {
    return __builtin_popcount(x);
}

int main() {
    int x;
    cin >> x;

    vector<int> more_ones;
    vector<int> others;

    for (int i = 0; i < x; ++i) {
        int input;
        cin >> input;

        string binary = to_binary_string(input);
        int ones = count(binary.begin(), binary.end(), '1');
        int zeros = count(binary.begin(), binary.end(), '0');

        if (ones > zeros) {
            more_ones.push_back(input);
        } else {
            others.push_back(input);
        }
    }

    sort(more_ones.begin(), more_ones.end(), [](int a, int b) {
        return count_ones(b) < count_ones(a);
    });

    vector<int> result;
    result.insert(result.end(), more_ones.begin(), more_ones.end());
    result.insert(result.end(), others.begin(), others.end());

    string combined_binary;
    for (int num : result) {
        combined_binary += to_binary_string(num);
    }

    cout << binary_to_decimal(combined_binary) << endl;

    return 0;
}