#include "partition.h"
#include <bits/stdc++.h>
using namespace std;

using ll = long long;

vector<int> solve(vector<int> a) {
    vector<pair<int, int>> x;
    for(int i = 0; i < (int)a.size(); i++) {
        x.push_back({a[i], i});
    }
    sort(x.begin(), x.end());
    sort(a.begin(), a.end());
    vector<ll> pref((int)a.size());
    for(int i = 0; i < (int)a.size(); i++) {
        ll c = 0;
        if(i > 0) c = pref[i - 1];
        pref[i] = c + a[i];
    }
    int SUM = pref[(int)a.size() - 1];
    ll sum = pref[(int)a.size() - 1] / 2;
    ll ans = (ll)1e18;
    auto f = [&](int x) {
        return max(x, SUM - x);
    };
    int l = -1, r = -1;
    for(int i = 0; i < (int)a.size(); i++) {
        if(ans > f(pref[i])) {
            ans = f(pref[i]);
            l = 0, r = i;
        }
        if(pref[i] < sum) continue;
        int l = 0, r = i, res = -1;
        while(l <= r) {
            int mid = (l + r) >> 1;
            if(pref[i] - pref[mid] >= sum) {
                l = mid + 1;
                res = mid;
            } else {
                r = mid - 1;
            }
        }
        if(res > -1 && ans > f(pref[i] - pref[res])) {
            ans = f(pref[i] - pref[res]);
            l = res + 1, r = i;
        }
        res++;
        if(ans > f(pref[i] - pref[res])) {
            ans = f(pref[i] - pref[res]);
            l = res + 1, r = i;
        }
    }
    bool flag = 0;
    int sm = 0;
    for(int i = l; i <= r; i++) sm += a[i];
    if(sm <= SUM - sm) flag = 0;
    else flag = 1;
    if(flag) {
        vector<int> otv;
        for(int i = l; i <= r; i++) otv.push_back(x[i].second);
        for(int i = 0; i < (int)a.size(); i++) {
            if(i < l || i > r) otv.push_back(x[i].second);
        }
        return otv;
    } else {
        vector<int> otv;
        for(int i = 0; i < (int)a.size(); i++) {
            if(i < l || i > r) otv.push_back(x[i].second);
        }
        for(int i = l; i <= r; i++) otv.push_back(x[i].second);

        return otv;
    }

}
