#include <bits/stdc++.h>
//#define int64_t long long
using namespace std;

std::vector<int> solve(std::vector<int> A){
    map<int64_t,int64_t> mp, cnt, mt;
    int64_t n = A.size();
    int64_t cur = 0;
    vector<int> a = A;
    for(auto &i : A) cnt[i]++;
    set<int64_t> st;
    for(int64_t i = 0; i < n; i++){
        if(mp[A[i]] < cnt[A[i]]-1){
            mp[A[i]]++;
            a[i] = (mp[a[i]] & 1 ? cur : -1);
            A[i] = -1;
            cur++;
        } else{
            st.insert(a[i]);
            a[i] = -2;
        }
    }
    int64_t subsm = 0, sum = 0;
    for(auto &i : st) sum += i;
    set<int64_t> stm;
    while(subsm < (sum+sum%2)/2){
        auto pos = st.lower_bound((sum+sum%2)/2-subsm);
        if(pos == st.end()) pos--;
        subsm += *pos;
        mt[*pos] = cur;
        stm.insert(*pos);
        //cerr << *pos << endl;
        cur++;
        pos--; 
    }
    set<int64_t> stt;
    for(auto &i : st){
        if(stm.find(i) == stm.end()){
            stt.insert(i);
        }
    }
    int64_t cnt1 = st.size()-stt.size(), cnt2 = 0;
    for(auto &i : stt){
        if(cnt2 >= cnt1){
            a[i] = cur;
            cnt1++;
        } else{
            a[i] = -1;
            cnt2++;
        }
    }
    for(int64_t i = 0; i < n; i++){
        if(a[i] == -2){
            a[i] = mt[A[i]];
        }
    }
    for(int64_t i = 0; i < n; i++){
        if(a[i] == -1){
            a[i] = cur;
            cur++;
        }
    }
    //for(auto &i : a) cout << i << " ";
    return a;
}

// signed main(){
//     cin.tie(0)->sync_with_stdio(0);
//     int _; cin >> _;
//     while(_--){
//         int n; cin >> n;
//         vector<int> a(n);
//         for(auto &i : a) cin >> i;
//         auto p = solve(a);
//     }
// }