Problem:
Score : 800 points

Problem Statement

You are given a string S consisting of lowercase English letters.
Determine whether we can turn S into a palindrome by repeating the operation of swapping two adjacent characters. If it is possible, find the minimum required number of operations.

Constraints

1 \leq |S| \leq 2 × 10^5

S consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:

S

Output

If we cannot turn S into a palindrome, print -1. Otherwise, print the minimum required number of operations.

Sample Input 1

eel

Sample Output 1

1

We can turn S into a palindrome by the following operation:

Swap the 2-nd and 3-rd characters. S is now ele.

Sample Input 2

ataatmma

Sample Output 2

4

We can turn S into a palindrome by the following operation:

Swap the 5-th and 6-th characters. S is now ataamtma.

Swap the 4-th and 5-th characters. S is now atamatma.

Swap the 3-rd and 4-th characters. S is now atmaatma.

Swap the 2-nd and 3-rd characters. S is now amtaatma.

Sample Input 3

snuke

Sample Output 3

-1

We cannot turn S into a palindrome.

Solution in C++14 (GCC 5.4.1) (judge verdict: Wrong Answer)

#include <iostream>
#include <iomanip>
#include <vector>
#include <algorithm>
#include <numeric>
#include <functional>
#include <cmath>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <sstream>
#include <string>
#define _repargs(_1,_2,_3,name,...) name
#define _rep(i,n) repi(i,0,n)
#define repi(i,a,b) for(int i=(int)(a);i<(int)(b);++i)
#define rep(...) _repargs(__VA_ARGS__,repi,_rep,)(__VA_ARGS__)
#define all(x) (x).begin(),(x).end()
#define mod 1000000007
#define inf 2000000007
#define mp make_pair
#define pb push_back
typedef long long ll;
using namespace std;
template <typename T>
inline void output(T a, int p = 0) {
    if(p) cout << fixed << setprecision(p)  << a << "\n";
    else cout << a << "\n";
}
// end of template

struct bit {
    vector<ll> v; // 部分和をbitで管理 仮想配列はa[1...n]
    bit(int n){ // bit(int n) : v(n + 1) {}
        v.resize(n + 1);
    }
    
    ll sum(int i){ // 仮想配列a[1]~a[i]の和を求める
        ll ret = 0;
        for(; i > 0; i -= i & -i){ // i & -i: iの1が立っている最高位bitをの2^を返す
            ret += v[i];
        }
        return ret;
    }
    
    void add(int i, ll w) { // index:iにwを加える
        for (; i < v.size(); i += i & -i) {
            v[i] += w;
        }
    }
};

int main() {
    cin.tie(0);
    ios::sync_with_stdio(0);
    // source code
    string s;
    cin >> s;
    int N = (int)s.size();
    vector<vector<int>> A(26);
    rep(i, s.size()){
        A[s[i] - 'a'].pb(i);
    }
    
    int odds = 0;
    int odd_ind = -1;
    rep(i, 26){
        if(A[i].size() % 2 == 1) {
            odds++;
            odd_ind = i;
        }
    }
    
    if(s.size() % 2 == 0 && odds > 0){
        output(-1);
        return 0;
    }
    if(s.size() % 2 == 1 && odds != 1){
        output(-1);
        return 0;
    }
    
    vector<ll> Q(N);
    if(s.size() % 2 == 1){
        int center = A[odd_ind][A[odd_ind].size() / 2];
//        ans = abs(N / 2 - center);
        Q[N / 2] = center;
    }
    
    
    
    
    vector<pair<ll, ll>> P;
    rep(i, 26){
        rep(j, A[i].size() / 2){
            P.pb({A[i][j], A[i][A[i].size() - 1 - j]});
        }
    }
    
    sort(all(P), [](const pair<ll, ll> &a, const pair<ll, ll> &b){
        return b.first - a.first + a.second - b.second > 0;
    });
//
//    rep(i, P.size()){
//        ans += abs(P[i].first - i) + abs(P[i].second - (N - 1 - i));
//    }
//
    
    
    rep(i, P.size()){
        Q[i] = P[i].first + 1;
        Q[N - 1 - i] = P[i].second + 1;
    }
    
    
    bit B(N);
    
    ll ans = 0;
    rep(i, N){
        ans += i - B.sum(Q[i]);
        B.add(Q[i], 1);
    }
    output(ans);
    
    return 0;
}