#include #include #define chmin(x,y) (x) = min((x),(y)) #define chmax(x,y) (x) = max((x),(y)) #define ld long double using namespace std; using namespace atcoder; using ll = long long; using mint = modint998244353; const ll mod = 998244353; using Graph = vector>>; //using Graph = vector>; const vector dx = {1,0,-1,0}, dy = {0,1,0,-1}; int main(){ // input int N,M; cin >> N >> M; vector A(N); for(int i = 0; i < N; i++) cin >> A[i]; Graph G(N); while(M--){ int a,b,c; cin >> a >> b >> c; G[--a].emplace_back(--b,c); } // prep bool inf = 0; for(int i = 0; i < N; i++){ // vector seen(N, false); // 既に見たことがある頂点か記録する priority_queue> pq; pq.emplace(0,i); vector satis(N,0); satis[i] = 0; while(!pq.empty() && !inf){ auto [tmp,pos] = pq.top(); pq.pop(); if(satis[pos] > tmp) continue; for(auto p : G[pos]){ auto nxt = p.first, cost = p.second; if(satis[nxt] < tmp + A[nxt] - cost){ pq.emplace(tmp+A[nxt]-cost,nxt); satis[nxt] = tmp+A[nxt]-cost; } if(nxt == i){ if(tmp + A[i] - cost > 0){ inf = 1; break; } } } } } // output if(inf) cout << "inf" << endl; else{ priority_queue> pq; pq.emplace(A[0],0); vector ans(N,0); ans[0] = A[0]; while(!pq.empty()){ auto [tmp,pos] = pq.top(); pq.pop(); if(ans[pos] > tmp) continue; for(auto p : G[pos]){ auto nxt = p.first, cost = p.second; if(ans[nxt] < tmp + A[nxt] - cost){ pq.emplace(tmp+A[nxt]-cost,nxt); ans[nxt] = tmp+A[nxt]-cost; } } } cout << ans[N-1] << endl; } }