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 <ccomplex>
#include <vector>
#include <cassert>
#include <utility>
#include <algorithm>
#include <string>
#include <deque>
#include <queue>
#include <functional>
#include <cmath>
#include <iomanip>
#include <map>
#include <numeric>
#include <list>
#include <assert.h>
#include <math.h>
#include <valarray>
#include <stdio.h>
#include <algorithm>
#include <set>
#include <complex>
#include <list>
#include <time.h>
#include <stack>
#include <locale>
#include <clocale>
#include <ctype.h>
#include <wchar.h>
#include <random>
#include <vector>

using namespace std;
typedef long long int LL;
typedef pair<long long int, long long int> pii;
typedef pair<double, double> pdd;

#define SORT(c) sort((c).begin(),(c).end())
#define BACKSORT(c) sort((c).begin(),(c).end(),std::greater<LL>())
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)


int xcomp(pair<LL, LL>& a, pair<LL, LL>& b) {
	if (a.first == b.first) {
		return a.second<b.second;
	}
	return a.first < b.first;
}

#define XSORT(c) sort((c).begin(),(c).end(),xcomp)

#define ALL(x) (x).begin(),(x).end()
LL mod = 1000000007;

int main() {
	string S;
	cin >> S;

	if (S.size() % 2 == 0) {
		vector<int> num(26, 0);
		vector<vector<int>> alpha(26, vector<int>(0));
		REP(i, S.size()) {
			num[S[i] - 'a']++;
			alpha[S[i] - 'a'].push_back(i);
		}

		int sum = 0;
		REP(i, num.size()) {
			if (num[i] % 2 != 0) {
				sum++;
			}
			else {

			}
		}

		if (sum >= 1) {
			cout << -1 << endl;
		}
		else {
			int sum = 0;

			vector<int> numused(26, 0);
			int idx = -1;
			REP(i, S.size() / 2) {
				while (true) {
					idx++;
					if ((numused[S[idx]-'a'] + 1)*2 <= num[S[idx] - 'a']) {
						sum += abs(alpha[S[idx] - 'a'][numused[S[idx] - 'a']] - i);
						sum += abs((int)alpha[S[idx] - 'a'][alpha[S[idx] - 'a'].size()-numused[S[idx] - 'a']-1] - (int)(S.size()-i-1));
						numused[S[idx] - 'a'] ++;
						break;
					}
				}
			}

			cout << sum/2 << endl;// /2
		}
	}
	else {
		vector<int> num(26, 0);
		vector<vector<int>> alpha(26, vector<int>(0));
		REP(i, S.size()) {
			num[S[i] - 'a']++;
			alpha[S[i] - 'a'].push_back(i);
		}

		int sum = 0;
		char hoge = ' ';
		REP(i, num.size()) {
			if (num[i] % 2 != 0) {
				sum++;
				hoge = i + 'a';
			}
			else {

			}
		}

		if (sum != 1) {
			cout << -1 << endl;
		}
		else {
			int sum = 0;

			vector<int> numused(26, 0);
			int idx = -1;
			REP(i, S.size() / 2) {
				while (true) {
					idx++;
					if ((numused[S[idx] - 'a'] + 1) * 2 <= num[S[idx] - 'a']) {
						sum += abs(alpha[S[idx] - 'a'][numused[S[idx] - 'a']] - i);
						sum += abs((int)alpha[S[idx] - 'a'][alpha[S[idx] - 'a'].size() - numused[S[idx] - 'a'] - 1] - (int)(S.size() - i - 1));
						numused[S[idx] - 'a'] ++;
						break;
					}
				}
			}

			sum+= abs(alpha[hoge - 'a'][alpha[hoge - 'a'].size()/2] - (int)S.size()/2);

			cout << sum / 2 << endl;// /2
		}
	}
}