#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 <int> f, s;
  vector <pair <int, int>> a;
  for (int i = 0; i < n; i++) {
    a.emplace_back(A[i], i);
  }
  vector <pair <int, int>> b;
  vector <pair <int, int>> c;
  for (int i = 0; i < n; i++) {
    if (!c.empty() && c.back().first != a[i].first) {
      for (int j = 0; j < (int)c.size() - (int)c.size() % 2; j++) {
        if (j % 2 == 0) f.push_back(c[j].second);
        else s.push_back(c[j].second);
      }
      if ((int)c.size() % 2 == 1) b.push_back(c.back());
      c.clear();
    }
    c.push_back(a[i]);
  }
  for (int j = 0; j < (int)c.size() - (int)c.size() % 2; j++) {
    if (j % 2 == 0) f.push_back(c[j].second);
    else s.push_back(c[j].second);
  }
  if ((int)c.size() % 2 == 1) b.push_back(c.back());
  a = b;
  ll sum = 0;
  for (auto [x, i] : a) {
    sum += x;
  }
  if (a.size() % 2 == 1) {
    for (int i = 0; i < (int)a.size(); i++) {
      if (i % 2 == 0) f.push_back(a[i].second);
      else s.push_back(a[i].second);
    }
  } else {
    ll cur = 0;
    for (int i = 0; i < (int)a.size(); i += 2) {
      cur += a[i].first;
    }
    int i = (int)a.size() - 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)) {
        f.push_back(a[j].second);
      } else {
        s.push_back(a[j].second);
      }
    }
  }
  vector <int> ans;
  for (int i : f) {
    ans.push_back(i);
  }
  for (int i : s) {
    ans.push_back(i);
  }
  return ans;
}
