Problem:
Problem C:
TransferTrain
うなぎは電車に乗るのが好きである. いま，うなぎは駅 A から駅 B へ電車を使って行こうとしている. うなぎは急いでいるので, 最短時間の経路を選ぶことにした. ただし，うなぎは乗り換えが苦手なので, 最短時間の経路が複数ある場合は最も乗り換え回数の少ない経路を選ぶことにした.
$N$ 本の路線がある. $i$ 本目の路線は $a_i$ 個の駅を通っている. $i$ 本目の路線の通る駅名は通る順に $s_{i,0}, ... , s_{i,a_i -1}$ であり, 駅間の所要時間は $t_{i,0}, ...,  t_{i,a_i - 2}$ である. 電車は路線上を上下方向に走っており, 入力で与えられた逆順に乗ることもできる. 複数の路線の同じ駅名は同じ駅を表しており，乗り換えをすることが出来る. 乗り換えには駅や路線によらず $T$ 分かかる.
一本の路線が同じ駅を複数回通ることもある．もし同じ路線，同じ駅の，路線内で異なる位置の駅に移動したい場合は乗り換えをする必要がある. たとえば, C - D - E - F - D - G という路線を使って C から G まで行く場合, 始点から終点まで一本の電車で行くことも，D 駅で乗り換えて C - D, D - G と乗ることもできる.
うなぎが駅 A から駅 B へ行くのにかかる時間と乗り換え回数を求めよ. 電車はとても頻繁に来るので待ち時間は無視してよい.
Constraints
$N$ will be between 1 and 50,000, inclusive.
$T$ will be an integer between 1 and 1,000, inclusive.
At least one train stops at A.
At least one train stops at B.
A and B will be distinct.
$a_i$ will be bigger than or equal to 2.
$a_1 + ... + a_N$ will be between 2 and 100,000, inclusive.
$s_{i,j}$ will contain between 1 and 10 characters, inclusive.
Each character in $s_{i,j}$ will be a letter ('A'-'Z', 'a'-'z').
$t_{i,j}$ will be an integer between 1 and 1,000, inclusive.
Input
入力は以下の形式で与えられる:
$N$ $T$
$A$ $B$
$a_1$
$s_{1,1}$ ... $s_{1,a_1}$
$t_{1,1}$ ... $t_{1,a_1 -1}$
...
$a_N$
$s_{N,1}$ ... $s_{N,a_N}$
$t_{N,1}$ ... $t_{N,a_N-1}$
Output
うなぎが駅 A から駅 B へ行くのにかかる時間と乗り換え回数を空白で区切って1 行に出力せよ.
Sample Input 1
2 10
Warsaw Petersburg
3
Kiev Moscow Petersburg
150 120
3
Moscow Minsk Warsaw
100 150
Sample Output 1
380 1
Sample Input 2
2 10
Warsaw Petersburg
3
Kiev Moscow Petersburg
150 120
2
Minsk Warsaw
150
Sample Output 2
-1

Submitted solutions:

code_snippet: #include<bits/stdc++.h>
using namespace std;

using lint = long long;

constexpr lint LLINF = 1LL << 61;
constexpr pair<lint, lint> INF = make_pair(LLINF, LLINF);

struct edge{
    int to;
    lint cost;
    lint trans;
};

struct node{
    int id;
    pair<lint, lint> cost;

    bool operator<(const node& rhs) const {
        return cost > rhs.cost;
    }
};

template<typename T>
bool chmin(T& a, T b){
    if(a > b){
        a = b;
        return true;
    }
    return false;
}

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);

    int N;
    lint T;
    cin >> N >> T;

    string A, B;
    cin >> A >> B;

    set< pair<string, pair<int, int> > > st;
    set<string> sta;

    vector<int> a(N);
    vector< vector<string> > s(N);
    vector< vector<lint> > t(N);
    for(int i = 0 ; i < N ; ++i){
        cin >> a[i];
        s[i].resize(a[i]);
        for(int j = 0 ; j < a[i] ; ++j){
            cin >> s[i][j];
            st.insert(make_pair(s[i][j], make_pair(i, j)));
            sta.insert(s[i][j]);
        }
        t[i].resize(a[i] - 1);
        for(int j = 0 ; j < a[i] - 1 ; ++j){
            cin >> t[i][j];
        }
    }

    int V = 0;
    map< pair<string, pair<int, int> >, int > mp;
    for(auto x : st){
        mp[x] = V;
        ++V;
    }

    map<string, int> mpsta;
    for(auto x : sta){
        mpsta[x] = V;
        ++V;
    }

    int start = mpsta[A];
    int goal = mpsta[B];

    vector< vector<edge> > g(V);
    for(int i = 0 ; i < N ; ++i){
        for(int j = 0 ; j < a[i] - 1 ; ++j){
            int u = mp[make_pair(s[i][j], make_pair(i, j))];
            int stu = mpsta[s[i][j]];
            int v = mp[make_pair(s[i][j + 1], make_pair(i, j + 1))];
            int stv = mpsta[s[i][j + 1]];
            lint c = t[i][j];
            g[u].push_back({v, c, 0LL});
            g[v].push_back({u, c, 0LL});
            g[u].push_back({stu, T, 0LL});
            g[stu].push_back({u, 0LL, 1LL});
            g[v].push_back({stv, T, 0LL});
            g[stv].push_back({v, 0LL, 1LL});
        }
    }

    vector< pair<lint, lint> > cost(V, INF);
    cost[start] = {0LL, 0LL};
    priority_queue<node> que;
    que.push({start, {0LL, 0LL}});

    while(!que.empty()){
        node now = que.top();
        que.pop();
        if(cost[now.id] < now.cost){
            continue;
        }
        for(auto e : g[now.id]){
            if(chmin(cost[e.to], make_pair(now.cost.first + e.cost, now.cost.second + e.trans))){
                que.push({e.to, make_pair(now.cost.first + e.cost, now.cost.second + e.trans)});
            }
        }
    }

    // for(int i = 0 ; i < V ; ++i){
    //     cout << i << ':';
    //     for(auto e : g[i]){
    //         cout << "   " << e.to << ' ' << e.cost << ' ' << e.trans;
    //     }
    //     cout << endl;
    // }

    // cout << start << ' ' << goal << endl;

    // for(auto x : sta){
    //     cout << x << ' ' << mpsta[x] << ' ' << cost[mpsta[x]].first << ' ' << cost[mpsta[x]].second << endl;
    // }
    // for(int i = 0 ; i < N ; ++i){
    //     for(int j = 0 ; j < a[i] ; ++j){
    //         cout << i << ' ' << s[i][j] << ' ' << mp[make_pair(s[i][j], i)] << ' ' << cost[mp[make_pair(s[i][j], i)]].first << ' ' << cost[mp[make_pair(s[i][j], i)]].second << endl;
    //     }
    // }

    // vector<int> tmp = {10, 5, 1, 3, 8, 2, 4, 9};
    // for(auto hoge : tmp){
    //     cout << cost[hoge].first << ' ' << cost[hoge].second << "  ";
    // }
    // cout << endl;

    if(cost[goal] < INF){
        cout << cost[goal].first - T << ' ' << cost[goal].second - 1 << endl;
    }else{
        cout << -1 << endl;
    }

    return 0;
}
accuracy: 1
time_ms: 270
memory_kb: 59036
final_rank: 17

code_snippet: #include <bits/stdc++.h>

using namespace std;

#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rrep(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)
#define ALL(v) (v).begin(), (v).end()
#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())
#define SZ(v) (int)v.size()
#define MIN(v) *min_element(ALL(v))
#define MAX(v) *max_element(ALL(v))
#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())
#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())

using uint = unsigned int;
using ll = long long int;
using ull = unsigned long long;
using i128 = __int128_t;
using u128 = __uint128_t;
const int inf = 0x3fffffff;
const ll INF = 0x1fffffffffffffff;

template <typename T> inline bool chmax(T &a, T b) {
    if (a < b) {
        a = b;
        return 1;
    }
    return 0;
}
template <typename T> inline bool chmin(T &a, T b) {
    if (a > b) {
        a = b;
        return 1;
    }
    return 0;
}
template <typename T, typename U> T ceil(T x, U y) {
    assert(y != 0);
    if (y < 0)
        x = -x, y = -y;
    return (x > 0 ? (x + y - 1) / y : x / y);
}
template <typename T, typename U> T floor(T x, U y) {
    assert(y != 0);
    if (y < 0)
        x = -x, y = -y;
    return (x > 0 ? x / y : (x - y + 1) / y);
}
template <typename T> int popcnt(T x) {
    return __builtin_popcountll(x);
}
template <typename T> int topbit(T x) {
    return (x == 0 ? -1 : 63 - __builtin_clzll(x));
}
template <typename T> int lowbit(T x) {
    return (x == 0 ? -1 : __builtin_ctzll(x));
}

template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
    os << "P(" << p.first << ", " << p.second << ")";
    return os;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
    os << "{";
    for (int i = 0; i < vec.size(); i++) {
        os << vec[i] << (i + 1 == vec.size() ? "" : ", ");
    }
    os << "}";
    return os;
}
template <typename T, typename U>
ostream &operator<<(ostream &os, const map<T, U> &map_var) {
    os << "{";
    for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {
        os << "(" << itr->first << ", " << itr->second << ")";
        itr++;
        if (itr != map_var.end())
            os << ", ";
        itr--;
    }
    os << "}";
    return os;
}
template <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) {
    os << "{";
    for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {
        os << *itr;
        ++itr;
        if (itr != set_var.end())
            os << ", ";
        itr--;
    }
    os << "}";
    return os;
}
#ifdef LOCAL
#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)
#else
#define show(...) true
#endif
template <typename T> void _show(int i, T name) {
    cerr << '\n';
}
template <typename T1, typename T2, typename... T3>
void _show(int i, const T1 &a, const T2 &b, const T3 &...c) {
    for (; a[i] != ',' && a[i] != '\0'; i++)
        cerr << a[i];
    cerr << ":" << b << " ";
    _show(i + 1, a, c...);
}

/**
 * @brief template
 */

map<string,int> mp;
int Cur = 0;

int getstring() {
    string S;
    cin >> S;
    if (mp.count(S)) return mp[S];
    mp[S] = Cur;
    Cur++;
    return Cur-1;
}

int main() {
    int M;
    ll T;
    cin >> M >> T;
    rep(i,0,2) getstring();
    vector<vector<int>> St(M);
    vector<vector<int>> Ord(M);
    vector<vector<ll>> Di(M);
    int N = 0;
    rep(i,0,M) {
        int A;
        cin >> A;
        St[i].resize(A), Ord[i].resize(A), Di[i].resize(A-1);
        rep(j,0,A) St[i][j] = getstring(), Ord[i][j] = N, N++;
        rep(j,0,A-1) cin >> Di[i][j];
    }
    int K = Cur;
    vector<vector<pair<int,pair<ll,int>>>> G(N+K);
    rep(i,0,M) {
        int A = St[i].size();
        rep(j,0,A) {
            G[St[i][j]].push_back({K+Ord[i][j],{T,1}});
            G[K+Ord[i][j]].push_back({St[i][j],{0LL,0}});
        }
        rep(j,0,A-1) {
            G[K+Ord[i][j]].push_back({K+Ord[i][j+1],{Di[i][j],0}});
            G[K+Ord[i][j+1]].push_back({K+Ord[i][j],{Di[i][j],0}});
        }
    }
    vector<pair<ll,int>> DP(N+K,{INF,inf});
    priority_queue<tuple<ll,int,int>,vector<tuple<ll,int,int>>,greater<tuple<ll,int,int>>> PQ;
    DP[0] = {0LL,0};
    PQ.push({0LL,0,0});
    while(!PQ.empty()) {
        auto [D,C,P] = PQ.top();
        PQ.pop();
        if (D > DP[P].first || (D == DP[P].first && C > DP[P].second)) continue;
        for (auto E : G[P]) {
            ll ND = D + E.second.first, NC = C + E.second.second, NP = E.first;
            if (chmin(DP[NP],{ND,NC})) {
                PQ.push({ND,NC,NP});
            }
        }
    }
    if (DP[1].first == INF) cout << -1 << endl;
    else cout << DP[1].first - T << ' ' << DP[1].second - 1 << endl;
}
accuracy: 1
time_ms: 100
memory_kb: 32800
final_rank: 8

code_snippet: #include <bits/stdc++.h>

template <class T>
struct Converter {
    std::map<T, int> converter;
    int convert(const T& s) {
        auto it{converter.find(s)};
        if (it == converter.end()) {
            int res{(int)converter.size()};
            return converter[s] = res;
        }
        else {
            return it->second;
        }
    }
    int input() {
        T s;
        std::cin >> s;
        return convert(s);
    }
    int size() const {
        return (int)converter.size();
    }
    bool contains(const T& s) const {
        return converter.find(s) != converter.end();
    }
};

using Cost = std::pair<long long, int>;
Cost operator+(const Cost& l, const Cost& r) {
    return Cost{l.first+r.first, l.second+r.second};
}

int main() {
    std::cin.tie(nullptr)->sync_with_stdio(false);

    int N, T;
    std::cin >> N >> T;
    Converter<std::string> stat;
    int st{stat.input()}, go{stat.input()};
    std::vector<int> a(N);
    std::vector<std::vector<int>> s(N);
    std::vector<std::vector<int>> t(N);
    for (int i{} ; i < N ; i++) {
        std::cin >> a[i];
        s[i] = std::vector<int>(a[i]);
        t[i] = std::vector<int>(a[i] - 1);
        for (auto& x : s[i]) x = stat.input();
        for (auto& x : t[i]) std::cin >> x;
    }
    Converter<std::tuple<int, int, int>> vs;
    for (int i{} ; i < stat.size() ; i++) {
        vs.convert(std::tuple{i, -1, -1});
    }
    for (int i{} ; i < N ; i++) {
        int j{};
        for (auto& x : s[i]) {
            vs.convert(std::tuple{x, i, j});
            j++;
        }
    }
    std::vector<std::vector<std::pair<int, Cost>>> g(vs.size());
    for (int i{} ; i < N ; i++) {
        for (int j{} ; j < a[i] ; j++) {
            assert(vs.contains(std::tuple{s[i][j], i, j}));         
            assert(vs.contains(std::tuple{s[i][j], -1, -1}));         
            int v{vs.convert(std::tuple{s[i][j], i, j})};
            int x{vs.convert(std::tuple{s[i][j], -1, -1})}; 
            g[x].emplace_back(v, Cost{(long long)T, 1});
            g[v].emplace_back(x, Cost{0LL, 0});
            if (j - 1 >= 0) {
                int p{vs.convert(std::tuple{s[i][j - 1], i, j - 1})};
                g[p].emplace_back(v, Cost{(long long)t[i][j - 1], 0});
                g[v].emplace_back(p, Cost{(long long)t[i][j - 1], 0});
            }
            if (j + 1 < a[i]) {
                int p{vs.convert(std::tuple{s[i][j + 1], i, j + 1})};
                g[p].emplace_back(v, Cost{(long long)t[i][j], 0});
                g[v].emplace_back(p, Cost{(long long)t[i][j], 0});
            }
        }
    }
    using qt = std::pair<Cost, int>;
    const Cost INF{(long long)4e18, (int)1e9};
    std::vector<Cost> dist(vs.size(), INF);
    std::priority_queue<qt, std::vector<qt>, std::greater<qt>> que;
    assert(vs.contains(std::tuple{st, -1, -1}));
    assert(vs.contains(std::tuple{go, -1, -1}));
    st = vs.convert(std::tuple{st, -1, -1});
    go = vs.convert(std::tuple{go, -1, -1});
    dist[st] = std::pair{0LL, 0};
    que.emplace(dist[st], st);
    while (que.size()) {
        auto [d, v]{que.top()};
        que.pop();
        if (dist[v] < d) continue;
        for (const auto& [x, w] : g[v]) {
            if (dist[x] > d + w) {
                dist[x] = d + w;
                que.emplace(dist[x], x);
            }
        }
    }
    if (dist[go] == INF) {
        std::cout << -1 << '\n';
    }
    else {
        std::cout << dist[go].first - T << ' ' << dist[go].second - 1 << '\n';
    }
}
accuracy: 1
time_ms: 200
memory_kb: 49768
final_rank: 16

code_snippet: #include <bits/stdc++.h>

using T = std::pair<long long, int>;
T add(T lhs, T rhs) {
    return {lhs.first + rhs.first, lhs.second + rhs.second};
}

const T INF = {1e18, 1e9};

struct Dijkstra {
    int n;
    std::vector<std::vector<std::pair<int, T>>> graph;
    Dijkstra(): n(0) {}
    void add_vertex(int d) {
        n += d;
        graph.resize(n);
    }
    void add_edge(int u, int v, T w) {
        assert(0 <= u && u < n);
        assert(0 <= v && v < n);
        graph[u].emplace_back(v, w);
        graph[v].emplace_back(u, w);
    }
    void add_diedge(int u, int v, T w) {
        assert(0 <= u && u < n);
        assert(0 <= v && v < n);
        graph[u].emplace_back(v, w);
    }
    std::vector<T> run(int start) {
        using P = std::pair<T, int>;
        std::vector<T> dist(n, INF);
        dist[start] = {0, 0};
        std::priority_queue<P, std::vector<P>, std::greater<P>> que;
        que.emplace(dist[start], start);
        while (!que.empty()) {
            auto [d, u] = que.top();
            que.pop();
            if (dist[u] < d) continue;
            for (auto [v, w]: graph[u]) {
                if (dist[v] > add(dist[u], w)) {
                    dist[v] = add(dist[u], w);
                    que.emplace(dist[v], v);
                }
            }
        }
        return dist;
    }
};

int main() {
    int n, t;
    std::cin >> n >> t;
    std::string start, goal;
    std::cin >> start >> goal;

    std::map<std::string, std::vector<int>> stations;
    int offset = 0;
    Dijkstra dijkstra;
    for (int i = 0; i < n; i++) {
        int m;
        std::cin >> m;
        std::vector<std::string> s(m);
        for (int j = 0; j < m; j++) std::cin >> s[j];
        std::vector<int> w(m - 1);
        for (int j = 0; j + 1 < m; j++) std::cin >> w[j];

        dijkstra.add_vertex(m);
        for (int j = 0; j < m; j++) stations[s[j]].push_back(offset + j);
        for (int j = 0; j + 1 < m; j++) {
            dijkstra.add_edge(j + offset, j + offset + 1, {w[j], 0});
        }
        offset += m;
    }
    dijkstra.add_vertex(stations.size());
    int start_i = -1;
    int goal_i = -1;
    for (auto [key, vec]: stations) {
        if (key == start) start_i = offset;
        if (key == goal) goal_i = offset;
        for (int v: vec) {
            dijkstra.add_diedge(v, offset, {0, 1});
            dijkstra.add_diedge(offset, v, {t, 0});
        }
        offset++;
    }
    auto ans = dijkstra.run(start_i)[goal_i];
    if (ans == INF) {
        std::cout << -1 << '\n';
    } else {
        std::cout << ans.first - t << ' ' << ans.second - 1 << '\n';
    }
}
accuracy: 1
time_ms: 120
memory_kb: 34184
final_rank: 13

code_snippet: #include<bits/stdc++.h>
#define mkpr(a, b) make_pair(a, b)
using namespace std;
using ll = long long;
template<class T> using min_heap = priority_queue<T, vector<T>, greater<T>>;

const int INF = 1e9;
const ll LINF = 1e18;

pair<ll, ll> add(const pair<ll, ll>& a, const pair<ll, ll>& b){
    return make_pair(a.first +  b.first, a.second + b.second);
}

int main(){
    int N, T;
    string A, B;
    cin >> N >> T >> A >> B;
    map<string, int> mp;
    vector<int> idsB;
    mp[A] = 0;
    mp[B] = 1;
    
    const int PARAM = 200010;
    const int HALF =  100005;
    vector<vector<pair<ll, pair<ll, ll>>>> G(PARAM);
    ll asum = 0;
    for(int i = 0; i < N; i++){
        int a; cin >> a;
        vector<int> id(a);
        vector<int> tim(a);
        for(int j = 0; j < a; j++){
            string s; cin >> s;
            if(mp.find(s) == mp.end()) mp[s] = mp.size();
            id[j] = mp[s];
            if(id[j] == 1) idsB.emplace_back(j + asum);
        }
        for(int j = 0; j < a - 1; j++) cin >> tim[j];

        for(int j = 0; j < a - 1; j++){
            int l = j + asum, r = j + asum + 1;
            G[l].emplace_back(mkpr(r, make_pair(tim[j], 0ll)));
            G[r].emplace_back(mkpr(l, make_pair(tim[j], 0ll)));
        }
        for(int j = 0; j < a; j++){
            int l = j + asum, r = id[j] + HALF;
            G[l].emplace_back(r, make_pair(T, 1ll));
            G[r].emplace_back(l, make_pair(0ll, 0ll));
        }

        asum += a;
    }
    //for(int i = 0; i < mp.size(); i++){
    //    G[i + HALF].emlace_back(i, make_pair(0ll, 0ll));
    //}

    // dijkstra
    min_heap<pair<pair<ll, ll>, ll>> heap;
    heap.push(make_pair(make_pair(0ll, 0ll), 0 + HALF));
    vector<pair<ll, ll>> dist(PARAM, make_pair(LINF, LINF));
    dist[0 + HALF] = make_pair(0ll, 0ll);

    while(!heap.empty()){
        auto [cst, u] = heap.top();
        heap.pop();
        if(cst > dist[u]) continue;
        for(auto [v, road] : G[u]){
            if(add(cst, road) < dist[v]){
                dist[v] = add(cst, road);
                heap.push(make_pair(add(cst, road), v));
            }
        }
    }

    if(idsB.empty()){
        cout << -1 << endl;
        return 0;
    }
    auto ans = dist[idsB[0]];
    for(auto id : idsB){
        ans = min(ans, dist[id]);
    }

    if(ans.first < LINF) cout << ans.first << " " << ans.second << endl;
    else cout << -1 << endl;
}
accuracy: 1
time_ms: 110
memory_kb: 32408
final_rank: 9

code_snippet: #include <bits/stdc++.h>
using namespace std;
#if __has_include(<atcoder/all>)
#include <atcoder/all>
using namespace atcoder;
template<int mod>istream &operator>>(istream &is,static_modint<mod> &a){long long b;is>>b;a=b;return is;}
istream &operator>>(istream &is,modint &a){long long b;cin>>b;a=b;return is;}
#endif
#ifdef LOCAL
#include "debug.h"
#else
#define debug(...) static_cast<void>(0)
#define debugg(...) static_cast<void>(0)
#define esper(...) static_cast<void>(0)
template<typename T1,typename T2>ostream &operator<<(ostream &os,const pair<T1,T2>&p){os<<p.first<<' '<<p.second;return os;}
#endif
using ll=long long;
using ull=unsigned long long;
using P=pair<ll,ll>;
template<typename T>using minque=priority_queue<T,vector<T>,greater<T>>;
template<typename T>bool chmax(T &a,const T &b){return (a<b?(a=b,true):false);}
template<typename T>bool chmin(T &a,const T &b){return (a>b?(a=b,true):false);}
template<typename T1,typename T2>istream &operator>>(istream &is,pair<T1,T2>&p){is>>p.first>>p.second;return is;}
template<typename T>istream &operator>>(istream &is,vector<T> &a){for(auto &i:a)is>>i;return is;}
template<typename T1,typename T2>void operator++(pair<T1,T2>&a,int n){a.first++,a.second++;}
template<typename T1,typename T2>void operator--(pair<T1,T2>&a,int n){a.first--,a.second--;}
template<typename T>void operator++(vector<T>&a,int n){for(auto &i:a)i++;}
template<typename T>void operator--(vector<T>&a,int n){for(auto &i:a)i--;}
#define reps(i,a,n) for(int i=(a);i<(n);i++)
#define rep(i,n) reps(i,0,n)
#define all(x) x.begin(),x.end()
#define pcnt(x) __builtin_popcountll(x)
#define fin(x) return cout<<x<<'\n',static_cast<void>(0)
ll myceil(ll a,ll b){return (a+b-1)/b;}
template<typename T,size_t n,size_t id=0>
auto vec(const int (&d)[n],const T &init=T()){
  if constexpr (id<n)return vector(d[id],vec<T,n,id+1>(d,init));
  else return init;
}
void SOLVE();
int main(){
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout<<fixed<<setprecision(16);
  #ifdef LOCAL
  clock_t start=clock();
  #endif
  int testcase=1;
  //cin>>testcase;
  for(int i=0;i<testcase;i++){
    SOLVE();
  }
  #ifdef LOCAL
  cerr<<"time:";
  cerr<<(clock()-start)/1000;
  cerr<<"ms\n";
  #endif
}
void SOLVE(){
  int n,t2;
  cin>>n>>t2;
  string st,gl;
  cin>>st>>gl;
  vector<int>a(n);
  vector<string>z;
  vector<vector<string>>s(n);
  vector<vector<int>>t(n);
  rep(i,n){
    cin>>a[i];
    s[i].resize(a[i]);
    t[i].resize(a[i]-1);
    cin>>s[i]>>t[i];
    rep(j,a[i])z.push_back(s[i][j]);
    rep(j,a[i]-1)t[i][j]*=2;
  }
  sort(all(z)),z.erase(unique(all(z)),z.end());
  int station=z.size();
  vector<int>rsum(n+1,0);
  rep(i,n)rsum[i+1]=rsum[i]+a[i];
  auto getid=[&](int rosenid,int idx)->int {return rsum[rosenid]+idx;};
  vector<vector<pair<int,int>>>to(station+rsum[n]);
  auto addedge=[&](int u,int v,int cost){
    to[u].push_back({v,cost});
    to[v].push_back({u,cost});
  };
  rep(i,n){
    rep(j,a[i]-1){
      addedge(getid(i,j),getid(i,j+1),t[i][j]);
    }
    rep(j,a[i]){
      int stid=lower_bound(all(z),s[i][j])-z.begin();
      addedge(getid(i,j),rsum[n]+stid,t2);
    }
  }
  int start=lower_bound(all(z),st)-z.begin();
  int goal=lower_bound(all(z),gl)-z.begin();
  if(start==station||goal==station)fin(-1);
  start+=rsum[n];
  goal+=rsum[n];
  vector<pair<int,int>>dst(to.size(),{1e9,0});
  auto chminpair=[&](pair<int,int>&x,pair<int,int>y)->bool {
    int cx=x.first+x.second*t2;
    int cy=y.first+y.second*t2;
    if(cx>cy){
      x=y;
      return true;
    }
    return false;
  };
  dst[start]={0,0};
  minque<pair<int,pair<pair<int,int>,int>>>que;
  que.push({0,{{0,0},start}});
  while(!que.empty()){
    auto [d,norx]=que.top();
    auto [norikae,x]=norx;
    que.pop();
    if(dst[x]!=norikae)continue;
    for(auto i:to[x]){
      pair<int,int>nxtnorikae=norikae;
      int nc=d;
      if((x<rsum[n])==(i.first<rsum[n])){
        nxtnorikae.first+=i.second;
        nc+=i.second;
      }
      else{
        nxtnorikae.second++;
        assert(i.second==t2);
        nc+=i.second;
      }
      if(chminpair(dst[i.first],nxtnorikae))que.push({nc,{dst[i.first],i.first}});
    }
  }
  pair<int,int>ans=dst[goal];
  if(ans.first==1e9)cout<<-1<<endl;
  else{
    ans.first/=2;
    ans.second/=2;
    ans.second--;
    ans.first+=ans.second*t2;
    cout<<ans<<endl;
  }
}
accuracy: 1
time_ms: 70
memory_kb: 24868
final_rank: 2

code_snippet: #line 2 "cp-library/src/cp-template.hpp"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using uint = unsigned int;
using ull  = unsigned long long;
using i32 = int;
using u32 = unsigned int;
using i64 = long long;
using u64 = unsigned long long;
using i128 = __int128_t;
template < class T > bool chmin(T& a, T b) { if(a > b) { a = b; return true; } return false; }
template < class T > bool chmax(T& a, T b) { if(a < b) { a = b; return true; } return false; }
template < class T, class U > T ceil (T x, U y) { return (x > 0 ? (x + y - 1) / y :           x / y); }
template < class T, class U > T floor(T x, U y) { return (x > 0 ?           x / y : (x - y + 1) / y); }
int popcnt(i32 x) { return __builtin_popcount(x); }
int popcnt(u32 x) { return __builtin_popcount(x); }
int popcnt(i64 x) { return __builtin_popcountll(x); }
int popcnt(u64 x) { return __builtin_popcountll(x); }

#line 2 "cp-library/src/utility/rep_itr.hpp"
template < class T > struct itr_rep {
    T i, d;
    constexpr itr_rep(const T i) noexcept : i(i), d(1) {}
    constexpr itr_rep(const T i, const T d) noexcept : i(i), d(d) {}
    void operator++() noexcept { i += d; }
    constexpr int operator*() const noexcept { return i; }
    constexpr bool operator!=(const itr_rep x) const noexcept { return d > 0 ? i < x.i : i > x.i; }
};

template < class T > struct rep {
    const itr_rep< T > s, t;
    constexpr rep(const T t) noexcept : s(0), t(t) {}
    constexpr rep(const T s, const T t) noexcept : s(s), t(t) {}
    constexpr rep(const T s, const T t, const T d) noexcept : s(s, d), t(t, d) {}
    constexpr auto begin() const noexcept { return s; }
    constexpr auto end  () const noexcept { return t; }
};

template < class T > struct revrep {
    const itr_rep < T > s, t;
    constexpr revrep(const T t) noexcept : s(t - 1, -1), t(-1, -1) {}
    constexpr revrep(const T s, const T t) noexcept : s(t - 1, -1), t(s - 1, -1) {}
    constexpr revrep(const T s, const T t, const T d) noexcept : s(t - 1, -d), t(s - 1, -d) {}
    constexpr auto begin() const noexcept { return s; }
    constexpr auto end  () const noexcept { return t; }
};
#line 3 "cp-library/src/utility/io.hpp"

/* 128bit integer */
istream& operator>>(istream& is, i128& x) {
    std::string s; is >> s;
    int pm = (s[0] == '-');
    x = 0;
    for(int i : rep(pm, int(s.size()))) x = x * 10 + (s[i] - '0');
    if(pm) x *= -1;
    return is;
}
ostream& operator<<(ostream& os, const i128& x) {
    if(x == 0) return os << '0';
    i128 y = x;
    if(y < 0) { os << '-'; y *= -1; }
    std::vector<int> ny;
    while(y > 0) { ny.push_back(y % 10); y /= 10; }
    for(int i : revrep(ny.size())) os << ny[i];
    return os;
}

template < class S, class T > istream& operator>>(istream& is,       std::pair< S, T >& x) { is >> x.first        >> x.second; return is; }
template < class S, class T > ostream& operator<<(ostream& os, const std::pair< S, T >& x) { os << x.first << " " << x.second; return os; }

namespace scanner {
    struct sca {
        template < class T > operator T() {
            T s; std::cin >> s; return s;
        }
    };
    struct vec {
        int n;
        vec(int n) : n(n) {}
        template < class T > operator std::vector< T >() {
            std::vector< T > v(n);
            for(T& x : v) std::cin >> x;
            return v;
        }
    };
    struct mat {
        int h, w;
        mat(int h, int w) : h(h), w(w) {}
        template < class T > operator std::vector< std::vector< T > >() {
            std::vector m(h, std::vector< T >(w));
            for(std::vector< T >& v : m) for(T& x : v) std::cin >> x;
            return m;
        }
    };
    struct speedup {
        speedup() {
            std::cin.tie(0);
            std::ios::sync_with_stdio(0);
        }
    } speedup_instance;
}
scanner::sca in() { return scanner::sca(); }
scanner::vec in(int n) { return scanner::vec(n); }
scanner::mat in(int h, int w) { return scanner::mat(h, w); }

namespace printer {
    void precision(int d) { std::cout << std::fixed << std::setprecision(d); }
    void flush() { std::cout.flush(); }
}

template < class T >
ostream& operator<<(ostream& os, const std::vector< T > a) {
    int n = a.size();
    for(int i : rep(n)) { os << a[i]; if(i != n - 1) os << ' '; }
    return os;
}

int print() { std::cout << '\n'; return 0; }
template < class head, class... tail > int print(head&& h, tail&&... t) {
    std::cout << h; if(sizeof...(tail)) std::cout << ' ';
    return print(std::forward<tail>(t)...);
}
template < class T > int print_n(const std::vector< T > a) {
    int n = a.size();
    for(int i : rep(n)) std::cout << a[i] << "\n";
    return 0;
}


#line 2 "cp-library/src/utility/key_val.hpp"

template < class K, class V >
struct key_val {
    K key; V val;
    key_val() {}
    key_val(K key, V val) : key(key), val(val) {}
    template < std::size_t Index >
    std::tuple_element_t< Index, key_val >& get() {
        if constexpr (Index == 0) return key;
        if constexpr (Index == 1) return val;
    }
};

namespace std {

template < class K, class V > struct tuple_size < key_val< K, V > > : integral_constant< size_t, 2 > {};
template < class K, class V > struct tuple_element < 0, key_val< K, V > > { using type = K; };
template < class K, class V > struct tuple_element < 1, key_val< K, V > > { using type = V; };

}
#line 2 "cp-library/src/utility/vec_op.hpp"
template < class T > key_val< int, T > max_of(const vector< T >& a) {
    int i = std::max_element(a.begin(), a.end()) - a.begin();
    return {i, a[i]};
}
template < class T > key_val< int, T > min_of(const vector< T >& a) {
    int i = std::min_element(a.begin(), a.end()) - a.begin();
    return {i, a[i]};
}
template < class S, class T > S sum_of(const vector< T >& a) {
    S sum = 0;
    for(const T x : a) sum += x;
    return sum;
}
template < class S, class T > vector< S > freq_of(const vector< T >& a, T L, T R) {
    vector< S > res(R - L, S(0));
    for(const T x : a) res[x - L] += 1;
    return res;
}
template < class S, class T > struct prefix_sum {
    vector< S > s;
    prefix_sum(const vector< T >& a) : s(a) {
        s.insert(s.begin(), S(0));
        for(int i : rep(a.size())) s[i + 1] += s[i];
    }
    // [L, R)
    S sum(int L, int R) { return s[R] - s[L]; }
};
#line 3 "cp-library/src/utility/heap.hpp"

template < class T > using heap_min = std::priority_queue< T, std::vector< T >, std::greater< T > >;
template < class T > using heap_max = std::priority_queue< T, std::vector< T >, std::less< T > >;

#line 27 "cp-library/src/cp-template.hpp"

#line 1 "cp-library/src/algorithm/bin_search.hpp"
template < class T, class F >
T bin_search(T ok, T ng, F f) {
    while(abs(ng - ok) > 1) {
        T mid = (ok + ng) / 2;
        (f(mid) ? ok : ng) = mid;
    }
    return ok;
}

template < class T, class F >
T bin_search_real(T ok, T ng, F f, int step = 80) {
    while(step--) {
        T mid = (ok + ng) / 2;
        (f(mid) ? ok : ng) = mid;
    }
    return ok;
}
#line 2 "cp-library/src/algorithm/argsort.hpp"

template < class T > std::vector< int > argsort(const std::vector< T > &a) {
    std::vector< int > ids((int)a.size());
    std::iota(ids.begin(), ids.end(), 0);
    std::sort(ids.begin(), ids.end(), [&](int i, int j) {
        return a[i] < a[j] || (a[i] == a[j] && i < j);
    });
    return ids;
}
#line 1 "macro.hpp"
namespace macro {

using size_type = int;
template < class container > void  sort(container& a) { std::sort(std:: begin(a), std:: end(a)); }
template < class container > void rsort(container& a) { std::sort(std::rbegin(a), std::rend(a)); }
template < class container > void reverse(container& a) { std::reverse(std::begin(a), std::end(a)); }
template < class container > void unique(container& a) {
    std::sort(std::begin(a), std::end(a));
    a.erase(std::unique(std::begin(a), std::end(a)), std::end(a));
}
template < class container > container  sorted(const container& a) { container b = a;  sort(b); return std::move(b); }
template < class container > container rsorted(const container& a) { container b = a; rsort(b); return std::move(b); }
template < class container, class compare > void sort(container& a, const compare& cmp) { std::sort(std::begin(a), std::end(a), cmp); }
template < class container, class compare > container sorted(const container& a, const compare& cmp) { container b = a; sort(b, cmp); return std::move(b); }
template < class container, class value > size_type lower_bound(const container& a, const value& x) { return std::lower_bound(std::begin(a), std::end(a), x) - std::begin(a); }
template < class container, class value > size_type upper_bound(const container& a, const value& x) { return std::upper_bound(std::begin(a), std::end(a), x) - std::begin(a); }

const std::vector<std::pair<size_type, size_type>> dir4 = { {+1,  0}, {-1,  0}, { 0, +1}, { 0, -1} };
const std::vector<std::pair<size_type, size_type>> dir8 = { {-1, -1}, {-1,  0}, {-1, +1}, { 0, -1}, { 0, +1}, {+1, -1}, {+1,  0}, {+1, +1} };

#ifdef _DEBUG
#define debug(x) std::cout << "[" << __LINE__ << "] " << #x << ": " << x << std::endl
#else
#define debug(x)
#endif

template < class container > void concat(container& a, const container& b) {
    a.insert(std::end(a), std::begin(b), std::end(b));
}
std::vector<size_type> iota(const size_type n) {
    std::vector<size_type> I(n);
    std::iota(std::begin(I), std::end(I), 0);
    return I;
}
template < class container > std::vector<size_type> sort_idx(const container& a) {
    const size_type n = a.size();
    std::vector<size_type> I = iota(n);
    std::sort(std::begin(I), std::end(I), [&](size_type i, size_type j) { return a[i] < a[j] or (a[i] == a[j] and i < j); });
    return I;
}
template < class container, class compare > std::vector<size_type> sort_idx(const container& a, const compare& cmp) {
    const size_type n = a.size();
    std::vector<size_type> I = iota(n);
    std::sort(std::begin(I), std::end(I), [&](size_type i, size_type j) { return cmp(a[i], a[j]) or (a[i] == a[j] and i < j); });
    return std::move(I);
}

struct grid {
    using size_type = int;
    size_type H, W;
    grid(const size_type H, const size_type W) : H(H), W(W) {}
    bool contains(const size_type i, const size_type j) {
        return 0 <= i and i < H and 0 <= j and j < W;
    }
};

using f64 = long double;

} // namespace macro

using namespace macro;
#line 3 "A.cpp"

int main() {
    int N = in(), T = in();

    map<string, int> mp;
    int sz = 0;
    auto input = [&]() {
        string s = in();
        if(mp.count(s)) return mp[s];
        return mp[s] = sz++;
    };

    int A = input(), B = input();
    vector<vector<int>> s(N), t(N);
    int sum_a = 0;
    for(int i : rep(N)) {
        int a = in(); sum_a += a;
        s[i].resize(a);
        for(int j : rep(a)) s[i][j] = input();
        t[i].resize(a - 1);
        for(int j : rep(a - 1)) t[i][j] = in();
    }

    vector<vector<pair<int,pair<int,int>>>> G(sum_a + sz);
    int now_a = 0;
    for(int i : rep(N)) {
        const int a = s[i].size();
        for(int j : rep(a - 1)) {
            G[now_a + j].push_back({now_a + (j + 1), {t[i][j], 0}});
            G[now_a + (j + 1)].push_back({now_a + j, {t[i][j], 0}});
        }
        for(int j : rep(a)) {
            G[now_a + j].push_back({sum_a + s[i][j], {0, 0}});
            G[sum_a + s[i][j]].push_back({now_a + j, {T, 1}});
        }
        now_a += a;
    }
    
    const int INF = 1e9;
    vector<pair<int,int>> dist(sum_a + sz, {INF, INF});
    heap_min<pair<pair<int,int>, int>> q;
    q.push({dist[sum_a + A] = {0, 0}, sum_a + A});
    while(!q.empty()){
        auto [uc, ui] = q.top(); q.pop();
        if(uc != dist[ui]) continue;
        for(auto [vi, vc] : G[ui]) {
            pair<int,int> new_dist = {uc.first + vc.first, uc.second + vc.second};
            if(chmin(dist[vi], new_dist)) q.push({dist[vi], vi});
        }
    }

    auto [time, cnt] = dist[sum_a + B];
    if(time == INF) {
        print(-1);
    } else {
        print(time - T, cnt - 1);
    }
}
accuracy: 1
time_ms: 80
memory_kb: 23528
final_rank: 3

code_snippet: #include <queue>
#include <string>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

struct money {
	int t, n;
	money operator+(const money& m) const {
		return money{t + m.t, n + m.n};
	}
	bool operator<(const money& m) const {
		return t != m.t ? t < m.t : n < m.n;
	}
};

struct edge {
	int to; money cost;
};

struct state {
	int pos; money cost;
	bool operator<(const state& s) const {
		return s.cost < cost;
	}
};

int main() {
	// step #1. input & make graph
	cin.tie(0);
	ios::sync_with_stdio(false);
	int N, T; string A, B;
	cin >> N >> T >> A >> B;
	vector<int> L(N);
	vector<vector<string> > S(N);
	vector<vector<int> > X(N);
	vector<vector<edge> > G;
	vector<string> col;
	vector<int> csize(N + 1);
	for (int i = 0; i < N; i++) {
		cin >> L[i];
		S[i].resize(L[i]);
		X[i].resize(L[i] - 1);
		for (int j = 0; j < L[i]; j++) {
			cin >> S[i][j];
		}
		for (int j = 0; j < L[i] - 1; j++) {
			cin >> X[i][j];
		}
		csize[i + 1] = csize[i] + L[i];
		G.resize(csize[i + 1]);
		for (int j = 0; j < L[i] - 1; j++) {
			G[csize[i] + j].push_back(edge{csize[i] + j + 1, money{X[i][j], 0}});
			G[csize[i] + j + 1].push_back(edge{csize[i] + j, money{X[i][j], 0}});
		}
		col.insert(col.end(), S[i].begin(), S[i].end());
	}
	sort(col.begin(), col.end());
	col.erase(unique(col.begin(), col.end()), col.end());
	G.resize(csize[N] + col.size());
	for (int i = 0; i < N; i++) {
		for (int j = 0; j < L[i]; j++) {
			int pos = lower_bound(col.begin(), col.end(), S[i][j]) - col.begin();
			G[csize[i] + j].push_back(edge{csize[N] + pos, money{T, 1}});
			G[csize[N] + pos].push_back(edge{csize[i] + j, money{0, 0}});
		}
	}

	// step #2. dijkstra
	int start = find(col.begin(), col.end(), A) - col.begin();
	int goal = find(col.begin(), col.end(), B) - col.begin();
	if (start == col.size() || goal == col.size()) {
		cout << -1 << endl;
	}
	else {
		start += csize[N];
		goal += csize[N];
		const int INF = 2012345678;
		vector<money> dist(csize[N] + col.size(), money{INF, INF});
		vector<bool> vis(csize[N] + col.size(), false);
		priority_queue<state> que;
		dist[start] = money{0, 0};
		que.push(state{start, money{0, 0}});
		while (!que.empty()) {
			state u = que.top();
			que.pop();
			if (!vis[u.pos]) {
				vis[u.pos] = true;
				for (edge e : G[u.pos]) {
					if (u.cost + e.cost < dist[e.to]) {
						dist[e.to] = u.cost + e.cost;
						que.push(state{e.to, dist[e.to]});
					}
				}
			}
		}
		if (dist[goal].t != INF) {
			cout << dist[goal].t - T << ' ' << dist[goal].n - 1 << endl;
		}
		else {
			cout << -1 << endl;
		}
	}

	return 0;
}
accuracy: 1
time_ms: 80
memory_kb: 25664
final_rank: 4

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;

#define rng(i, l, r) for(int i = int(l); i < int(r); i++)
#define rep(i, n) rng(i, 0, n)
#define sz(v) int(v.size())
#define foa(s, v) for(auto& s : v)
#define all(v) v.begin(), v.end()

// clang-format off
template <class T> using V = vector<T>;
template <class T> bool chmin(T &a, T b) { return a > b ? a = b, 1 : 0; }
template <class T> bool chmax(T &a, T b) { return a < b ? a = b, 1 : 0; }

template<class T>
ostream& operator<<(ostream& os, const vector<T> &vec) {
	os << "{ ";
	foa(c, vec) {
		os << c << ", ";
	}
	os << "}";
	return os;
}

template<class T>
ostream& operator<<(ostream& os, const vector<vector<T>> &vec) {
	os << "{ ";
	foa(c, vec) {
		os << "\n\t" << c << ", ";
	}
	os << "\t}";
	return os;
}


#ifdef LOCAL
#define debug(x) cerr << #x << ": " << x << endl
#else
#define debug(x) void(0)
#endif

constexpr ll INF = 4e18;
constexpr ll md = 1e9 + 7;
int n;
void solve() {

	const int lines = n;
	ll time_norikae; cin >> time_norikae;	

	map<string, int> name_id;

	string start, goal;
	cin >> start >> goal;

	name_id[start] = 1;
	name_id[goal] = 1;

	vector<vector<string>> terminals(n);
	vector<vector<ll>> times(n);
	rep(j, n) {
		auto& row = terminals[j];
		int a; cin >> a;
		rep(i, a) {
			string s; cin >> s;
			row.push_back(s);
			name_id[s] = 1;
		}
		rep(i, a-1) {
			ll tm; cin >> tm;
			times[j].push_back(tm);
		}
	}

	int ver = 0;
	foa(t, name_id) t.second = ver++;

	vector<vector<int>> terminal_ids(n);
	rep(j, n) {
		auto& row =terminal_ids[j];
		row.resize(sz(terminals[j]));
		foa(c, row) {
			c = ver++;
		}
	}

	vector<vector<pair<ll, int>>> g(ver);

	debug(terminals);
	debug(terminal_ids);

	foa(t, name_id) {
		debug(t.first);
		debug(t.second);
	}

	auto add_edge = [&](int from, int to, ll len, int norikae) {
		g[from].emplace_back((len + time_norikae * norikae) * md + norikae, to);
		debug(from);
		debug(to);
		debug(len);
		debug(norikae);
	};

	rep(line_id, lines) {
		const int a = sz(terminals[line_id]);
		rep(i, a-1) {
			int left = terminal_ids[line_id][i];
			int right = terminal_ids[line_id][i+1];
			add_edge(left, right, times[line_id][i], 0);
			add_edge(right, left, times[line_id][i], 0);
		}

		rep(i, a) {
			int tid = terminal_ids[line_id][i];
			int nid = name_id[terminals[line_id][i]];

			add_edge(nid, tid, 0LL, 1);
			add_edge(tid, nid, 0LL, 0);
		}
	}



	vector<ll> dist(ver, INF);
	priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<pair<ll, int>>> q;
	
	auto add_cost = [&](ll d, int nxt_ver) {
		if(chmin(dist[nxt_ver], d)) {
			q.emplace(dist[nxt_ver], nxt_ver);
		}
	};

	add_cost(0LL, name_id[start]);
	while(q.size()) {
		int now;
		ll d;
		tie(d, now) = q.top();
		q.pop();
		if(d > dist[now]) continue;

		debug(d);
		debug(now);

		for(auto [edge_cost, nxt] : g[now]) {
			add_cost(d + edge_cost, nxt);
		}
	}

	ll ans = dist[name_id[goal]];

	if(ans > INF / 2) {
		cout << -1 << endl;
		return;
	}

	ll tm, norikae;
	tm  = ans / md - time_norikae;
	norikae = ans % md - 1;

	cout << tm << " " << norikae << endl;
	return;
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	while(cin >> n && n) solve();
}
accuracy: 1
time_ms: 100
memory_kb: 29980
final_rank: 6

code_snippet: #include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#define _GLIBCXX_DEBUG
using namespace std;
using std::cout;
using std::cin;
using std::endl;
using ll=long long;
using ld=long double;
ll ILL=1167167167167167167;
const int INF=1050000000;
const ll mod=1e9+7;
#define rep(i,a) for (ll i=0;i<a;i++)
#define all(p) p.begin(),p.end()
template<class T> using _pq = priority_queue<T, vector<T>, greater<T>>;
template<class T> ll LB(vector<T> &v,T a){return lower_bound(v.begin(),v.end(),a)-v.begin();}
template<class T> ll UB(vector<T> &v,T a){return upper_bound(v.begin(),v.end(),a)-v.begin();}
template<class T> bool chmin(T &a,const T &b){if(a>b){a=b;return 1;}else return 0;}
template<class T> bool chmax(T &a,const T &b){if(a<b){a=b;return 1;}else return 0;}
template<class T> void So(vector<T> &v) {sort(v.begin(),v.end());}
template<class T> void Sore(vector<T> &v) {sort(v.begin(),v.end(),[](T x,T y){return x>y;});}
void yneos(bool a){if(a) cout<<"Yes\n"; else cout<<"No\n";}
template<class T> void vec_out(vector<T> &p){for(int i=0;i<(int)(p.size());i++){if(i) cout<<" ";cout<<p[i];}cout<<"\n";}


void solve();
// oddloop
int main() {
	solve();
}

void solve(){
	int N,T;
	cin>>N>>T;
	string A,B; 
	cin>>A>>B; 
	map<string,int> m;
	int ind=0;
	vector<vector<string>> S(N);
	vector<vector<int>> p(N);
	int X=0;
	rep(i,N){
		int a;
		cin>>a;
		X+=a;
		S[i].resize(a);
		p[i].resize(a-1);
		rep(j,a){
			cin>>S[i][j];
			if(!m.count(S[i][j])){
				m[S[i][j]]=ind;
				ind++;
			}
		}
		rep(j,a-1) cin>>p[i][j];
	}
	vector<vector<pair<int,pair<int,int>>>> G(X+ind);
	vector<pair<int,int>> seen(X+ind,{INF,0});
	seen[X+m[A]]={0,0};
	_pq<tuple<int,int,int>> pq;
	pq.push({0,0,X+m[A]});
	int Y=0;
	rep(i,N){
		rep(j,(int)(S[i].size())){
			G[Y].push_back({X+m[S[i][j]],{T,0}});
			G[X+m[S[i][j]]].push_back({Y,{0,1}});
			if(j){
				G[Y].push_back({Y-1,{p[i][j-1],0}});
				G[Y-1].push_back({Y,{p[i][j-1],0}});
			}
			Y++;
		}
	}
	while(!pq.empty()){
		int ne,time,ri;
		tie(time,ri,ne)=pq.top();
		pq.pop();
		if(seen[ne]>make_pair(time,ri)) continue;
		for(auto x:G[ne]){
			if(chmin(seen[x.first],{x.second.first+time,x.second.second+ri})){
				pq.push({seen[x.first].first,seen[x.first].second,x.first});
			}
		}
	}
	if(seen[X+m[B]].first==INF) cout<<"-1\n";
	else cout<<seen[X+m[B]].first-T<<" "<<seen[X+m[B]].second-1<<"\n";
}
accuracy: 1
time_ms: 120
memory_kb: 25960
final_rank: 7

code_snippet: #include "bits/stdc++.h"
using namespace std;

typedef string::const_iterator State;
#define eps 1e-8L
#define MAX_MOD 1000000007LL
#define GYAKU 500000004LL
#define MOD 998244353LL
#define pb push_back
#define mp make_pair
typedef long long ll;
typedef long double ld;
#define REP(a, b) for (long long(a) = 0; (a) < (b); ++(a))
#define ALL(x) (x).begin(), (x).end()

#define int long long
vector<pair<int, pair<int, int>>> vertexs[200000];
pair<int, int> dp[200000];
void solve()
{
  int n, t;
  cin >> n >> t;
  string a, b;
  cin >> a >> b;
  map<string, vector<int>> nexts;
  int cnt = 0;
  REP(i, n)
  {
    int c;
    cin >> c;
    REP(q, c)
    {
      string s;
      cin >> s;
      nexts[s].push_back(cnt + q);
    }
    REP(q, c - 1)
    {
      int a;
      cin >> a;
      vertexs[cnt + q].push_back({cnt + q + 1, {a, 0}});
      vertexs[cnt + q + 1].push_back({cnt + q, {a, 0}});
    }
    cnt += c;
  }
  int starts = 190000, ends = 19000;
  for (auto x : nexts)
  {
    for (auto y : x.second)
    {
      vertexs[cnt].push_back({y, {t, 1}});
      vertexs[y].push_back({cnt, {0, 0}});
    }
    if (a == x.first)
      starts = cnt;
    if (b == x.first)
      ends = cnt;
    cnt++;
  }
  REP(i, 200000)
  {
    dp[i] = {1e18, 0};
  }
  priority_queue<pair<pair<int, int>, int>, vector<pair<pair<int, int>, int>>, greater<pair<pair<int, int>, int>>> next_go;
  next_go.push({{0, 0}, starts});
  dp[starts] = {0, 0};
  while (!next_go.empty())
  {
    pair<pair<int, int>, int> now = next_go.top();
    next_go.pop();
    if (dp[now.second] != now.first)
      continue;
    for (auto x : vertexs[now.second])
    {
      pair<int, int> next_gos = now.first;
      next_gos.first += x.second.first;
      next_gos.second += x.second.second;
      if (dp[x.first] > next_gos)
      {
        dp[x.first] = next_gos;
        next_go.push({next_gos, x.first});
      }
    }
  }

  pair<int, int> ans = dp[ends];
  if (ans.first == 1e18)
  {
    cout << -1 << endl;
  }
  else
  {
    cout << ans.first - t << " " << ans.second - 1 << endl;
  }
}

#undef int

// generated by oj-template v4.7.2
// (https://github.com/online-judge-tools/template-generator)
int main()
{
  // Fasterize input/output script
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout << fixed << setprecision(100);
  // scanf/printf user should delete this fasterize input/output script

  int t = 1;
  // cin >> t; // comment out if solving multi testcase
  for (int testCase = 1; testCase <= t; ++testCase)
  {
    solve();
  }
  return 0;
}
accuracy: 1
time_ms: 100
memory_kb: 35292
final_rank: 10

code_snippet: #include <bits/stdc++.h>
using namespace std;

const int INF = 1e9;

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int N, T;
    string A, B;
    cin >> N >> T >> A >> B;
    vector<int> a(N);
    vector<vector<int>> s(N), t(N);
    map<string, int> mp;
    int n = 0;
    for (int i = 0; i < N; i++) {
        cin >> a[i];
        n += 2 * a[i] - 2;
        for (int j = 0; j < a[i]; j++) {
            string x;
            cin >> x;
            if (!mp.count(x)) mp[x] = mp.size();
            s[i].emplace_back(mp[x]);
        }
        for (int j = 0; j < a[i] - 1; j++) {
            int x;
            cin >> x;
            t[i].emplace_back(x);
        }
    }
    n += mp.size();

    vector<vector<tuple<int, int, int>>> G(n);
    int cur = mp.size();
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < a[i] - 1; j++, cur += 2) {
            {
                G[cur].emplace_back(cur + 1, t[i][j], 0);
                G[cur + 1].emplace_back(cur, t[i][j], 0);
            }
            if (j > 0) {
                G[cur].emplace_back(cur - 1, 0, 0);
                G[cur - 1].emplace_back(cur, 0, 0);
            }
            {
                G[cur].emplace_back(s[i][j], T, 1);
                G[s[i][j]].emplace_back(cur, 0, 0);
            }
            {
                G[cur + 1].emplace_back(s[i][j + 1], T, 1);
                G[s[i][j + 1]].emplace_back(cur + 1, 0, 0);
            }
        }
    }

    int start = mp[A], to = mp[B];
    vector<pair<int, int>> dp(n, {INF, INF});
    priority_queue<tuple<int, int, int>> pq;
    dp[start] = {0, 0};
    pq.emplace(0, 0, start);

    while (!pq.empty()) {
        auto t = pq.top();
        pq.pop();
        int v, cost1, cost2;
        tie(cost1, cost2, v) = t;
        cost1 *= -1, cost2 *= -1;
        if (dp[v] < make_pair(cost1, cost2)) continue;
        for (auto& e : G[v]) {
            int u = get<0>(e), ncost1 = cost1 + get<1>(e), ncost2 = cost2 + get<2>(e);
            if (make_pair(ncost1, ncost2) < dp[u]) {
                dp[u] = make_pair(ncost1, ncost2);
                pq.emplace(-ncost1, -ncost2, u);
            }
        }
    }

    auto res = dp[to];
    int cost = res.first - T, time = res.second - 1;
    if (time > INF / 2)
        cout << -1 << '\n';
    else
        cout << cost << ' ' << time << '\n';
    return 0;
}
accuracy: 1
time_ms: 120
memory_kb: 33940
final_rank: 12

code_snippet: #define LOCAL
#include <bits/stdc++.h>
using namespace std;
#pragma region Macros
typedef long long ll;
typedef __int128_t i128;
typedef unsigned int uint;
typedef unsigned long long ull;
#define ALL(x) (x).begin(), (x).end()

template <typename T> istream& operator>>(istream& is, vector<T>& v) {
    for (T& x : v) is >> x;
    return is;
}
template <typename T> ostream& operator<<(ostream& os, const vector<T>& v) {
    for (int i = 0; i < (int)v.size(); i++) {
        os << v[i] << (i + 1 == (int)v.size() ? "" : " ");
    }
    return os;
}
template <typename T, typename U> ostream& operator<<(ostream& os, const pair<T, U>& p) {
    os << '(' << p.first << ',' << p.second << ')';
    return os;
}
template <typename T, typename U> ostream& operator<<(ostream& os, const map<T, U>& m) {
    os << '{';
    for (auto itr = m.begin(); itr != m.end();) {
        os << '(' << itr->first << ',' << itr->second << ')';
        if (++itr != m.end()) os << ',';
    }
    os << '}';
    return os;
}
template <typename T, typename U> ostream& operator<<(ostream& os, const unordered_map<T, U>& m) {
    os << '{';
    for (auto itr = m.begin(); itr != m.end();) {
        os << '(' << itr->first << ',' << itr->second << ')';
        if (++itr != m.end()) os << ',';
    }
    os << '}';
    return os;
}
template <typename T> ostream& operator<<(ostream& os, const set<T>& s) {
    os << '{';
    for (auto itr = s.begin(); itr != s.end();) {
        os << *itr;
        if (++itr != s.end()) os << ',';
    }
    os << '}';
    return os;
}
template <typename T> ostream& operator<<(ostream& os, const multiset<T>& s) {
    os << '{';
    for (auto itr = s.begin(); itr != s.end();) {
        os << *itr;
        if (++itr != s.end()) os << ',';
    }
    os << '}';
    return os;
}
template <typename T> ostream& operator<<(ostream& os, const unordered_set<T>& s) {
    os << '{';
    for (auto itr = s.begin(); itr != s.end();) {
        os << *itr;
        if (++itr != s.end()) os << ',';
    }
    os << '}';
    return os;
}
template <typename T> ostream& operator<<(ostream& os, const deque<T>& v) {
    for (int i = 0; i < (int)v.size(); i++) {
        os << v[i] << (i + 1 == (int)v.size() ? "" : " ");
    }
    return os;
}

template <int i, typename T> void print_tuple(ostream&, const T&) {}
template <int i, typename T, typename H, class... Args> void print_tuple(ostream& os, const T& t) {
    if (i) os << ',';
    os << get<i>(t);
    print_tuple<i + 1, T, Args...>(os, t);
}
template <typename... Args> ostream& operator<<(ostream& os, const tuple<Args...>& t) {
    os << '{';
    print_tuple<0, tuple<Args...>, Args...>(os, t);
    return os << '}';
}

void debug_out() { cerr << '\n'; }
template <class Head, class... Tail> void debug_out(Head&& head, Tail&&... tail) {
    cerr << head;
    if (sizeof...(Tail) > 0) cerr << ", ";
    debug_out(move(tail)...);
}
#ifdef LOCAL
#define debug(...)                                                                   \
    cerr << " ";                                                                     \
    cerr << #__VA_ARGS__ << " :[" << __LINE__ << ":" << __FUNCTION__ << "]" << '\n'; \
    cerr << " ";                                                                     \
    debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif

template <typename T> T gcd(T x, T y) { return y != 0 ? gcd(y, x % y) : x; }
template <typename T> T lcm(T x, T y) { return x / gcd(x, y) * y; }

int topbit(signed t) { return t == 0 ? -1 : 31 - __builtin_clz(t); }
int topbit(long long t) { return t == 0 ? -1 : 63 - __builtin_clzll(t); }
int botbit(signed a) { return a == 0 ? 32 : __builtin_ctz(a); }
int botbit(long long a) { return a == 0 ? 64 : __builtin_ctzll(a); }
int popcount(signed t) { return __builtin_popcount(t); }
int popcount(long long t) { return __builtin_popcountll(t); }
bool ispow2(int i) { return i && (i & -i) == i; }

template <class T> T ceil(T x, T y) {
    assert(y >= 1);
    return (x > 0 ? (x + y - 1) / y : x / y);
}
template <class T> T floor(T x, T y) {
    assert(y >= 1);
    return (x > 0 ? x / y : (x - y + 1) / y);
}

template <class T1, class T2> inline bool chmin(T1& a, T2 b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}
template <class T1, class T2> inline bool chmax(T1& a, T2 b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}
#pragma endregion

const int INF = 1e9;
const long long IINF = 1e18;
const int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
const char dir[4] = {'D', 'R', 'U', 'L'};
const long long MOD = 1000000007;
// const long long MOD = 998244353;

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int N, T;
    string A, B;
    cin >> N >> T >> A >> B;
    vector<int> a(N);
    vector<vector<int>> s(N), t(N);
    map<string, int> mp;
    int n = 0;
    for (int i = 0; i < N; i++) {
        cin >> a[i];
        n += 2 * a[i] - 2;
        for (int j = 0; j < a[i]; j++) {
            string x;
            cin >> x;
            if (!mp.count(x)) mp[x] = mp.size();
            s[i].emplace_back(mp[x]);
        }
        for (int j = 0; j < a[i] - 1; j++) {
            int x;
            cin >> x;
            t[i].emplace_back(x);
        }
    }
    n += mp.size();

    vector<vector<tuple<int, int, int>>> G(n);
    int cur = mp.size();
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < a[i] - 1; j++, cur += 2) {
            G[cur].emplace_back(cur + 1, t[i][j], 0);
            G[cur + 1].emplace_back(cur, t[i][j], 0);
            if (j > 0) {  // そのまま乗るならタダ
                G[cur].emplace_back(cur - 1, 0, 0);
                G[cur - 1].emplace_back(cur, 0, 0);
            }
            G[cur].emplace_back(s[i][j], T, 1);  // 乗り換え
            G[s[i][j]].emplace_back(cur, 0, 0);
            G[cur + 1].emplace_back(s[i][j + 1], T, 1);
            G[s[i][j + 1]].emplace_back(cur + 1, 0, 0);
        }
    }

    int start = mp[A], to = mp[B];
    vector<pair<int, int>> dp(n, {INF, INF});
    priority_queue<tuple<int, int, int>> pq;
    dp[start] = {0, 0};
    pq.emplace(0, 0, start);

    while (!pq.empty()) {
        auto t = pq.top();
        pq.pop();
        int v, cost1, cost2;
        tie(cost1, cost2, v) = t;
        cost1 *= -1, cost2 *= -1;
        if (dp[v] < make_pair(cost1, cost2)) continue;
        for (auto& e : G[v]) {
            int u = get<0>(e), ncost1 = cost1 + get<1>(e), ncost2 = cost2 + get<2>(e);
            if (make_pair(ncost1, ncost2) < dp[u]) {
                dp[u] = make_pair(ncost1, ncost2);
                pq.emplace(-ncost1, -ncost2, u);
            }
        }
    }

    auto res = dp[to];
    int cost = res.first - T, time = res.second - 1;
    if (time > INF / 2)
        cout << -1 << '\n';
    else
        cout << cost << ' ' << time << '\n';
    return 0;
}
accuracy: 1
time_ms: 110
memory_kb: 33868
final_rank: 11

code_snippet: #pragma GCC optimize("Ofast")
#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <queue>
#include <cstdio>
#include <ctime>
#include <assert.h>
#include <chrono>
#include <random>
#include <numeric>
#include <set>
#include <deque>
#include <stack>
#include <sstream>
#include <utility>
#include <cstring>
#include <unordered_map>
#include <unordered_set>
#include <tuple>
#include <array>
using namespace std;
typedef long long int ll;
typedef unsigned long long ull;

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll myRand(ll B) {
    return (ull)rng() % B;
}

int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int n; cin >> n;
    int Ti; cin >> Ti;
    map<string,int> mp;
    auto id=[&](string str)->int{
        if(mp.find(str) == mp.end()){
            mp[str] = mp.size();
        }
        return mp[str];
    };
    string A,B; cin >> A >> B;
    int S = id(A); int T = id(B);
    vector<vector<int>> v(n);
    vector<vector<int>> t(n);
    vector<pair<int,int>> a; 
    int m = 0;
    for(int i=0;i<n;i++){
        int sz; cin >> sz;
        m += sz;
        for(int j=0;j<sz;j++){
            string s; cin >> s;
            v[i].push_back(id(s));
            a.push_back({i,j});
        }
        for(int j=1;j<sz;j++){
            int tt; cin >> tt;
            t[i].push_back(tt);
        }
    }
    pair<int,int> INF={2e9,2e9};
    int sz = mp.size();
    vector<pair<int,int>> dp(m+sz,INF);
    priority_queue<pair<pair<int,int>,int>,vector<pair<pair<int,int>,int>>,greater<pair<pair<int,int>,int>>> pq;
    vector<vector<int>> sta(sz);
    for(int i=0;i<m;i++){
        if(S == v[a[i].first][a[i].second]){
            dp[i] = {0,0};
            pq.push({{0,0},i});
        }
        sta[v[a[i].first][a[i].second]].push_back(i);
    }
    while(pq.size()){
        auto p = pq.top(); pq.pop();
        int cur = p.second;
        if(dp[cur] != p.first)continue;
        if(cur >= m){
            for(int nx:sta[cur-m]){
                if(dp[nx]>p.first){
                    dp[nx] = p.first;
                    pq.push({p.first,nx});
                }
            }
        }
        else{
            int i = a[cur].first;
            int j = a[cur].second;
            {
                auto pp = p.first;
                pp.first += Ti;
                pp.second++;
                int nx = m+v[i][j];
                if(dp[nx]>pp){
                    dp[nx] = pp;
                    pq.push({pp,nx});
                }
            }
            if(j+1<v[i].size()){
                auto pp = p.first;
                pp.first += t[i][j];
                int nx = cur+1;
                if(dp[nx]>pp){
                    dp[nx] = pp;
                    pq.push({pp,nx});
                }
            }
            if(j-1>=0){
                auto pp = p.first;
                pp.first += t[i][j-1];
                int nx = cur-1;
                if(dp[nx]>pp){
                    dp[nx] = pp;
                    pq.push({pp,nx});
                }
            }
        }
    }
    auto res = INF;
    for(int i=0;i<m;i++){
        if(v[a[i].first][a[i].second] == T){
            res = min(res,dp[i]);
        }
    }
    if(res == INF){
        cout << -1 << endl;
    }
    else{
        cout << res.first << " " << res.second << endl;
    }
}
accuracy: 1
time_ms: 70
memory_kb: 18096
final_rank: 1

code_snippet: #pragma GCC target("avx2")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
//#include <atcoder/all>
using namespace std;
//using namespace atcoder;
#define DEBUG
#ifdef DEBUG
template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
    os << '(' << p.first << ',' << p.second << ')';
    return os;
}
template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
    os << '{';
    for(int i = 0; i < (int)v.size(); i++) {
        if(i) { os << ','; }
        os << v[i];
    }
    os << '}';
    return os;
}
void debugg() { cerr << endl; }
template <class T, class... Args>
void debugg(const T &x, const Args &... args) {
    cerr << " " << x;
    debugg(args...);
}
#define debug(...)                                                             \
    cerr << __LINE__ << " [" << #__VA_ARGS__ << "]: ", debugg(__VA_ARGS__)
#define dump(x) cerr << __LINE__ << " " << #x << " = " << (x) << endl
#else
#define debug(...) (void(0))
#define dump(x) (void(0))
#endif
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vector<ll>> vvl;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<double> vd;
typedef pair<ll,ll> P;
typedef pair<int,int> pii;
typedef vector<P> vpl;
typedef tuple<ll,ll,ll> tapu;
#define rep(i,n) for(int i=0; i<(n); i++)
#define REP(i,a,b) for(int i=(a); i<(b); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
const int inf = (1<<30) - 1;
const ll linf = 1LL<<61;
const int MAX = 510000;
int dy[8] = {0,1,0,-1,1,-1,-1,1};
int dx[8] = {-1,0,1,0,1,-1,1,-1};
const double pi = 3.14159265358979;
const double eps = 1e-8;
template<typename T1,typename T2> inline bool chmin(T1 &a,T2 b){
	if(a>b){
		a = b; return true;
	}
	else return false;
}
template<typename T1,typename T2> inline bool chmax(T1 &a,T2 b){
	if(a<=b){
		a = b; return true;
	}
	else return false;
}
template<typename T> inline void print(T &a){
    int sz = a.size();
    for(auto itr = a.begin(); itr != a.end(); itr++){
		cout << *itr;
        sz--;
        if(sz) cout << " ";
	}
    cout << "\n";
}
template<typename T1,typename T2> inline void print2(T1 a, T2 b){
	cout << a << " " << b << "\n";
}
template<typename T1,typename T2,typename T3> inline void print3(T1 a, T2 b, T3 c){
	cout << a << " " << b << " " << c << "\n";
}
void mark() {cout << "#" << "\n";}
ll pcount(ll x) {return __builtin_popcountll(x);}
const int mod = 1e9 + 7;
//const int mod = 998244353;

struct edge{
    int to,c,t;
    edge(int x, int y, int z) : to(x), c(y), t(z) {}
};

vector<vector<edge>> G(202020);

int main(){
    int n,T; cin >> n >> T;
    string a,b; cin >> a >> b;
    map<string,vector<int>> ls;
    map<string,int> mp;
    int now = 0;
    rep(i,n){
        int k; cin >> k;
        rep(j,k){
            string s; cin >> s;
            mp[s] = 0;
            ls[s].push_back(now+j);
        }
        now++;
        rep(j,k-1){
            int t; cin >> t;
            G[now-1].emplace_back(now,t,0);
            G[now].emplace_back(now-1,t,0);
            now++;
        }
    }
    for(auto &x : mp){
        mp[x.first] = now;
        for(auto y : ls[x.first]){
            G[y].emplace_back(now,T,1);
            G[now].emplace_back(y,0,0);
        }
        now++;
    }
    vpl d(now,{inf,inf});
    d[mp[a]] = {0,0};
    priority_queue<tapu,vector<tapu>,greater<tapu>> pq;
    pq.emplace(0,0,mp[a]);
    auto judge = [&](int to, int c, int t) -> bool {
        if(d[to].first > c) return true;
        if(d[to].first == c && d[to].second > t) return true;
        return false; 
    };
    while(!pq.empty()){
        int cost,times,u;
        tie(cost,times,u) = pq.top(); pq.pop();
        if(d[u].first > cost) continue;
        for(auto v : G[u]){
            int nc = cost + v.c;
            int nt = times + v.t;
            if(judge(v.to,nc,nt)){
                d[v.to] = {nc,nt};
                pq.emplace(nc,nt,v.to);
            }
        }
    }
    if(d[mp[b]].first == inf) cout << -1 << endl;
    else cout << d[mp[b]].first - T << " " << d[mp[b]].second - 1 << endl;
}
accuracy: 1
time_ms: 140
memory_kb: 33876
final_rank: 14

code_snippet: #include<bits/stdc++.h>
using namespace std;
#define ALL(x) begin(x),end(x)
#define rep(i,n) for(int i=0;i<(n);i++)
#define debug(v) cout<<#v<<":";for(auto x:v){cout<<x<<' ';}cout<<endl;
#define mod 998244353
using ll=long long;
const int INF=1000000000;
const ll LINF=1001002003004005006ll;
int dx[]={1,0,-1,0},dy[]={0,1,0,-1};
template<class T>bool chmax(T &a,const T &b){if(a<b){a=b;return true;}return false;}
template<class T>bool chmin(T &a,const T &b){if(b<a){a=b;return true;}return false;}

struct IOSetup{
    IOSetup(){
        cin.tie(0);
        ios::sync_with_stdio(0);
        cout<<fixed<<setprecision(12);
    }
} iosetup;
 
template<typename T>
ostream &operator<<(ostream &os,const vector<T>&v){
    for(int i=0;i<(int)v.size();i++) os<<v[i]<<(i+1==(int)v.size()?"":" ");
    return os;
}
template<typename T>
istream &operator>>(istream &is,vector<T>&v){
    for(T &x:v)is>>x;
    return is;
}

// graph template
// ref : https://ei1333.github.io/library/graph/graph-template.cpp
template<typename T=int>
struct Edge{
    int from,to;
    T w;
    int idx;
    Edge()=default;
    Edge(int from,int to,T w=1,int idx=-1):from(from),to(to),w(w),idx(idx){}
    operator int() const{return to;}
};

template<typename T=int>
struct Graph{
    vector<vector<Edge<T>>> g;
    int V,E;
    Graph()=default;
    Graph(int n):g(n),V(n),E(0){}

    size_t size(){
        return g.size();
    }
    void resize(int k){
        g.resize(k);
    }
    inline const vector<Edge<T>> &operator[](int k)const{
        return (g.at(k));
    }
    inline vector<Edge<T>> &operator[](int k){
        return (g.at(k));
    }
    void add_directed_edge(int from,int to,T cost=1){
        g[from].emplace_back(from,to,cost,E++);
    }
    void add_edge(int from,int to,T cost=1){
        g[from].emplace_back(from,to,cost,E);
        g[to].emplace_back(to,from,cost,E++);
    }
    void read(int m,int pad=-1,bool weighted=false,bool directed=false){
        for(int i=0;i<m;i++){
            int u,v;cin>>u>>v;
            u+=pad,v+=pad;
            T w=T(1);
            if(weighted) cin>>w;
            if(directed) add_directed_edge(u,v,w);
            else         add_edge(u,v,w);
        }
    }
};

template<typename T>
struct Dijkstra{
    const T inf;
    Graph<T> g;
    vector<T> d;
    vector<int> prev;
    
    Dijkstra(Graph<T> g):inf(numeric_limits<T>::max()/2),g(g){}

    vector<T> build(int st){
        d.assign(g.V,inf);
        prev.assign(g.V,-1);
        d[st]=0;
        priority_queue<pair<T,int>,vector<pair<T,int>>,greater<pair<T,int>>> que;
        que.emplace(d[st],st);
        while(!que.empty()){
            auto p=que.top();que.pop();
            int cur=p.second;
            if(d[cur]<p.first) continue;
            for(auto &e:g[cur]){
                if(d[e]>d[cur]+e.w){
                    d[e]=d[cur]+e.w;
                    prev[e]=cur;
                    que.emplace(d[e],e);
                }
            }
        }
        return d;
    }
    
    vector<int> get_path(int gl){
        vector<int> ret;
        if(d[g]==inf) return ret;
        for(;gl!=-1;gl=prev[gl]) ret.push_back(gl);
        reverse(ret.begin(),ret.end());
        return ret;
    }
};

map<string,vector<int>> station;
vector<vector<string>> v(50001);
vector<vector<ll>> cost(50001);

using P=pair<ll,ll>;
Graph<P> G(200010);
vector<P> dis(200010,P(LINF,LINF));

signed main(){
    int N;ll T; cin>>N>>T;
    string A,B;cin>>A>>B;

    rep(i,N){
        int m;cin>>m;
        v[i].resize(m);
        cost[i].resize(m-1);
        cin>>v[i];
        cin>>cost[i];
    }

    int idx=0;
    rep(i,N) for(auto &s:v[i]) station[s].push_back(idx++);
    for(auto &p:station) p.second.push_back(idx++);
    

    priority_queue<pair<P,int>,vector<pair<P,int>>,greater<pair<P,int>>> que;
    idx=0;
    rep(i,N){
        int m=(int)v[i].size();
        rep(j,m){
            G.add_directed_edge(idx,station[v[i][j]].back(),P(T,1));
            G.add_directed_edge(station[v[i][j]].back(),idx,P(0,0));
            if(j+1<m) G.add_edge(idx,idx+1,P(cost[i][j],0));
            if(v[i][j]==A) dis[idx]=P(0,0),que.emplace(P(0,0),idx);
            idx++;
        }
    }

    while(!que.empty()){
        auto [p,cur]=que.top();que.pop();
        if(dis[cur]<p) continue;
        for(auto &e:G[cur]){
            P nxt=P(p.first+e.w.first,p.second+e.w.second);
            if(chmin(dis[e],nxt)) que.emplace(nxt,e);   
        }
    }

    P res=P(LINF,LINF);
    for(auto &i:station[B]) chmin(res,dis[i]);
    if(res.first<LINF) cout<<res.first<<" "<<res.second<<endl;
    else cout<<-1<<endl;
    return 0;
}
accuracy: 1
time_ms: 130
memory_kb: 45088
final_rank: 15

code_snippet: #include<bits/stdc++.h>
using namespace std;
#define ALL(x) begin(x),end(x)
#define rep(i,n) for(int i=0;i<(n);i++)
#define debug(v) cout<<#v<<":";for(auto x:v){cout<<x<<' ';}cout<<endl;
#define mod 998244353
using ll=long long;
const int INF=1000000000;
const ll LINF=1001002003004005006ll;
int dx[]={1,0,-1,0},dy[]={0,1,0,-1};
template<class T>bool chmax(T &a,const T &b){if(a<b){a=b;return true;}return false;}
template<class T>bool chmin(T &a,const T &b){if(b<a){a=b;return true;}return false;}

struct IOSetup{
    IOSetup(){
        cin.tie(0);
        ios::sync_with_stdio(0);
        cout<<fixed<<setprecision(12);
    }
} iosetup;
 
template<typename T>
ostream &operator<<(ostream &os,const vector<T>&v){
    for(int i=0;i<(int)v.size();i++) os<<v[i]<<(i+1==(int)v.size()?"":" ");
    return os;
}
template<typename T>
istream &operator>>(istream &is,vector<T>&v){
    for(T &x:v)is>>x;
    return is;
}

// graph template
// ref : https://ei1333.github.io/library/graph/graph-template.cpp
template<typename T=int>
struct Edge{
    int from,to;
    T w;
    int idx;
    Edge()=default;
    Edge(int from,int to,T w=1,int idx=-1):from(from),to(to),w(w),idx(idx){}
    operator int() const{return to;}
};

template<typename T=int>
struct Graph{
    vector<vector<Edge<T>>> g;
    int V,E;
    Graph()=default;
    Graph(int n):g(n),V(n),E(0){}

    size_t size(){
        return g.size();
    }
    void resize(int k){
        g.resize(k);
    }
    inline const vector<Edge<T>> &operator[](int k)const{
        return (g.at(k));
    }
    inline vector<Edge<T>> &operator[](int k){
        return (g.at(k));
    }
    void add_directed_edge(int from,int to,T cost=1){
        g[from].emplace_back(from,to,cost,E++);
    }
    void add_edge(int from,int to,T cost=1){
        g[from].emplace_back(from,to,cost,E);
        g[to].emplace_back(to,from,cost,E++);
    }
    void read(int m,int pad=-1,bool weighted=false,bool directed=false){
        for(int i=0;i<m;i++){
            int u,v;cin>>u>>v;
            u+=pad,v+=pad;
            T w=T(1);
            if(weighted) cin>>w;
            if(directed) add_directed_edge(u,v,w);
            else         add_edge(u,v,w);
        }
    }
};

template<typename T>
struct Dijkstra{
    const T inf;
    Graph<T> g;
    vector<T> d;
    vector<int> prev;
    
    Dijkstra(Graph<T> g):inf(numeric_limits<T>::max()/2),g(g){}

    vector<T> build(int st){
        d.assign(g.V,inf);
        prev.assign(g.V,-1);
        d[st]=0;
        priority_queue<pair<T,int>,vector<pair<T,int>>,greater<pair<T,int>>> que;
        que.emplace(d[st],st);
        while(!que.empty()){
            auto p=que.top();que.pop();
            int cur=p.second;
            if(d[cur]<p.first) continue;
            for(auto &e:g[cur]){
                if(d[e]>d[cur]+e.w){
                    d[e]=d[cur]+e.w;
                    prev[e]=cur;
                    que.emplace(d[e],e);
                }
            }
        }
        return d;
    }
    
    vector<int> get_path(int gl){
        vector<int> ret;
        if(d[g]==inf) return ret;
        for(;gl!=-1;gl=prev[gl]) ret.push_back(gl);
        reverse(ret.begin(),ret.end());
        return ret;
    }
};

map<string,vector<int>> station;
vector<vector<string>> v(50001);
vector<vector<ll>> cost(50001);

using P=pair<ll,ll>;
Graph<P> G(100010);
vector<P> dis(100010,P(LINF,LINF));

signed main(){
    int N;ll T; cin>>N>>T;
    string A,B;cin>>A>>B;

    rep(i,N){
        int m;cin>>m;
        v[i].resize(m);
        cost[i].resize(m-1);
        cin>>v[i];
        cin>>cost[i];
    }

    int idx=0;
    rep(i,N) for(auto &s:v[i]) station[s].push_back(idx++);
    
    priority_queue<pair<P,int>,vector<pair<P,int>>,greater<pair<P,int>>> que;
    idx=0;
    rep(i,N){
        int m=(int)v[i].size();
        rep(j,m){
            for(auto &x:station[v[i][j]]) G.add_edge(idx,x,P(T,1));
            if(j+1<m) G.add_edge(idx,idx+1,P(cost[i][j],0));
            if(v[i][j]==A) dis[idx]=P(0,0),que.emplace(P(0,0),idx);
            idx++;
        }
    }
    
    while(!que.empty()){
        auto [p,cur]=que.top();que.pop();
        if(dis[cur]<p) continue;
        for(auto &e:G[cur]){
            P nxt=P(p.first+e.w.first,p.second+e.w.second);
            if(chmin(dis[e],nxt)) que.emplace(nxt,e);   
        }
    }

    P res=P(LINF,LINF);
    for(auto &i:station[B]) chmin(res,dis[i]);
    if(res.first<LINF) cout<<res.first<<" "<<res.second<<endl;
    else cout<<-1<<endl;
    return 0;
}
accuracy: 0.7948717948717948
time_ms: 110
memory_kb: 53464
final_rank: 20

code_snippet: #include<bits/stdc++.h>
using namespace std;
#define ALL(x) begin(x),end(x)
#define rep(i,n) for(int i=0;i<(n);i++)
#define debug(v) cout<<#v<<":";for(auto x:v){cout<<x<<' ';}cout<<endl;
#define mod 998244353
using ll=long long;
const int INF=1000000000;
const ll LINF=1001002003004005006ll;
int dx[]={1,0,-1,0},dy[]={0,1,0,-1};
template<class T>bool chmax(T &a,const T &b){if(a<b){a=b;return true;}return false;}
template<class T>bool chmin(T &a,const T &b){if(b<a){a=b;return true;}return false;}

struct IOSetup{
    IOSetup(){
        cin.tie(0);
        ios::sync_with_stdio(0);
        cout<<fixed<<setprecision(12);
    }
} iosetup;
 
template<typename T>
ostream &operator<<(ostream &os,const vector<T>&v){
    for(int i=0;i<(int)v.size();i++) os<<v[i]<<(i+1==(int)v.size()?"":" ");
    return os;
}
template<typename T>
istream &operator>>(istream &is,vector<T>&v){
    for(T &x:v)is>>x;
    return is;
}

// graph template
// ref : https://ei1333.github.io/library/graph/graph-template.cpp
template<typename T=int>
struct Edge{
    int from,to;
    T w;
    int idx;
    Edge()=default;
    Edge(int from,int to,T w=1,int idx=-1):from(from),to(to),w(w),idx(idx){}
    operator int() const{return to;}
};

template<typename T=int>
struct Graph{
    vector<vector<Edge<T>>> g;
    int V,E;
    Graph()=default;
    Graph(int n):g(n),V(n),E(0){}

    size_t size(){
        return g.size();
    }
    void resize(int k){
        g.resize(k);
    }
    inline const vector<Edge<T>> &operator[](int k)const{
        return (g.at(k));
    }
    inline vector<Edge<T>> &operator[](int k){
        return (g.at(k));
    }
    void add_directed_edge(int from,int to,T cost=1){
        g[from].emplace_back(from,to,cost,E++);
    }
    void add_edge(int from,int to,T cost=1){
        g[from].emplace_back(from,to,cost,E);
        g[to].emplace_back(to,from,cost,E++);
    }
    void read(int m,int pad=-1,bool weighted=false,bool directed=false){
        for(int i=0;i<m;i++){
            int u,v;cin>>u>>v;
            u+=pad,v+=pad;
            T w=T(1);
            if(weighted) cin>>w;
            if(directed) add_directed_edge(u,v,w);
            else         add_edge(u,v,w);
        }
    }
};

template<typename T>
struct Dijkstra{
    const T inf;
    Graph<T> g;
    vector<T> d;
    vector<int> prev;
    
    Dijkstra(Graph<T> g):inf(numeric_limits<T>::max()/2),g(g){}

    vector<T> build(int st){
        d.assign(g.V,inf);
        prev.assign(g.V,-1);
        d[st]=0;
        priority_queue<pair<T,int>,vector<pair<T,int>>,greater<pair<T,int>>> que;
        que.emplace(d[st],st);
        while(!que.empty()){
            auto p=que.top();que.pop();
            int cur=p.second;
            if(d[cur]<p.first) continue;
            for(auto &e:g[cur]){
                if(d[e]>d[cur]+e.w){
                    d[e]=d[cur]+e.w;
                    prev[e]=cur;
                    que.emplace(d[e],e);
                }
            }
        }
        return d;
    }
    
    vector<int> get_path(int gl){
        vector<int> ret;
        if(d[g]==inf) return ret;
        for(;gl!=-1;gl=prev[gl]) ret.push_back(gl);
        reverse(ret.begin(),ret.end());
        return ret;
    }
};

map<string,vector<int>> station;
vector<vector<string>> v(50001);
vector<vector<ll>> cost(50001);

signed main(){
    int N;ll T; cin>>N>>T;
    string A,B;cin>>A>>B;

    rep(i,N){
        int m;cin>>m;
        v[i].resize(m);
        cost[i].resize(m-1);
        cin>>v[i];
        cin>>cost[i];
    }

    int idx=0;
    rep(i,N) for(auto &s:v[i]) station[s].push_back(idx++);
    
    using P=pair<ll,ll>;
    Graph<P> G(idx);
    vector<P> dis(idx,P(LINF,LINF));
    priority_queue<pair<P,int>,vector<pair<P,int>>,greater<pair<P,int>>> que;
    idx=0;
    rep(i,N){
        int m=(int)v[i].size();
        rep(j,m){
            for(auto &x:station[v[i][j]]) G.add_edge(idx,x,P(T,1));
            if(j+1<m) G.add_edge(idx,idx+1,P(cost[i][j],0));
            if(v[i][j]==A) dis[idx]=P(0,0),que.emplace(P(0,0),idx);
            idx++;
        }
    }
    
    while(!que.empty()){
        auto [p,cur]=que.top();que.pop();
        if(dis[cur]<p) continue;
        for(auto &e:G[cur]){
            P nxt=P(p.first+e.w.first,p.second+e.w.second);
            if(chmin(dis[e],nxt)) que.emplace(nxt,e);   
        }
    }

    P res=P(LINF,LINF);
    for(auto &i:station[B]) chmin(res,dis[i]);
    if(res.first<LINF) cout<<res.first<<" "<<res.second<<endl;
    else cout<<-1<<endl;
    return 0;
}
accuracy: 0.7948717948717948
time_ms: 110
memory_kb: 49720
final_rank: 19

code_snippet: #include<bits/stdc++.h>
using namespace std;
#define ALL(x) begin(x),end(x)
#define rep(i,n) for(int i=0;i<(n);i++)
#define debug(v) cout<<#v<<":";for(auto x:v){cout<<x<<' ';}cout<<endl;
#define mod 998244353
using ll=long long;
const int INF=1000000000;
const ll LINF=1001002003004005006ll;
int dx[]={1,0,-1,0},dy[]={0,1,0,-1};
template<class T>bool chmax(T &a,const T &b){if(a<b){a=b;return true;}return false;}
template<class T>bool chmin(T &a,const T &b){if(b<a){a=b;return true;}return false;}

struct IOSetup{
    IOSetup(){
        cin.tie(0);
        ios::sync_with_stdio(0);
        cout<<fixed<<setprecision(12);
    }
} iosetup;
 
template<typename T>
ostream &operator<<(ostream &os,const vector<T>&v){
    for(int i=0;i<(int)v.size();i++) os<<v[i]<<(i+1==(int)v.size()?"":" ");
    return os;
}
template<typename T>
istream &operator>>(istream &is,vector<T>&v){
    for(T &x:v)is>>x;
    return is;
}

// graph template
// ref : https://ei1333.github.io/library/graph/graph-template.cpp
template<typename T=int>
struct Edge{
    int from,to;
    T w;
    int idx;
    Edge()=default;
    Edge(int from,int to,T w=1,int idx=-1):from(from),to(to),w(w),idx(idx){}
    operator int() const{return to;}
};

template<typename T=int>
struct Graph{
    vector<vector<Edge<T>>> g;
    int V,E;
    Graph()=default;
    Graph(int n):g(n),V(n),E(0){}

    size_t size(){
        return g.size();
    }
    void resize(int k){
        g.resize(k);
    }
    inline const vector<Edge<T>> &operator[](int k)const{
        return (g.at(k));
    }
    inline vector<Edge<T>> &operator[](int k){
        return (g.at(k));
    }
    void add_directed_edge(int from,int to,T cost=1){
        g[from].emplace_back(from,to,cost,E++);
    }
    void add_edge(int from,int to,T cost=1){
        g[from].emplace_back(from,to,cost,E);
        g[to].emplace_back(to,from,cost,E++);
    }
    void read(int m,int pad=-1,bool weighted=false,bool directed=false){
        for(int i=0;i<m;i++){
            int u,v;cin>>u>>v;
            u+=pad,v+=pad;
            T w=T(1);
            if(weighted) cin>>w;
            if(directed) add_directed_edge(u,v,w);
            else         add_edge(u,v,w);
        }
    }
};

template<typename T>
struct Dijkstra{
    const T inf;
    Graph<T> g;
    vector<T> d;
    vector<int> prev;
    
    Dijkstra(Graph<T> g):inf(numeric_limits<T>::max()/2),g(g){}

    vector<T> build(int st){
        d.assign(g.V,inf);
        prev.assign(g.V,-1);
        d[st]=0;
        priority_queue<pair<T,int>,vector<pair<T,int>>,greater<pair<T,int>>> que;
        que.emplace(d[st],st);
        while(!que.empty()){
            auto p=que.top();que.pop();
            int cur=p.second;
            if(d[cur]<p.first) continue;
            for(auto &e:g[cur]){
                if(d[e]>d[cur]+e.w){
                    d[e]=d[cur]+e.w;
                    prev[e]=cur;
                    que.emplace(d[e],e);
                }
            }
        }
        return d;
    }
    
    vector<int> get_path(int gl){
        vector<int> ret;
        if(d[g]==inf) return ret;
        for(;gl!=-1;gl=prev[gl]) ret.push_back(gl);
        reverse(ret.begin(),ret.end());
        return ret;
    }
};



signed main(){
    int N;ll T; cin>>N>>T;
    string A,B;cin>>A>>B;

    map<string,vector<int>> station;
    vector<vector<string>> v(N);
    vector<vector<ll>> cost(N);
    rep(i,N){
        int m;cin>>m;
        v[i].resize(m);
        cost[i].resize(m-1);
        cin>>v[i];
        cin>>cost[i];

    }

    int idx=0;
    rep(i,N) for(auto &s:v[i]) station[s].push_back(idx++);
    
    using P=pair<ll,ll>;
    Graph<P> G(idx);
    vector<P> dis(idx,P(LINF,LINF));
    priority_queue<pair<P,int>,vector<pair<P,int>>,greater<pair<P,int>>> que;
    idx=0;
    rep(i,N){
        int m=(int)v[i].size();
        rep(j,m){
            for(auto &x:station[v[i][j]]) G.add_edge(idx,x,P(T,1));
            if(j+1<m) G.add_edge(idx,idx+1,P(cost[i][j],0));
            if(v[i][j]==A) dis[idx]=P(0,0),que.emplace(P(0,0),idx);
            idx++;
        }
    }
    
    while(!que.empty()){
        auto [p,cur]=que.top();que.pop();
        if(dis[cur]<p) continue;
        for(auto &e:G[cur]){
            P nxt=P(p.first+e.w.first,p.second+e.w.second);
            if(chmin(dis[e],nxt)) que.emplace(nxt,e);   
        }
    }

    P res=P(LINF,LINF);
    for(auto &i:station[B]) chmin(res,dis[i]);
    if(res.first<LINF) cout<<res.first<<" "<<res.second<<endl;
    else cout<<-1<<endl;
    return 0;
}
accuracy: 0.7948717948717948
time_ms: 110
memory_kb: 47224
final_rank: 18

code_snippet: #include <iostream>
#include <vector>
#include <string>
#include <queue>
#include <map>

template <class T>
using MinHeap = std::priority_queue<T, std::vector<T>, std::greater<T>>;

template <class Cost = int>
struct Edge {
    int src, dst;
    Cost cost;

    Edge() = default;
    Edge(int src, int dst, Cost cost = 1)
        : src(src), dst(dst), cost(cost){};

    bool operator<(const Edge<Cost>& e) const { return cost < e.cost; }
    bool operator>(const Edge<Cost>& e) const { return cost > e.cost; }
};

template <class Cost = int>
struct Graph : public std::vector<std::vector<Edge<Cost>>> {
    using std::vector<std::vector<Edge<Cost>>>::vector;

    void span(bool direct, int src, int dst, Cost cost = 1) {
        (*this)[src].emplace_back(src, dst, cost);
        if (!direct) (*this)[dst].emplace_back(dst, src, cost);
    }
};

using namespace std;

void solve() {
    int n, t;
    cin >> n >> t;

    map<string, int> dec;
    auto id = [&]() {
        string s;
        cin >> s;

        if (!dec.count(s)) {
            int k = dec.size();
            dec[s] = k;
        }
        return dec[s];
    };
    int a = id(), b = id();

    vector<vector<int>> vss(n);
    vector<vector<int>> dss(n);
    int ksum = 0;
    for (int i = 0; i < n; ++i) {
        int k;
        cin >> k;
        ksum += k;

        vss[i].resize(k);
        for (auto& v : vss[i]) v = id();

        dss[i].resize(k - 1);
        for (auto& d : dss[i]) cin >> d;
    }

    int nn = dec.size();
    Graph<int> graph(nn + ksum);
    {
        int kb = nn;
        for (int i = 0; i < n; ++i) {
            int k = vss[i].size();

            for (int j = 0; j < k; ++j) {
                graph.span(true, vss[i][j], kb + j, 0);
                graph.span(true, kb + j, vss[i][j], t);
            }

            for (int j = 0; j + 1 < k; ++j) {
                graph.span(false, kb + j, kb + j + 1, dss[i][j]);
            }

            kb += k;
        }
    }

    vector<pair<int, int>> dist(graph.size(), {-1, -1});
    dist[a] = {0, 0};
    MinHeap<pair<pair<int, int>, int>> que;
    que.emplace(dist[a], a);

    while (!que.empty()) {
        auto [d, v] = que.top();
        que.pop();
        if (d > dist[v]) continue;

        for (const auto& e : graph[v]) {
            int u = e.dst;

            auto nd = d;
            nd.first += e.cost;
            if (u < nn) ++nd.second;

            if (dist[u].first != -1 &&
                dist[u] <= nd) continue;
            dist[u] = nd;
            que.emplace(dist[u], u);
        }
    }

    auto [d, x] = dist[b];
    if (d == -1) {
        cout << "-1\n";
    } else {
        cout << d - t << " " << x - 1 << "\n";
    }
}

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);

    solve();

    return 0;
}
accuracy: 1
time_ms: 90
memory_kb: 24436
final_rank: 5