#include <bits/stdc++.h>
#define INF 1000000000
#define fi first
#define se second
#include "partition.h"
const int LOG=26;
#pragma GCC optimize ("Ofast", "unroll-loops")
#pragma GCC target ("avx2")
using namespace std;

vector<int> solve(vector<int> a) {
    int tot=0, n=a.size(), minim=INF, i, j;
    vector<int>ans;
    unordered_map<int, vector<int>>h;
    h.clear();
    vector<int>v, t, ind;
    v.resize(LOG);
    int poww=(1<<LOG), ok=0;
    int p1=-1, p2=n, num=0;
    t.resize(LOG);
    ind.resize(LOG);
    ans.resize(n);
    vector<int>poww2(LOG);
    for (i=0; i<LOG; i++) t[i]=ind[i]=0;
    poww2[0]=1;
    for (i=1; i<LOG; i++) {
        poww2[i]=poww2[i-1]*2;
    }
    for (i=0; i<n; i++) {
        h[a[i]].push_back(i);
    }
    for (i=LOG-1; i>=0; i--) {
        if (h.find(poww2[i])==h.end()) continue;
        vector<int>cc=h[poww2[i]];
        int sz=cc.size();
        for (j=0; j<sz/2; j++) {
            ans[++p1]=cc[j];
        }
        for (j=sz-1; j>=(sz+1)/2; j--) {
            ans[--p2]=cc[j];
        }
        if (sz%2==1) {
            t[i]=1;
            ind[i]=cc[sz/2];
            num++;
        }
    }
    int nr=0;
    for (i=0; i<LOG; i++) {
        nr+=t[i];
        if (t[i]==1) {
            if (nr<=num/2-1) {
                ans[++p1]=ind[i];
            }
            if (nr>=num/2 && nr<num) ans[--p2]=ind[i];
            if (nr==num) ans[++p1]=ind[i];
        }
    }
    return ans;
}

