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 Rust (1.15.1) (judge verdict: Wrong Answer)

use std::collections::VecDeque;
fn main() {
    let mut deqs = vec![VecDeque::new(); 26];
    let s: String = readln();
    let s = s.as_bytes();
    for (i, &x) in s.iter().enumerate() {
        deqs[(x as u32 - 'a' as u32) as usize].push_back(i as i32);
    }
    let mut ans = 0;

    for i in 0..s.len()/2 {
        let p = i as i32;
        let q = (s.len()-1-i) as i32;
        let mp = (0..26).filter_map(|x| {
                           if deqs[x].len() >= 2 {
                               Some(((p - deqs[x].front().unwrap()).abs() + 
                                   (q - deqs[x].back().unwrap()).abs(), x))
                           } else {
                               None
                           }
        }).min();
        match mp {
            Some((cost, idx)) => {
                ans += cost;
                deqs[idx].pop_front();
                deqs[idx].pop_back();
            },
            None => {
                ans = -2;
                break;
            }
        }
    }

    if ans >= 0 && s.len() % 2 == 1 {
        let p = (s.len() / 2) as i32;
        let mp = (0..26).filter_map(|x| {
            if deqs[x].len() >= 1 {
                Some(((p - deqs[x].front().unwrap()).abs(), x))
            } else {
                None
            }
        }).min();
        match mp {
            Some((cost, idx)) => {
                ans += cost;
                deqs[idx].pop_front();
            },
            None => {
                ans = -2;
            }
        }
    }

    println!("{}", ans/2);
}

// --- template ---
#[allow(unused_imports)]
use std::cmp::{max, min};
#[allow(unused_imports)]
use std::collections::HashSet;

pub trait FromLn {
    fn fromln(s: &str) -> Self;
}
pub fn readln<T: FromLn>() -> T {
    let mut buf = String::new();
    let _ = ::std::io::stdin().read_line(&mut buf).unwrap();
    T::fromln(buf.trim())
}
pub fn readlns<T: FromLn>(n: usize) -> Vec<T> {
    let mut vs = vec![];
    for _ in 0..n { vs.push(readln()); }
    vs
}
macro_rules! fromln_primitives {
    ($($t:ty),*) => { $(
        impl FromLn for $t {
            fn fromln(s: &str) -> $t {
                s.parse().unwrap()
            }
        }
    )* }
}
fromln_primitives!(String, bool, f32, f64, isize, i8, i16, i32, i64, usize, u8, u16, u32, u64);
impl<T> FromLn for Vec<T> where T: FromLn {
    fn fromln(s: &str) -> Vec<T> {
        s.split_whitespace().map(T::fromln).collect()
    }
}
impl FromLn for Vec<char> {
    fn fromln(s: &str) -> Vec<char> {
        s.chars().collect()
    }
}
macro_rules! fromln_tuple {
    ($($t:ident),*) => {
        impl<$($t),*> FromLn for ($($t),*) where $($t: FromLn),* {
            fn fromln(s: &str) -> ($($t),*) {
                let mut it = s.split_whitespace();
                let t = ($($t::fromln(it.next().unwrap())),*);
                assert_eq!(it.next(), None);
                t
            }
        }
    }
}
fromln_tuple!(A, B);
fromln_tuple!(A, B, C);
fromln_tuple!(A, B, C, D);
fromln_tuple!(A, B, C, D, E);
fromln_tuple!(A, B, C, D, E, F);