#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#include "partition.h"

using namespace std;

vector<int> solve(vector<int> A) {
    int n = A.size();

    vector<pair<int, int>> Ap(n + 1);

    long long sum = 0;
    for (int i = 0; i < n; ++i) {
        Ap[i + 1] = {A[i], i};
        sum += A[i];
    }
    sum = (sum + 1) / 2;

    stable_sort(Ap.begin() + 1, Ap.end());

    int ptr = n + 1;
    long long pref = 0, suff = 0;

    int mn = n + 1, mni = -1, mnj = -1;
    while (ptr >= 1) {
        suff += Ap[--ptr].first;
        if (suff >= sum) {
            break;
        }
    }

    int cur = 0;
    for (int i = 0; i <= n; ++i) {
        while (ptr <= n && (i >= ptr || pref + suff - Ap[ptr].first >= sum)) {
            suff -= Ap[ptr++].first;
        }

        cur = abs(n - 2 * ptr + 2 * i + 2);

        if (mn > cur) {
            mn = cur, mni = i, mnj = ptr;
        }

        pref += Ap[i + 1].first;
    }

    ptr = 0;
    vector<int> ans(n);
    for (int i = 1; i <= mni; ++i) {
        ans[ptr++] = (Ap[i].second);
    }

    for (int i = mnj; i <= n; ++i) {
        ans[ptr++] = (Ap[i].second);
    }

    for (int i = mni + 1; i < mnj; ++i) {
        ans[ptr++] = (Ap[i].second);
    }

    return ans;
}
