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;

    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:40:20: error: template argument 1 is invalid
   40 | priority_queue<data> pq;
      |                    ^
a.cc:40:20: error: template argument 2 is invalid
a.cc:40:20: error: template argument 3 is invalid
a.cc: In function 'void solve()':
a.cc:65:16: error: request for member 'push' in 'pq', which is of non-class type 'int'
   65 |             pq.push(data(c[i], s[i], 0));
      |                ^~~~
a.cc:65:21: error: reference to 'data' is ambiguous
   65 |             pq.push(data(c[i], s[i], 0));
      |                     ^~~~
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:67:16: error: request for member 'push' in 'pq', which is of non-class type 'int'
   67 |             pq.push(data(c[i], s[i], 1));
      |                ^~~~
a.cc:67:21: error: reference to 'data' is ambiguous
   67 |             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:25:8: note:                 'struct data'
   25 | struct data
      |        ^~~~
a.cc:70:15: error: request for member 'empty' in 'pq', which is of non-class type 'int'
   70 |     while(!pq.empty())
      |               ^~~~~
a.cc:72:9: error: reference to 'data' is ambiguous
   72 |         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:25:8: note:                 'struct data'
   25 | struct data
      |        ^~~~
a.cc:72:33: error: request for member 'pop' in 'pq', which is of non-class type 'int'
   72 |         data cur = pq.top(); pq.pop();
      |                                 ^~~
a.cc:73:22: error: 'cur' was not declared in this scope
   73 |         string now = cur.state;
      |                      ^~~
a.cc:123:28: error: request for member 'push' in 'pq', which is of non-class type 'int'
  123 |                         pq.push(data(nval, t, 0));
      |                            ^~~~
a.cc:123:33: error: reference to 'data' is ambiguous
  123 |                         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:25:8: note:                 'struct data'
   25 | struct data
      |        ^~~~
a.cc:138:28: error: request for member 'push' in 'pq', which is of non-class type 'int'
  138 |                         pq.push(data(nval, t, 1));
      |                            ^~~~
a.cc:138:33: error: reference to 'data' is ambiguous
  138 |                         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:25:8: note:                 'struct data'
   25 | struct data
      |        ^~~~
a.cc:156:28: error: request for member 'push' in 'pq', which is of non-class type 'int'
  156 |                         pq.push(data(nval, t, 1));
      |                            ^~~~
a.cc:156:33: error: reference to 'data' is ambiguous
  156 |                         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:25:8: note:                 'struct data'
   25 | struct data
      |        ^~~~
a.cc:171:28: error: request for member 'push' in 'pq', which is of non-class type 'int'
  171 |                         pq.push(data(nval, t, 0));
      |                            ^~~~
a.cc:171:33: error: reference to 'data' is ambiguous
  171 |                         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:25:8: note:                 'struct data'
   25 | struct data
      |        ^~~~