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

using namespace std;

const int maxn = 1e5 + 5;
const int MAXPWR = 30;

int n;
int cnt[MAXPWR];
vector<int> position[MAXPWR];

int findLog2(int x)
{
    int cnt = 0;

    while (x != 1)
    {
        ++cnt;
        x /= 2;
    }

    return cnt;
}

std::vector<int> solve(std::vector<int> A)
{
    n = A.size();

    for (int i = 0; i < n; ++i)
    {
        int p = findLog2(A[i]);

        ++cnt[p];
        position[p].push_back(i);
    }

    vector<int> ans(n, 0);
    int l = 0, r = n - 1;

    vector<int> other;

    for (int i = 0; i < MAXPWR; ++i)
    {
        if (position[i].empty()) continue;

        for (int j = 0; j + 2 <= position[i].size(); j += 2)
        {
            ans[l++] = position[i][j];
            ans[r--] = position[i][j + 1];
        }

        if (int(position[i].size()) % 2 == 1)
        {
            other.push_back(position[i].back());
        }

        position[i].clear();
    }

    int otherSz = other.size();
    swap(other[otherSz - 1], other[otherSz / 2 - 1]);

    for (int i = l; i <= r; ++i)
    {
        ans[i] = other[i - l];
    }

    return ans;
}
