Language: C++

#include<bits/stdc++.h>
#define int long long
#define fi first
#define se second
#define pii pair<int,int>
#define mp make_pair
#define tp tuple<int,int,int>
#define mtp make_tuple
using namespace std;
const int N=10005;
int n,d[N][2],c[N];
map<string,int>id;
string s[N],a[N];
vector<tp>adj[N][2];
priority_queue<tp,vector<tp>,greater<tp>>pq;


main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    memset(d,-1,sizeof d);
    cin>>n;
    int cnt=0;
    for(int i=1;i<=n;i++)
    {
        cin>>s[i]>>c[i];
        string t;
        for(int j=0;j<s[i].size();j++)
        {
            t+=s[i][j];
            if(!id[t])
            {
                id[t]=++cnt;
                a[cnt]=t;
            }
        }
        t.clear();
        for(int j=s[i].size()-1;j>=0;j--)
        {
            t+=s[i][j];
            reverse(t.begin(),t.end());
            if(!id[t])
            {
                id[t]=++cnt;
                a[cnt]=t;
            }
            reverse(t.begin(),t.end());
        }
    }
    for(int i=1;i<=cnt;i++)
    {
        string t=a[i];
        reverse(t.begin(),t.end());
        for(int j=1;j<=n;j++)
        {
            if(s[j].size()>=t.size())
            {
                bool kt=true;
                for(int k=0;k<t.size();k++) if(s[j][k]!=t[k]) kt=false;
                string tmp=s[j];
                tmp.erase(0,t.size());
                if(kt)
                {
                    if(s[j].size()==t.size()) adj[i][0].push_back(mtp(0,0,c[j]));
                    else adj[i][0].push_back(mtp(id[tmp],1,c[j]));
                }
            }
        }
        for(int j=1;j<=n;j++)
        {
            if(s[j].size()>=t.size())
            {
                bool kt=true;
                int l=1;
                int r=s[j].size()-t.size();
                for(int k=s[j].size()-1;k>=r;k--)
                {
                    if(s[j][k]!=t[t.size()-l]) kt=false;
                    l++;
                }
                string tmp=s[j];
                tmp.erase(s[j].size()-t.size(),t.size());
                if(kt)
                {
                    if(s[j].size()==t.size()) adj[i][1].push_back(mtp(0,0,c[j]));
                    else adj[i][1].push_back(mtp(id[tmp],0,c[j]));
                }
            }
        }
    }
    for(int i=1;i<=cnt;i++)
    {
        for(int j=0;j<2;j++) d[i][j]=1e18;
    }
    for(int i=1;i<=n;i++)
    {
        d[id[s[i]]][0]=min(d[id[s[i]]][0],c[i]);
        d[id[s[i]]][1]=min(d[id[s[i]]][1],c[i]);
        pq.push(mtp(c[i],id[s[i]],0));
        pq.push(mtp(c[i],id[s[i]],1));
        for(int j=0;j<s[i].size();j++)
        {
            bool kt=true;
            for(int k=j-1,t=j+1;k>=0&&t<s[i].size();k--,t++) if(s[i][k]!=s[i][t]) kt=false;
            if(!kt) continue;
            if(s[i].size()%2&&j==s[i].size()/2) d[0][0]=min(d[0][0],c[i]);
            else if(j<s[i].size()/2)
            {
                string t=s[i];
                t.erase(0,j*2+1);
                d[id[t]][1]=min(d[id[t][1],c[i]);
                pq.push(mtp(c[i],id[t],1));
                //cout<<t<<' '<<id[t]<<' '<<1<<endl;
            }
            else if(j>=s[i].size()/2)
            {
                int z=s[i].size()-j-1;
                string t;
                for(int k=0;k<s[i].size()-(z*2+1);k++) t+=s[i][k];
                d[id[t]][0]=min(d[id[t][0],c[i]);
                pq.push(mtp(c[i],id[t],0));
                //cout<<t<<' '<<id[t]<<' '<<0<<endl;
            }
        }
    }
    while(!pq.empty())
    {
        int dist,i,j;
        tie(dist,i,j)=pq.top();
        pq.pop();
        if(d[i][j]!=dist) continue;
        for(auto&x:adj[i][j])
        {
            int c,ni,nj;
            tie(ni,nj,c)=x;
            if(d[ni][nj]>d[i][j]+c)
            {
                d[ni][nj]=d[i][j]+c;
                pq.push(mtp(d[ni][nj],ni,nj));
                //if(ni==13&&nj==1) cout<<i<<' '<<j<<' '<<a[i]<<'\n';
            }
        }
    }
    if(d[0][0]==1e18) d[0][0]=-1;
    cout<<d[0][0];
}


Compiler output:
a.cc:18:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   18 | main()
      | ^~~~
a.cc: In function 'int main()':
a.cc:112:40: error: invalid types 'std::map<std::__cxx11::basic_string<char>, long long int>::mapped_type {aka long long int}[int]' for array subscript
  112 |                 d[id[t]][1]=min(d[id[t][1],c[i]);
      |                                        ^
a.cc:112:48: error: expected ']' before ')' token
  112 |                 d[id[t]][1]=min(d[id[t][1],c[i]);
      |                                                ^
      |                                                ]
a.cc:121:40: error: invalid types 'std::map<std::__cxx11::basic_string<char>, long long int>::mapped_type {aka long long int}[int]' for array subscript
  121 |                 d[id[t]][0]=min(d[id[t][0],c[i]);
      |                                        ^
a.cc:121:48: error: expected ']' before ')' token
  121 |                 d[id[t]][0]=min(d[id[t][0],c[i]);
      |                                                ^
      |                                                ]