Language: C++

#line 2 "Library\\gcc_option.hpp"
#ifdef LOCAL
    #define _GLIBCXX_DEBUG
#else
    #pragma GCC optimize("O3")
    #pragma GCC target("avx,avx2")
    #pragma GCC optimize("unroll-loops")
#endif
#line 2 "atcoder-workspace\\abc175\\f\\f.cpp"
#include <bits/stdc++.h>
#line 1 "Library\\extlib.hpp"
#include <ext/algorithm>
#include <ext/array_allocator.h>
#include <ext/atomicity.h>
#include <ext/bitmap_allocator.h>
#include <ext/cast.h>
#include <ext/concurrence.h>
#include <ext/debug_allocator.h>
#include <ext/extptr_allocator.h>
#include <ext/functional>
#include <ext/iterator>
#include <ext/malloc_allocator.h>
#include <ext/memory>
#include <ext/mt_allocator.h>
#include <ext/new_allocator.h>
#include <ext/numeric>
#include <ext/pod_char_traits.h>
#include <ext/pointer.h>
#include <ext/pool_allocator.h>
#include <ext/rb_tree>
#include <ext/rope>
#include <ext/slist>
#include <ext/stdio_filebuf.h>
#include <ext/stdio_sync_filebuf.h>
#include <ext/throw_allocator.h>
#include <ext/typelist.h>
#include <ext/type_traits.h>
#include <ext/vstring.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/priority_queue.hpp>
#include <ext/pb_ds/exception.hpp>
#include <ext/pb_ds/hash_policy.hpp>
#include <ext/pb_ds/list_update_policy.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/trie_policy.hpp>
#line 5 "Library\\config.hpp"
namespace config
{
    const auto start_time{std::chrono::system_clock::now()};
    int64_t elapsed()
    {
        using namespace std::chrono;
        const auto end_time{system_clock::now()};
        return duration_cast<milliseconds>(end_time - start_time).count();
    }
    __attribute__((constructor)) void setup()
    {
        using namespace std;
        ios::sync_with_stdio(false);
        cin.tie(nullptr);
        cout << fixed << setprecision(15);
    #ifdef _buffer_check
        atexit([]{ ofstream cnsl("CON"); char bufc; if(cin >> bufc) cnsl << "\n\033[1;35mwarning\033[0m: buffer not empty.\n\n"; });
    #endif
    }
    unsigned cases(void), caseid = 1;
    template <class C> void main() { for(const unsigned total = cases(); caseid <= total; ++caseid) C(); }
} // namespace config
#line 3 "Library\\utils\\iostream_overload.hpp"
namespace std
{
    template <class T, class U> istream &operator>>(istream &is, pair<T, U> &p) { return is >> p.first >> p.second; }
    template <class T, class U> ostream &operator<<(ostream &os, const pair<T, U> &p) { return os << p.first << ' ' << p.second; }
    template <class tuple_t, size_t index> struct tupleis { static istream &apply(istream &is, tuple_t &t) { tupleis<tuple_t, index - 1>::apply(is, t); return is >> get<index>(t); } };
    template <class tuple_t> struct tupleis<tuple_t, SIZE_MAX> { static istream &apply(istream &is, tuple_t &t) { return is; } };
    template <class... T> istream &operator>>(istream &is, tuple<T...> &t) { return tupleis<tuple<T...>, tuple_size<tuple<T...>>::value - 1>::apply(is, t); }
    template <> istream &operator>>(istream &is, tuple<> &t) { return is; }
    template <class tuple_t, size_t index> struct tupleos { static ostream &apply(ostream &os, const tuple_t &t) { tupleos<tuple_t, index - 1>::apply(os, t); return os << ' ' << get<index>(t); } };
    template <class tuple_t> struct tupleos<tuple_t, 0> { static ostream &apply(ostream &os, const tuple_t &t) { return os << get<0>(t); } };
    template <class... T> ostream &operator<<(ostream &os, const tuple<T...> &t) { return tupleos<tuple<T...>, tuple_size<tuple<T...>>::value - 1>::apply(os, t); }
    template <> ostream &operator<<(ostream &os, const tuple<> &t) { return os; }
    template <class Container, typename Value = typename Container::value_type, enable_if_t<!is_same<decay_t<Container>, string>::value, nullptr_t> = nullptr>
    istream& operator>>(istream& is, Container &cont) { for(auto&& e : cont) is >> e; return is; }
    template <class Container, typename Value = typename Container::value_type, enable_if_t<!is_same<decay_t<Container>, string>::value, nullptr_t> = nullptr>
    ostream& operator<<(ostream& os, const Container &cont) { bool flag = 1; for(auto&& e : cont) flag ? flag = 0 : (os << ' ', 0), os << e; return os; }
} // namespace std
#line 3 "Library\\utils\\read.hpp"
namespace workspace {
// read with std::cin.
template <class T = void>
struct read
{
    typename std::remove_const<T>::type value;
    template <class... types>
    read(types... args) : value(args...) { std::cin >> value; }
    operator T() const { return value; }
};
template <>
struct read<void>
{
    template <class T>
    operator T() const { T value; std::cin >> value; return value; }
};
} // namespace workspace
#line 3 "Library\\utils\\casefmt.hpp"
namespace workspace {
std::ostream &casefmt(std::ostream& os) { return os << "Case #" << config::caseid << ": "; }
} // namespace workspace
#line 3 "Library\\utils\\fixed_point.hpp"
namespace workspace {
// specify the return type of lambda.
template <class lambda_type>
class fixed_point
{
    lambda_type func;
public:
    fixed_point(lambda_type &&f) : func(std::move(f)) {}
    template <class... Args> auto operator()(Args &&... args) const { return func(*this, std::forward<Args>(args)...); }
};
} // namespace workspace
#line 3 "Library\\utils\\chval.hpp"
namespace workspace {
template <class T, class Comp = std::less<T>> bool chle(T &x, const T &y, Comp comp = Comp()) { return comp(y, x) ? x = y, true : false; }
template <class T, class Comp = std::less<T>> bool chge(T &x, const T &y, Comp comp = Comp()) { return comp(x, y) ? x = y, true : false; }
} // namespace workspace
#line 5 "Library\\utils\\binary_search.hpp"
namespace workspace {
// binary search on discrete range.
template <class iter_type, class pred_type, std::enable_if_t<std::is_convertible_v<std::invoke_result_t<pred_type, iter_type>, bool>, std::nullptr_t> = nullptr>
iter_type binary_search(iter_type ok, iter_type ng, pred_type pred)
{
    assert(ok != ng);
    intmax_t dist(ng - ok);
    while(std::abs(dist) > 1)
    {
        iter_type mid(ok + dist / 2);
        if(pred(mid)) ok = mid, dist -= dist / 2;
        else ng = mid, dist /= 2;
    }
    return ok;
}
// binary search on real numbers.
template <class real_type, class pred_type, std::enable_if_t<std::is_convertible_v<std::invoke_result_t<pred_type, real_type>, bool>, std::nullptr_t> = nullptr>
real_type binary_search(real_type ok, real_type ng, const real_type eps, pred_type pred)
{
    assert(ok != ng);
    while(std::abs(ok - ng) > eps)
    {
        real_type mid{(ok + ng) / 2};
        (pred(mid) ? ok : ng) = mid;
    }
    return ok;
}
} // namespace workspace
#line 2 "Library\\alias.hpp"
using namespace std; using namespace __gnu_cxx;
using i32 = int_least32_t; using i64 = int_least64_t;
using p32 = pair<i32, i32>; using p64 = pair<i64, i64>;
template <class T, class Comp = less<T>> using heap = priority_queue<T, vector<T>, Comp>;
template <class T> using hashset = unordered_set<T>;
template <class Key, class Value> using hashmap = unordered_map<Key, Value>;
#line 12 "atcoder-workspace\\abc175\\f\\f.cpp"
namespace workspace { struct solver; } int main() { config::main<workspace::solver>(); }
unsigned config::cases() {
    // return -1; // not specify
    // return read(); // given
    return 1;
}

#line 20 "atcoder-workspace\\abc175\\f\\f.cpp"

struct workspace::solver { // start here!

bool ispref(string s1,string s2)
{
    return s1==s2.substr(0,s1.size());
}

bool issuf(string s1,string s2)
{
    if(s1.size()>s2.size()) return 0;
    return s1==s2.substr(s2.size()-s1.size(),s1.size());
}

bool ispalin(string s)
{
    return s==string(rbegin(s),rend(s));
}

string rev(string s)
{
    return string(rbegin(s),rend(s));
}

solver()
{
    // input
    int n=read();
    vector<string> str(n);
    vector<int> cst(n);
    for(int i=0; i<n; ++i)
    {
        cin>>str[i]>>cst[i];
    }

    // maps to int(node id)
    // info: (left or right, str)
    using info=pair<bool,string>;
    hashmap<pair<bool,string>,int> hsh;
    vector<info> infos;
    vector<i64> we; // coming cost

    const bool isl=0,isr=1;
    auto apnd=[&](string s,bool lr)
    {
        int id=infos.size();
        infos.emplace_back(lr,s);
        hsh.emplace(make_pair(lr,s),id);
    };

    apnd("",isl);
    apnd("",isr);
    we.emplace_back(0);
    we.emplace_back(0);
    for(int i=0; i<n; i++)
    {
        string s=str[i];
        string sb;
        for(char c:s)
        {
            sb+=c;
            apnd(sb,isl);
            we.emplace_back(cst[i]);
        }
        sb="";
        for(char c:rev(s))
        {
            sb+=c;
            apnd(rev(sb),isr);
            we.emplace_back(cst[i]);
        }
    }

    // dijkstra
    const i64 inf=1e18;
    vector<i64> dist(infos.size(),inf);
    heap<p64,greater<>> qu;
    qu.emplace(0,0);
    qu.emplace(0,1);
    dist[0]=dist[1]=0;
    while(!qu.empty())
    {
        auto [d,now]=qu.top();
        qu.pop();
        if(d!=dist[now]) continue;
        auto [lr,ns]=infos[now];
        reverse(begin(ns), end(ns));

        // consider edges from now
        vector<int> go;
        if(lr==isl)
        {
            // add right
            for(auto s: str)
            {
                if(ispref(ns,s))
                {
                    int to=hsh[{isr, s.substr(ns.size())}];
                    go.emplace_back(to);
                }
            }
        }
        else
        {
            // add left
            for(auto s:str)
            {
                if(issuf(ns,s))
                {
                    int to=hsh[{isl, s.substr(0,s.size()-ns.size())}];
                    go.emplace_back(to);
                }
            }
        }

        // go
        for(int to: go)
        {
            if(chle(dist[to],dist[now]+we[to]))
            {
                qu.emplace(dist[to],to);
            }
        }
    }
    auto getd=[&](string s, bool lr)
    {
        info ifo{lr,s};
        if(hsh.count(ifo)) return dist[hsh[ifo]];
        return inf;
    };

    // try all possible centers
    i64 ans=inf;
    for(int i=0; i<n; i++)
    {
        auto s=str[i];
        // s=rev(s);
        chle(ans, cst[i]+getd(rev(s),isl));
        chle(ans, cst[i]+getd(rev(s),isr));
        string sb;
        for(auto c: s)
        {
            sb+=c;
            if(ispalin(sb))
            {
                auto rest=s.substr(sb.size());
                chle(ans, cst[i]+getd(rev(rest),isr));
            }
        }
        sb="";
        for(auto c:rev(s))
        {
            sb+=c;
            if(ispalin(sb))
            {
                auto rest=s.substr(0,s.size()-sb.size());
                chle(ans, cst[i]+getd(rev(rest),isl));
            }
        }
    }
    if(ans<inf) cout << ans << "\n";
    else cout<<"-1\n";
}};


Compiler output:
Library\extlib.hpp:2:10: fatal error: ext/array_allocator.h: No such file or directory
compilation terminated.