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 (GCC 5.4.1) (judge verdict: Runtime Error)

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

typedef long long int int64;

#define ABS(a) ((a)>=0?(a):-(a))

typedef struct vector{
  int *a;
  int len;
  int maxLen;
} vec;

vec* newVector(void){
  vec *v=(vec *)calloc(1,sizeof(vec));
  v->a=NULL;
  v->len=0;
  v->maxLen=0;
  return v;
}

void push_back(vec *v,int x){
  if(v->len==v->maxLen){
    v->maxLen=2*v->maxLen+1;
    v->a=realloc(v->a,sizeof(int)*v->maxLen);
  }
  v->a[v->len++]=x;
}

int64 calcInversion(int *a,int n,int *tmp){
  if(n<=1) return 0;
  int m=n/2;
  int64 res=calcInversion(a,m,tmp)+calcInversion(a+m,n-m,tmp);
  int i=0;
  int j=m;
  int k=0;
  while(i<m && j<n){
    if(a[i]<=a[j]){
      tmp[k++]=a[i++];
    } else {
      res+=m-i;
      tmp[k++]=a[j++];
    }
  }
  memcpy(tmp+k,a+i,sizeof(int)*(m-i));
  memcpy(a,tmp,sizeof(int)*(k+m-i));
  return res;
}

int64 calc(char *s,int n,vec **v){
  int64 ans=0;
  int cnt[26]={0};
  int sum=0;
  int i;
  for(i=0;i<n;i++){
    int k=s[i]-'a';
    cnt[k]++;
    printf("%d %d\n",sum,cnt[k]);
    if(cnt[k]>v[k]->len/2){
      sum++;
    } else {
      ans+=sum;
    }
  }
  memset(cnt,0,sizeof(int)*26);
  int *array=(int *)calloc(n/2,sizeof(int));
  int len=0;
  for(i=n-1;i>=0;i--){
    int k=s[i]-'a';
    if(cnt[k]<v[k]->len/2){
      array[len++]=v[k]->a[cnt[k]];
    }
    cnt[k]++;
  }
  for(i=0;i<len;i++) printf("%d ",array[i]);
  printf("\n");
  int *tmp=(int *)calloc(n/2,sizeof(int));
  ans+=calcInversion(array,len,tmp);
  free(tmp);
  return ans;
}

void run(void){
  char *s=(char *)calloc(200000+1,sizeof(char));
  scanf("%s",s);
  int n=strlen(s);
  const int f=26;
  vec **v=(vec **)calloc(f,sizeof(vec *));
  int i;
  for(i=0;i<f;i++) v[i]=newVector();
  for(i=0;i<n;i++) push_back(v[s[i]-'a'],i+1);
  int odd=0;
  for(i=0;i<f;i++) if(v[i]->len%2) odd++;
  if(odd>1){
    printf("-1\n");
    return;
  }
  if(odd==1) exit(1);
  int64 ans=calc(s,n,v);
  printf("%lld\n",ans);
}

int main(void){
  run();
  return 0;
}