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 <bits/stdc++.h>
#include <iostream>
#include <cstdio>
#include <vector>
#include <queue>
#include <map>
#include <cstring>
#include <string>
#include <math.h>
#include <algorithm>
#define rep(i, n) for(int i = 0; i < (int)n; i++)
#define repp(i, m, n) for(int i = m; i < (int)n; i++)
#define reps(i, n) for(int i = 1; i <= (int)n; i++)
#define pb push_back
const int    INF = 100000000;
const double EPS = 1e-10;
const int    MOD = 1000000007;
using namespace std;
typedef long long ll;
typedef pair<int, int> pai;
typedef pair<ll,ll> pal;

ll tento;
int retu1[100010], retu[100010], sagyo[100010];

void merge(int i, int j)
{
	if(j==i+1 && retu[i]<retu[j]) tento++, swap(retu[i], retu[j]);
	else if(j>i+1)
	{
		int m=(i+j)/2;
		merge(i, m);
		merge(m+1, j);
		for(int k=i; k<=j; k++) sagyo[k]=retu[k];
		int l=i, r=m+1;
		while(l<=m || r<=j)
		{
			if(l==m+1)
			{
				retu[r]=sagyo[r];
				r++;
			} 
			else if(r==j+1)
			{
				retu[l+r-m-1]=sagyo[l];
				tento+=r-m-1;
				l++;
			}
			else
			{
				if(sagyo[l]>sagyo[r])
				{
					retu[l+r-m-1]=sagyo[l];
					tento+=r-m-1;
					l++;
				}
				else
				{
					retu[l+r-m-1]=sagyo[r];
					r++;
				}
			}
		}
	}
}



int main()
{
	vector<int> alph[26];
	string s;
	cin >> s;
	int n=s.size();
	rep(i, n) alph[(int)s[i]-(int)'a'].pb(i);
	int muri=0, mu;
	rep(i, 26) if(alph[i].size()%2==1) muri++, mu=i;
	if(muri>1)
	{
		cout << "-1" << endl;
		return 0;
	}
	int kosu[26]={};
	int cnt=0;
	rep(i, n)
	{
		int tmp=(int)s[i]-(int)'a';
		kosu[tmp]++;
		if(2*kosu[tmp]<=alph[tmp].size())
		{
			retu[cnt]=alph[tmp][alph[tmp].size()-kosu[tmp]];
			retu1[cnt]=retu[cnt];
			cnt++;
		}
	}
	merge(0, cnt-1);
	ll ans=tento;
	rep(i, cnt) ans+=n-1-i-retu1[i];
	if(muri==1)
	{
		muri=alph[mu][alph[mu].size()/2];
		rep(i, cnt)
		{
			if(i>=muri && retu1[i]>muri) ans++;
		}
	}
	cout << ans << endl;
	return 0;
}