#pragma GCC optimize("Ofast")

#include "partition.h"
#include <iostream>
#include <vector>

using namespace std;

vector<int> solve(vector<int> a) {
    int n = (int)a.size();
    long long S = 0;
    for (int i : a) S += i;

    vector<int> b;
    b.reserve(n);

    vector<vector<int> > buk(26);
    for (int i = 0; i < n; i++) buk[__lg(a[i])].push_back(i);
    for (int i = 0; i <= 25; i++) for (int x : buk[i]) b.push_back(1 << i);

    int len = n >> 1;
    long long sum = 0;
    vector<bool> left(n);
    for (int i = 0; i < len; i++) sum += b[i], left[i] = 1;
    for (int i = 0; i < len; i++) {
        if ((sum << 1) >= S) break;
        left[i] = 0;
        left[n - i - 1] = 1;
        sum -= b[i];
        sum += b[n - i - 1];
    }
    vector<int> ans;
    ans.reserve(n);
    for (int i = 0; i < n; i++) if (left[i]) ans.push_back(buk[b[i]].back()), buk[b[i]].pop_back();
    for (int i = 0; i < n; i++) if (!left[i]) ans.push_back(buk[b[i]].back()), buk[b[i]].pop_back();
    return ans;
}
