Language: C++

#include <iostream>
#include <vector>
#include <utility>
#include <string>
#include <map>
#include <algorithm>
#include <set>
#include <queue>
#include <functional>
#include <cmath>
#define rep(i,n) for(int i = 0; i < (int)(n); i++)
#define show(x) cerr << #x << " = " << x << endl;
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
ll mergecount(vector<int> &a) {
  ll count = 0;
  int n = a.size();
  if (n > 1) {
    vector<int> b(a.begin(), a.begin() + n/2);
    vector<int> c(a.begin() + n/2, a.end());
    count += mergecount(b);
    count += mergecount(c);
    for (int i = 0, j = 0, k = 0; i < n; ++i)
      if (k == c.size())       a[i] = b[j++];
      else if (j == b.size())  a[i] = c[k++];
      else if (b[j] <= c[k])   a[i] = b[j++];
      else                   { a[i] = c[k++]; count += n/2 - j; }
  }
  return count;
}
int main(){
    string S;
    cin >> S;
    int N = (int) S.size();
    vector<int>A;
    int cnt[26] = {};
    rep(i,N){
        A.push_back(S[i]-'a');
        cnt[A[i]]++;
    }
    int man = -1;
    rep(i,26){
        if(cnt[i]%2){
            if(man != -1){
                cout << -1 << endl;
                return 0;
            }
            man = i;
        }
    }
    assert(man == -1);
    ll ans = 0;
    int k = 0;
    int cnt2[26] = {};
    vector<int>B;
    vector<bool>used(N,false);
    rep(i,N){
        if(cnt[A[i]]%2){
            if(cnt[A[i]]/2>cnt2[A[i]]){
                ans += k;
                B.push_back(A[i]);
                cnt2[A[i]]++;
                used[i] = true;
            }else{
                k++;
            }
        }else{
            if(cnt[A[i]]/2>cnt2[A[i]]){
                ans += k;
                B.push_back(A[i]);
                cnt2[A[i]]++;
                used[i] = true;
            }else{
                k++;
            }
        }
    }
    k = 0;
    vector<int>C;
    rep(i,N){
        if(cnt[A[i]]%2){
            if(!used[i])ans += 0;
            else C.push_back(A[i]);
        }else if(!used[i]){
            k++;
            C.push_back(A[i]);
        }
    }
    k = 0;
    rep(i,N){
        if(cnt[A[i]]%2){
            if(!used[i]){
                ans += k;
                break;
            }
            // else C.push_back(A[i]);
        }else if(!used[i]){
            k++;
            // C.push_back(A[i]);
        }
    }
    reverse(C.begin(), C.end());
    // cerr << ans << endl;
    // cerr << B.size() <<" " << C.size()  << endl;
    // rep(i,N/2){
    //     cout << B[i] << " ";
    // }
    // cout << endl;
    // rep(i,N/2){
    //     cout << C[i] <<  " " ;
    // }
    // cout << endl;
    vector<int>D[26];
    rep(i,N/2){
        D[B[i]].push_back(i);
    }
    rep(i,26)reverse(D[i].begin(), D[i].end());
    vector<int>E;
    rep(i,N/2){
        E.push_back(D[C[i]].back());
        D[C[i]].pop_back();
    }
    // rep(i,N/2){
    //     cerr << E[i] << "-- ";
    // }
    // cout << endl;
    cout << ans + mergecount(E) << endl;
}

Compiler output:
a.cc: In function 'int main()':
a.cc:52:5: error: 'assert' was not declared in this scope
   52 |     assert(man == -1);
      |     ^~~~~~
a.cc:11:1: note: 'assert' is defined in header '<cassert>'; this is probably fixable by adding '#include <cassert>'
   10 | #include <cmath>
  +++ |+#include <cassert>
   11 | #define rep(i,n) for(int i = 0; i < (int)(n); i++)