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 rep(i,  x,  y) for(int i = x; i <= y; i ++)
#define ll unsigned long long
#define X first
#define Y second
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define ls rt << 1
#define rs rt << 1 | 1
#define INF 0x3f3f3f3f
#define mod 1000000007
using namespace std;
inline ll read()
{
	ll x = 0, f = 1; char ch = getchar();
	while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); }
	while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); }
	return x * f;
}
const int N = 2e5 + 10;
inline int lowbit(int x){return x&-x;}
char s[N];
int n;
bool sc[N];
int seg[N << 2], lt[N << 2], cx[27] ,cxb[27];
stack<int> sta[27];
inline void push_down(int rt)
{
	seg[rt] += lt[rt];
	lt[ls] += lt[rt];
	lt[rs] += lt[rt];
	lt[rt]=0;
}
inline int query(int x,int l,int r,int rt)
{
	push_down(rt);
	if(x == l && x == r) return seg[rt];
	int mid = l + r >> 1;
	if(mid >= x) return query(x, l, mid, ls);
	else return query(x, mid + 1, r, rs);
}
inline void xg(int v, int x, int y, int l, int r, int rt)
{
	push_down(rt);
	if(x == l && y == r)
	{
		lt[rt] += v;
		return;
	}
	int mid = l + r >> 1;
	if(mid < x) xg(v, x, y, mid + 1, r, rs);
	else if(mid >= y) xg(v, x, y, l, mid, ls);
	else
	{
		xg(v, x, mid, l, mid, ls);
		xg(v, mid + 1, y, mid + 1, r, rs);
	}
}
int main()
{
	scanf("%s", s);
	n = strlen(s);
	int f = 0;
	rep(i, 0, n - 1) cx[s[i] - 'a'] ++;
	rep(i, 0, 26) if(cx[i] & 1) f ++;
	if(n % 2 != f)
	{
		puts("-1");
		exit(0);
	}
	f = 0;
	rep(i, 0, n/2 - 1)
	{
		cxb[s[i] - 'a'] ++;
		if(cx[s[i] - 'a'] % 2 == 1 && cxb[s[i] - 'a'] * 2 > cx[s[i] - 'a']) f = 1;
	}
	if(f)
	{
		rep(i, 0, n/2 - 1) swap(s[i], s[n - i - 1]);
	} 
	rep(i, 0, n - 1) sta[s[i] - 'a'].push(i);
	ll ans = 0;
	int use = n / 2;
	for(int i = 0; use; i ++)
	{
		if(sc[i]) continue;
		int pla = sta[s[i] - 'a'].top();
		sc[pla] = 1;
		sta[s[i] - 'a'].pop();
		xg(-1, pla + 1, n - 1, 0, n - 1, 1);
		ans += n - (n / 2 - use) - 1 - pla - query(pla, 0, n - 1, 1);
		use --;
	}
	cout << ans;
	return 0;
}