#include #include using namespace std; using namespace atcoder; #define rep(i, n) for(int i=0;i<(n);++i) #define rep1(i, n) for(int i=1;i<=(n);i++) #define ll long long using mint = modint998244353; using P = pair; using lb = long double; using T = tuple; #ifdef LOCAL # include # define dbg(...) debug_print::multi_print(#__VA_ARGS__, __VA_ARGS__) #else # define dbg(...) (static_cast(0)) #endif int main() { int n, m; cin >> n >> m; vector a(n); rep(i,n) cin >> a[i]; vector>> g(n), rev(n); rep(i,m){ int a, b, c; cin >> a >> b >> c; --a;--b; g[a].emplace_back(b,c); rev[b].emplace_back(a,c); } vector cnt(n); queue q; q.push(0); while(!q.empty()) { int u = q.front();q.pop(); cnt[u]++; for(auto [v, c] : g[u]) { if(cnt[v]==0) { q.push(v); } } } q.push(n-1); while(!q.empty()) { int u = q.front();q.pop(); cnt[u]++; for(auto [v, c] :rev[u]) { if(cnt[v]==1) { q.push(v); } } } vector dist(n, -1e18); dist[0] = 0; dbg(cnt); rep(j,2*n+1) { bool upd = false; rep(i,n-1){ if(cnt[i]!=2) continue; for(auto [v, c] : g[i]) { if(cnt[v]!=2) continue; if(dist[v]