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: Runtime Error)

#include <bits/stdc++.h>

using namespace std;
#define _for(i,j,N) for(int i = (j);i < (N);i++)
#define _rep(i,j,N) for(int i = (j);i <= (N);i++)
#define ALL(x) x.begin(),x.end()
#define PB push_back
#define LL long long

template<typename T>
ostream& operator<<(ostream& os,const vector<T>& v) {
    _for(i,0,v.size()) os << v[i] << " ";
    return os;
}

template<typename T>
ostream& operator<<(ostream& os,const set<T>& v){
    for(typename set<T>::iterator it = v.begin();it != v.end();it++)
    os << *it <<" ";
    return os;
}

struct lines{
    int l,r;
    lines(int l,int r):l(l),r(r){;}
    bool operator<(const lines &b){
        return l < b.l;
    }
};

const int maxn = 1e5+5;
int A[maxn];
int C[maxn];
vector<lines> vl;
vector<int> alpha[26];
string s;

int lowbit(int x){return x&(-x);}

void add(int a,int v){
    for(int i = a; i < maxn;i += lowbit(i)){
        C[i] += v;
    }
}
int sum(int a){
    int ret = 0;
    for(int i = a;i > 0;i-=lowbit(i)){
        ret += C[i];
    }
    return ret;
}

LL rev_p(int sz){
    LL ret = 0;
    _rep(i,1,sz){
        ret += sum(A[i]-1);
        //cout <<ret<<endl;
        add(A[i],1);
    }
    return ret;
}

int main()
{
    cin >> s;
    int sz = s.size();


    _for(i,0,sz){
        int d = s[i] - 'a';
        alpha[d].push_back(i);
    }
    int odd = 0;
    _for(i,0,26){
        int szi = alpha[i].size();
        if(szi % 2){
            odd++;
            _rep(j,0,szi/2){
                vl.push_back(lines(alpha[i][j]+1,alpha[i][szi-1-j]+1));
            }
        }
        else{
            _for(j,0,szi/2){
                vl.push_back(lines(alpha[i][j]+1,alpha[i][szi-1-j]+1));
            }
        }
    }
    if(odd >= 2){
            cout << -1 <<endl;
            return 0;
    }

    sort(ALL(vl));
    int cnt = 1;
    int rev = sz;
    _for(i,0,vl.size()){
        lines &ll = vl[i];
        if(ll.l != ll.r){
            A[cnt++] = ll.l;
            A[rev--] = ll.r;
        }
        else A[cnt++] = ll.l;
    }

    reverse(A+1,A+sz+1);

    //_for(i,1,4) cout << A[i] << endl;

    cout << rev_p(sz) << endl;

    return 0;
}