#include using namespace std; typedef long long ll; typedef long double ld; typedef pair PP; // #define MOD 1000000007 #define MOD 998244353 #define INF 2305843009213693951 //#define INF 810114514 #define PI 3.141592653589 #define setdouble setprecision #define REP(i,n) for(ll i=0;i<(n);++i) #define OREP(i,n) for(ll i=1;i<=(n);++i) #define RREP(i,n) for(ll i=(n)-1;i>=0;--i) #define ORREP(i,n) for(ll i=(n);i>=1;--i) #define rep(i,a,b) for(ll i=(a);i<=(b);++i) #define ALL(v) (v).begin(), (v).end() #define GOODBYE do { cout << "-1" << endl; return 0; } while (false) #define MM <<" "<< #define Endl endl #define debug true #define debug2 false class BellmanFord{ // Copyright (c) 2023 0214sh7 // https://github.com/0214sh7/library/ private: typedef std::pair,long long> P; int V,E; // long long INF = (1LL<<61); std::vector,long long>> es; public: void init(int N,std::vector,long long>> edge){ //辺数をもとめる  E=edge.size(); //頂点数を決定する V=N; es=edge; } BellmanFord(int N,std::vector,long long>> edge){ init(N,edge); } std::vector> solve(int s){ std::vector> d; //INFで初期化する for(int i=0;id[k.first.first].first+k.second){ d[k.first.second].first=d[k.first.first].first+k.second; if(j>=V){ d[k.first.second].second=true; } update=true; } } if(!update)break; } return d; } }; int main(void){ //cin.tie(nullptr); //ios::sync_with_stdio(false); ll N,M; cin >> N >> M; vector A(N); REP(i,N){ cin >> A[i]; } vector U(M),V(M),C(M); std::vector,long long>> E; REP(i,M){ cin >> U[i] >> V[i] >> C[i]; U[i]--; V[i]--; E.push_back({{U[i],V[i]},C[i]-A[V[i]]}); } BellmanFord bellmanford(N,E); std::vector> B = bellmanford.solve(0); ll Ans = 0; for(int i=0;i