Submission

Status:

PP-PPPP-PP

Subtask/Task Score:

80/100

Score: 80

User: konthaina_TH

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

Language: cpp

Time: 0.003 second

Submitted On: 2026-09-26 03:25:25

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

string to_binary(bitset<64> b) {
    string str = b.to_string();
    auto pos = str.find_first_not_of('0');
    return (pos != string::npos) ? str.substr(pos) : "0";
}

int main() {
    int n;
    cin >> n;
    vector<int> a(n);
    vector<bitset<64>> bit(n);
    vector<int> cnt_1(n);
    for (int i=0;i<n;i++) {
        cin >> a[i];
        bit[i] = bitset<64>(a[i]);
        cnt_1[i] = bit[i].count();
        //bit[i].erase(0,min(s[i].find_first_not_of('0'),s[i].size()-1));
    }
    for (int i=0;i<n;i++) {
        for (int j=i+1;j<n;j++) {
            if (cnt_1[i] < cnt_1[j]) {
                swap(cnt_1[i],cnt_1[j]);
                swap(bit[i],bit[j]);
                swap(a[i],a[j]);
            }
            else if (cnt_1[i] == cnt_1[j]) {
                string s1 = to_binary(bit[i]);
                string s2 = to_binary(bit[j]);
                if (s1 + s2 < s2 + s1) {
                    swap(cnt_1[i],cnt_1[j]);
                    swap(bit[i],bit[j]);
                    swap(a[i],a[j]);
                }
            }
        }
    }
    string s = "";
    for (int i = 0;i<n;i++) {
        string str = bit[i].to_string();
        auto pos = str.find_first_not_of('0');
        if (pos != string::npos) {
            str = str.substr(pos);
        }
        else {
            str = "0";
        }
        s += str;
    }
    unsigned long long val = stoull(s,nullptr,2);
    cout << val << "\n";
    return 0;
}