Language: C++

#pragma comment(linker, "/STACK:1073741824")

#include<iostream>
#include<vector>
#include<map>
#include<set>
#include<deque>
#include<unordered_set>
#include<unordered_map>
#include<algorithm>
#include<cmath>
#include<ctime>
#include<fstream>
#include<iterator>
#include<functional>
#include<bitset>
#include<memory>
#include<sstream>
#include<new>
#include<cassert>
#include<iomanip>

using namespace std::placeholders;

typedef std::vector<int> vi;
typedef std::vector<bool> vb;
typedef long long ll;
typedef long double ld;
typedef std::vector<ll> vll;
typedef std::pair<int, int> pii;
typedef std::pair<ll, ll> pll;
typedef std::pair<bool, bool> pbb;

const int inf = (int)2 * 1e9;
const ll linf = (ll) 1e18 * 8;
const ld eps = 1e-5;
const int mod = (int) 1e9 + 7;

#define it(v) begin(v), end(v)
#define pb push_back
#define eb emplace_back
#define mp std::make_pair
#define fst first
#define scn second
#define sgn(x) (x > 0 ? 1 : (x == 0 ? 0 : -1))

ll res = 0;

vi subarr(vi &arr, int s, int f)
{
	vi ret;
	for (int i = s; i < f; i++)
	{
		ret.pb(arr[i]);
	}
	return ret;
}

vi merge(vi &left, vi &right)
{
	vi ret;
	int pl = 0, pr = 0;
	while (pl != left.size() || pr != right.size())
	{
		if (pl == left.size() || (pr != right.size() && right[pr] < left[pl]))
		{
			res += left.size() - pl;
			ret.pb(right[pr++]);
		}
		else
		{
			ret.pb(left[pl++]);
		}
	}
	return ret;
}

vi solve(vi arr)
{
	if (arr.size() == 1)
		return arr;
	int m = arr.size() / 2;
	return merge(solve(subarr(arr, 0, m)), solve(subarr(arr, m, arr.size())));
}

int main()
{
	std::ios_base::sync_with_stdio(false);
	std::string s;
	std::cin >> s;
	int n = s.size();
	std::map<char, vi> map;
	for (int i = 0; i < n; i++)
	{
		map[s[i]].pb(i);
	}
	bool flag = false;
	vi dest(n);
	std::vector<pii> pairs;
	for (auto i : map)
	{
		if (i.scn.size() % 2 == 1)
		{
			if (flag)
			{
				std::cout << -1;
				return 0;
			}
			flag = true;
			dest[i.scn[i.scn.size() / 2]] = n / 2;
		}
		for (int j = 0; j < i.scn.size() / 2; j++)
		{
			pairs.eb(i.scn[j], i.scn[i.scn.size() - j - 1]);
		}
	}
	std::sort(it(pairs));
	for (int i = 0; i < pairs.size(); i++)
	{
		dest[pairs[i].fst] = i;
		dest[pairs[i].scn] = n - i - 1;
	}
	solve(dest);
	std::cout << res;
}

Compiler output:
a.cc: In function 'vi solve(vi)':
a.cc:83:21: error: no matching function for call to 'merge(vi, vi)'
   83 |         return merge(solve(subarr(arr, 0, m)), solve(subarr(arr, m, arr.size())));
      |                ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
a.cc:59:4: note: candidate: 'vi merge(vi&, vi&)' (near match)
   59 | vi merge(vi &left, vi &right)
      |    ^~~~~
a.cc:59:4: note:   conversion of argument 2 would be ill-formed:
a.cc:83:53: error: cannot bind non-const lvalue reference of type 'vi&' {aka 'std::vector<int>&'} to an rvalue of type 'vi' {aka 'std::vector<int>'}
   83 |         return merge(solve(subarr(arr, 0, m)), solve(subarr(arr, m, arr.size())));
      |                                                ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/14/algorithm:61,
                 from a.cc:10:
/usr/include/c++/14/bits/stl_algo.h:4857:5: note: candidate: 'template<class _IIter1, class _IIter2, class _OIter> _OIter std::merge(_IIter1, _IIter1, _IIter2, _IIter2, _OIter)'
 4857 |     merge(_InputIterator1 __first1, _InputIterator1 __last1,
      |     ^~~~~
/usr/include/c++/14/bits/stl_algo.h:4857:5: note:   candidate expects 5 arguments, 2 provided
/usr/include/c++/14/bits/stl_algo.h:4908:5: note: candidate: 'template<class _IIter1, class _IIter2, class _OIter, class _Compare> _OIter std::merge(_IIter1, _IIter1, _IIter2, _IIter2, _OIter, _Compare)'
 4908 |     merge(_InputIterator1 __first1, _InputIterator1 __last1,
      |     ^~~~~
/usr/include/c++/14/bits/stl_algo.h:4908:5: note:   candidate expects 6 arguments, 2 provided
In file included from /usr/include/c++/14/algorithm:86:
/usr/include/c++/14/pstl/glue_algorithm_defs.h:412:1: note: candidate: 'template<class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _ForwardIterator, class _Compare> __pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator2> std::merge(_ExecutionPolicy&&, _ForwardIterator1, _ForwardIterator1, _ForwardIterator2, _ForwardIterator2, _ForwardIterator, _Compare)'
  412 | merge(_ExecutionPolicy&& __exec, _ForwardIterator1 __first1, _ForwardIterator1 __last1, _ForwardIterator2 __first2,
      | ^~~~~
/usr/include/c++/14/pstl/glue_algorithm_defs.h:412:1: note:   candidate expects 7 arguments, 2 provided
/usr/include/c++/14/pstl/glue_algorithm_defs.h:417:1: note: candidate: 'template<class _ExecutionPolicy, class _ForwardIterator1, class _ForwardIterator2, class _ForwardIterator> __pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, _ForwardIterator2> std::merge(_ExecutionPolicy&&, _ForwardIterator1, _ForwardIterator1, _ForwardIterator2, _ForwardIterator2, _ForwardIterator)'
  417 | merge(_ExecutionPolicy&& __exec, _ForwardIterator1 __first1, _ForwardIterator1 __last1, _ForwardIterator2 __first2,
      | ^~~~~
/usr/include/c++/14/pstl/glue_algorithm_defs.h:417:1: note:   candidate expects 6 arguments, 2 provided