#include #include #include using namespace std; using namespace atcoder; using ll = long long; using mint = modint998244353; //using mint = modint1000000007; #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define repu(i, s, t) for (int i = (int)(s); i < (int)(t); i++) #define repd(i, s, t) for (int i = (int)(s)-1; i >= (int)(t); i--) #define all(v) v.begin(), v.end() template bool chmax(T &a, const T b) { if(a >= b) return false; a = b; return true; } template bool chmin(T &a, const T b) { if(a <= b) return false; a = b; return true; } template istream& operator>>(istream &in, vector &a) { for(T &x: a) in >> x; return in; } template ostream& operator<<(ostream &out, const vector &a) { for(const T &x: a) out << x << ' '; return out; } const int di[] = {1, 0, -1, 0, 1, 1, -1, -1, 0}; const int dj[] = {0, 1, 0, -1, -1, 1, 1, -1, 0}; using COST = ll; const COST Z = 0; const COST E = 1; const COST INF = 1e18; struct DIJ { struct EDGE{ int to; COST cost; }; using EList = vector; using GRAPH = vector; using pci = pair; int n; GRAPH g; vector> dist; DIJ(int n_): n(n_), g(n_), dist(n_) {} void add_edge(int a, int b, COST c = E) { g[a].push_back(EDGE{b, c}); } vector& operator[](int i) { if(!dist[i].size()) solve(i); return dist[i]; } void solve(int s) { dist[s].resize(n, INF); priority_queue, greater> wait; dist[s][s] = Z; wait.emplace(Z, s); while(!wait.empty()) { int pos; COST cost; tie(cost, pos) = wait.top(); wait.pop(); if(dist[s][pos] < cost) continue; for(auto ed : g[pos]) { COST tmp = cost + ed.cost; if(dist[s][ed.to] <= tmp) continue; dist[s][ed.to] = tmp; wait.emplace(tmp, ed.to); } } } }; int main() { int n, m; cin >> n >> m; vector w(n); cin >> w; DIJ g(n); rep(i, m) { int u, v; ll t; cin >> u >> v >> t; u--; v--; g.add_edge(u, v, t); g.add_edge(v, u, t); } vector ord(n); iota(all(ord), 0); sort(all(ord), [&](int l, int r) { return g[0][l] < g[0][r]; }); map skip; skip[0] = 0; int pre = 0, ls = 0; rep(i, n) { if(w[ord[i]] >= w[ord[pre]]) continue; int tmp = w[ord[pre]] - w[ord[i]]; ll bord = (g[0][ord[i]] - g[0][ord[pre]] + tmp - 1) / tmp; if(bord >= ls) { skip[bord] = i; pre = i; ls = bord; } } ll ans = g[0][n-1]; rep(i, n) { int p = (*--skip.upper_bound(w[i])).second; chmin(ans, g[0][ord[p]] + g[n-1][i] + ll(w[ord[p]]) * w[i]); } cout << ans << endl; return 0; }