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 <stdio.h>  
#include <algorithm>  
#include <assert.h>
#include <bitset>
#include <cmath>  
#include <complex>  
#include <deque>  
#include <functional>  
#include <iostream>  
#include <limits.h>  
#include <map>  
#include <math.h>  
#include <queue>  
#include <set>  
#include <stdlib.h>  
#include <string.h>  
#include <string>  
#include <time.h>  
#include <unordered_map>  
#include <unordered_set>  
#include <vector>  
#define ll long long
#define rep2(i,a,b) for(ll i=a;i<=b;++i)
#define rep(i,n) for(ll i=0;i<n;i++)
#define rep3(i,a,b) for(ll i=a;i>=b;i--)
#define REP(e,v) for(auto e:v)
#define pii pair<int,int>
#define pll pair<ll,ll>
#define tii tuple<int,int,int>
#define pq priority_queue<int>
#define pqg priority_queue<int,vector<int>,greater<int>>
#define pb push_back
#define edge(v,a,b) v[a].pb(b);v[b].pb(a);
#define MAX_V 400010
#define vec vector<int>
#define vecll vector<ll>
#define vecpii vector<pii>
#define endl "\n"
#define ALL(c) (c).begin(),(c).end()
using namespace std;
int in() {int x;scanf("%d",&x);return x;}
ll lin() {ll x;scanf("%lld",&x);return x;}
void makegraph(int x,vector<vec> &v){rep(i,x-1){int a=in(),b=in();v[a].pb(b);v[b].pb(a);}}
template<typename A, size_t NNN, typename T>
void Fill(A (&array)[NNN], const T &val){
    fill( (T*)array, (T*)(array+NNN), val );
}
#define INF 1e9+7
#define LLINF 1e18+7
ll MOD=1e9+7;
#define N 255050

ll n,m,k;
ll a[N];
template<typename T>
struct BIT{
    vector<T> data;

    BIT(int size){
        data.assign(++size,0);
    }
    T sum(int k){
        T ret=0;
        for(++k;k>0;k-=k&-k) ret+=data[k];
        return (ret);
    }
    void add(int k,T x){
        for(++k;k<data.size();k+=k&-k)data[k]+=x;
    }
};
int mem[N];
main(){
    string s;
    cin>>s;
    n=s.size();
    vector<vector<ll>> v(26);
    vector<ll> t;
    rep(i,n){
        v[(ll)(s[i]-'a')].pb(i);
        t.pb((ll)(s[i]-'a'));
    }
    ll res=0;
    rep(i,26){
        res+=v[i].size()%2;
    }
    if(res>1){cout<<-1;return 0;}
    vector<ll> now(26,0);
    vector<ll> pre,suf,prenum;
    rep(i,n){
        if((v[t[i]].size())/2<=now[t[i]]){
            continue;
        }
        pre.pb(v[t[i]][now[t[i]]++]);
        prenum.pb(t[i]);
    }
    rep2(i,1,pre.size()){
        ll p=prenum[pre.size()-i];
        suf.pb(v[p][now[p]++]);
    }
    BIT<ll> BIT(n+1);
    ll ans=0;
    
    rep(i,pre.size()){
        ans+=BIT.sum(pre[i]);
        BIT.add(pre[i],1);
    }
    if(res){
        ll q;
        rep(i,26)if(v[i].size()%2){q=i;break;}
        ll num=v[q][(v[q].size()-1)/2];
        ans+=BIT.sum(num);
        BIT.add(num,1);
    }
    rep(i,suf.size()){
        ans+=BIT.sum(suf[i]);;
        BIT.add(suf[i],1);
    }
    cout<<(n*(n-1)/2-ans);
}