#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
std::vector<int> solve(std::vector<int> A) {
    int n = A.size();
    int l = n / 2, r = l;
    if (n & 1) {
        r++;
    }
    long long s = 0;
    vector<int> g[27];
    for (int i = 0; i < n; ++i) {
        int fs = __builtin_ffs(A[i]);
        fs--;
        //cout << fs << endl;
        g[fs].push_back(i);
        s += A[i];
    }
    int b[n];
    vector<int> ans;
    int it = 0;
    for (int i = 0; i < 27; ++i) {
        for (int idx : g[i]) {
            b[it++] = idx;
        }
    }
    bool can[n];
   long long pref[n];
    can[0] = true;
    pref[0] = A[0];
    for (int i = 1; i < n; ++i) {
        pref[i] = pref[i - 1] + A[b[i]];
        if (2ll * pref[i] < s || 2ll * (pref[i] - (long long)A[b[i]]) < s) can[i] = true;
        else can[i] = false;
    }
    while (true) {
        int k;
        if (abs(l - n + l) < abs(r - n + r)) k = l, l--;
        else k = r, r++;
        long long sum = pref[k - 1];
        if (2 * sum < s) {
            int l = 1, r = min(k, n - k);
            while (true) {
                if (l >= r) break;
                int mid = (l + r) >> 1;
                if (2 * (sum - pref[mid - 1] + s - pref[n - mid - 1]) >= s) r = mid;
                else l = mid + 1;
            }
            for (int i = l; i < k; ++i) ans.push_back(b[i]);
            for (int i = n - l; i < n; ++i) ans.push_back(b[i]);
            for (int i = 0; i < l; ++i) ans.push_back(b[i]);
            for (int i = k; i < n - l; ++i) ans.push_back(b[i]);
            return ans;
        } else {
            if (can[k - 1]) {
                for (int i = 0; i < n; ++i) ans.push_back(b[i]);
                return ans;
            }
        }
    }
    return ans;
}
