#include "partition.h"

#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi2,popcnt,lzcnt")

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

std::vector<int> solve(std::vector<int> A) {
  int n = A.size();
  vector <pair <int, int>> a;
  a.reserve(n);
  pair <int, int> mx = {-1, -1};
  ll sum = 0;
  for (int i = 0; i < n; i++) {
    mx = max(mx, {A[i], i});
    sum += A[i];
  }
  for (int i = 0; i < n; i++) {
    if (i != mx.second) a.emplace_back(A[i], i);
  }
  a.push_back(mx);
  for (int i = 0; i < n-1; i += 2) {
    if (a[i].first > a[i+1].first) {
      swap(a[i], a[i+1]);
    }
  }
  vector <int> f, s;
  f.reserve(n/2);
  s.reserve(n/2);
  ll cur = 0;
  for (int i = 0; i < n; i += 2) {
    cur += a[i].first;
  }
  int i;
  if (n % 2 == 1) {
    int i = n - 3;
  } else {
    i = n - 2;
  }
  while (cur * 2 < sum) {
    cur += a[i+1].first - a[i].first;
    i -= 2;
  }
  i += 2;
  for (int j = 0; j < (int)a.size(); j++) {
    if (j % 2 == (j >= i) || (n % 2 == 1 && j == n-1)) {
      f.push_back(a[j].second);
    } else {
      s.push_back(a[j].second);
    }
  }
  vector <int> ans;
  ans.reserve(n);
  for (int i : f) {
    ans.push_back(i);
  }
  for (int i : s) {
    ans.push_back(i);
  }
  return ans;
}
