#include "partition.h"
#include "bits/stdc++.h"
using namespace std;

int cnt[27];

vector<int>get_ans(vector<pair<int,int>>A){
      vector<int>res;
      int n=(int)A.size();
      sort(A.begin(),A.end());
      if(n==1) return {0};
      for(int i=0;i<n/2-1;i++) res.push_back(A[i].second);
      res.push_back(A[n-1].second);
      for(int i=n/2-1;i<n-1;i++) res.push_back(A[i].second);
      return res;
}

int vl(int v)
{
      int res=0;
      while(v>0){
            v/=2;
            ++res;
      }
      return res;
}

vector<int>solve(vector<int>A)
{
      int n=(int)A.size();
      for(int i=0;i<n;i++) cnt[vl(A[i])]++;
      vector<int>v1,v2;
      vector<pair<int,int>>a;
      for(int i=0;i<n;i++){
            if(!cnt[vl(A[i])]) continue;
            if(cnt[vl(A[i])]%2==0){
                  v2.push_back(i);
            }
            else{
                  if(cnt[vl(A[i])]==1) a.push_back({A[i],i});
                  else v1.push_back(i);
            }
            cnt[vl(A[i])]--;
      }
      vector<int>res;
      for(int x:v1) res.push_back(x);
      vector<int>aa=get_ans(a);
      for(int x:aa) res.push_back(x);
      for(int x:v2) res.push_back(x);
      return res;
}
