#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i,n) for(ll i=0,endrep=(n); i<endrep; ++i)
#define rep1(i,n) for(ll i=1,endrep=(n); i<=endrep; ++i)
#define revrep(i,n) for(ll i=(ll)(n)-1; i>=0; --i)
inline constexpr ll Inf = (1ULL << 60) -123456789;
#define fastio cin.tie(0); ios_base::sync_with_stdio(false); cout<<fixed<<setprecision(10);
#define newl '\n'
#define YN(e) ((e)?"Yes":"No")
#define all(a) begin(a),end(a)
#define rall(a) rbegin(a),rend(a)
#define delif(c,pred) (c).erase(remove_if(all(c),(pred)), end(c))
template <class T,class U> bool updmax(T& a, U b) { if(b>a){ a=b; return true;} return false;}
template <class T,class U> bool updmin(T& a, U b) { if(b<a){ a=b; return true;} return false;}
inline constexpr int Mod = 1000000007;
//inline constexpr int Mod = 998244353;
#define dbg(a) cerr << #a << ": " << a << endl;
#define dbgs(s) cerr << #s << endl;
#define dbg1(a,n) cerr<<#a<<": "; rep(i,n) cerr<<a[i]<<" "; cerr<<endl;
#define dbg2(m,h,w) cerr<<#m<<":"<<endl; rep(i,h){ rep(j,w)cerr<<m[i][j]<<" "; cerr<<endl; }
template <class T, class U> ostream& operator << (ostream& os, pair<T,U> v) {os<<v.first<<","<<v.second;return os;}
template <class T, size_t N> ostream& operator << (ostream& os, array<T,N> v) {rep(i,N)os<<v[i]<<(i+1<(ll)N?" ":"");return os;}
int rng(int n) { return rand()/(RAND_MAX+1.0)*n; }

ll dp[1<<18];
int main() {
    fastio;
    ll ans{};
    ll N,M;
    cin >> N >> M;
    vector<vector<array<ll,2>>> m(N+1);
    rep(i,M) {
        ll x,y,z;
        cin >> x >> y >> z;
        m[x].push_back({y,z});
    }
    dp[0] = 1;
    rep(p,1<<N) {
        rep(j,N) {
            if (p>>j&1) continue;
            int to = p|(1<<j);
            dp[to] += dp[p];
            for (auto [y,z]:m[__builtin_popcount(p)+1]) {
                int c = to & ((1<<y)-1);
                if (__builtin_popcount(c) > z) {
                    dp[to] = 0;
                    break;
                }
            }
        }
    }
    ans = dp[(1<<N)-1];
    cout << ans << endl;
}

#include <bits/stdc++.h>
using namespace std;
static const int INF=1000000007;
#define rep(i,n) for(int i=0;i<n;i++)
int A[200010];
int dp[200010];

int main(){
  int n,m;
  cin>>n>>m;
  rep(i,n) cin>>A[i];
  vector<int>edge[n];
  rep(i,m){
    int a,b;
    cin>>a>>b;
    a--;
    b--;
    edge[a].push_back(b);
  }
  rep(i,n) dp[i]=INF;
  for(int i=0;i<n;i++){
    for(auto j:edge[i]){
      dp[j]=min(dp[j],min(dp[i],A[i]));
    }
  }
  int ans=-INF;
  rep(i,n){
    if(dp[i] != INF){
      ans=max(ans,A[i]-dp[i]);
    }
  }
  cout<<ans<<endl;
  return 0;
}
