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

    vector<pair<long long, int>> query;
    query.push_back({(sum + 1) / 2 - pref[0], 0});
    for (int i = 1; i <= n; ++i) {
        query.push_back({(sum + 1) / 2 - pref[i], i});
    }

    vector<int> qans(n + 1);
    for (int i = n - 1; i >= 0; --i) {
        while (!query.empty() && suff[i] >= query.back().first) {
            qans[query.back().second] = i;
            query.pop_back();
        }
    }

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

    auto check = [&](long long cur, int ptr) {
        int l = qans[ptr + 1];

        int fp = n - l + ptr + 1;
        int sp = n - fp;

        return abs(fp - sp);
    };

    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;
}
