Problem:
Score : 600 points

Problem Statement

We have N strings of lowercase English letters: S_1, S_2, \cdots, S_N.

Takahashi wants to make a string that is a palindrome by choosing one or more of these strings - the same string can be chosen more than once - and concatenating them in some order of his choice.

The cost of using the string S_i once is C_i, and the cost of using it multiple times is C_i multiplied by that number of times.

Find the minimum total cost needed to choose strings so that Takahashi can make a palindrome.

If there is no choice of strings in which he can make a palindrome, print -1.

Constraints

1 \leq N \leq 50

1 \leq |S_i| \leq 20

S_i consists of lowercase English letters.

1 \leq C_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N
S_1 C_1
S_2 C_2
:
S_N C_N

Output

Print the minimum total cost needed to choose strings so that Takahashi can make a palindrome, or -1 if there is no such choice.

Sample Input 1

3
ba 3
abc 4
cbaa 5

Sample Output 1

7

We have ba, abc, and cbaa.

For example, we can use ba once and abc once for a cost of 7, then concatenate them in the order abc, ba to make a palindrome.
Also, we can use abc once and cbaa once for a cost of 9, then concatenate them in the order cbaa, abc to make a palindrome.

We cannot make a palindrome for a cost less than 7, so we should print 7.

Sample Input 2

2
abcab 5
cba 3

Sample Output 2

11

We can choose abcab once and cba twice, then concatenate them in the order abcab, cba, cba to make a palindrome for a cost of 11.

Sample Input 3

4
ab 5
cba 3
a 12
ab 10

Sample Output 3

8

We can choose a once, which is already a palindrome, but it is cheaper to concatenate ab and cba.

Sample Input 4

2
abc 1
ab 2

Sample Output 4

-1

We cannot make a palindrome, so we should print -1.

Solution in Rust (1.42.0) (judge verdict: Accepted)

// This code is generated by [cargo-atcoder](https://github.com/tanakh/cargo-atcoder)
// Original source code:
/*
use competitive::prelude::*;
use std::cmp::Reverse;
use std::collections::BinaryHeap;

#[argio(output = AtCoder)]
fn main(n: usize, sc: [(Chars, i64); n]) -> Option<i64> {
    let mut q = BinaryHeap::<(Reverse<i64>, Result<&[char], &[char]>)>::new();

    for (s, c) in sc.iter() {
        q.push((Reverse(*c), Err(s)));
        q.push((Reverse(*c), Ok(s)));
        for i in 0..s.len() {
            if let Some(t) = check(&s[0..i], &s[i..]) {
                q.push((Reverse(*c), t));
            }
            if let Some(t) = check(&s[0..i], &s[i + 1..]) {
                q.push((Reverse(*c), t));
            }
        }
    }

    let mut ss = BTreeMap::new();

    while let Some((c, s)) = q.pop() {
        if matches!(s, Err(&[])) {
            return Some(c.0);
        }

        ss.insert(s.clone(), c);

        for (t, d) in sc.iter() {
            match s {
                Err(l) => {
                    if let Some(s) = check(l, t) {
                        if !ss.contains_key(&s) {
                            q.push((Reverse(c.0 + d), s));
                        }
                    }
                }
                Ok(r) => {
                    if let Some(s) = check(t, r) {
                        if !ss.contains_key(&s) {
                            q.push((Reverse(c.0 + d), s));
                        }
                    }
                }
            }
        }
    }

    None
}

fn check<'a>(l: &'a [char], r: &'a [char]) -> Option<Result<&'a [char], &'a [char]>> {
    if l.len() < r.len() {
        for i in 0..l.len() {
            if l[l.len() - 1 - i] != r[i] {
                return None;
            }
        }
        Some(Ok(&r[l.len()..]))
    } else {
        for i in 0..r.len() {
            if l[l.len() - 1 - i] != r[i] {
                return None;
            }
        }
        Some(Err(&l[0..l.len() - r.len()]))
    }
}
*/

fn main() {
    let exe = "/tmp/binA2FB62FC";
    std::io::Write::write_all(&mut std::fs::File::create(exe).unwrap(), &decode(BIN)).unwrap();
    std::fs::set_permissions(exe, std::os::unix::fs::PermissionsExt::from_mode(0o755)).unwrap();
    std::process::exit(std::process::Command::new(exe).status().unwrap().code().unwrap())
}

fn decode(v: &str) -> Vec<u8> {
    let mut ret = vec![];
    let mut buf = 0;
    let mut tbl = vec![64; 256];
    for i in 0..64 { tbl[TBL[i] as usize] = i as u8; }
    for (i, c) in v.bytes().filter_map(|c| { let c = tbl[c as usize]; if c < 64 { Some(c) } else { None } }).enumerate() {
        match i % 4 {
            0 => buf = c << 2,
            1 => { ret.push(buf | c >> 4); buf = c << 4; }
            2 => { ret.push(buf | c >> 2); buf = c << 6; }
            3 => ret.push(buf | c),
            _ => unreachable!(),
        }
    }
    ret
}

const TBL: &'static [u8] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
const BIN: &'static str = "<base64-encoded ELF executable removed>";