#include <bits/stdc++.h>

using namespace std;
using ll = long long;

mt19937 rng(5125123);

const int MAXN = 1e7 + 7;

// short int lg[(1 << 25) + 1];

short int lg[MAXN];
int ord[MAXN];
ll pref[MAXN];

vector<int> solve(vector<int> A) {
    int n = A.size();
    vector<int> cnt(30);
    for (int i = 0; i < n; ++i) {
        lg[i] = __lg(A[i]);
    }
    vector<int> pos(30);
    int kek = 0;
    for (int i = 0; i < 30; ++i) {
        pos[i] = kek;
        kek += cnt[i];
    }
    for (int i = 0; i < n; ++i) {
        int v = lg[i];
        ord[pos[v]] = i;
        ++pos[v];
    }
    ll sum = 0;
    for (int i = 0; i < n; ++i) {
        sum += A[i];
    }
    for (int i = 0; i < n; ++i) {
        pref[i] = (i == 0 ? 0 : pref[i - 1]) + A[ord[i]];
    }
    auto get_sum = [&](int l, int r) {
        if (l == 0) return pref[r];
        return pref[r] - pref[l - 1];
    };
    int pA = n, pB = 0;
    int ans = 1e9;
    int whol, whor;
    int r = 0;
    for (int l = 0; l < n; ++l) {
        while (r < n && get_sum(l, r) < (sum + 1) / 2) ++r;
        if (r == n) continue;
        ll s = get_sum(l, r);
        int x = r - l + 1;
        int y = n - x;
        if (s >= (sum + 1) / 2) {
            if (ans > abs(x - y)) {
                ans = abs(x - y);
                whol = l;
                whor = r;
            }
        }
        if (ans == 0) break;
    }
    vector<int> sol(n);
    int id = 0;
    for (int i = whol; i <= whor; ++i) sol[id++] = ord[i];
    for (int i = 0; i < whol; ++i) sol[id++] = ord[i];
    for (int i = whor + 1; i < n; ++i) sol[id++] = ord[i];
    return sol;
}

/*
8
2 2 3 2 3 2 2 3
X X   X   X X
6 5 3 1 0 2 4 7 


*/

// int main() {
//     ios::sync_with_stdio(0); 
//     cin.tie(0); cout.tie(0);
//     int n; cin >> n;
//     vector<int> A(n);
//     for (int i = 0; i < n; ++i) cin >> A[i];
//     auto res = solve(A);
//     for (auto x : res) cout << x << " ";
//     cout << "\n";
//     return 0;
// }