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 C++ (GCC 9.2.1) (judge verdict: Wrong Answer)

#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx")
#pragma GCC optimize("unroll-loops")
//#pragma warning(disable : 4996)

#ifdef _MSC_VER
#include <intrin.h>
#define __builtin_popcount __popcnt
#define __builtin_popcountll __popcnt64

#endif

#include <limits.h>
#include <math.h>
#include <time.h>

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <complex>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

#define REP(i, n) for (int i = 0; i < (n); ++i)
#define REPR(i, n) for (int i = n - 1; i >= 0; --i)
#define FOR(i, m, n) for (int i = m; i < n; ++i)
#define FORR(i, m, n) for (int i = m - 1; i >= n; --i)
#define SORT(v, n) sort(v, v + n);
#define VSORT(v) sort(v.begin(), v.end());
#define REVERSE(v, n) reverse(v, v + n);
#define VREVERSE(v) reverse(v.begin(), v.end())
#define ll long long
#define print(x) cout << (x) << '\n'
#define pe(x) cout << (x) << " "
#define DEBUG(x) cout << #x << ": " << x << endl
#define lb(v, n) lower_bound(v.begin(), v.end(), (n))
#define ub(v, n) upper_bound(v.begin(), v.end(), (n))
#define int long long
//#define double long double
#define all(x) (x).begin(), (x).end()
#define print_space(v) REP(i, v.size()) cout << v[i] << " \n"[i==(int)v.size()-1]
template <typename T1, typename T2> inline void chmin(T1& a, T2 b) { if (a > b) a = b; }
template <typename T1, typename T2> inline void chmax(T1& a, T2 b) { if (a < b) a = b; }
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
std::random_device rd;
std::mt19937 mt(rd());
constexpr ll MOD = 1e9 + 7;
constexpr int MAX = 500050;
const double pi = acos(-1);
constexpr double EPS = 1e-8;
constexpr ll LINF = 1e17 + 1;
constexpr int INF = 1e9 + 1;
//int dx[4] = { 0,0,-1,1 }, dy[4] = { 1,-1,0,0 };

struct Trie {
	bool f;
	bool isend;
	int cost;
	Trie *par;
	Trie *next[26];
	Trie() { isend = false; f = 0; par = (Trie*)0; fill(next, next + 26, (Trie*)0); cost = INF; }
};
Trie *ins(string t, Trie *r,int cost) {
	for (int i = 0; i < t.size(); i++) {
		int c = t[i] - 'a';
		if (!r->next[c])r->next[c] = new Trie;
		r->next[c]->par = r;
		r = r->next[c];
		r->f = 1;
		if (i == (int)t.size() - 1) {
			r->isend = true;
			chmin(r->cost, cost);
		}
	}
	return r;
}
void show(Trie *r, string &s) {
	cerr << s << endl;
	REP(i, 26) {
		if (r->next[i]) {
			s += (char)(i + 'a');
			show(r->next[i], s);
			s.pop_back();
		}
	}
}
string S[51];
string Sr[51];
bool is_palindrome(string s) {
	int n = s.size();
	REP(i, n)if (s[i] != s[n - i - 1])return false;
	return true;
}
int C[51];	Trie *ze = 0;
int dfs(string s, Trie *r1, Trie *r2, bool rev);
int dfs2(Trie *r1,Trie *r2,Trie *r, bool rev, int n,string &s) {
	//cerr << "dfs2 ";
	//DEBUG(s);
	//DEBUG(rev);
	ll res = LINF;
	if ((int)s.size() >= n)return res;
	if (r->isend) {
		chmin(res,r->cost + dfs(s, r1, r2, rev));
	}
	REP(i, 26) {
		if (r->next[i]) {
			s += (char)(i + 'a');
			chmin(res, dfs2(r1, r2, r->next[i], rev, n, s));
			s.pop_back();
		}
	}
	return res;
}

int dfs(string s, Trie *r1,Trie *r2, bool rev) {
	while (r1->par != ze)r1 = r1->par;
	while (r2->par != ze)r2 = r2->par;
	ll res = LINF;
	int n = s.size();
	//DEBUG(s);
	//DEBUG(rev);
	if (is_palindrome(s))return 0;
	if (!rev) {
		Trie *r = r2;
		bool done = false;
		REP(i, n) {
			if (r->next[s[i] - 'a']) {
				r = r->next[s[i] - 'a'];
				if (r->isend) {
					chmin(res, r->cost+dfs(s.substr(i + 1, n - i - 1),r1,r2,rev));
				}
				if (i == n - 1)done = true;
			}
			else break;
		}
		string t;
		if (done)chmin(res, dfs2(r1, r2, r, !rev, n, t));
	}
	else {
		Trie *r = r1;
		bool done = false;
		REP(i, n) {
			if (r->next[s[i] - 'a']) {
				r = r->next[s[i] - 'a'];
				if (r->isend) {
					chmin(res, r->cost+dfs(s.substr(i + 1, n - i - 1), r1, r2, rev));
				}
				if (i == n - 1)done = true;
			}
			else break;
		}
		string t;
		if (done)chmin(res, dfs2(r1, r2, r, !rev, n, t));
	}
	return res;
}
void solve() {
	int N; cin >> N;
	Trie *r1 = new Trie;
	Trie *r2 = new Trie;
	REP(i, N) {
		cin >> S[i] >> C[i];
		Sr[i] = S[i];
		VREVERSE(Sr[i]);
	}

	REP(i, N) {
		ins(S[i], r1, C[i]);
		while (r1->par != ze)r1 = r1->par;
		ins(Sr[i], r2, C[i]);
		while (r2->par != ze)r2 = r2->par;
	}
	string t;
	ll ans = LINF;
	REP(i, N) {
		chmin(ans, C[i]+dfs(S[i], r1, r2, false));
		chmin(ans, C[i] + dfs(Sr[i], r1, r2, true));
	}
	if (ans < LINF)print(ans);
	else print(-1);
}



signed main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	//int q;
	//cin >> q;
	//while (q--)
	solve();

}