#include "partition.h"
#include <bits/stdc++.h>

using namespace std;
const int N = 3e7+2;
int cnt[N];

std::vector<int> solve(std::vector<int> A) {
    int n = (int)A.size();
    vector<int> pos[26];
    for (int i = 0; i < n; ++i) {
		int x = 31-__builtin_clz(A[i]);
		pos[x].push_back(i);
	}
	
	vector<int> c, s;
	for (int i = 25; i >= 0; --i) {
		if ((int)pos[i].size() > 0) {
			int m = (int)pos[i].size()/2;
			for (int j = 0; j < m; ++j)c.push_back(pos[i].back()), pos[i].pop_back();
			for (int j = 0; j < m; ++j)s.push_back(pos[i].back()), pos[i].pop_back();
		}
	}
	
	vector<int> b;
	for (int i = 25; i >= 0; --i)
		if ((int)pos[i].size() > 0)b.push_back(pos[i].back());
	int m = ((int)b.size()-1)/2;
	while (m--) {
		c.push_back(b.back());
		b.pop_back();
	}
	for (auto i: b)c.push_back(i);
	for (auto i: s)c.push_back(i);
	return c;
}
