Submission

Status:

PPPPPPPPPP

Subtask/Task Score:

100/100

Score: 100

User: letdown

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

Language: cpp

Time: 0.003 second

Submitted On: 2025-10-13 09:12:30

#include <iostream>
#include <math.h>
#include <vector>
#include <algorithm>

using namespace std;

string toTwo(int dec) {
    if (dec == 0) return "0";
    int n = dec;
    string ans = "";
    while (n > 0)
    {
        ans += to_string(n % 2);
        n /= 2;
    }
    int l = ans.length();
    for (int i = 0; i < l/2; i++) {
        char t = ans[i];
        ans[i] = ans[l-i-1];
        ans[l-i-1] = t;
    }
    return ans;
}

int twoToDec(string tw) {
    if (tw == "0") return 0;
    int ans=0;
    int l = tw.length();
    for (int i = l-1; i >= 0; i--) {
        ans += int(tw[i] - '0') * pow(2, l-i-1);
    }
    return ans;
}

int compBinConcat(string a, string b) {
    return (a+b > b+a);
}

int main() {
    int n;
    string ansStr = "";
    cin >> n;

    vector<string> v;
    for (int i = 0; i < n; i++) {
        int t;
        cin >> t;
        v.push_back(toTwo(t));
    }

    sort(v.begin(), v.end(), compBinConcat);
    // for (auto i : v) cout << i << " ";

    for (auto i : v) ansStr += i;
    cout << twoToDec(ansStr);
}