#include<bits/stdc++.h>
#define int long long
using namespace std;
const int INF = 1e9;
int n, m, timee , scc = 0, res = -1;
int low[200001], num[200001], dele[200001], longchim[200001];
vector<int> p[200001];
vector<int> newl[200001];
stack<int> st;
int c[200001], dp[200001];
void nhap() {
memset(dp, -1, sizeof(dp));
cin >> n >> m;
for (int i = 1; i <= n; i++) {
cin >> c[i];
}
for (int i = 1; i <= m; i++) {
int a, b;
cin >> a >> b;
p[a].push_back(b);
}
}
void cc(int u) {
low[u] = num[u] = ++timee;
st.push(u);
for (int v : p[u]) {
if (dele[v]) continue;
if (!num[v]) {
cc(v);
low[u] = min(low[u], low[v]);
}
else low[u] = min(low[u], num[v]);
}
if (low[u] == num[u]) {
scc++;
int sum = 0;
while (true) {
int l = st.top();
st.pop();
sum += c[l];
dele[l] = scc;
if (l == u) break;
}
longchim[scc] = sum;
}
}
int dfs(int u) {
if (dp[u] != -1) return dp[u];
dp[u] = longchim[u];
for (int v : newl[u]) {
dfs(v);
dp[u] = max(dp[u], dp[v] + longchim[u]);
}
return dp[u];
}
signed main() {
nhap();
for (int i = 1; i <= n; i++) {
if (!num[i]) cc(i);
}
for (int i = 1; i <= n; i++) {
for (int v : p[i]) {
if (dele[v] != dele[i]) newl[dele[i]].push_back(dele[v]);
}
}
for (int i = 1; i <= scc; i++) {
res = max(res, dfs(i));
}
cout << res;
return 0;
}
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KI2RlZmluZSBpbnQgbG9uZyBsb25nCnVzaW5nIG5hbWVzcGFjZSBzdGQ7CmNvbnN0IGludCBJTkYgPSAxZTk7CmludCBuLCBtLCB0aW1lZSAsIHNjYyA9IDAsIHJlcyA9IC0xOwppbnQgbG93WzIwMDAwMV0sIG51bVsyMDAwMDFdLCBkZWxlWzIwMDAwMV0sIGxvbmdjaGltWzIwMDAwMV07CnZlY3RvcjxpbnQ+IHBbMjAwMDAxXTsKdmVjdG9yPGludD4gbmV3bFsyMDAwMDFdOwpzdGFjazxpbnQ+IHN0OwppbnQgY1syMDAwMDFdLCBkcFsyMDAwMDFdOwp2b2lkIG5oYXAoKSB7CiAgICBtZW1zZXQoZHAsIC0xLCBzaXplb2YoZHApKTsKICAgIGNpbiA+PiBuID4+IG07CiAgICBmb3IgKGludCBpID0gMTsgaSA8PSBuOyBpKyspIHsKICAgICAgICBjaW4gPj4gY1tpXTsKICAgIH0KICAgIGZvciAoaW50IGkgPSAxOyBpIDw9IG07IGkrKykgewogICAgICAgIGludCBhLCBiOwogICAgICAgIGNpbiA+PiBhID4+IGI7CiAgICAgICAgcFthXS5wdXNoX2JhY2soYik7CiAgICB9Cn0Kdm9pZCBjYyhpbnQgdSkgewogICAgbG93W3VdID0gbnVtW3VdID0gKyt0aW1lZTsKICAgIHN0LnB1c2godSk7CiAgICBmb3IgKGludCB2IDogcFt1XSkgewogICAgICAgIGlmIChkZWxlW3ZdKSBjb250aW51ZTsKICAgICAgICBpZiAoIW51bVt2XSkgewogICAgICAgICAgICBjYyh2KTsKICAgICAgICAgICAgbG93W3VdID0gbWluKGxvd1t1XSwgbG93W3ZdKTsKICAgICAgICB9CiAgICAgICAgZWxzZSBsb3dbdV0gPSBtaW4obG93W3VdLCBudW1bdl0pOwogICAgfQogICAgaWYgKGxvd1t1XSA9PSBudW1bdV0pIHsKICAgICAgICBzY2MrKzsKICAgICAgICBpbnQgc3VtID0gMDsKICAgICAgICB3aGlsZSAodHJ1ZSkgewogICAgICAgICAgICBpbnQgbCA9IHN0LnRvcCgpOwogICAgICAgICAgICBzdC5wb3AoKTsKICAgICAgICAgICAgc3VtICs9IGNbbF07CiAgICAgICAgICAgIGRlbGVbbF0gPSBzY2M7CiAgICAgICAgICAgIGlmIChsID09IHUpIGJyZWFrOwogICAgICAgIH0KICAgICAgICBsb25nY2hpbVtzY2NdID0gc3VtOwogICAgfQp9CmludCBkZnMoaW50IHUpIHsKICAgIGlmIChkcFt1XSAhPSAtMSkgcmV0dXJuIGRwW3VdOwogICAgZHBbdV0gPSBsb25nY2hpbVt1XTsKICAgIGZvciAoaW50IHYgOiBuZXdsW3VdKSB7CiAgICAgICAgZGZzKHYpOwogICAgICAgIGRwW3VdID0gbWF4KGRwW3VdLCBkcFt2XSArIGxvbmdjaGltW3VdKTsKICAgIH0KICAgIHJldHVybiBkcFt1XTsKfQpzaWduZWQgbWFpbigpIHsKICAgIG5oYXAoKTsKICAgIGZvciAoaW50IGkgPSAxOyBpIDw9IG47IGkrKykgewogICAgICAgIGlmICghbnVtW2ldKSBjYyhpKTsKICAgIH0KICAgIGZvciAoaW50IGkgPSAxOyBpIDw9IG47IGkrKykgewogICAgICAgIGZvciAoaW50IHYgOiBwW2ldKSB7CiAgICAgICAgICAgIGlmIChkZWxlW3ZdICE9IGRlbGVbaV0pIG5ld2xbZGVsZVtpXV0ucHVzaF9iYWNrKGRlbGVbdl0pOwogICAgICAgIH0KICAgIH0KICAgIGZvciAoaW50IGkgPSAxOyBpIDw9IHNjYzsgaSsrKSB7CiAgICAgICAgcmVzID0gbWF4KHJlcywgZGZzKGkpKTsKICAgIH0KICAgIGNvdXQgPDwgcmVzOwogICAgcmV0dXJuIDA7Cn0K