Language: C++

#pragma GCC optimize ("O3")
#pragma GCC target ("sse4")

#include <bits/stdc++.h>

using namespace std;

typedef pair<int, int> ii;
typedef vector<int> vi;
typedef long long ll;

#define f first
#define s second
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define sz(x) (int)x.size()
#define all(x) begin(x), end(x)
#define rsz resize

const int md = 1e9+7;
const ll inf = 1e18;
const int maxn = 55;
const int maxl = 25;

template<class T> void ckmin(T &a, T b) { a = min(a, b); }
template<class T> void ckmax(T &a, T b) { a = max(a, b); }

char s[maxn][maxl];
int c[maxn];
int len[maxn];

int n;

bool checkpal(int k, int x, int y)
{
	for(; x<= y; x++, y--)
	{
		if(s[x] != s[y]) return false;
	}
	return true;
}

bool prfpal[maxn][maxl];
bool sufpal[maxn][maxl];

ll dist[maxn][maxl][2];

struct node
{
	int id, pos, isPref;
	ll d;
	node(int a, int b, int c, ll _d)
	{
		id = a;
		pos = b;
		isPref = c;
		d = _d;
	}
	bool operator < (node other) const
	{
		return d> other.d;
	}
};

int main()
{
	scanf("%d", &n);
	for(int i = 0; i< n; i++)
	{
		scanf("%s", s[i]);
		scanf("%d", &c[i]);
		len[i] = strlen(s[i]);
		int k = len[i];
		for(int j = 0; j< k; j++) prfpal[i][j] = checkpal(i, 0, j);
		for(int j = 0; j< k; j++) sufpal[i][j] = checkpal(i, j, k-1); 
	}
	
	for(int i = 0; i< n; i++) for(int j = 0; j<= len[i]; j++) dist[i][j][0] = dist[i][j][1] = 4e18;

	priority_queue<node> pq;
	for(int i = 0; i< n; i++)
	{
		dist[i][len[i]-1][1] = c[i];
		pq.push(node(i, len[i]-1, 1, c[i]));
	}

	while(!pq.empty())
	{
		node kk = pq.top(); pq.pop();
		int id = kk.id, pos = kk.pos, isPref = kk.isPref;
		ll d = kk.d;
		if(isPref && prfpal[id][pos] || !isPref && sufpal[id][pos] || isPref && pos == -1 || isSuf && pos == len[id])
		{
			printf("%lld\n", d);
			return 0;
		}
		if(d> dist[id][pos][isPref]) continue;

		for(int i = 0; i< n; i++)
		{
			if(isPref)
			{
				int p1 = 0, p2 = pos;
				bool bad = false;
				while(p1< len[i] && p2>= 0)
				{
					if(s[i][p1] == s[id][p2]) p1++, p2--;
					else
					{
						bad = true;
						break;
					}
				}
				if(bad) continue;
				int nid, npos, nisPref;
				ll nd = d+c[i];
				if(p1 == len[i])
				{
					nid = id;
					npos = p2;
					nisPref = isPref;
				}
				else
				{
					nid = i;
					npos = p1;
					nisPref = !isPref;
				}
				if(nd< dist[nid][npos][nisPref])
				{
					pq.push(node(nid, npos, nisPref, nd));
				}
			}
			else
			{
				int p1 = pos, p2 = len[i]-1;
				bool bad = false;
				while(p1< len[id] && p2>= 0)
				{
					if(s[id][p1] == s[i][p2]) p1++, p2--;
					else
					{
						bad = true;
						break;
					}
				}
				if(bad) continue;
				int nid, npos, nisPref;
				ll nd = d+c[i];
				if(p1 == len[id])
				{
					nid = i;
					npos = p2;
					nisPref = !isPref;
				}
				else
				{
					nid = id;
					npos = p1;
					nisPref = isPref;
				}
				if(nd< dist[nid][npos][nisPref])
				{
					pq.push(node(nid, npos, nisPref, nd));
				}
			}
		}
	}
	printf("-1\n");
}

Compiler output:
a.cc: In function 'int main()':
a.cc:93:102: error: 'isSuf' was not declared in this scope
   93 |                 if(isPref && prfpal[id][pos] || !isPref && sufpal[id][pos] || isPref && pos == -1 || isSuf && pos == len[id])
      |                                                                                                      ^~~~~