/*
  _      __        __         __        ____                    ___    _                   __
 | | /| / / ___ _ / /_ ____  / /       / __ \  ___  ___        / _ \  (_) ___  ____ ___   / /
 | |/ |/ / / _ `// __// __/ / _ \     / /_/ / / _ \/ -_)      / ___/ / / / -_)/ __// -_) /_/ 
 |__/|__/  \_,_/ \__/ \__/ /_//_/     \____/ /_//_/\__/      /_/    /_/  \__/ \__/ \__/ (_)  
                                                                                             
*/
 
#pragma GCC optimize("03")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
#include "partition.h"
#define ll long long
#define ld long double
#define all(v) v.begin(), v.end()
#define allr(v, l, r) v.begin() + l, v.begin() + r;
#define endl "\n"
#define newline cout << endl;
using namespace std;

// constants
const int INF = 1e9;

// functions

// structs

// globals

// notes
/*
 -stuff you should look for-
* int overflow, array bounds
* special cases (n=1?)
* do something instead of nothing and stay organized
* WRITE STUFF DOWN
* DON'T GET STUCK ON ONE APPROACH

continue - skip the rest in the loop
*/

vector <int> solve(vector<int> A)
{
    int n = (int)A.size();
    vector <pair <int, int> > a(n);
    for (int i = 0; i < n; i++)
    {
        a[i] = {A[i], i};
    }
    if (n == 1)
    {
        vector <int> res;
        res.push_back(0);
        
        return res;
    }
    
    sort(all(a), [&](const pair <int, int> &x, const pair <int, int> &y)
    {
        return x.first > y.first;
    });
    ll S = 0LL;
    for (int i = 0; i < n; i++)
    {
        S += 1LL * a[i].first;
    }
    int k;
    ll cur = 0LL;
    for (k = 0; k < n; k++)
    {
        cur += 1LL * a[k].first;
        if (cur * 2 >= S)
        {
            break;
        }
    }
    swap(a[0], a[k]);
    
    vector <pair <int, int> > bk;
    for (int i = k + 1; i < n; i++)
    {
        bk.push_back({a[i].first, i});
    }
    vector <int> assigned(n, -1);
    for (int i = 0; i < (int)bk.size(); i++)
    {
        assigned[bk[i].second] = i;
    }
    
    int x = a[0].first, j = k, best = k + 1 - n + k + 1;
    while (!bk.empty() && best)
    {
        ll nw = cur - 1LL * x + bk.back().first;
        if (nw * 2 >= S)
        {
            break;
        }
        if (j + 2 - n + j + 2 > 0)
        {
            break;
        }
        cur += 1LL * x + 1LL * bk.back().first;
        
        swap(a[bk.back().second], a[j + 1]);
        int getout = assigned[j + 1];
        bk[getout].second = bk.back().second;
        assigned[bk.back().second] = getout;
        
        swap(a[j], a[j + 1]);
        bk.pop_back();
        assigned[j + 1] = -1;
        j++;
    }
    
    vector <int> res;
    for (auto [val, id] : a)
    {
        res.push_back(id);
    }
    return res;
}

/*
$$$$$$$$\ $$$$$$$$\ 
$$  _____|\____$$  |
$$ |          $$  / 
$$$$$\       $$  /  
$$  __|     $$  /   
$$ |       $$  /    
$$$$$$$$\ $$$$$$$$\ 
\________|\________|
*/