atcoder#ARC088C. [ARC088E] Papple Sort

[ARC088E] Papple Sort

Score : 800800 points

Problem Statement

You are given a string SS consisting of lowercase English letters. Determine whether we can turn SS into a palindrome by repeating the operation of swapping two adjacent characters. If it is possible, find the minimum required number of operations.

Constraints

  • 1S2×1051 \leq |S| \leq 2 \times 10^5
  • SS consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:

SS

Output

If we cannot turn SS into a palindrome, print -1. Otherwise, print the minimum required number of operations.

eel
1

We can turn SS into a palindrome by the following operation:

  • Swap the 22-nd and 33-rd characters. SS is now ele.
ataatmma
4

We can turn SS into a palindrome by the following operation:

  • Swap the 55-th and 66-th characters. SS is now ataamtma.
  • Swap the 44-th and 55-th characters. SS is now atamatma.
  • Swap the 33-rd and 44-th characters. SS is now atmaatma.
  • Swap the 22-nd and 33-rd characters. SS is now amtaatma.
snuke
-1

We cannot turn SS into a palindrome.