#include <bits/stdc++.h>
#include "partition.h"
using namespace std;
vector<int> solve2(vector<pair<long long,long long>> v)
{
    vector<int>ans;
    int n=v.size();
    long long sum=0,sum1=0;
    for(int i=0;i<n;i++)
    {
        sum1+=v[i].first;
    }
    sum1=sum1/2ll+sum1%2ll;
    sort(v.begin(),v.end());
    long long res=1e9,res1=-1,k=0;
    for(int i=0;i<n;i++)
    {
        sum+=v[i].first;
        if(sum>=sum1)
        {
            while(sum>=sum1)
            {
                int temp=n-(i-k+1);
                if(abs(temp-(i-k+1))<res)
                {
                    res1=k;
                    res=abs(temp-(i-k+1));
                }
                sum-=v[k].first;
                k++;
            }
        }
    }
    sum=0;
    for(int i=res1;i<n;i++)
    {
        ans.push_back(v[i].second);
    }
    for(int i=0;i<res1;i++)
    {
        ans.push_back(v[i].second);
    }
    return ans;
}
vector<int> solve(vector<int> a)
{
    map<int,int>m;
    int n=a.size();
    vector<int>res1,res2,res3;
    for(int i=0;i<n;i++)
    {
        if(m[a[i]]==0)
        {
            m[a[i]]=i+1;
        }
        else
        {
            res1.push_back(m[a[i]]-1);
            res2.push_back(i);
            m[a[i]]=0;
        }
    }
    vector<pair<long long,long long>>v2;
    int k=1;
    for(int i=0;i<=25;i++)
    {
        if(m[k]!=0)v2.push_back({k,m[k]-1});
        k=k*2;
    }
    res3=solve2(v2);
    for(auto i:res3)res1.push_back(i);
    for(auto i:res2)res1.push_back(i);
    return res1;
}
