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: Accepted)

#include<bits/stdc++.h>
#define ALL(c) (c).begin(),(c).end()
#define EACH(i,c) for(auto i=(c).begin();i!=(c).end();++i)
using namespace std;
#define int long long

template<typename T>
struct Fenwick{
    vector<T> bit;
    T mod;
    Fenwick(int n){
        bit = vector<T>(n + 1);
        mod = 0;
    }
    void add(int i, T v){
        int n = bit.size();
        ++i;
        while(i < n){
            bit[i] += v;
            i += i & -i;
        }
    }
    T get(int i){
        T res = 0;
        ++i;
        while(i > 0){
            res += bit[i];
            i -= i & -i;
        }
        return res;
    }
};

int func(const string &S, const string &T) {
    int N = S.size();
    Fenwick<int> bit(N + 10);
    vector<queue<int>> v(0x100);
    for(int i = 0; i < N; ++i) {
        v[S[i]].push(i);
    }
    int res = 0;
    for(int i = 0; i < N; ++i) {
        char c = T[i];
        int p = v[c].front();
        v[c].pop();
        int q = p + i - bit.get(p);
//        cerr << c << "," << p << "," << q << ","<< q - i << endl;
        res += q - i;
        bit.add(p, 1);
    }
    return res;
}

void solve(std::string S){
    int N = S.size();
    vector<int> d(0x100);
    for(int i = 0; i < N; ++i) {
        d[S[i]]++;
    }
    int c = 0;
    char ex = 'A';
    for(int i = 0; i < 0x100; ++i) {
        if (d[i] % 2 != 0){
            ++c;
            ex = i;
        }
    }
    if ((N % 2 == 0 && c > 0) || (N % 2 == 1 && c != 1)) {
        cout << -1 << endl;
        return;
    }
    vector<int> e(0x100);
    string T = "", U = "";
    int init = 0;
    for(int i = 0; i < N; ++i) {
        char c = S[i];
        if (e[c] < d[c] / 2) {
            init += i - T.size();
            T += c;
        } else {
            U += c;
        }
        ++e[c];
    }
    if (N % 2 != 0) {
        for(int i = 0; i < U.size(); ++i) {
            if (U[i] == ex) {
                init += i;
                if (i == 0) {
                    U = U.substr(1);
                } else if(i + 1 == U.size()) {
                    U.pop_back();
                } else {
                    U = U.substr(0, i) + U.substr(i + 1);
                }
                break;
            }
        }
    }
//    cerr << T << init << U << endl;
    reverse(ALL(U));

    cout << init + func(T, U) << endl;
}

signed main(){
    std::string S;
    std::cin >> S;
    solve(S);
    return 0;
}