Problem:
フクロモモンガ(Sugar Glider)
フクロモモンガのJOI 君が住んでいる森にはユーカリの木が $N$ 本生えており，それらの木には1 から $N$ の番号がついている．木 $i$ の高さは $H_i$ メートルである．
JOI 君が相互に直接飛び移ることのできる木の組が $M$ 組あり，各組の木の間を飛び移るためにかかる時間が定まっている．JOI 君が木の間を飛び移っている間は，地面からの高さが1 秒あたり1 メートル下がる．すなわち，JOI 君の現在の地面からの高さが $h$ メートル，木の間を飛び移るためにかかる時間が $t$ 秒であるとき，飛び移った後の地面からの高さは $h - t$ メートルとなる．ただし，$h - t$ が0 よりも小さくなる場合や行き先の木の高さよりも大きくなる場合は飛び移ることができない．
さらに，JOI 君は木の側面を上下に移動することによって，地面からの高さを0 メートルから今いる木の高さの範囲で増減させることができる．JOI 君が地面からの高さを1 メートル増加または減少させるためには1 秒の時間がかかる．
JOI 君は，木1 の高さ$X$ メートルの位置から木$N$ の頂上(高さ$H_N$ メートルの位置) に行こうとしており，そのためにかかる時間の最小値を知りたい．
課題
各木の高さと，JOI 君が直接飛び移ることができる木の組の情報と，最初JOI 君がいる場所の高さが与えられる．木 $N$ の頂上に行くためにかかる時間の最小値を求めるプログラムを作成せよ.
入力
標準入力から以下のデータを読み込め．
1 行目には，整数 $N, M, X$ が空白を区切りとして書かれている．これは，木の本数が $N$ 本，移動できる木の組が $M$ 組あり，最初JOI 君が木1 の高さ $X$ メートルの位置にいることを表す．
続く $N$ 行のうちの $i$ 行目$(1 \leq i \leq N)$ には，整数 $H_i$ が書かれている．これは，木 $i$ の高さが $H_i$ メートルであることを表す．
続く$M$ 行のうちの $j$ 行目$(1 \leq j \leq M)$ には，整数 $A_j, B_j, T_j$ $(1 \leq A_j \leq N, 1 \leq B_j \leq N, A_j \ne B_j)$ が空白を区切りとして書かれている．これは，木 $A_j$ と木 $B_j$ の間を相互に $T_j$ 秒で飛び移ることができることを表している．また，$1 \leq j < k \leq M$ ならば，$(A_j, B_j) \ne (A_k, B_k)$ および$(A_j, B_j) \ne (B_k, A_k)$ を満たす．
出力
標準出力に，木1 の高さ $X$ メートルの位置から木 $N$ の頂上に行くためにかかる時間の最小値を秒単位で表す整数を1 行で出力せよ．ただし，そのような方法がない場合は代わりに -1 を出力せよ．
制限
すべての入力データは以下の条件を満たす．
$2 \leq N \leq 100000$
$1 \leq M \leq 300000$
$1 \leq H_i \leq 1000000000 (1 \leq i \leq N)$
$1 \leq T_j \leq 1000000000 (1 \leq j \leq M)$
$0 \leq X \leq H_1$
入出力例
入力例 1
5 5 0
50
100
25
30
10
1 2 10
2 5 50
2 4 20
4 3 1
5 4 20
出力例 1
110
例えば，以下のように移動すればよい．
1. 木1 を50 メートル登る．
2. 木1 から木2 に飛び移る．
3. 木2 から木4 に飛び移る．
4. 木4 から木5 に飛び移る．
5. 木5 を10 メートル登る．
入力例 2
2 1 0
1
1
1 2 100
出力例 2
-1
JOI 君は木1 から木2 に飛び移ることができない．
入力例 3
4 3 30
50
10
20
50
1 2 10
2 3 10
3 4 10
出力例 3
100
問題文と自動審判に使われるデータは、
情報オリンピック日本委員会
が作成し公開している問題文と採点用テストデータです。

Submitted solutions:

code_snippet: #include<iostream>
#include<vector>
#include<queue>
#include<functional>
using namespace std;
vector<pair<long long, long long>>x[120000];
pair<long long, long long>dist[120000];
long long n, m, h, a[320000], b[320000], c[320000], H[120000];
priority_queue<pair<long long, long long>, vector<pair<long long, long long>>, greater<pair<long long, long long>>>Q;
void dijkstra() {
	for (int i = 1; i <= n; i++)dist[i] = make_pair(999999999999999999LL, -999999999999999999LL);
	Q.push(make_pair(0, 1)); dist[1] = make_pair(0, h);
	while (!Q.empty()) {
		long long a1 = Q.top().first, a2 = Q.top().second; Q.pop();
		for (int i = 0; i < x[a2].size(); i++) {
			long long to = x[a2][i].first, cost = x[a2][i].second, height = dist[a2].second - cost;
			if (height < 0) { cost -= height; height = 0; }
			if (height > H[to]) { cost += (height - H[to]); height = H[to]; }
			if (dist[to] > make_pair(a1 + cost, height)) {
				dist[to] = make_pair(a1 + cost, height);
				Q.push(make_pair(dist[to].first, to));
			}
		}
	}
}
int main() {
	cin >> n >> m >> h; for (int i = 1; i <= n; i++)cin >> H[i];
	for (int i = 0; i < m; i++) {
		cin >> a[i] >> b[i] >> c[i];
		if (H[a[i]] >= c[i])x[a[i]].push_back(make_pair(b[i], c[i]));
		if (H[b[i]] >= c[i])x[b[i]].push_back(make_pair(a[i], c[i]));
	}
	dijkstra();
	long long J = dist[n].first + (H[n] - dist[n].second);
	if (J >= 1000000000000000LL)J = -1;
	cout << J << endl;
	return 0;
}
accuracy: 1
time_ms: 180
memory_kb: 32500
final_rank: 5

code_snippet: #include<cstdio>
#include<queue>
#include<vector>
#include<algorithm>
using namespace std;
const int MAXN = 100005;
const long long INF = 100001000010000100ll;

int H[MAXN];
long long dist[MAXN];
vector<int> to[MAXN], cost[MAXN];

struct Dat{
	long long d;
	int v;
	Dat(long long d, int v):d(d),v(v){}
	bool operator<(const Dat& a)const{ return d < a.d;}
};
int main(){
	int N, M, X, A, B, T;
	scanf("%d%d%d",&N,&M,&X);
	
	for(int i = 1; i <= N; i++) scanf("%d",&H[i]);
	for(int i = 0; i < M; i++){
		scanf("%d%d%d",&A,&B,&T);
		to[A].push_back(B);
		cost[A].push_back(T);
		to[B].push_back(A);
		cost[B].push_back(T);
	}
	
	for(int i = 1; i <= N; i++) dist[i] = -INF;
	
	priority_queue<Dat> q;
	q.push(Dat(X,1));
	
	while(!q.empty()){
		Dat now = q.top(); q.pop();
		if(dist[now.v] != -INF) continue;
		dist[now.v] = now.d;
		for(int i = 0; i < to[now.v].size(); i++){
			int u = to[now.v][i], c = cost[now.v][i];
			if(c > H[now.v]) continue;
			q.push(Dat(min(now.d-c, (long long)H[u]), u));
		}
	}
	
	if(dist[N] == -INF) puts("-1");
	else printf("%lld\n",X+H[N]-dist[N]*2);
	
	return 0;
}
accuracy: 1
time_ms: 130
memory_kb: 32408
final_rank: 4

code_snippet: using namespace std;
using ll = long long;
using ull = unsigned long long;

#include <bits/stdc++.h>

void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
    cerr << " " << to_string(H);
    debug_out(T...);
}

#ifdef __LOCAL
#define debug(...)                                                      \
    cerr << "\033[33m(line:" << __LINE__ << ") " << "[" << #__VA_ARGS__ \
         << "]:",                                                       \
        debug_out(__VA_ARGS__);                                         \
    cerr << "\033[m";
#else
#define debug(...)  //   :)
#endif
#define _overload3(_1, _2, _3, name, ...) name
#define _rep(i, n) repi(i, 0, n)
#define repi(i, a, b) for (int i = int(a); i < int(b); ++i)
#define rep(...) _overload3(__VA_ARGS__, repi, _rep, )(__VA_ARGS__)

#define ii tuple<int, int>
#define iiget(t, x, y) \
    x = get<0>(t);     \
    y = get<1>(t);
#define iii tuple<int, int, int>
#define iiiget(t, x, y, z) \
    x = get<0>(t);         \
    y = get<1>(t);         \
    z = get<2>(t);
#define vi vector<int>
#define vvi vector<vi>
#define vvvi vector<vvvi>
#define vll vector<ll>
#define vvll vector<vll>
#define vvvll vector<vvll>

#define vc vector<char>
#define vvc vector<vc>
#define vvvc vector<vvc>
#define INF 1000000000001000

template <typename T>
inline bool chmin(T& a, const T& b) {
    bool compare = a > b;
    if (a > b) a = b;
    return compare;
}
template <typename T>
inline bool chmax(T& a, const T& b) {
    bool compare = a < b;
    if (a < b) a = b;
    return compare;
}

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

    ll N, M, X, a, b, t;

    cin >> N >> M >> X;

    ll w[N];
    rep(i, N) cin >> w[i];

    vector<pair<ll, ll>> aj[N];
    ll dp[N];
    bool c[N]{};
    priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<pair<ll, int>>>
        q;
    pair<ll, int> tv;
    ll ans;
    int v;

    rep(i, M) {
        cin >> a >> b >> t;
        aj[a - 1].push_back(make_pair(b - 1, t));
        aj[b - 1].push_back(make_pair(a - 1, t));
    }

    memset(dp, 0x3f, N * sizeof(ll));
    dp[0] = 0;
    q.push({0, 0});

    auto getHeight = [&](int v, ll t) -> ll {
        ll h = max(0ll, X - t);
        chmin(h, w[v]);
        return h;
    };

    auto getElpT = [&](int u, int v, ll h, ll wid) -> ll {
        if (h - wid >= w[v])
            return h - w[v];
        else if (h - wid >= 0)
            return wid;
        else if (w[u] - wid >= 0)
            return wid * 2 - h;
        else
            return INF + 10;
    };

    while (!q.empty()) {
        tv = q.top();
        q.pop();
        t = tv.first;
        v = tv.second;
        debug(t, v);
        ll height = getHeight(v, t);
        if (v == N - 1) {
            dp[N - 1] = t;
            break;
        }
        if (c[v]) continue;
        c[v] = true;
        for (auto it = aj[v].begin(); it != aj[v].end(); it++) {
            if (!c[it->first]) {
                ll elpt = getElpT(v, it->first, height, it->second);
                //                debug(elpt);
                if (elpt <= INF) {
                    debug(v, it->first, t + elpt);
                    q.push(make_pair(t + elpt, it->first));
                }
            }
        }
    }

    ans = getHeight(N - 1, dp[N - 1]);
    ans = dp[N - 1] + w[N - 1] - ans;

    cout << ((ans < INF) ? ans : -1) << endl;

    return 0;
}
accuracy: 1
time_ms: 90
memory_kb: 28076
final_rank: 2

code_snippet: using namespace std;
using ll = long long;
using ull = unsigned long long;

#include <bits/stdc++.h>

void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
    cerr << " " << to_string(H);
    debug_out(T...);
}

#ifdef __LOCAL
#define debug(...)                                                      \
    cerr << "\033[33m(line:" << __LINE__ << ") " << "[" << #__VA_ARGS__ \
         << "]:",                                                       \
        debug_out(__VA_ARGS__);                                         \
    cerr << "\033[m";
#else
#define debug(...)  //   :)
#endif
#define _overload3(_1, _2, _3, name, ...) name
#define _rep(i, n) repi(i, 0, n)
#define repi(i, a, b) for (int i = int(a); i < int(b); ++i)
#define rep(...) _overload3(__VA_ARGS__, repi, _rep, )(__VA_ARGS__)

#define ii tuple<int, int>
#define iiget(t, x, y) \
    x = get<0>(t);     \
    y = get<1>(t);
#define iii tuple<int, int, int>
#define iiiget(t, x, y, z) \
    x = get<0>(t);         \
    y = get<1>(t);         \
    z = get<2>(t);
#define vi vector<int>
#define vvi vector<vi>
#define vvvi vector<vvvi>
#define vll vector<ll>
#define vvll vector<vll>
#define vvvll vector<vvll>

#define vc vector<char>
#define vvc vector<vc>
#define vvvc vector<vvc>
#define INF 1000000000001

template <typename T>
inline bool chmin(T& a, const T& b) {
    bool compare = a > b;
    if (a > b) a = b;
    return compare;
}
template <typename T>
inline bool chmax(T& a, const T& b) {
    bool compare = a < b;
    if (a < b) a = b;
    return compare;
}

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

    ll N, M, X, a, b, t;

    cin >> N >> M >> X;

    ll w[N];
    rep(i, N) cin >> w[i];

    vector<pair<ll, ll>> aj[N];
    ll dp[N];
    bool c[N]{};
    priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<pair<ll, int>>>
        q;
    pair<ll, int> tv;
    ll ans;
    int v;

    rep(i, M) {
        cin >> a >> b >> t;
        aj[a-1].push_back(make_pair(b-1, t));
        aj[b-1].push_back(make_pair(a-1, t));
    }

    memset(dp, 0x3f, N * sizeof(ll));
    dp[0] = 0;
    q.push({0, 0});
    
    auto getHeight = [&](int v, ll t) -> ll {
        ll h = max(0ll, X - t);
        chmin(h, w[v]);
        return h;
    };

    auto getElpT = [&](int u, int v, ll h, ll wid) -> ll {
        if (h - wid >= w[v])
            return h - w[v];
        else if (h - wid >= 0)
            return wid;
        else if (w[u] - wid >= 0)
            return wid * 2 - h;
        else
            return INF + 10;
    };

    while (!q.empty()) {
        tv = q.top();
        q.pop();
        t = tv.first;
        v = tv.second;
        debug(t, v);
        ll height = getHeight(v, t);
        if (v == N - 1) {
            dp[N-1] = t;
            break;
        }
        if (c[v]) continue;
        c[v] = true;
        for (auto it = aj[v].begin(); it != aj[v].end(); it++) {
            if (!c[it->first]) {
                ll elpt = getElpT(v, it->first, height, it->second);
//                debug(elpt);
                if (elpt <= INF) {
                    debug(v, it->first, t + elpt);
                    q.push(make_pair(t + elpt,it->first));
                }
            }
        }
    }

    ans = getHeight(N - 1, dp[N - 1]);
    ans = dp[N - 1] + w[N - 1] - ans;

    cout << ((ans < INF) ? ans : -1) << endl;

    return 0;
}
accuracy: 0.5873015873015873
time_ms: 80
memory_kb: 26236
final_rank: 20

code_snippet: using namespace std;
using ll = long long;
using ull = unsigned long long;

#include <bits/stdc++.h>

void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
    cerr << " " << to_string(H);
    debug_out(T...);
}

#ifdef __LOCAL
#define debug(...)                                                      \
    cerr << "\033[33m(line:" << __LINE__ << ") " << "[" << #__VA_ARGS__ \
         << "]:",                                                       \
        debug_out(__VA_ARGS__);                                         \
    cerr << "\033[m";
#else
#define debug(...)  //   :)
#endif
#define _overload3(_1, _2, _3, name, ...) name
#define _rep(i, n) repi(i, 0, n)
#define repi(i, a, b) for (int i = int(a); i < int(b); ++i)
#define rep(...) _overload3(__VA_ARGS__, repi, _rep, )(__VA_ARGS__)

#define ii tuple<int, int>
#define iiget(t, x, y) \
    x = get<0>(t);     \
    y = get<1>(t);
#define iii tuple<int, int, int>
#define iiiget(t, x, y, z) \
    x = get<0>(t);         \
    y = get<1>(t);         \
    z = get<2>(t);
#define vi vector<int>
#define vvi vector<vi>
#define vvvi vector<vvvi>
#define vll vector<ll>
#define vvll vector<vll>
#define vvvll vector<vvll>

#define vc vector<char>
#define vvc vector<vc>
#define vvvc vector<vvc>
#define INF 100000000000

template <typename T>
inline bool chmin(T& a, const T& b) {
    bool compare = a > b;
    if (a > b) a = b;
    return compare;
}
template <typename T>
inline bool chmax(T& a, const T& b) {
    bool compare = a < b;
    if (a < b) a = b;
    return compare;
}

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

    ll N, M, X, a, b, t;

    cin >> N >> M >> X;

    ll w[N];
    rep(i, N) cin >> w[i];

    vector<pair<ll, ll>> aj[N];
    ll dp[N];
    bool c[N]{};
    priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<pair<ll, int>>>
        q;
    pair<ll, int> tv;
    ll ans;
    int v;

    rep(i, M) {
        cin >> a >> b >> t;
        aj[a-1].push_back(make_pair(b-1, t));
        aj[b-1].push_back(make_pair(a-1, t));
    }

    memset(dp, 0x3f, N * sizeof(ll));
    dp[0] = 0;
    q.push({0, 0});
    
    auto getHeight = [&](int v, ll t) -> ll {
        ll h = max(0ll, X - t);
        chmin(h, w[v]);
        return h;
    };

    auto getElpT = [&](int u, int v, ll h, ll wid) -> ll {
        if (h - wid >= w[v])
            return h - w[v];
        else if (h - wid >= 0)
            return wid;
        else if (w[u] - wid >= 0)
            return wid * 2 - h;
        else
            return INF + 10;
    };

    while (!q.empty()) {
        tv = q.top();
        q.pop();
        t = tv.first;
        v = tv.second;
        debug(t, v);
        ll height = getHeight(v, t);
        if (v == N - 1) {
            dp[N-1] = t;
            break;
        }
        if (c[v]) continue;
        c[v] = true;
        for (auto it = aj[v].begin(); it != aj[v].end(); it++) {
            if (!c[it->first]) {
                ll elpt = getElpT(v, it->first, height, it->second);
//                debug(elpt);
                if (elpt <= INF) {
                    debug(v, it->first, t + elpt);
                    q.push(make_pair(t + elpt,it->first));
                }
            }
        }
    }

    ans = getHeight(N - 1, dp[N - 1]);
    ans = dp[N - 1] + w[N - 1] - ans;

    cout << ((ans < INF) ? ans : -1) << endl;

    return 0;
}
accuracy: 0.5873015873015873
time_ms: 80
memory_kb: 25776
final_rank: 19

code_snippet: #include <stdio.h>
#include <algorithm>
#include <queue>
#include <vector>
using namespace std;

int h[100100]; // 高度情報を格納する配列
vector<pair<int, int>> g[100100]; // グラフの隣接リスト
long long d1[100100]; // 各頂点までの最短距離を格納する配列
int v[100100]; // 頂点が訪問済みかどうかを記録する配列

int main() {
    int a, b;
    long long x;
    
    // 入力処理
    scanf("%d %d %lld", &a, &b, &x);
    for (int i = 0; i < a; i++) {
        scanf("%d", h + i); // 各頂点の高度を入力
    }
    for (int i = 0; i < b; i++) {
        int p, q, r;
        scanf("%d %d %d", &p, &q, &r);
        p--; q--;
        g[p].push_back(make_pair(q, r)); // 辺の情報をグラフに追加
        g[q].push_back(make_pair(p, r)); // 無向グラフなので両方向に追加
    }

    // 初期化
    for (int i = 0; i < a; i++) d1[i] = 21000000000000000LL;
    priority_queue<pair<long long, int>> Q;
    Q.push(make_pair(0, 0));
    d1[0] = 0;

    // ダイクストラ法
    while (!Q.empty()) {
        int at = Q.top().second;
        long long cost = -Q.top().first;
        Q.pop();
        if (v[at]) continue;
        v[at] = 1;

        for (int i = 0; i < g[at].size(); i++) {
            long long T = cost + g[at][i].second;
            if (-T + x > h[g[at][i].first]) {
                T = x - h[g[at][i].first];
            }
            if (g[at][i].second > h[at]) continue;

            if (!v[g[at][i].first] && T < d1[g[at][i].first]) {
                d1[g[at][i].first] = T;
                Q.push(make_pair(-d1[g[at][i].first], g[at][i].first));
            }
        }
    }

    // 結果の出力
    if (d1[a - 1] > 1000000000000000LL) {
        printf("-1\n");
    } else {
        long long ret = d1[a - 1];
        ret += d1[a - 1] - x;
        printf("%lld\n", ret + h[a - 1]);
    }

    return 0;
}
accuracy: 1
time_ms: 80
memory_kb: 16120
final_rank: 1

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

int main() {
	int64_t N, M, X;
	cin >> N >> M >> X;
	vector<int64_t> vec(N);
	for (int i = 0; i < N; i++) {
		cin >> vec.at(i);
	}
	vector<vector<pair<int,int64_t>>> E(N, vector<pair<int,int64_t>>(0));
	for (int i = 0; i < M; i++) {
		int a, b;
		int64_t t;
		cin >> a >> b >> t;
		a--;b--;
		if (vec.at(a) >= t) {
			pair<int, int64_t> p = make_pair(b, t);
			E.at(a).push_back(p);
		}
		if (vec.at(b) >= t) {
			pair<int, int64_t> q = make_pair(a, t);
			E.at(b).push_back(q);
		}
	}

	int64_t INF = 100000000000000000;
	vector<int64_t> cost(N, INF);
	cost.at(0) = 0;
	priority_queue<pair<int64_t, int>> Q;
	pair<int64_t, int> p = make_pair(0, 0);
	Q.push(p);
	while (Q.size() != 0) {
		pair<int64_t, int> q = Q.top();
		Q.pop();
		int a;
		int64_t c;
		tie(c, a) = q;
		c *= (-1);
		if (cost.at(a)  < c) {
			continue;
		}
		int64_t h = 0LL;
		if (X - cost.at(a) > 0LL) {
			h = X - cost.at(a);
		}
		for (pair<int, int64_t> r : E.at(a)) {
			int64_t t,tt;
			int b;
			tie(b,t) = r;
			if (h - t > vec.at(b)) {
				tt = h - vec.at(b);
			}
			else if (0 <= h - t && h-t<= vec.at(b)) {
				tt = t;
			}
			else {
				tt = 2 * t - h;
			}
			if (cost.at(b) > cost.at(a) + tt) {
				cost.at(b) = cost.at(a) + tt;
				pair<int64_t, int> s = make_pair((-1)*cost.at(b), b);
				Q.push(s);
			}
		}
	}

	int64_t H = 0LL;
	if (X - cost.at(N - 1) > 0LL) {
		H = X - cost.at(N - 1);
	}
	int64_t ans = vec.at(N - 1) + cost.at(N - 1) - H;
	if (ans < INF) {
		cout << ans << endl;
	}
	else {
		cout << -1 << endl;
	}
}
accuracy: 1
time_ms: 180
memory_kb: 21228
final_rank: 3

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int(i) = 0; (i) < (int)(n); ++(i))
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define fi first
#define se second
using pi = pair<int, int>;
using P = pair<ll, int>;
struct edge {
  int to;
  ll cost;
};
const int V = 100000;
const int VV = 1000000;
const ll INF = LLONG_MAX / 3;
vector<edge> G[V], gg[VV];
set<int> height[V];
int n, m;
ll X;
int H[V];
map<pi, int> v2id;
int max_h[V] = {};
ll d[VV];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n >> m >> X;
  rep(i, n) cin >> H[i];
  rep(i, m) {
    int a, b, t;
    cin >> a >> b >> t;
    --a;
    --b;
    G[a].pb({b, t});
    G[b].pb({a, t});
  }
  int ct = 1;
  priority_queue<pi> que;
  que.push(pi(X, 0));
  v2id[pi(0, X)] = 0;
  max_h[0] = X;
  height[0].insert(X);
  while (!que.empty()) {
    pi p = que.top();
    que.pop();
    int v = p.se, h = p.fi;
    if (h < max_h[v])
      continue;
    for (const auto &e : G[v]) {
      int bh = h;
      int nh = h - e.cost;
      if (nh < 0)
        continue;
      if (nh > H[e.to]) {
        bh = H[e.to] + e.cost;
        nh = H[e.to];
      }
      if (nh > max_h[e.to]) {
        int &id1 = v2id[pi(v, bh)];
        if (!id1) {
          id1 = ct++;
          height[v].insert(bh);
        }
        int &id2 = v2id[pi(e.to, nh)];
        if (!id2) {
          id2 = ct++;
          height[e.to].insert(nh);
        }
        int uu = id1, vv = id2;
        gg[uu].pb({vv, e.cost});
        max_h[e.to] = nh;
        que.push(pi(max_h[e.to], e.to));
      }
    }
  }
  rep(i, n) for (const auto &e : G[i]) {
    if (H[i] < e.cost)
      continue;
    int &id1 = v2id[pi(i, e.cost)];
    if (!id1) {
      id1 = ct++;
      height[i].insert(e.cost);
    }
    int &id2 = v2id[pi(e.to, 0)];
    if (!id2) {
      id2 = ct++;
      height[e.to].insert(0);
    }
    int uu = id1;
    int vv = id2;
    gg[uu].pb({vv, e.cost});
  }
  int &id = v2id[pi(n - 1, H[n - 1])];
  if (!id) {
    id = ct++;
    height[n - 1].insert(H[n - 1]);
  }
  rep(i, n) {
    int S = height[i].size();
    auto itr = height[i].begin();
    rep(j, S - 1) {
      auto nx = ++itr;
      itr--;
      int &id1 = v2id[pi(i, *itr)];
      int &id2 = v2id[pi(i, *nx)];
      int uu = id1;
      int vv = id2;
      int dist = *nx - *itr;
      gg[uu].pb({vv, dist});
      gg[vv].pb({uu, dist});
      itr = nx;
    }
  }
  int start = v2id[pi(0, X)];
  int goal = id;
  fill(d, d + VV, INF);
  d[start] = 0;
  priority_queue<P, vector<P>, greater<P>> pq;
  pq.push(pi(0, start));
  while (!pq.empty()) {
    P now = pq.top();
    pq.pop();
    int v = now.se;
    if (now.fi > d[v])
      continue;
    for (const auto &e : gg[v]) {
      if (d[e.to] > d[v] + e.cost) {
        d[e.to] = d[v] + e.cost;
        pq.push(pi(d[e.to], e.to));
      }
    }
  }
  ll ans = d[goal];
  if (ans == INF)
    ans = -1;
  cout << ans << '\n';
  return 0;
}
accuracy: 1
time_ms: 960
memory_kb: 189072
final_rank: 6

code_snippet: #include <vector>
#include <climits>
#include <queue>
#include <set>
#include <map>

using ll = long long;
using pi = std::pair<int, int>;
using P = std::pair<ll, int>;

struct edge {
  int to;
  ll cost;
};

const int V = 100000;
const int VV = 1000000;
const ll INF = LLONG_MAX / 3;

std::vector<edge> G[V], gg[VV];
std::set<int> height[V];
int n, m;
ll X;
int H[V];
std::map<pi, int> v2id;
int max_h[V] = {};
ll d[VV];

int main() {
  scanf(" %d %d %lld", &n, &m, &X);
  for(int i = 0; i < n; ++i) 
    scanf(" %d", &H[i]);
  for(int i = 0; i < m; ++i) {
    int a, b, t;
    scanf(" %d %d %d", &a, &b, &t);
    --a; --b;
    G[a].push_back({b, t});
    G[b].push_back({a, t});
  }
  
  int ct = 1;
  std::priority_queue<pi, std::vector<pi>, std::less<pi>> que;
  que.push(pi(X, 0));
  v2id[pi(0, X)] = 0;
  max_h[0] = X;
  height[0].insert(X);
  
  while (!que.empty()) {
    pi p = que.top(); que.pop();
    int v = p.second, h = p.first;
    if (h < max_h[v]) continue;
    for (const auto &e : G[v]) {
      int bh = h, nh = h - e.cost;
      if (nh < 0) continue;
      if (nh > H[e.to]) {
        bh = H[e.to] + e.cost;
        nh = H[e.to];
      }
      if (nh > max_h[e.to]) {
        if (!v2id.count(pi(v, bh))) {
          v2id[pi(v, bh)] = ct++;
          height[v].insert(bh);
        }
        if (!v2id.count(pi(e.to, nh))) {
          v2id[pi(e.to, nh)] = ct++;
          height[e.to].insert(nh);
        }
        int uu = v2id[pi(v, bh)], vv = v2id[pi(e.to, nh)];
        gg[uu].push_back({vv, e.cost});
        max_h[e.to] = nh;
        que.push(pi(max_h[e.to], e.to));
      }
    }
  }
  
  for(int i = 0; i < n; ++i) 
    for (const auto &e : G[i]) {
      if (H[i] < e.cost) continue;
      if (!v2id.count(pi(i, e.cost))) {
        v2id[pi(i, e.cost)] = ct++;
        height[i].insert(e.cost);
      }
      if (!v2id.count(pi(e.to, 0))) {
        v2id[pi(e.to, 0)] = ct++;
        height[e.to].insert(0);
      }
      int uu = v2id[pi(i, e.cost)];
      int vv = v2id[pi(e.to, 0)];
      gg[uu].push_back({vv, e.cost});
    }
    
  if (!v2id.count(pi(n - 1, H[n - 1]))) {
    v2id[pi(n - 1, H[n - 1])] = ct++;
    height[n - 1].insert(H[n - 1]);
  }
  
  for(int i = 0; i < n; ++i) {
    int S = height[i].size();
    auto itr = height[i].begin();
    for(int j = 0; j < S - 1; ++j) {
      auto nx = std::next(itr);
      int uu = v2id[pi(i, *itr)];
      int vv = v2id[pi(i, *nx)];
      int dist = *nx - *itr;
      gg[uu].push_back({vv, dist});
      gg[vv].push_back({uu, dist});
      itr = nx;
    }
  }
  
  int start = v2id[pi(0, X)];
  int goal = v2id[pi(n - 1, H[n - 1])];
  std::fill(d, d + VV, INF);
  d[start] = 0;
  std::priority_queue<P, std::vector<P>, std::greater<P>> pq;
  pq.push(pi(0, start));
  
  while (!pq.empty()) {
    P now = pq.top(); pq.pop();
    int v = now.second;
    if (now.first > d[v]) continue;
    for (const auto &e : gg[v]) {
      if (d[e.to] > d[v] + e.cost) {
        d[e.to] = d[v] + e.cost;
        pq.push(pi(d[e.to], e.to));
      }
    }
  }
  
  ll ans = d[goal] == INF ? -1 : d[goal];
  printf("%lld\n", ans);
  return 0;
}
accuracy: 1
time_ms: 1060
memory_kb: 188500
final_rank: 8

code_snippet: #include <vector>
#include <queue>
#include <set>
#include <map>
#include <limits>
#include <cstdio>
#include <cassert>

using ll = long long;
using pi = std::pair<int, int>;
using P = std::pair<ll, int>;

struct edge {
  int to;
  ll cost;
};

const int V = 100000;
const int VV = 1000000;
const ll INF = std::numeric_limits<ll>::max() / 3;

std::vector<edge> G[V], gg[VV];
std::set<int> height[V];
int n, m;
ll X;
int H[V];
std::map<pi, int> v2id;
int max_h[V] = {};
ll d[VV];

int main() {
  scanf(" %d %d %lld", &n, &m, &X);
  for (int i = 0; i < n; ++i) scanf(" %d", &H[i]);
  for (int i = 0; i < m; ++i) {
    int a, b, t;
    scanf(" %d %d %d", &a, &b, &t);
    --a;
    --b;
    G[a].push_back({b, t});
    G[b].push_back({a, t});
  }
  int ct = 1;
  std::priority_queue<pi, std::vector<pi>, std::greater<pi>> que;
  que.push(pi(X, 0));
  v2id[pi(0, X)] = 0;
  max_h[0] = X;
  height[0].insert(X);
  while (!que.empty()) {
    pi p = que.top();
    que.pop();
    int v = p.second, h = p.first;
    if (h < max_h[v]) continue;
    for (const auto &e : G[v]) {
      int bh = h;
      int nh = h - e.cost;
      if (nh < 0) continue;
      if (nh > H[e.to]) {
        bh = H[e.to] + e.cost;
        nh = H[e.to];
      }
      if (nh > max_h[e.to]) {
        if (!v2id.count(pi(v, bh))) {
          v2id[pi(v, bh)] = ct++;
          height[v].insert(bh);
        }
        if (!v2id.count(pi(e.to, nh))) {
          v2id[pi(e.to, nh)] = ct++;
          height[e.to].insert(nh);
        }
        int uu = v2id[pi(v, bh)], vv = v2id[pi(e.to, nh)];
        gg[uu].push_back({vv, e.cost});
        max_h[e.to] = nh;
        que.push(pi(max_h[e.to], e.to));
      }
    }
  }
  for (int i = 0; i < n; ++i) {
    for (const auto &e : G[i]) {
      if (H[i] < e.cost) continue;
      if (!v2id.count(pi(i, e.cost))) {
        v2id[pi(i, e.cost)] = ct++;
        height[i].insert(e.cost);
      }
      if (!v2id.count(pi(e.to, 0))) {
        v2id[pi(e.to, 0)] = ct++;
        height[e.to].insert(0);
      }
      int uu = v2id[pi(i, e.cost)];
      int vv = v2id[pi(e.to, 0)];
      gg[uu].push_back({vv, e.cost});
    }
  }
  if (!v2id.count(pi(n - 1, H[n - 1]))) {
    v2id[pi(n - 1, H[n - 1])] = ct++;
    height[n - 1].insert(H[n - 1]);
  }
  for (int i = 0; i < n; ++i) {
    int S = height[i].size();
    auto itr = height[i].begin();
    for (int j = 0; j < S - 1; ++j) {
      auto nx = std::next(itr);
      assert(v2id.count(pi(i, *itr)));
      assert(v2id.count(pi(i, *nx)));
      int uu = v2id[pi(i, *itr)];
      int vv = v2id[pi(i, *nx)];
      int dist = *nx - *itr;
      gg[uu].push_back({vv, dist});
      gg[vv].push_back({uu, dist});
      itr = nx;
    }
  }
  int start = v2id[pi(0, X)];
  int goal = v2id[pi(n - 1, H[n - 1])];
  std::fill(d, d + VV, INF);
  d[start] = 0;
  std::priority_queue<P, std::vector<P>, std::greater<P>> pq;
  pq.push(pi(0, start));
  while (!pq.empty()) {
    P now = pq.top();
    pq.pop();
    int v = now.second;
    if (now.first > d[v]) continue;
    for (const auto &e : gg[v]) {
      if (d[e.to] > d[v] + e.cost) {
        d[e.to] = d[v] + e.cost;
        pq.push(pi(d[e.to], e.to));
      }
    }
  }
  ll ans = d[goal];
  if (ans == INF) ans = -1;
  printf("%lld\n", ans);
  return 0;
}
accuracy: 0.7301587301587301
time_ms: 860
memory_kb: 188500
final_rank: 16

code_snippet: #include <cstdio>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <limits>

using namespace std;

using ll = long long;
using pi = pair<int, int>;
using P = pair<ll, int>;
struct edge {
  int to;
  ll cost;
};

const int V = 100000;
const int VV = 1000000;
const ll INF = numeric_limits<ll>::max() / 3;

vector<edge> G[V], gg[VV];
set<int> height[V];
int n, m;
ll X;
int H[V];
map<pi, int> v2id;
int max_h[V] = {};
ll d[VV];

int main() {
  scanf(" %d %d %lld", &n, &m, &X);
  for(int i = 0; i < n; ++i) scanf(" %d", &H[i]);
  for(int i = 0; i < m; ++i) {
    int a, b, t;
    scanf(" %d %d %d", &a, &b, &t);
    --a;
    --b;
    G[a].push_back({b, t});
    G[b].push_back({a, t});
  }

  int ct = 1;
  priority_queue<pi, vector<pi>, greater<pi>> que;
  que.push(pi(X, 0));
  v2id[pi(0, X)] = 0;
  max_h[0] = X;
  height[0].insert(X);

  while (!que.empty()) {
    pi p = que.top();
    que.pop();
    int v = p.second, h = p.first;
    if (h < max_h[v])
      continue;
    for (const auto &e : G[v]) {
      int bh = h;
      int nh = h - e.cost;
      if (nh < 0)
        continue;
      if (nh > H[e.to]) {
        bh = H[e.to] + e.cost;
        nh = H[e.to];
      }
      if (nh > max_h[e.to]) {
        if (!v2id.count(pi(v, bh))) {
          v2id[pi(v, bh)] = ct++;
          height[v].insert(bh);
        }
        if (!v2id.count(pi(e.to, nh))) {
          v2id[pi(e.to, nh)] = ct++;
          height[e.to].insert(nh);
        }
        int uu = v2id[pi(v, bh)], vv = v2id[pi(e.to, nh)];
        gg[uu].push_back({vv, e.cost});
        max_h[e.to] = nh;
        que.push(pi(nh, e.to));
      }
    }
  }

  for(int i = 0; i < n; ++i) for (const auto &e : G[i]) {
    if (H[i] < e.cost)
      continue;
    if (!v2id.count(pi(i, e.cost))) {
      v2id[pi(i, e.cost)] = ct++;
      height[i].insert(e.cost);
    }
    if (!v2id.count(pi(e.to, 0))) {
      v2id[pi(e.to, 0)] = ct++;
      height[e.to].insert(0);
    }
    int uu = v2id[pi(i, e.cost)];
    int vv = v2id[pi(e.to, 0)];
    gg[uu].push_back({vv, e.cost});
  }

  if (!v2id.count(pi(n - 1, H[n - 1]))) {
    v2id[pi(n - 1, H[n - 1])] = ct++;
    height[n - 1].insert(H[n - 1]);
  }

  for(int i = 0; i < n; ++i) {
    int S = height[i].size();
    auto itr = height[i].begin();
    for(int j = 0; j < S - 1; ++j) {
      auto nx = itr;
      ++nx;
      int uu = v2id[pi(i, *itr)];
      int vv = v2id[pi(i, *nx)];
      int dist = *nx - *itr;
      gg[uu].push_back({vv, dist});
      gg[vv].push_back({uu, dist});
      itr = nx;
    }
  }

  int start = v2id[pi(0, X)];
  int goal = v2id[pi(n - 1, H[n - 1])];
  fill(d, d + VV, INF);
  d[start] = 0;
  priority_queue<P, vector<P>, greater<P>> pq;
  pq.push(pi(0, start));

  while (!pq.empty()) {
    P now = pq.top();
    pq.pop();
    int v = now.second;
    if (now.first > d[v])
      continue;
    for (const auto &e : gg[v]) {
      if (d[e.to] > d[v] + e.cost) {
        d[e.to] = d[v] + e.cost;
        pq.push(pi(d[e.to], e.to));
      }
    }
  }

  ll ans = d[goal];
  if (ans == INF)
    ans = -1;
  printf("%lld\n", ans);
  return 0;
}
accuracy: 0.7301587301587301
time_ms: 850
memory_kb: 188468
final_rank: 15

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int(i) = 0; (i) < (int)(n); ++(i))
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define fi first
#define se second
using pi = pair<int, int>;
using P = pair<ll, int>;
struct edge {
  int to;
  ll cost;
};
const int V = 100000;
const int VV = 800000;
const ll INF = LLONG_MAX / 3;
vector<edge> G[V], gg[VV];
set<int> height[V];
int n, m;
ll X;
int H[V];
map<pi, int> v2id;
int max_h[V] = {};
ll d[VV];
int main() {
  scanf(" %d %d %lld", &n, &m, &X);
  rep(i, n) scanf(" %d", &H[i]);
  rep(i, m) {
    int a, b, t;
    scanf(" %d %d %d", &a, &b, &t);
    --a;
    --b;
    G[a].pb({b, t});
    G[b].pb({a, t});
  }
  int ct = 1;
  priority_queue<pi> que;
  que.push(pi(X, 0));
  v2id[pi(0, X)] = 0;
  max_h[0] = X;
  height[0].insert(X);
  while (!que.empty()) {
    pi p = que.top();
    que.pop();
    int v = p.se, h = p.fi;
    if (h < max_h[v])
      continue;
    for (const auto &e : G[v]) {
      int bh = h;
      int nh = h - e.cost;
      if (nh < 0)
        continue;
      if (nh > H[e.to]) {
        bh = H[e.to] + e.cost;
        nh = H[e.to];
      }
      if (nh > max_h[e.to]) {
        if (!v2id.count(pi(v, bh))) {
          v2id[pi(v, bh)] = ct++;
          height[v].insert(bh);
        }
        if (!v2id.count(pi(e.to, nh))) {
          v2id[pi(e.to, nh)] = ct++;
          height[e.to].insert(nh);
        }
        int uu = v2id[pi(v, bh)], vv = v2id[pi(e.to, nh)];
        gg[uu].pb({vv, e.cost});
        max_h[e.to] = nh;
        que.push(pi(max_h[e.to], e.to));
      }
    }
  }
  rep(i, n) for (const auto &e : G[i]) {
    if (H[i] < e.cost)
      continue;
    if (!v2id.count(pi(i, e.cost))) {
      v2id[pi(i, e.cost)] = ct++;
      height[i].insert(e.cost);
    }
    if (!v2id.count(pi(e.to, 0))) {
      v2id[pi(e.to, 0)] = ct++;
      height[e.to].insert(0);
    }
    int uu = v2id[pi(i, e.cost)];
    int vv = v2id[pi(e.to, 0)];
    gg[uu].pb({vv, e.cost});
  }
  if (!v2id.count(pi(n - 1, H[n - 1]))) {
    v2id[pi(n - 1, H[n - 1])] = ct++;
    height[n - 1].insert(H[n - 1]);
  }
  rep(i, n) {
    int S = height[i].size();
    auto itr = height[i].begin();
    rep(j, S - 1) {
      auto nx = itr;
      ++nx;
      assert(v2id.count(pi(i, *itr)));
      assert(v2id.count(pi(i, *nx)));
      int uu = v2id[pi(i, *itr)];
      int vv = v2id[pi(i, *nx)];
      int dist = *nx - *itr;
      gg[uu].pb({vv, dist});
      gg[vv].pb({uu, dist});
      itr = nx;
    }
  }
  int start = v2id[pi(0, X)];
  int goal = v2id[pi(n - 1, H[n - 1])];
  fill(d, d + VV, INF);
  d[start] = 0;
  priority_queue<P, vector<P>, greater<P>> pq;
  pq.push(pi(0, start));
  while (!pq.empty()) {
    P now = pq.top();
    pq.pop();
    int v = now.se;
    if (now.fi > d[v])
      continue;
    for (const auto &e : gg[v]) {
      if (d[e.to] > d[v] + e.cost) {
        d[e.to] = d[v] + e.cost;
        pq.push(pi(d[e.to], e.to));
      }
    }
  }
  ll ans = d[goal];
  if (ans == INF)
    ans = -1;
  printf("%lld\n", ans);
  return 0;
}
accuracy: 1
time_ms: 1130
memory_kb: 182684
final_rank: 9

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int(i) = 0; (i) < (int)(n); ++(i))
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define fi first
#define se second
using pi = pair<int, int>;
using P = pair<ll, int>;
struct edge {
  int to;
  ll cost;
};
const int V = 100000;
const int VV = 1000000;
const ll INF = LLONG_MAX / 3;
vector<edge> G[V], gg[VV];
set<int> height[V];
int n, m;
ll X;
int H[V];
map<pi, int> v2id;
int max_h[V] = {};
ll d[VV];
int main() {
  scanf(" %d %d %lld", &n, &m, &X);
  rep(i, n) scanf(" %d", &H[i]);
  rep(i, m) {
    int a, b, t;
    scanf(" %d %d %d", &a, &b, &t);
    --a;
    --b;
    G[a].pb({b, t});
    G[b].pb({a, t});
  }
  int ct = 1;
  priority_queue<pi> que;
  que.push(pi(X, 0));
  v2id[pi(0, X)] = 0;
  max_h[0] = X;
  height[0].insert(X);
  while (!que.empty()) {
    pi p = que.top();
    que.pop();
    int v = p.se, h = p.fi;
    if (h < max_h[v])
      continue;
    for (const auto &e : G[v]) {
      int bh = h;
      int nh = h - e.cost;
      if (nh < 0)
        continue;
      if (nh > H[e.to]) {
        bh = H[e.to] + e.cost;
        nh = H[e.to];
      }
      if (nh > max_h[e.to]) {
        if (!v2id.count(pi(v, bh))) {
          v2id[pi(v, bh)] = ct++;
          height[v].insert(bh);
        }
        if (!v2id.count(pi(e.to, nh))) {
          v2id[pi(e.to, nh)] = ct++;
          height[e.to].insert(nh);
        }
        int uu = v2id[pi(v, bh)], vv = v2id[pi(e.to, nh)];
        gg[uu].pb({vv, e.cost});
        max_h[e.to] = nh;
        que.push(pi(max_h[e.to], e.to));
      }
    }
  }
  rep(i, n) for (const auto &e : G[i]) {
    if (H[i] < e.cost)
      continue;
    if (!v2id.count(pi(i, e.cost))) {
      v2id[pi(i, e.cost)] = ct++;
      height[i].insert(e.cost);
    }
    if (!v2id.count(pi(e.to, 0))) {
      v2id[pi(e.to, 0)] = ct++;
      height[e.to].insert(0);
    }
    int uu = v2id[pi(i, e.cost)];
    int vv = v2id[pi(e.to, 0)];
    gg[uu].pb({vv, e.cost});
  }
  if (!v2id.count(pi(n - 1, H[n - 1]))) {
    v2id[pi(n - 1, H[n - 1])] = ct++;
    height[n - 1].insert(H[n - 1]);
  }
  rep(i, n) {
    int S = height[i].size();
    auto itr = height[i].begin();
    rep(j, S - 1) {
      auto nx = itr;
      ++nx;
      assert(v2id.count(pi(i, *itr)));
      assert(v2id.count(pi(i, *nx)));
      int uu = v2id[pi(i, *itr)];
      int vv = v2id[pi(i, *nx)];
      int dist = *nx - *itr;
      gg[uu].pb({vv, dist});
      gg[vv].pb({uu, dist});
      itr = nx;
    }
  }
  int start = v2id[pi(0, X)];
  int goal = v2id[pi(n - 1, H[n - 1])];
  fill(d, d + VV, INF);
  d[start] = 0;
  priority_queue<P, vector<P>, greater<P>> pq;
  pq.push(pi(0, start));
  while (!pq.empty()) {
    P now = pq.top();
    pq.pop();
    int v = now.se;
    if (now.fi > d[v])
      continue;
    for (const auto &e : gg[v]) {
      if (d[e.to] > d[v] + e.cost) {
        d[e.to] = d[v] + e.cost;
        pq.push(pi(d[e.to], e.to));
      }
    }
  }
  ll ans = d[goal];
  if (ans == INF)
    ans = -1;
  printf("%lld\n", ans);
  return 0;
}
accuracy: 1
time_ms: 1110
memory_kb: 188764
final_rank: 12

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int(i) = 0; (i) < (int)(n); ++(i))
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define fi first
#define se second
using pi = pair<int, int>;
using P = pair<ll, int>;
struct edge {
  int to;
  ll cost;
};
const int V = 100000;
const int VV = 1000000;
const ll INF = LLONG_MAX / 3;

vector<edge> G[V], gg[VV];
set<int> height[V];
int n, m;
ll X;
int H[V];
unordered_map<int, int> v2id;
int max_h[V] = {};
ll d[VV];

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  cin >> n >> m >> X;
  rep(i, n) cin >> H[i];
  rep(i, m) {
    int a, b, t;
    cin >> a >> b >> t;
    --a;
    --b;
    G[a].pb({b, t});
    G[b].pb({a, t});
  }

  int ct = 1;
  priority_queue<pi> que;
  que.push(pi(X, 0));
  v2id[0 * V + X] = 0;
  max_h[0] = X;
  height[0].insert(X);
  while (!que.empty()) {
    pi p = que.top();
    que.pop();
    int v = p.se, h = p.fi;
    if (h < max_h[v])
      continue;
    for (const auto &e : G[v]) {
      int bh = h;
      int nh = h - e.cost;
      if (nh < 0)
        continue;
      if (nh > H[e.to]) {
        bh = H[e.to] + e.cost;
        nh = H[e.to];
      }
      if (nh > max_h[e.to]) {
        if (v2id.find(v * V + bh) == v2id.end()) {
          v2id[v * V + bh] = ct++;
          height[v].insert(bh);
        }
        if (v2id.find(e.to * V + nh) == v2id.end()) {
          v2id[e.to * V + nh] = ct++;
          height[e.to].insert(nh);
        }
        int uu = v2id[v * V + bh], vv = v2id[e.to * V + nh];
        gg[uu].pb({vv, e.cost});
        max_h[e.to] = nh;
        que.push(pi(max_h[e.to], e.to));
      }
    }
  }

  rep(i, n) for (const auto &e : G[i]) {
    if (H[i] < e.cost)
      continue;
    if (v2id.find(i * V + e.cost) == v2id.end()) {
      v2id[i * V + e.cost] = ct++;
      height[i].insert(e.cost);
    }
    if (v2id.find(e.to * V + 0) == v2id.end()) {
      v2id[e.to * V + 0] = ct++;
      height[e.to].insert(0);
    }
    int uu = v2id[i * V + e.cost];
    int vv = v2id[e.to * V + 0];
    gg[uu].pb({vv, e.cost});
  }

  if (v2id.find((n - 1) * V + H[n - 1]) == v2id.end()) {
    v2id[(n - 1) * V + H[n - 1]] = ct++;
    height[n - 1].insert(H[n - 1]);
  }

  rep(i, n) {
    int S = height[i].size();
    auto itr = height[i].begin();
    rep(j, S - 1) {
      auto nx = itr;
      ++nx;
      assert(v2id.find(i * V + *itr) != v2id.end());
      assert(v2id.find(i * V + *nx) != v2id.end());
      int uu = v2id[i * V + *itr];
      int vv = v2id[i * V + *nx];
      int dist = *nx - *itr;
      gg[uu].pb({vv, dist});
      gg[vv].pb({uu, dist});
      itr = nx;
    }
  }

  int start = v2id[0 * V + X];
  int goal = v2id[(n - 1) * V + H[n - 1]];
  fill(d, d + VV, INF);
  d[start] = 0;
  priority_queue<P, vector<P>, greater<P>> pq;
  pq.push(pi(0, start));
  while (!pq.empty()) {
    P now = pq.top();
    pq.pop();
    int v = now.se;
    if (now.fi > d[v])
      continue;
    for (const auto &e : gg[v]) {
      if (d[e.to] > d[v] + e.cost) {
        d[e.to] = d[v] + e.cost;
        pq.push(pi(d[e.to], e.to));
      }
    }
  }

  ll ans = d[goal];
  if (ans == INF)
    ans = -1;
  cout << ans << "\n";
  return 0;
}
accuracy: 0.6190476190476191
time_ms: 720
memory_kb: 176912
final_rank: 17

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int(i) = 0; (i) < (int)(n); ++(i))
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define fi first
#define se second
using pi = pair<int, int>;
using P = pair<ll, int>;
struct edge {
  int to;
  ll cost;
};
const int V = 100000;
const int VV = 1000000;
const ll INF = LLONG_MAX / 3;
vector<edge> G[V], gg[VV];
set<int> height[V];
int n, m;
ll X;
int H[V];
unordered_map<int, int> v2id;
int max_h[V] = {};
ll d[VV];
int main() {
  scanf(" %d %d %lld", &n, &m, &X);
  rep(i, n) scanf(" %d", &H[i]);
  rep(i, m) {
    int a, b, t;
    scanf(" %d %d %d", &a, &b, &t);
    --a;
    --b;
    G[a].pb({b, t});
    G[b].pb({a, t});
  }
  int ct = 1;
  priority_queue<pi> que;
  que.push(pi(X, 0));
  v2id[0 * V + X] = 0;
  max_h[0] = X;
  height[0].insert(X);
  while (!que.empty()) {
    pi p = que.top();
    que.pop();
    int v = p.se, h = p.fi;
    if (h < max_h[v])
      continue;
    for (const auto &e : G[v]) {
      int bh = h;
      int nh = h - e.cost;
      if (nh < 0)
        continue;
      if (nh > H[e.to]) {
        bh = H[e.to] + e.cost;
        nh = H[e.to];
      }
      if (nh > max_h[e.to]) {
        if (!v2id.count(v * V + bh)) {
          v2id[v * V + bh] = ct++;
          height[v].insert(bh);
        }
        if (!v2id.count(e.to * V + nh)) {
          v2id[e.to * V + nh] = ct++;
          height[e.to].insert(nh);
        }
        int uu = v2id[v * V + bh], vv = v2id[e.to * V + nh];
        gg[uu].pb({vv, e.cost});
        max_h[e.to] = nh;
        que.push(pi(max_h[e.to], e.to));
      }
    }
  }
  rep(i, n) for (const auto &e : G[i]) {
    if (H[i] < e.cost)
      continue;
    if (!v2id.count(i * V + e.cost)) {
      v2id[i * V + e.cost] = ct++;
      height[i].insert(e.cost);
    }
    if (!v2id.count(e.to * V + 0)) {
      v2id[e.to * V + 0] = ct++;
      height[e.to].insert(0);
    }
    int uu = v2id[i * V + e.cost];
    int vv = v2id[e.to * V + 0];
    gg[uu].pb({vv, e.cost});
  }
  if (!v2id.count((n - 1) * V + H[n - 1])) {
    v2id[(n - 1) * V + H[n - 1]] = ct++;
    height[n - 1].insert(H[n - 1]);
  }
  rep(i, n) {
    int S = height[i].size();
    auto itr = height[i].begin();
    rep(j, S - 1) {
      auto nx = itr;
      ++nx;
      assert(v2id.count(i * V + *itr));
      assert(v2id.count(i * V + *nx));
      int uu = v2id[i * V + *itr];
      int vv = v2id[i * V + *nx];
      int dist = *nx - *itr;
      gg[uu].pb({vv, dist});
      gg[vv].pb({uu, dist});
      itr = nx;
    }
  }
  int start = v2id[0 * V + X];
  int goal = v2id[(n - 1) * V + H[n - 1]];
  fill(d, d + VV, INF);
  d[start] = 0;
  priority_queue<P, vector<P>, greater<P>> pq;
  pq.push(pi(0, start));
  while (!pq.empty()) {
    P now = pq.top();
    pq.pop();
    int v = now.se;
    if (now.fi > d[v])
      continue;
    for (const auto &e : gg[v]) {
      if (d[e.to] > d[v] + e.cost) {
        d[e.to] = d[v] + e.cost;
        pq.push(pi(d[e.to], e.to));
      }
    }
  }
  ll ans = d[goal];
  if (ans == INF)
    ans = -1;
  printf("%lld\n", ans);
  return 0;
}
accuracy: 0.6190476190476191
time_ms: 780
memory_kb: 176628
final_rank: 18

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int(i) = 0; (i) < (int)(n); ++(i))
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define fi first
#define se second
using pi = pair<int, int>;
using P = pair<ll, int>;
struct edge {
  int to;
  ll cost;
};
const int V = 200000;
const int VV = 2000000;
const ll INF = LLONG_MAX / 3;
vector<edge> G[V], gg[VV];
int n, m;
ll X;
int H[V];
map<pi, int> v2id;
int max_h[V] = {};
ll d[VV];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  cin >> n >> m >> X;
  rep(i, n) cin >> H[i];
  rep(i, m) {
    int a, b, t;
    cin >> a >> b >> t;
    --a; --b;
    G[a].pb({b, t});
    G[b].pb({a, t});
  }
  int ct = 1;
  priority_queue<pi> que;
  que.push(pi(X, 0));
  v2id[pi(0, X)] = 0;
  max_h[0] = X;
  vector<int> height[n];
  height[0].pb(X);
  while (!que.empty()) {
    pi p = que.top();
    que.pop();
    int v = p.se, h = p.fi;
    if (h < max_h[v])
      continue;
    for (const auto &e : G[v]) {
      int bh = h;
      int nh = h - e.cost;
      if (nh < 0)
        continue;
      if (nh > H[e.to]) {
        bh = H[e.to] + e.cost;
        nh = H[e.to];
      }
      if (nh > max_h[e.to]) {
        if (!v2id.count(pi(v, bh))) {
          v2id[pi(v, bh)] = ct++;
          height[v].pb(bh);
        }
        if (!v2id.count(pi(e.to, nh))) {
          v2id[pi(e.to, nh)] = ct++;
          height[e.to].pb(nh);
        }
        int uu = v2id[pi(v, bh)], vv = v2id[pi(e.to, nh)];
        gg[uu].pb({vv, e.cost});
        max_h[e.to] = nh;
        que.push(pi(max_h[e.to], e.to));
      }
    }
  }
  rep(i, n) for (const auto &e : G[i]) {
    if (H[i] < e.cost)
      continue;
    if (!v2id.count(pi(i, e.cost))) {
      v2id[pi(i, e.cost)] = ct++;
      height[i].pb(e.cost);
    }
    if (!v2id.count(pi(e.to, 0))) {
      v2id[pi(e.to, 0)] = ct++;
      height[e.to].pb(0);
    }
    int uu = v2id[pi(i, e.cost)];
    int vv = v2id[pi(e.to, 0)];
    gg[uu].pb({vv, e.cost});
  }
  if (!v2id.count(pi(n - 1, H[n - 1]))) {
    v2id[pi(n - 1, H[n - 1])] = ct++;
    height[n - 1].pb(H[n - 1]);
  }
  rep(i, n) {
    sort(all(height[i]));
    height[i].erase(unique(all(height[i])), height[i].end());
    int S = height[i].size();
    rep(j, S - 1) {
      int uu = v2id[pi(i, height[i][j])];
      int vv = v2id[pi(i, height[i][j + 1])];
      int dist = height[i][j + 1] - height[i][j];
      gg[uu].pb({vv, dist});
      gg[vv].pb({uu, dist});
    }
  }
  int start = v2id[pi(0, X)];
  int goal = v2id[pi(n - 1, H[n - 1])];
  fill(d, d + VV, INF);
  d[start] = 0;
  priority_queue<P, vector<P>, greater<P>> pq;
  pq.push(pi(0, start));
  while (!pq.empty()) {
    P now = pq.top();
    pq.pop();
    int v = now.se;
    if (now.fi > d[v])
      continue;
    for (const auto &e : gg[v]) {
      if (d[e.to] > d[v] + e.cost) {
        d[e.to] = d[v] + e.cost;
        pq.push(pi(d[e.to], e.to));
      }
    }
  }
  ll ans = d[goal];
  if (ans == INF)
    ans = -1;
  cout << ans << '\n';
  return 0;
}
accuracy: 1
time_ms: 990
memory_kb: 193308
final_rank: 7

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int(i) = 0; (i) < (int)(n); ++(i))
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define fi first
#define se second
using pi = pair<int, int>;
using P = pair<ll, int>;
struct edge {
  int to;
  ll cost;
};
const int V = 100000;
const int VV = 1000000;
const ll INF = LLONG_MAX / 3;
vector<edge> G[V], gg[VV];
set<int> height[V];
int n, m;
ll X;
int H[V];
map<pi, int> v2id;
int max_h[V] = {};
ll d[VV];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cin >> n >> m >> X;
  rep(i, n) cin >> H[i];
  rep(i, m) {
    int a, b, t;
    cin >> a >> b >> t;
    --a;
    --b;
    G[a].pb({b, t});
    G[b].pb({a, t});
  }
  int ct = 1;
  priority_queue<pi> que;
  que.push(pi(X, 0));
  v2id[pi(0, X)] = 0;
  max_h[0] = X;
  height[0].insert(X);
  while (!que.empty()) {
    pi p = que.top();
    que.pop();
    int v = p.se, h = p.fi;
    if (h < max_h[v])
      continue;
    for (const auto &e : G[v]) {
      int bh = h;
      int nh = h - e.cost;
      if (nh < 0)
        continue;
      if (nh > H[e.to]) {
        bh = H[e.to] + e.cost;
        nh = H[e.to];
      }
      if (nh > max_h[e.to]) {
        if (!v2id.count(pi(v, bh))) {
          v2id[pi(v, bh)] = ct++;
          height[v].insert(bh);
        }
        if (!v2id.count(pi(e.to, nh))) {
          v2id[pi(e.to, nh)] = ct++;
          height[e.to].insert(nh);
        }
        int uu = v2id[pi(v, bh)], vv = v2id[pi(e.to, nh)];
        gg[uu].pb({vv, e.cost});
        max_h[e.to] = nh;
        que.push(pi(max_h[e.to], e.to));
      }
    }
  }
  rep(i, n) for (const auto &e : G[i]) {
    if (H[i] < e.cost)
      continue;
    if (!v2id.count(pi(i, e.cost))) {
      v2id[pi(i, e.cost)] = ct++;
      height[i].insert(e.cost);
    }
    if (!v2id.count(pi(e.to, 0))) {
      v2id[pi(e.to, 0)] = ct++;
      height[e.to].insert(0);
    }
    int uu = v2id[pi(i, e.cost)];
    int vv = v2id[pi(e.to, 0)];
    gg[uu].pb({vv, e.cost});
  }
  if (!v2id.count(pi(n - 1, H[n - 1]))) {
    v2id[pi(n - 1, H[n - 1])] = ct++;
    height[n - 1].insert(H[n - 1]);
  }
  rep(i, n) {
    int S = height[i].size();
    auto itr = height[i].begin();
    rep(j, S - 1) {
      auto nx = itr;
      ++nx;
      assert(v2id.count(pi(i, *itr)));
      assert(v2id.count(pi(i, *nx)));
      int uu = v2id[pi(i, *itr)];
      int vv = v2id[pi(i, *nx)];
      int dist = *nx - *itr;
      gg[uu].pb({vv, dist});
      gg[vv].pb({uu, dist});
      itr = nx;
    }
  }
  int start = v2id[pi(0, X)];
  int goal = v2id[pi(n - 1, H[n - 1])];
  fill(d, d + VV, INF);
  d[start] = 0;
  priority_queue<P, vector<P>, greater<P>> pq;
  pq.push(pi(0, start));
  while (!pq.empty()) {
    P now = pq.top();
    pq.pop();
    int v = now.se;
    if (now.fi > d[v])
      continue;
    for (const auto &e : gg[v]) {
      if (d[e.to] > d[v] + e.cost) {
        d[e.to] = d[v] + e.cost;
        pq.push(pi(d[e.to], e.to));
      }
    }
  }
  ll ans = d[goal];
  if (ans == INF)
    ans = -1;
  cout << ans << '\n';
  return 0;
}
accuracy: 1
time_ms: 1120
memory_kb: 188976
final_rank: 14

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int(i) = 0; (i) < (int)(n); ++(i))
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define fi first
#define se second
using pi = pair<int, int>;
using P = pair<ll, int>;
struct edge {
  int to;
  ll cost;
};
const int V = 100000;
const int VV = 1000000;
const ll INF = LLONG_MAX / 3;
vector<edge> G[V], gg[VV];
set<int> height[V];
int n, m;
ll X;
int H[V];
map<pi, int> v2id;
int max_h[V] = {};
ll d[VV];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> n >> m >> X;
  rep(i, n) cin >> H[i];
  rep(i, m) {
    int a, b, t;
    cin >> a >> b >> t;
    --a;
    --b;
    G[a].pb({b, t});
    G[b].pb({a, t});
  }
  int ct = 1;
  priority_queue<pi> que;
  que.push(pi(X, 0));
  v2id[pi(0, X)] = 0;
  max_h[0] = X;
  height[0].insert(X);
  while (!que.empty()) {
    pi p = que.top();
    que.pop();
    int v = p.se, h = p.fi;
    if (h < max_h[v])
      continue;
    for (const auto &e : G[v]) {
      int bh = h;
      int nh = h - e.cost;
      if (nh < 0)
        continue;
      if (nh > H[e.to]) {
        bh = H[e.to] + e.cost;
        nh = H[e.to];
      }
      if (nh > max_h[e.to]) {
        if (!v2id.count(pi(v, bh))) {
          v2id[pi(v, bh)] = ct++;
          height[v].insert(bh);
        }
        if (!v2id.count(pi(e.to, nh))) {
          v2id[pi(e.to, nh)] = ct++;
          height[e.to].insert(nh);
        }
        int uu = v2id[pi(v, bh)], vv = v2id[pi(e.to, nh)];
        gg[uu].pb({vv, e.cost});
        max_h[e.to] = nh;
        que.push(pi(max_h[e.to], e.to));
      }
    }
  }
  rep(i, n) for (const auto &e : G[i]) {
    if (H[i] < e.cost)
      continue;
    if (!v2id.count(pi(i, e.cost))) {
      v2id[pi(i, e.cost)] = ct++;
      height[i].insert(e.cost);
    }
    if (!v2id.count(pi(e.to, 0))) {
      v2id[pi(e.to, 0)] = ct++;
      height[e.to].insert(0);
    }
    int uu = v2id[pi(i, e.cost)];
    int vv = v2id[pi(e.to, 0)];
    gg[uu].pb({vv, e.cost});
  }
  if (!v2id.count(pi(n - 1, H[n - 1]))) {
    v2id[pi(n - 1, H[n - 1])] = ct++;
    height[n - 1].insert(H[n - 1]);
  }
  rep(i, n) {
    int S = height[i].size();
    auto itr = height[i].begin();
    rep(j, S - 1) {
      auto nx = itr;
      ++nx;
      assert(v2id.count(pi(i, *itr)));
      assert(v2id.count(pi(i, *nx)));
      int uu = v2id[pi(i, *itr)];
      int vv = v2id[pi(i, *nx)];
      int dist = *nx - *itr;
      gg[uu].pb({vv, dist});
      gg[vv].pb({uu, dist});
      itr = nx;
    }
  }
  int start = v2id[pi(0, X)];
  int goal = v2id[pi(n - 1, H[n - 1])];
  fill(d, d + VV, INF);
  d[start] = 0;
  priority_queue<P, vector<P>, greater<P>> pq;
  pq.push(pi(0, start));
  while (!pq.empty()) {
    P now = pq.top();
    pq.pop();
    int v = now.se;
    if (now.fi > d[v])
      continue;
    for (const auto &e : gg[v]) {
      if (d[e.to] > d[v] + e.cost) {
        d[e.to] = d[v] + e.cost;
        pq.push(pi(d[e.to], e.to));
      }
    }
  }
  ll ans = d[goal];
  if (ans == INF)
    ans = -1;
  cout << ans << endl;
  return 0;
}
accuracy: 1
time_ms: 1110
memory_kb: 188976
final_rank: 13

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int(i) = 0; (i) < (int)(n); ++(i))
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define fi first
#define se second
using pi = pair<int, int>;
using P = pair<ll, int>;
struct edge {
  int to;
  ll cost;
};
const int V = 100000;
const int VV = 1000000;
const ll INF = LLONG_MAX / 3;
vector<edge> G[V], gg[VV];
set<int> height[V];
int n, m;
ll X;
int H[V];
map<pi, int> v2id;
int max_h[V] = {};
ll d[VV];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  cin >> n >> m >> X;
  rep(i, n) cin >> H[i];
  rep(i, m) {
    int a, b, t;
    cin >> a >> b >> t;
    --a;
    --b;
    G[a].pb({b, t});
    G[b].pb({a, t});
  }
  int ct = 1;
  priority_queue<pi> que;
  que.push(pi(X, 0));
  v2id[pi(0, X)] = 0;
  max_h[0] = X;
  height[0].insert(X);
  while (!que.empty()) {
    pi p = que.top();
    que.pop();
    int v = p.se, h = p.fi;
    if (h < max_h[v])
      continue;
    for (const auto &e : G[v]) {
      int bh = h;
      int nh = h - e.cost;
      if (nh < 0)
        continue;
      if (nh > H[e.to]) {
        bh = H[e.to] + e.cost;
        nh = H[e.to];
      }
      if (nh > max_h[e.to]) {
        if (!v2id.count(pi(v, bh))) {
          v2id[pi(v, bh)] = ct++;
          height[v].insert(bh);
        }
        if (!v2id.count(pi(e.to, nh))) {
          v2id[pi(e.to, nh)] = ct++;
          height[e.to].insert(nh);
        }
        int uu = v2id[pi(v, bh)], vv = v2id[pi(e.to, nh)];
        gg[uu].pb({vv, e.cost});
        max_h[e.to] = nh;
        que.push(pi(max_h[e.to], e.to));
      }
    }
  }
  rep(i, n) for (const auto &e : G[i]) {
    if (H[i] < e.cost)
      continue;
    if (!v2id.count(pi(i, e.cost))) {
      v2id[pi(i, e.cost)] = ct++;
      height[i].insert(e.cost);
    }
    if (!v2id.count(pi(e.to, 0))) {
      v2id[pi(e.to, 0)] = ct++;
      height[e.to].insert(0);
    }
    int uu = v2id[pi(i, e.cost)];
    int vv = v2id[pi(e.to, 0)];
    gg[uu].pb({vv, e.cost});
  }
  if (!v2id.count(pi(n - 1, H[n - 1]))) {
    v2id[pi(n - 1, H[n - 1])] = ct++;
    height[n - 1].insert(H[n - 1]);
  }
  rep(i, n) {
    int S = height[i].size();
    auto itr = height[i].begin();
    rep(j, S - 1) {
      auto nx = itr;
      ++nx;
      assert(v2id.count(pi(i, *itr)));
      assert(v2id.count(pi(i, *nx)));
      int uu = v2id[pi(i, *itr)];
      int vv = v2id[pi(i, *nx)];
      int dist = *nx - *itr;
      gg[uu].pb({vv, dist});
      gg[vv].pb({uu, dist});
      itr = nx;
    }
  }
  int start = v2id[pi(0, X)];
  int goal = v2id[pi(n - 1, H[n - 1])];
  fill(d, d + VV, INF);
  d[start] = 0;
  priority_queue<P, vector<P>, greater<P>> pq;
  pq.push(pi(0, start));
  while (!pq.empty()) {
    P now = pq.top();
    pq.pop();
    int v = now.se;
    if (now.fi > d[v])
      continue;
    for (const auto &e : gg[v]) {
      if (d[e.to] > d[v] + e.cost) {
        d[e.to] = d[v] + e.cost;
        pq.push(pi(d[e.to], e.to));
      }
    }
  }
  ll ans = d[goal];
  if (ans == INF)
    ans = -1;
  cout << ans << '\n';
  return 0;
}
accuracy: 1
time_ms: 1100
memory_kb: 189084
final_rank: 11

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int(i) = 0; (i) < (int)(n); ++(i))
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define fi first
#define se second
using pi = pair<int, int>;
using P = pair<ll, int>;
struct edge {
  int to;
  ll cost;
};
const int V = 100000;
const int VV = 1000000;
const ll INF = LLONG_MAX / 3;
vector<edge> G[V], gg[VV];
multiset<int> height[V];
int n, m;
ll X;
int H[V];
map<pi, int> v2id;
int max_h[V] = {};
ll d[VV];
int main() {
  scanf(" %d %d %lld", &n, &m, &X);
  rep(i, n) scanf(" %d", &H[i]);
  rep(i, m) {
    int a, b, t;
    scanf(" %d %d %d", &a, &b, &t);
    --a;
    --b;
    G[a].pb({b, t});
    G[b].pb({a, t});
  }
  int ct = 1;
  priority_queue<pi> que;
  que.push(pi(X, 0));
  v2id[pi(0, X)] = 0;
  max_h[0] = X;
  height[0].insert(X);
  while (!que.empty()) {
    pi p = que.top();
    que.pop();
    int v = p.se, h = p.fi;
    if (h < max_h[v])
      continue;
    for (const auto &e : G[v]) {
      int bh = h;
      int nh = h - e.cost;
      if (nh < 0)
        continue;
      if (nh > H[e.to]) {
        bh = H[e.to] + e.cost;
        nh = H[e.to];
      }
      if (nh > max_h[e.to]) {
        if (!v2id.count(pi(v, bh))) {
          v2id[pi(v, bh)] = ct++;
          height[v].insert(bh);
        }
        if (!v2id.count(pi(e.to, nh))) {
          v2id[pi(e.to, nh)] = ct++;
          height[e.to].insert(nh);
        }
        int uu = v2id[pi(v, bh)], vv = v2id[pi(e.to, nh)];
        gg[uu].pb({vv, e.cost});
        max_h[e.to] = nh;
        que.push(pi(max_h[e.to], e.to));
      }
    }
  }
  rep(i, n) for (const auto &e : G[i]) {
    if (H[i] < e.cost)
      continue;
    if (!v2id.count(pi(i, e.cost))) {
      v2id[pi(i, e.cost)] = ct++;
      height[i].insert(e.cost);
    }
    if (!v2id.count(pi(e.to, 0))) {
      v2id[pi(e.to, 0)] = ct++;
      height[e.to].insert(0);
    }
    int uu = v2id[pi(i, e.cost)];
    int vv = v2id[pi(e.to, 0)];
    gg[uu].pb({vv, e.cost});
  }
  if (!v2id.count(pi(n - 1, H[n - 1]))) {
    v2id[pi(n - 1, H[n - 1])] = ct++;
    height[n - 1].insert(H[n - 1]);
  }
  rep(i, n) {
    int S = height[i].size();
    auto itr = height[i].begin();
    rep(j, S - 1) {
      auto nx = itr;
      ++nx;
      assert(v2id.count(pi(i, *itr)));
      assert(v2id.count(pi(i, *nx)));
      int uu = v2id[pi(i, *itr)];
      int vv = v2id[pi(i, *nx)];
      int dist = *nx - *itr;
      gg[uu].pb({vv, dist});
      gg[vv].pb({uu, dist});
      itr = nx;
    }
  }
  int start = v2id[pi(0, X)];
  int goal = v2id[pi(n - 1, H[n - 1])];
  fill(d, d + VV, INF);
  d[start] = 0;
  priority_queue<P, vector<P>, greater<P>> pq;
  pq.push(pi(0, start));
  while (!pq.empty()) {
    P now = pq.top();
    pq.pop();
    int v = now.se;
    if (now.fi > d[v])
      continue;
    for (const auto &e : gg[v]) {
      if (d[e.to] > d[v] + e.cost) {
        d[e.to] = d[v] + e.cost;
        pq.push(pi(d[e.to], e.to));
      }
    }
  }
  ll ans = d[goal];
  if (ans == INF)
    ans = -1;
  printf("%lld\n", ans);
  return 0;
}
accuracy: 1
time_ms: 1100
memory_kb: 188880
final_rank: 10