#include "partition.h"
#include <bits/stdc++.h>
#define pb push_back
#define ff first
#define ss second
#define all(v) v.begin(),v.end()
#define ll long long
using namespace std;

vector<int> solve(vector<int> a) {
    vector <int> lg;
    for (int i : a) {
        int x = -1;
        while (i) {
            x++;
            i /= 2;
        }
        lg.pb(x);
    }
    int n = a.size();
    vector <pair<int, int>> v;
    vector <int> cnt[27];
    for (int i = 0; i < n; i++) {
        cnt[lg[i]].pb(i);
    }
    for (int i = 0; i <= 26; i++) {
        for (int &j : cnt[i]) {
            v.pb({a[j], j});
        }
    }
    deque <int> res;
    int c = 1;
    vector <pair<int, int>> b;
    deque <pair<int, int>> memo;
    for (int i = 1; i < n; i++) {
        if (v[i].ff == v[i-1].ff) {
             c++;
        }
        else {
            if (c % 2) {
                b.pb(v[i-1]);
            }
            memo.pb({v[i-1].ff, c / 2 * 2});
            c = 1;
        }
    }
    memo.pb({v.back().ff, c / 2 * 2});
    if (c % 2) b.pb(v.back());
    n = b.size();
    res.push_front(b.back().ss);
    for (int i = 0; i < (n + 1) / 2 - 1; i++) {
        res.push_front(b[i].ss);
    }
    for (int i = ceil(1. * n / 2) - 1; i < n-1; i++) {
        res.pb(b[i].ss);
    }
    int x = 0;
    for (int i = 0; i < n; i++) {
        if (!memo.size()) break;
        if (memo[0].ff == v[i].ff) {
            for (int j = i; j < i + memo[0].ss; j++) {
                if (j % 2) res.pb(v[j].ss);
                else res.push_front(v[j].ss);
            }
            i += memo[0].ss - 1;
            memo.pop_front();
        }
    }
    vector <int> ans;
    for (int &i : res) ans.pb(i);
    return ans;
}
