#include <vector>
#include <map>
#include <algorithm>
using namespace std;
std::vector<int> solve(std::vector<int> A)
{
    vector<int> st, dr;
    map<int, int> last;
    for(int i = 0; i < A.size(); i++)
    {
        if(last[A[i]])
        {
            st.push_back(last[A[i]] - 1);
            dr.push_back(i);
            last[A[i]] = 0;
        }
        else
            last[A[i]] = i + 1;
    }
    vector<pair<int, int>> rem;
    for(auto it : last)
        if(it.second)
            rem.push_back({it.first, it.second - 1});
    sort(rem.begin(), rem.end());
    long long sumst = 0, sumdr = 0;
    for(int i = rem.size() - 1; i >= 0; i--)
    {
        if(sumst <= sumdr)
            st.push_back(rem[i].second), sumst += rem[i].first;
        else
            dr.push_back(rem[i].second), sumdr += rem[i].first; 
    }
    for(auto it : dr)
        st.push_back(it);
    return st;
}