Language: C++

#define taskname "test"

#include <bits/stdc++.h>

using namespace std;

#define sz(x) (int)x.size()
#define fi first
#define se second

typedef long long lli;
typedef pair<int, int> pii;

const int maxn = 55;
const lli inf = 1e18;

int n;
string s[maxn];
int c[maxn];

map<pair<string, int>, lli> mp;

lli ans = inf;

struct data
{
    lli val;
    string state;
    int from;

    bool operator <(const data&other) const
    {
        return val > other.val;
    }
};

priority_queue<data> pq;

void read_input()
{
    cin >> n;
    for(int i = 0; i < n; ++i)
        cin >> s[i] >> c[i];
}

bool ispalind(string s)
{
    for(int i = 0; i < sz(s) / 2; ++i)
        if(s[i] != s[sz(s) - i - 1]) return false;
    return true;
}

void solve()
{
    for(int i = 0; i < n; ++i)
    {
        if(ispalind(s[i]))
            ans = min(ans, (lli)c[i]);
        else
        {
            mp[make_pair(s[i], 0)] = c[i];
            pq.push({c[i], s[i], 0});
            mp[make_pair(s[i], 1)] = c[i];
            pq.push({c[i], s[i], 1});
        }
    }
    while(!pq.empty())
    {
        data cur = pq.top(); pq.pop();
        string now = cur.state;
        int from = cur.from;
        if(cur.val != mp[make_pair(now, from)])
            continue;
        lli val = cur.val;
        for(int i = 0; i < n; ++i)
        {
            bool ok = true;
            for(int j = 0; j < min(sz(now), sz(s[i])); ++j)
            {
                if(from == 0)
                {
                    if(now[j] != s[i][sz(s[i]) - j - 1])
                    {
                        ok = false;
                        break;
                    }
                }
                else
                {
                    if(now[sz(now) - j - 1] != s[i][j])
                    {
                        ok = false;
                        break;
                    }
                }
            }
            if(!ok) continue;
            lli nval = val + c[i];
            if(abs(sz(s[i]) - sz(now)) <= 1)
            {
                ans = min(ans, nval);
                continue;
            }
            string t;
            t.resize(max(sz(s[i]), sz(now)) - min(sz(s[i]), sz(now)));
            if(sz(now) > sz(s[i]))
            {
                if(from == 0)
                {
                    for(int j = sz(s[i]); j < sz(now); ++j)
                        t[j - sz(s[i])] = now[j];
                    if(ispalind(t))
                    {
                        ans = min(ans, nval);
                        continue;
                    }
                    if(!mp.count(make_pair(t, 0)) || mp[make_pair(t, 0)] > nval)
                    {
                        mp[make_pair(t, 0)] = nval;
                        pq.push({nval, t, 0});
                    }
                }
                else
                {
                    for(int j = 0; j < sz(now) - sz(s[i]); ++j)
                        t[j] = now[j];
                    if(ispalind(t))
                    {
                        ans = min(ans, nval);
                        continue;
                    }
                    if(!mp.count(make_pair(t, 1)) || mp[make_pair(t, 1)] > nval)
                    {
                        mp[make_pair(t, 1)] = nval;
                        pq.push({nval, t, 1});
                    }
                }
            }
            else
            {
                if(from == 0)
                {
                    for(int j = 0; j < sz(s[i]) - sz(now); ++j)
                        t[j] = s[i][j];
                    if(ispalind(t))
                    {
                        ans = min(ans, nval);
                        continue;
                    }
                    if(!mp.count(make_pair(t, 1)) || mp[make_pair(t, 1)] > nval)
                    {
                        mp[make_pair(t, 1)] = nval;
                        pq.push({nval, t, 1});
                    }
                }
                else
                {
                    for(int j = sz(now); j < sz(s[i]); ++j)
                        t[j - sz(now)] = s[i][j];
                    if(ispalind(t))
                    {
                        ans = min(ans, nval);
                        continue;
                    }
                    if(!mp.count(make_pair(t, 0)) || mp[make_pair(t, 0)] > nval)
                    {
                        mp[make_pair(t, 0)] = nval;
                        pq.push({nval, t, 0});
                    }
                }
            }
        }
    }
    if(ans == inf) ans = -1;
    cout << ans << '\n';
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    read_input();
    solve();
}



Compiler output:
a.cc:37:20: error: template argument 1 is invalid
   37 | priority_queue<data> pq;
      |                    ^
a.cc:37:20: error: template argument 2 is invalid
a.cc:37:20: error: template argument 3 is invalid
a.cc: In function 'void solve()':
a.cc:62:16: error: request for member 'push' in 'pq', which is of non-class type 'int'
   62 |             pq.push({c[i], s[i], 0});
      |                ^~~~
a.cc:64:16: error: request for member 'push' in 'pq', which is of non-class type 'int'
   64 |             pq.push({c[i], s[i], 1});
      |                ^~~~
a.cc:67:15: error: request for member 'empty' in 'pq', which is of non-class type 'int'
   67 |     while(!pq.empty())
      |               ^~~~~
a.cc:69:9: error: reference to 'data' is ambiguous
   69 |         data cur = pq.top(); pq.pop();
      |         ^~~~
In file included from /usr/include/c++/14/string:53,
                 from /usr/include/c++/14/bitset:52,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:52,
                 from a.cc:3:
/usr/include/c++/14/bits/range_access.h:344:5: note: candidates are: 'template<class _Tp> constexpr const _Tp* std::data(initializer_list<_Tp>)'
  344 |     data(initializer_list<_Tp> __il) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:334:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr _Tp* std::data(_Tp (&)[_Nm])'
  334 |     data(_Tp (&__array)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:323:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(const _Container&)'
  323 |     data(const _Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:312:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(_Container&)'
  312 |     data(_Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
a.cc:25:8: note:                 'struct data'
   25 | struct data
      |        ^~~~
a.cc:69:33: error: request for member 'pop' in 'pq', which is of non-class type 'int'
   69 |         data cur = pq.top(); pq.pop();
      |                                 ^~~
a.cc:70:22: error: 'cur' was not declared in this scope
   70 |         string now = cur.state;
      |                      ^~~
a.cc:120:28: error: request for member 'push' in 'pq', which is of non-class type 'int'
  120 |                         pq.push({nval, t, 0});
      |                            ^~~~
a.cc:135:28: error: request for member 'push' in 'pq', which is of non-class type 'int'
  135 |                         pq.push({nval, t, 1});
      |                            ^~~~
a.cc:153:28: error: request for member 'push' in 'pq', which is of non-class type 'int'
  153 |                         pq.push({nval, t, 1});
      |                            ^~~~
a.cc:168:28: error: request for member 'push' in 'pq', which is of non-class type 'int'
  168 |                         pq.push({nval, t, 0});
      |                            ^~~~