Language: C++

#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;
using ll = long long;

struct E{
    int to,co;
    E(int to=0,int co=0):to(to),co(co){}
};

string rev(string s){
    reverse(s.begin(),s.end());
    return s;
}

int main(){
    int m;
    cin>>m;
    vector<string> s(m);
    vector<int> c(m);
    rep(i,m)cin>>s[i]>>c[i];
    rep(i,m){
        s.push_back(rev(s[i]));
        c.push_back(c[i]);
    }
    int n=1;
    vector<vector<int> > id(m*2);
    rep(i,m*2){
        int l=s[i].size();
        id[i].resize(l);
        rep(j,l){
            id[i][j]=n++;
        }
    }
    vector<vector<E> > g(n);
    rep(i,m*2){
        int l=s[i].size();
        rep(j,l){
            int v=id[i][j];
            rep(ni,m*2){
                if(i/m==ni/m)continue;
                int nl=s[ni].size();
                int w=min(nl,l-j);
                if(s[i].substr(j,w)!=s[ni].substr(0,w))continue;
                int u=0;
                if(nl>w){
                    u=id[ni][w];
                }
                else if(l>j+w){
                    u=id[i][j+w];
                }else u=0;
                g[v].emplace_back(u,c[ni]);
            }
        }
    }
    const ll INF=1e18;
    vector<ll> dist(n,INF);
    priority_queue<pair<ll,int>,vector<pair<ll,int>>,greater<pair<ll,int>>> q;
    auto push=[&](int v,ll x){
        if(dist[v]<=x)return;
        dist[v]=x;
        q.emplace(x,v);
    };
    rep(i,m)push(id[i][0],c[i]);
    while(!q.empty()){
        ll x=q.top().first;
        int v=q.top().second;q.pop();
        if(dist[v]!=x)continue;
        for(E e : g[v]){
            push(e.to,x+e.co);
        }
    }
    ll ans=dist[0];
    rep(i,m*2){
        int l=s[i].size();
        rep(j,l){
            string t=s[i].substr(j);
            if(t==rev(t)){
                ans=min(ans,dist[id[i][j]]);
            }
        }
    }
    if(ans==INF)cout<<-1<<endl;
    else cout<<ans<<endl;

    return 0;
}
        rep(j,l){
            string t=s[i].substr(j);
            if(t==rev(t)){
                ans=min(ans,dist[id[i][j]]);
            }
        }
    }
    if(ans==INF)cout<<-1<<endl;
    else cout<<ans<<endl;

    return 0;
}

Compiler output:
a.cc:2:18: error: expected unqualified-id before 'for'
    2 | #define rep(i,n) for(int i=0;i<n;i++)
      |                  ^~~
a.cc:88:9: note: in expansion of macro 'rep'
   88 |         rep(j,l){
      |         ^~~
a.cc:88:13: error: 'j' does not name a type
   88 |         rep(j,l){
      |             ^
a.cc:2:30: note: in definition of macro 'rep'
    2 | #define rep(i,n) for(int i=0;i<n;i++)
      |                              ^
a.cc:88:13: error: 'j' does not name a type
   88 |         rep(j,l){
      |             ^
a.cc:2:34: note: in definition of macro 'rep'
    2 | #define rep(i,n) for(int i=0;i<n;i++)
      |                                  ^
a.cc:94:5: error: expected declaration before '}' token
   94 |     }
      |     ^
a.cc:95:5: error: expected unqualified-id before 'if'
   95 |     if(ans==INF)cout<<-1<<endl;
      |     ^~
a.cc:96:5: error: expected unqualified-id before 'else'
   96 |     else cout<<ans<<endl;
      |     ^~~~
a.cc:98:5: error: expected unqualified-id before 'return'
   98 |     return 0;
      |     ^~~~~~
a.cc:99:1: error: expected declaration before '}' token
   99 | }
      | ^