#include "partition.h"
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <map>

using namespace std;
using ll = long long;

int n;
int f[27];
vector <int> ans, st, dr, ramase;

vector<int> solve(vector<int> A) {
    ans.clear();
    st.clear();
    dr.clear();
    ramase.clear();
    n = A.size();
    for(int i = 0; i <= 25; i++)
        f[i] = -1;

    /*int nr = 1;
    for(int exp = 0; exp <= 26; exp++) {
        int put = 31 - __builtin_clz(nr);
        cout << nr << " " << put << '\n';
        if(exp < 26)
            nr *= 2;
    }*/

    for(int i = 0; i < n; i++) {
        int put = 31 - __builtin_clz(A[i]);
        if(f[put] == -1)
            f[put] = i;
        else {
            st.push_back(f[put]);
            dr.push_back(i);
            f[put] = -1;
        }
    }

    for(int i = 0; i <= 25; i++) {
        if(f[i] != -1)
            ramase.push_back(i);
    }

    if(ramase.size() > 0) {
        n = ramase.size();
        int pos = 0; ///pozitia din RAMASE
        int cate = n / 2;
        if(n % 2 == 1)
            cate++;
        ///cate am nevoie pt prima parte
        for(int i = 1; i < cate; i++) {
            ans.push_back(f[ramase[pos]]);
            pos++;
        }
        ans.push_back(f[ramase[n - 1]]);
        while(pos < n - 1) {
            ans.push_back(f[ramase[pos]]);
            pos++;
        }
    }
    vector <int> sol;
    for(auto x : st)
        sol.push_back(x);
    for(auto x : ans)
        sol.push_back(x);
    for(auto x : dr)
        sol.push_back(x);
    return sol;
}