#include "partition.h"
#include <algorithm>
#include <iostream>
#include <random>
#include <chrono>
#include <queue>

using namespace std;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

vector<int> solve(vector<int> a) {
    int n = a.size();
    if(n == 1) {
        return {0};
    }

    vector<int> idx(n), viz(n);
    long long sum = 0;
    for(int i = 0; i < n; i++) {
        idx[i] = i;
        sum += a[i];
    }

    vector<int> x, y;
    for(int i = 1; i < n; i += 2) {
        if(a[idx[i - 1]] < a[idx[i]]) {
            x.push_back(idx[i - 1]);
            y.push_back(idx[i]);
        } else {
            y.push_back(idx[i - 1]);
            x.push_back(idx[i]);
        }
    }
    if(n % 2 == 1) {
        y.push_back(idx[n - 1]);
    }
    idx.clear();
    for(auto it : x) {
        idx.push_back(it);
    }
    for(auto it : y) {
        idx.push_back(it);
    }
    int cnt = 0;
    long long aux = 0;
    for(int i = n - 1; i >= 0; i--) {
        if(aux < (sum + 1) / 2) {
            aux += a[idx[i]];
            cnt++;
        }
    }
    if(cnt >= (n + 1) / 2) {
        reverse(idx.begin(), idx.end());
        return idx;
    }

    vector<int> answer, q(n, -1);
    vector<long long> sp(n);
    for(int i = 0; i < n; i++) {
        sp[i] = a[idx[i]];
        if(i > 0) {
            sp[i] += sp[i - 1];
        }
    }
    aux = 0;
    for(int i = 0; i < (n + 1) / 2; i++) {
        answer.push_back(idx[i]);
        viz[idx[i]] = 1;
        aux += a[idx[i]];
        int cate = (n + 1) / 2 - i - 1;
        for(int j = i; j + cate < n; j++) {
            if(aux + sp[j + cate] - sp[j] >= (sum + 1) / 2) {
                for(int s = j + 1; s <= j + cate; s++) {
                    answer.push_back(idx[s]);
                    viz[idx[s]] = 1;
                }
                for(int s = 0; s < n; s++) {
                    if(!viz[s]) {
                        answer.push_back(s);
                    }
                }
                return answer;
            }
        }
    }

    return {-1};
}
