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 <algorithm>
#include <vector>
#include <map>
#include <set>
#include <string>
#include <cmath>
#include <iomanip>
#include <queue>
#include <unordered_map>
#include <unordered_set>
#include <bitset>
#include <cassert>
#include <cstring>
#include <climits>

using namespace std;

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

typedef long long ll;
typedef pair<int, int> P;
typedef vector<int> V;
typedef map<int, int> M;

constexpr ll INF = 1e18;
constexpr ll MOD = 1e9 + 7;
constexpr double PI = 3.14159265358979323846;
constexpr int dx[] = {0, 0, 1, -1};
constexpr int dy[] = {1, -1, 0, 0};

int bit[1123456], bit_n;

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

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

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);

    string s;
    cin >> s;

    int cnt[30];
    fill(cnt, cnt + 26, 0);

    REP(i, s.size())
    cnt[s[i] - 'a']++;

    int one = 0;
    int onec = 0;
    int onea;
    REP(i, 26)
    {
        one += cnt[i] % 2;
        if (cnt[i] % 2)
            onec = cnt[i], onea = i;
    }

    onec = onec / 2 + 1;

    if (one > 2)
    {
        cout << -1 << endl;
        return 0;
    }

    ll res = 0;

    vector<char> l;
    vector<char> r;

    REP(i, s.size())
    {
        if (cnt[s[i] - 'a'] <= 0)
        {
            r.push_back(s[i]);
            continue;
        }

        if (cnt[s[i] - 'a'] == 1)
        {
            cnt[s[i] - 'a']--;
            continue;
        }

        res += i - l.size();
        cnt[s[i] - 'a'] -= 2;
        l.push_back(s[i]);
    }

    if (one)
    {
        int j = 0;
        while (onec)
        {
            if (s[j] - 'a' == onea)
                onec--;
            j++;
        }

        j--;

        res += j - l.size();
    }

    reverse(r.begin(), r.end());

    V num[30];

    REP(i, r.size())
    num[r[i] - 'a'].push_back(i);

    int acnt[30];
    fill(acnt, acnt + 26, 0);

    int dec = 0;

    bit_n = l.size();
    fill(bit, bit + bit_n, 0);

    REP(i, l.size())
    {
        int ind = l[i] - 'a';
        res += (num[ind][acnt[ind]] + (dec - bit_sum(num[ind][acnt[ind]] + 1))) - i;
        bit_add(num[ind][acnt[ind]] + 1, 1);
        dec++;
        acnt[ind]++;
    }

    cout << res << endl;

    return 0;
}