Language: C++

#define taskname "test"
 
#include <algorithm>
#include <iostream>
#include <cstdio>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
 
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;
 
    data() {}
    data(lli x, string s, int t) : val(x), state(s), from(t) {}
 
    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(data(c[i], s[i], 0));
            mp[make_pair(s[i], 1)] = c[i];
            pq.push(data(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(data(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(data(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(data(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(data(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:48:20: error: template argument 1 is invalid
   48 | priority_queue<data> pq;
      |                    ^
a.cc:48:20: error: template argument 2 is invalid
a.cc:48:20: error: template argument 3 is invalid
a.cc: In function 'void solve()':
a.cc:73:16: error: request for member 'push' in 'pq', which is of non-class type 'int'
   73 |             pq.push(data(c[i], s[i], 0));
      |                ^~~~
a.cc:73:21: error: reference to 'data' is ambiguous
   73 |             pq.push(data(c[i], s[i], 0));
      |                     ^~~~
In file included from /usr/include/c++/14/string:53,
                 from /usr/include/c++/14/bits/locale_classes.h:40,
                 from /usr/include/c++/14/bits/ios_base.h:41,
                 from /usr/include/c++/14/ios:44,
                 from /usr/include/c++/14/ostream:40,
                 from /usr/include/c++/14/iostream:41,
                 from a.cc:4:
/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:33:8: note:                 'struct data'
   33 | struct data
      |        ^~~~
a.cc:75:16: error: request for member 'push' in 'pq', which is of non-class type 'int'
   75 |             pq.push(data(c[i], s[i], 1));
      |                ^~~~
a.cc:75:21: error: reference to 'data' is ambiguous
   75 |             pq.push(data(c[i], s[i], 1));
      |                     ^~~~
/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:33:8: note:                 'struct data'
   33 | struct data
      |        ^~~~
a.cc:78:15: error: request for member 'empty' in 'pq', which is of non-class type 'int'
   78 |     while(!pq.empty())
      |               ^~~~~
a.cc:80:9: error: reference to 'data' is ambiguous
   80 |         data cur = pq.top(); pq.pop();
      |         ^~~~
/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:33:8: note:                 'struct data'
   33 | struct data
      |        ^~~~
a.cc:80:33: error: request for member 'pop' in 'pq', which is of non-class type 'int'
   80 |         data cur = pq.top(); pq.pop();
      |                                 ^~~
a.cc:81:22: error: 'cur' was not declared in this scope
   81 |         string now = cur.state;
      |                      ^~~
a.cc:131:28: error: request for member 'push' in 'pq', which is of non-class type 'int'
  131 |                         pq.push(data(nval, t, 0));
      |                            ^~~~
a.cc:131:33: error: reference to 'data' is ambiguous
  131 |                         pq.push(data(nval, t, 0));
      |                                 ^~~~
/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:33:8: note:                 'struct data'
   33 | struct data
      |        ^~~~
a.cc:146:28: error: request for member 'push' in 'pq', which is of non-class type 'int'
  146 |                         pq.push(data(nval, t, 1));
      |                            ^~~~
a.cc:146:33: error: reference to 'data' is ambiguous
  146 |                         pq.push(data(nval, t, 1));
      |                                 ^~~~
/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:33:8: note:                 'struct data'
   33 | struct data
      |        ^~~~
a.cc:164:28: error: request for member 'push' in 'pq', which is of non-class type 'int'
  164 |                         pq.push(data(nval, t, 1));
      |                            ^~~~
a.cc:164:33: error: reference to 'data' is ambiguous
  164 |                         pq.push(data(nval, t, 1));
      |                                 ^~~~
/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:33:8: note:                 'struct data'
   33 | struct data
      |        ^~~~
a.cc:179:28: error: request for member 'push' in 'pq', which is of non-class type 'int'
  179 |                         pq.push(data(nval, t, 0));
      |                            ^~~~
a.cc:179:33: error: reference to 'data' is ambiguous
  179 |                         pq.push(data(nval, t, 0));
      |                                 ^~~~
/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:33:8: note: