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: Wrong Answer)

#include <iostream>
#include <vector>
#include <utility>
#include <string>
#include <map>
#include <algorithm>
#include <set>
#include <queue>
#include <functional>
#include <cmath>
#define rep(i,n) for(int i = 0; i < (int)(n); i++)
#define show(x) cerr << #x << " = " << x << endl;
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
ll mergecount(vector<int> &a) {
  ll count = 0;
  int n = a.size();
  if (n > 1) {
    vector<int> b(a.begin(), a.begin() + n/2);
    vector<int> c(a.begin() + n/2, a.end());
    count += mergecount(b);
    count += mergecount(c);
    for (int i = 0, j = 0, k = 0; i < n; ++i)
      if (k == c.size())       a[i] = b[j++];
      else if (j == b.size())  a[i] = c[k++];
      else if (b[j] <= c[k])   a[i] = b[j++];
      else                   { a[i] = c[k++]; count += n/2 - j; }
  }
  return count;
}
int main(){
    string S;
    cin >> S;
    int N = (int) S.size();
    vector<int>A;
    int cnt[26] = {};
    rep(i,N){
        A.push_back(S[i]-'a');
        cnt[A[i]]++;
    }
    int man = -1;
    rep(i,26){
        if(cnt[i]%2){
            if(man != -1){
                cout << -1 << endl;
                return 0;
            }
            man = i;
        }
    }
    ll ans = 0;
    int k = 0;
    int cnt2[26] = {};
    vector<int>B;
    vector<bool>used(N,false);
    rep(i,N){
        if(cnt[A[i]]%2){
            if(cnt[A[i]]/2>cnt2[A[i]]){
                ans += k;
                B.push_back(A[i]);
                cnt2[A[i]]++;
                used[i] = true;
            }else{
                k++;
            }
        }else{
            if(cnt[A[i]]/2>cnt2[A[i]]){
                ans += k;
                B.push_back(A[i]);
                cnt2[A[i]]++;
                used[i] = true;
            }else{
                k++;
            }
        }
    }
    k = 0;
    vector<int>C;
    rep(i,N){
        if(cnt[A[i]]%2){
            if(!used[i])ans += 0;
            else C.push_back(A[i]);
        }else if(!used[i]){
            k++;
            C.push_back(A[i]);
        }
    }
    k = 0;
    rep(i,N){
        if(cnt[A[i]]%2){
            if(!used[i]){
                ans += k;
                break;
            }
            // else C.push_back(A[i]);
        }else if(!used[i]){
            k++;
            // C.push_back(A[i]);
        }
    }
    reverse(C.begin(), C.end());
    // cerr << ans << endl;
    // cerr << B.size() <<" " << C.size()  << endl;
    // rep(i,N/2){
    //     cout << B[i] << " ";
    // }
    // cout << endl;
    // rep(i,N/2){
    //     cout << C[i] <<  " " ;
    // }
    // cout << endl;
    vector<int>D[26];
    rep(i,N/2){
        D[B[i]].push_back(i);
    }
    rep(i,26)reverse(D[i].begin(), D[i].end());
    vector<int>E;
    rep(i,N/2){
        E.push_back(D[C[i]].back());
        D[C[i]].pop_back();
    }
    // rep(i,N/2){
    //     cerr << E[i] << "-- ";
    // }
    // cout << endl;
    cout << ans + mergecount(E) << endl;
}