// P1073 最优贸易。价格是 1..100,单调标签至多改善 100 次。 #include using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n, m; if (!(cin >> n >> m)) return 0; vector price(n + 1); for (int i = 1; i <= n; ++i) cin >> price[i]; vector> g(n + 1), rg(n + 1); auto add = [&](int u, int v) { g[u].push_back(v); rg[v].push_back(u); }; for (int i = 0, u, v, z; i < m; ++i) { cin >> u >> v >> z; add(u,v); if (z == 2) add(v,u); } vector low(n + 1, 101), high(n + 1, -1); vector inq(n + 1, false); queue q; low[1] = price[1]; q.push(1); inq[1] = true; while (!q.empty()) { int u = q.front(); q.pop(); inq[u] = false; for (int v : g[u]) { int cand = min(low[u], price[v]); if (cand < low[v]) { low[v] = cand; if (!inq[v]) { q.push(v); inq[v] = true; } } } } high[n] = price[n]; q.push(n); inq[n] = true; while (!q.empty()) { int u = q.front(); q.pop(); inq[u] = false; for (int v : rg[u]) { int cand = max(high[u], price[v]); if (cand > high[v]) { high[v] = cand; if (!inq[v]) { q.push(v); inq[v] = true; } } } } int ans = 0; for (int v = 1; v <= n; ++v) if (low[v] <= 100 && high[v] >= 1) ans = max(ans, high[v]-low[v]); cout << ans << '\n'; }