#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);

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

    sort(Ap.begin(), Ap.end());

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

    int mn = n + 1, mni = -1;

    while (ptr >= 0) {
        suff += Ap[ptr--].first;
        if (suff >= sum) {
            break;
        }
    }
    ptr = min(n - 1, ptr + 1);

    vector<int> qans(n + 1), qptr(n + 1);
    qans[0] = abs(n - 2 * max(ptr, 0));
    qptr[0] = max(ptr, 0);

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

        qans[i] = abs(n - 2 * max(i, ptr) + 2 * i);
        qptr[i] = max(i, ptr);

        if (mn > qans[i]) {
            mn = qans[i], mni = i - 1;
        }
    }

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

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

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

    return ans;
}
