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 (Clang 3.8.0) (judge verdict: Runtime Error)

#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include <algorithm>
#include <numeric>
#include <utility>
#include <sys/time.h>
#include <cmath>
#include <tuple>
#include <queue>

using namespace std;

int bit[2*(1<<10)], n;

int sum(int i) {
    int s = 0;
    while (i > 0) {
        s += bit[i];
        i -= i & -i;
    }
    return s;
}

void add(int i, int x) {
    while (i <= n) {
        bit[i] += x;
        i += i & -i;
    }
}

void solve(string S){
    vector<int> bin(26, 0);
    for (auto&& c : S) {
        bin[c - 'a']++;
    }
    bool flag = false;
    char cent;
    for (int i = 0; i < 26; i++) {
        if (bin[i]%2 != 0) {
            if (flag == false) {
                flag = true;
                cent = 'a' + i;
            } else {
                std::cout << -1 << std::endl;
                return;
            }
        }
        bin[i] /= 2;
    }
    string cir;
    for (auto&& c : S) {
        if (bin[c - 'a'] > 0) {
            cir.push_back(c);
            bin[c - 'a']--;
        }
    }
    vector< vector< int > > vv(26, vector<int>());
    int l = 0;
    for(int i = 0; i < cir.size(); i++) {
        vv[cir[i] - 'a'].push_back(l);
        l++;
    }
    if(flag) {
        vv[cent - 'a'].push_back(l);
        l++;
    }
    for (int i = 0; i < cir.size(); i++) {
        vv[cir[cir.size()-i-1] - 'a'].push_back(l);
        l++;
    }
    vector<int> itr(26, 0);
    vector<int> a(S.size());
    for (int i = 0; i < S.size(); i++) {
        a[i] = vv[S[i] - 'a'][itr[S[i] - 'a']]+1;
        itr[S[i] - 'a']++;
    }
    long long ans = 0;
    n = S.size();
    for (int i = 0; i < n+1; i++) {
        bit[i] = 0;
    }
    for (int i = 0; i < S.size(); i++) {
        ans += i - sum(a[i]);
        add(a[i], 1);
    }
    std::cout << ans << std::endl;
}

int main(){	
    struct timeval start,end;
    long long span;
    gettimeofday(&start,NULL);

    string S;
    cin >> S;
    solve(S);

    gettimeofday(&end,NULL);
    span = (end.tv_sec -start.tv_sec)*1000000LL + (end.tv_usec - start.tv_usec);
    std::cerr << "--Total Time: " << span/1000 << "ms" << endl;
    return 0;
}