#include "partition.h"
#include <bits/stdc++.h>
using namespace std;
int n;
vector<int> solve2(vector<int> vec) {
    unordered_map<int, int> ump;
    ump.reserve(1e6);
    vector<int> ans(n), positions;
    vector<bool> used(n);
    int poz1 = 0, poz2 = n - 1, biggest = 0, pairs = 0;

    for(int i = 0; i < n; i++) {
        int nr = ump[vec[i]];

        if(nr == 0) {
            ump[vec[i]] = i;
        } else {
            ans[poz1++] = nr;
            ans[poz2--] = i;
            pairs++;
            used[nr] = used[i] = 1;
            ump[vec[i]] = 0;
        }
    }

    int pozbig = 0;

    for(int i = 0; i < n; i++) {
        if(!used[i]) {
            positions.push_back(i);

            if(biggest < vec[i]) {
                biggest = vec[i];
                pozbig = i;
            }
        }
    }

    int remaining = positions.size();

//    if(!remaining)
    return ans;

    if(remaining % 2 == 0) {
        int i;

        int pozpos = 0;

        for(i = 0; i < remaining / 2 - 1; i++) {
            if(positions[pozpos] == pozbig) {
                pozpos++;
            }

            ans[i + pairs] = positions[pozpos++];
        }

        ans[i + pairs] = pozbig;
        i++;

        for(; i < remaining; i++)
            ans[i + pairs] = positions[pozpos++];
    } else {
        int i;

        int pozpos = 0;

        for(i = 0; i < remaining / 2; i++) {
            if(positions[pozpos] == pozbig) {
                pozpos++;
            }

            ans[i + pairs] = positions[pozpos++];
        }

        ans[i + pairs] = pozbig;
        i++;

        for(; i < remaining; i++)
            ans[i + pairs] = positions[pozpos++];
    }

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