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

using namespace std;

const long long inf = 1e18;

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

    vector<pair<int, int>> Ap;

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

    sort(Ap.begin(), Ap.end());
    vector<long long> pref(n + 1), suff(n + 1);
    for (int i = n - 1; i >= 0; --i) {
        suff[i] = Ap[i].first + suff[i + 1];
    }

    for (int i = 0; i < n; ++i) {
        pref[i + 1] = pref[i] + Ap[i].first;
    }

    int ptr = n;
    vector<int> qans(n + 1);
    for (int i = n - 1; i >= 0; --i) {
        while (ptr >= 0 && suff[i] >= (sum + 1) / 2 - pref[ptr]) {
            qans[ptr--] = i;
        }
    }

    for (int i = 0; i <= n; ++i) {
        qans[i] = max(qans[i], i);
    }

    auto check = [&](long long cur, int ptr) {
        return abs(n - 2 * qans[ptr + 1] + 2 * ptr + 2);
    };

    int mn = n + 1, mni = -1;
    for (int i = 0; i <= n; ++i) {
        if (mn > check(pref[i], i - 1)) {
            mn = check(pref[i], i - 1);
            mni = i - 1;
        }
    }

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

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

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

    return ans;
}
