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>
#define ll long long
#define ull unsigned long long
#define MP make_pair
#define PII pair<int, int>
#define PLL pair<ll, ll>
#define PLI pair<ll, int>
#define PIL pair<int, ll>
#define STR string
#define Fst first
#define Snd second
#define SHN 1e9
#define LSHN 1e18
using namespace std;
string s;
int tac[30];
int n,len;
set<int> pos[30];
int st[450],ed[450];
bool visit[200010],sti[200010],edi[200010];
ll res;
int dist(int xt,int yt)
{
	if (xt==-1e9 || xt==1e9) return 1e9;
	int x=xt/len,xi=xt%len;
	int t=0;
	for (int j=0;j<x;j++) t-=st[j];
	for (int j=0;j<=xi;j++) t-=sti[x*len+j];
	x=(xt+t)/len,xi=(xt+t)%len;
	for (int j=0;j<x;j++) t+=ed[j];
	for (int j=0;j<=xi;j++) t+=edi[x*len+j];
	//cout <<xt<<" "<<x<<" "<<xi<<" "<<t<<" ####\n";
	return abs(yt-xt)+t;
}
int main()
{
	cin >>s;
	if (n==1)
	{
		cout <<"0";
		return 0;
	}
	n=(int)s.size(),len=(int)sqrt(n);
	for (int i=0;i<n;i++)
	{
		tac[s[i]-'a']++;
		pos[s[i]-'a'].insert(i);
	}
	int t=0,ti;
	for (int i=0;i<26;i++)
	{
		t+=tac[i]%2;
		if (tac[i]%2) ti=i;
		pos[i].insert(-1e9);
		pos[i].insert(1e9);
	}
	if (t>1)
	{
		cout <<"-1";
		return 0; 
	}
	if (n%2)
	{
		set<int>::iterator it=pos[ti].lower_bound(n/2),jt=it;
		jt--;
		if (*it-n/2<n/2-*jt)
		{
			res+=*it-n/2;
			visit[*it]=1;
			st[*it/len]++,ed[n/2/len]++;
			sti[*it]=edi[n/2]=1;
			pos[ti].erase(it);
		}
		else
		{
			res+=n/2-*jt;
			visit[*jt]=1;
			st[*jt/len]++,ed[n/2/len]++;
			sti[*jt]=edi[n/2]=1;
			pos[ti].erase(jt);
		}
	}
	for (int i=0,tac=0;tac<n/2 && i<n;i++)
	{
		if (visit[i]) continue;
		set<int>::iterator it=pos[s[i]-'a'].lower_bound(n-tac-1),jt=it;
		if (*it==i) it++;
		jt--;
		if (*jt==i) jt--;
		int x=dist(*it,n-tac-1),y=dist(*jt,n-tac-1);
		if (x<y)
		{
			//cout <<i<<" "<<x<<" "<<*it<<" "<<n-tac-1<<" ###### x\n";
			res+=x;
			visit[*it]=1;
			st[*it/len]++,ed[(n-tac-1)/len]++;
			sti[*it]=edi[n-tac-1]=1;
			pos[s[i]-'a'].erase(it);
		}
		else
		{
			//cout <<i<<" "<<y<<" "<<*jt<<" "<<n-tac-1<<" ###### y\n";
			res+=y;
			visit[*jt]=1;
			st[*jt/len]++,ed[(n-tac-1)/len]++;
			sti[*jt]=edi[n-tac-1]=1;
			pos[s[i]-'a'].erase(jt);
		}
		tac++;
	} 
	cout <<res;
	return 0;
}