#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
const int N = 1e4 + 5;
template <typename T>
struct Stack
{
T s[N]; int tp;
Stack() { tp = 0; }
void push(T x) { s[++tp] = x; }
void pop() { tp--; }
T top() { return s[tp]; }
};
int a[N];
vector<int> G[N];
int dfn[N], low[N], scc[N], sc;
bool ins[N];
int sum[N], dp[N];
void tarjan(int u)
{
static int cur = 0;
static Stack<int> s;
dfn[u] = low[u] = ++cur;
s.push(u); ins[u] = true;
for (int v : G[u])
if (!dfn[v]) tarjan(v), low[u] = min(low[u], low[v]);
else if (ins[v]) low[u] = min(low[u], dfn[v]);
if (low[u] != dfn[u]) return;
sc++;
while (ins[u])
{
int t = s.top();
ins[t] = false;
scc[t] = sc;
sum[sc] += a[t];
for (int v : G[t])
dp[sc] = max(dp[sc], dp[ scc[v] ]);
dp[sc] += sum[sc];
s.pop();
}
}
int main()
{
int n, m;
cin >> n >> m;
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = 1, u, v; i <= m; i++)
cin >> u >> v, G[u].push_back(v);
for (int i = 1; i <= n; i++)
if (!dfn[i]) tarjan(i);
cout << *max_element(dp + 1, dp + sc + 1);
}