Language: C++

#include <bits/stdc++.h>

using namespace std;

using ll = long long;

//1-indexであることに注意
struct BIT {
    int n;
    vector<int> data;

    BIT(int n) : n(n), data(n + 1, 0) {}

    int sum(int i) {
        int s = 0;
        while (i > 0) {
            s += data[i];
            i -= i & -i;
        }
        return s;
    }

    void add(int i, int x) {
        while (i <= n) {
            data[i] += x;
            i += i & -i;
        }
    }
};

ll solve(string& L, string& R) {
    vector< deque<int> > hoge(26);
    for (int i = 0; i < R.size(); i++) {
        hoge[R[i] - 'a'].push_back(i + 1);
    }
    vector<int> foo;
    for (char c : L) {
        foo.push_back(hoge[c - 'a'].front());
        hoge[c - 'a'].pop_front();
    }
    BIT b(foo.size());
    ll res = 0;
    for (int i = 0; i < foo.size(); i++) {
        res += i - b.sum(foo[i]);
        b.add(foo[i], 1);
    }
    return res;
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    string s;
    cin >> s;
    vector<int> cnt(26, 0);
    for (char c : s) cnt[c - 'a']++;
    int n = s.length();
    int hoge = 0;
    int idx = -1;
    for (int i = 0; i < 26; i++) {
        hoge += cnt[i] % 2;
        if (cnt[i] % 2) idx = i;
    }
    if (hoge > n % 2) {
        cout << -1 << endl;
        return 0;
    }

    for (int i = 0; i < 26; i++) {
        cnt[i] = cnt[i] / 2;
    }

    if (idx != -1) {
        for (int i = 0; i < n; i++) {
            if (s[i] - 'a' == idx) {
                idx = i;
                break;
            }
        }
    }

    ll ans = 0;
    string L = "", R = "";
    for (int i = 0, cur = 0; i < n; i++) {
        if (idx == i) {
            ans += i - cur;
        } else if (cnt[s[i] - 'a']) {
            cnt[s[i] - 'a']--;
            ans += i - cur;
            L += s[i];
            cur++;
        } else {
            R += s[i];
        }
    }
    
    //reverse(R.begin(), R.end());
    ll bar = solve(L, R);
    reverse(R.begin(), R.end());
    bar = min(hoge, solve(L, R));
    ans += bar;
    cout << ans << endl;
    return 0;
}

Compiler output:
a.cc: In function 'int main()':
a.cc:100:14: error: no matching function for call to 'min(int&, ll)'
  100 |     bar = min(hoge, solve(L, R));
      |           ~~~^~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/14/algorithm:60,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:51,
                 from a.cc:1:
/usr/include/c++/14/bits/stl_algobase.h:233:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::min(const _Tp&, const _Tp&)'
  233 |     min(const _Tp& __a, const _Tp& __b)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:233:5: note:   template argument deduction/substitution failed:
a.cc:100:14: note:   deduced conflicting types for parameter 'const _Tp' ('int' and 'll' {aka 'long long int'})
  100 |     bar = min(hoge, solve(L, R));
      |           ~~~^~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:281:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::min(const _Tp&, const _Tp&, _Compare)'
  281 |     min(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:281:5: note:   candidate expects 3 arguments, 2 provided
In file included from /usr/include/c++/14/algorithm:61:
/usr/include/c++/14/bits/stl_algo.h:5686:5: note: candidate: 'template<class _Tp> constexpr _Tp std::min(initializer_list<_Tp>)'
 5686 |     min(initializer_list<_Tp> __l)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5686:5: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5696:5: note: candidate: 'template<class _Tp, class _Compare> constexpr _Tp std::min(initializer_list<_Tp>, _Compare)'
 5696 |     min(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5696:5: note:   template argument deduction/substitution failed:
a.cc:100:14: note:   mismatched types 'std::initializer_list<_Tp>' and 'int'
  100 |     bar = min(hoge, solve(L, R));
      |           ~~~^~~~~~~~~~~~~~~~~~~