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: Accepted)

#include <iostream>
#include <iomanip>
#include <fstream>
#include <sstream>
#include <string>
#include <cmath>
#include <cstdio>
#include <cctype>
#include <ctime>
#include <cstdlib>
#include <cstring>
#include <cassert>
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <complex>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cwchar>
#include <cwctype>
#include <exception>
#include <locale>
#include <numeric>
#include <new>
#include <stdexcept>
#include <limits>
#include <valarray>
#include <set>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <list>
#include <utility>
#include <bitset>
#include <algorithm>
#include <functional>
#define rep(i,n) for(int i=0;i<(n);i++)
#define rep1(i,n) for(int i=1;i<=(n);i++)
#define MP make_pair
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int INF=1e9+7;
//fenwick tree
int n,tree[200005];
int query(int x){
	x++;
	int ans=0;
	while(x>0){
		ans+=tree[x];
		x-=x&(-x);
	}
	return ans;
}
void modify(int id,int v){
	id++;
	while(id<=n){
		tree[id]+=v;
		id+=id&(-id);
	}
}
//main part
char S[200005];
vector<int> ch[26];
vector<int> h1;
int grp[200005],opp[200005];
int main(){
	scanf("%s",S);
	n=strlen(S);
	rep(k,n)ch[S[k]-'a'].push_back(k);
	int cnt=0;
	rep(k,26)if(ch[k].size()&1)cnt++;
	if(cnt>1){
		printf("-1\n");
		return 0;
	}
	rep(i,26){
		int j=0;
		for(;j+j+1<ch[i].size();j++)grp[ch[i][j]]=0;
		if(j+j+1==ch[i].size())grp[ch[i][j++]]=-1;
		for(;j<ch[i].size();j++){
			grp[ch[i][j]]=1;
			opp[ch[i][j]]=ch[i][ch[i].size()-j-1];
		}
	}
	int sz0=0;
	long long ans=0LL;
	rep(k,n){
		if(grp[k]==1)h1.push_back(opp[k]);
		else if(grp[k]==0){
			sz0++;
			ans+=h1.size();
		}else ans+=h1.size()+((n-1)>>1)-sz0;
	}
	rep(k,h1.size()){
		ans+=query(h1[k]);
		modify(h1[k],1);
	}
	printf("%lld\n",ans);
	return 0;
}