#include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; using ll=long long; #include using mint=atcoder::modint998244353; ostream& operator<<(ostream& os,const mint& x){ os<>(istream& is,mint& x){ int t; is>>t; x=t; return is; } template ostream& operator<<(ostream& os,const pair& p); template istream& operator>>(istream& is,pair& p); template ostream& operator<<(ostream& os,const array& arr); template istream& operator>>(istream& is,array& arr); template ostream& operator<<(ostream& os,const vector& vec); template istream& operator>>(istream& is,vector& vec); template ostream& operator<<(ostream& os,const pair& p){ os< istream& operator>>(istream& is,pair& p){ is>>p.first>>p.second; return is; } template ostream& operator<<(ostream& os,const array& arr){ for(int i=0;i istream& operator>>(istream& is,array& arr){ for(int i=0;i>arr[i]; return is; } template ostream& operator<<(ostream& os,const vector& vec){ for(int i=0;i istream& operator>>(istream& is,vector& vec){ for(int i=0;i>vec[i]; return is; } template void input_vec(Vecs&... vs) { const auto n = get<0>(tie(vs...)).size(); for (size_t i = 0; i < n; ++i) ((cin >> vs[i]), ...); } template vector make_unique(vector vec){ ranges::sort(vec); vec.erase(unique(vec.begin(),vec.end()),vec.end()); return vec; } template pair,vector> make_rank(const vector& vec, Comp comp = {}, Proj proj = {}) { int n = vec.size(); vector argsort(n); iota(argsort.begin(), argsort.end(), 0); ranges::stable_sort(argsort, comp, [&](int i) -> decltype(auto) { return invoke(proj, vec[i]); }); vector rank(n); for(int i=0;i; using vvl=vector>; using vvvl=vector>>; using vi=vector; using vvi=vector>; using vvvi=vector>>; int main(){ cin.tie(nullptr); ios::sync_with_stdio(false); cout<>n>>m; vl p(n); cin>>p; vi u(m),v(m); vl t(m); input_vec(u,v,t); for(int i=0;i; ll inf=1e18; vector

dp(n,P{inf,-1}),ep(n,P{inf,-1}); for(int i=0;ivoid{ if(dp[i].second!=-1){ if(dp[j].first>dp[i].first+w){ if(dp[j].second!=dp[i].second&&dp[j].second!=-1){ ep[j]=dp[j]; } dp[j]=P{dp[i].first+w,dp[i].second}; }else if(ep[j].first>dp[i].first+w&&dp[j].second!=dp[i].second){ ep[j]=P{dp[i].first+w,dp[i].second}; } } if(ep[i].second!=-1){ if(ep[j].first>ep[i].first+w&&dp[j].second!=ep[i].second){ ep[j]=P{ep[i].first+w,ep[i].second}; } } }; for(int tt=0;tt>> revgraph(n); for(int i=0;i bfs; bfs.push(i); vl seen(n,inf); seen[i]=a[i]-p[i]; while(!bfs.empty()){ int x=bfs.front(); bfs.pop(); for(auto[w,y]:revgraph[x]){ if(seen[y]!=inf)continue; if(dp[y].second!=i){ if(dp[y].first+w==seen[x]){ seen[y]=dp[y].first; bfs.push(y); } }else{ if(ep[y].first+w==seen[x]){ seen[y]=ep[y].first; bfs.push(y); } } } } for(int j=0;j