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

using namespace std;

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];
    }
    sort(idx.begin(), idx.end(), [&](int x, int y) {
        return a[x] < a[y];
    });

    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 / 2) {
        reverse(idx.begin(), idx.end());
        return idx;
    }

    vector<int> answer;
    queue<int> st;
    aux = 0;
    for(int i = 0; i <= n / 2; i++) {
        answer.push_back(idx[i]);
        viz[idx[i]] = 1;
        aux += a[idx[i]];
        int cate = n / 2 - i;
        for(int j = i + 1; j <= n; j++) {
            if(aux >= (sum + 1) / 2) {
                break;
            }
            if((int)st.size() < cate) {
                st.push(idx[j]);
                viz[idx[j]] = 1;
                aux += a[idx[j]];
            } else {
                aux -= a[st.front()];
                viz[st.front()] = 0;
                st.pop();
                aux += a[idx[j]];
                viz[idx[j]] = 1;
                st.push(idx[j]);
            }
        }

        if((int)st.size() == cate && aux >= (sum + 1) / 2) {
            while(!st.empty()) {
                answer.push_back(st.front());
                st.pop();
            }
            for(int j = 0; j < n; j++) {
                if(!viz[j]) {
                    answer.push_back(j);
                }
            }
            return answer;
        }

        while(!st.empty()) {
            viz[st.front()] = 0;
            aux -= a[st.front()];
            st.pop();
        }
    }

    return {-1};
}
