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 <cassert>
#include <climits>
#include <bitset>
#include <stack>
#include <queue>
#include <iomanip>
#include <limits>
#include <string>
#include <cmath>
#include <set>
#include <map>
#include <math.h>
#include <algorithm>
#include <vector>
#include <string.h>

using namespace std;
typedef long long ll;

typedef pair<ll,ll> P;
long long int INF = 1e18;
double Pi = 3.1415926535897932384626;
long long int mod = 1000000007;

vector<ll> G[500005];
vector<P> tree[500010];
priority_queue <ll> pql;
priority_queue <P> pqp;
//big priority queue
priority_queue <ll,vector<ll>,greater<ll> > pqls;
priority_queue <P,vector<P>,greater<P> > pqps;
priority_queue <P,vector<P>,greater<P> > pqps2;
//small priority queue
//top pop

int dx[8]={1,0,-1,0,1,1,-1,-1};
int dy[8]={0,1,0,-1,1,-1,-1,1};
char dir[] = "RULD";
ll bit[500005];
//↓,→,↑,←


#define p(x) cout<<x<<endl;
#define el cout<<endl;
#define pe(x) cout<<x<<" ";
#define ps(x) cout<<fixed<<setprecision(15)<<x<<endl;
#define pu(x) cout<<x;
#define re(i,a,b) for(i=a;i<=b;i++);
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define deba(x) cout<< #x << " = " << x <<endl

ll rui(ll abc,ll bed){
    //abcのbed乗を計算する
    if(bed==0){return 1;}
    else{
        ll ced = rui(abc,bed/2);
        ced *= ced;
        ced %= mod;
        if(bed%2==1){ced*=abc; ced%=mod;}
        return ced;
    }
}
ll gcd(ll number1,ll number2){
	if(number1 > number2){
		swap(number1,number2);
	}
	if(number1 == 0 || number1 == number2){
		return number2;
	}else{
		return gcd(number2 % number1,number1);
	}
}

ll i,j,k,ii,jj,r,l;
ll n,m,num,ans;
ll a,b,c,d,e,g,h;
ll x[500005],y[500005],z[500005];
char s[500005];
vector<ll> pos[40];
ll kazu[40];
ll lside[40],rside[40];
ll dist;

ll bitsum(ll i){
	ll s = 0;
	while(i>0){
		s += bit[i];
		i -= i & -i;
	}
	return s;
}
void bitadd(ll b,ll x){
	while(b <= n){
		
		bit[b] += x;
		b += b & -b;
	}
}

int main(){
    cin >> s;
	n = strlen(s);
	for(i=0;i<n;i++){
		pos[s[i] - 'a'].pb(i);
	}
	for(i=0;i<26;i++){
		a = pos[i].size();
		if(a%2 == 1)b++;
		lside[i] = 0;
		rside[i] = a - 1;
		kazu[i] = a;
	}
	if(b >= 2){
		p(-1);
		return 0;
	}
	while(1){
		//p("hoi");
		num = INF;
		ll chara = -1;
		ll ld,rd;
		for(i=0;i<26;i++){			
			if(kazu[i] <= 1)continue;
			l = pos[i][lside[i]];
			r = pos[i][rside[i]];
			//pe(l);p(r);
			l -= bitsum(l+1);
			r -= bitsum(r+1);
			//char a[10];
			//a[0] = (char)('a'+i);
			//pe(a[0]);
			//pe(l);p(r);
			dist = l + (n - 1) - r;
			if(num > dist){
				ld = l;
				rd = r;
				num = dist;
				chara = i;
			}
		}
		if(num == INF || chara == -1)break;
		//l = pos[chara][lside[chara]];
		//r = pos[chara][rside[chara]];
		//l -= bitsum(l+1);
		//r -= bitsum(r+1);
		//p(chara);
		//p(num);
		//if(num == INF || chara == -1)break;
		//chara文字を消す
		ans += num;
		lside[chara]++;
		rside[chara]--;
		kazu[chara] -= 2;
		bitadd(ld+1,1);
		bitadd(rd+1,1);
		n -= 2;
	}
	//p("hoi");
	p(ans);
	
    return 0;
}