rt
# include <bits/stdc++.h>
# define I return
# define AK 0
# define IOI ;
typedef long long ll;
using namespace std;
int n, m, t, tot, x[10005], y[10005], a[10005], low[10005], dfn[10005], col[10005], s[10005], du[10005], dp[10005], maxx;
vector <int> v[10005];
stack <int> st;
queue <int> q;
void tarjan (int x) {
low[x] = dfn[x] = ++ tot, st.push (x);
for (const int& i : v[x])
if (! dfn[i])
tarjan (i), low[x] = min (low[x], low[i]);
else if (! col[i])
low[x] = min (low[x], dfn[i]);
if (low[x] == dfn[x]) {
col[x] = ++ t;
while (st.top () != x)
col[st.top ()] = t, st.pop ();
st.pop ();
return ;
}
return ;
}
int main () {
ios::sync_with_stdio (0);
cin.tie (0);
cout.tie (0);
cin >> n >> m;
for (int i = 1; i <= n; ++ i)
cin >> a[i];
for (int i = 0; i < m; ++ i)
cin >> x[i] >> y[i], v[x[i]].emplace_back (y[i]);
for (int i = 1; i <= n; ++ i)
if (! dfn[i])
tarjan (i);
// for (int i = 1; i <= n; ++ i) cerr << col[i] << ' ';
// return 0;
for (int i = 1; i <= n; ++ i)
v[i].clear (), s[col[i]] += a[i];
// for (int i = 1; i <= t; ++ i) cout << s[i] << ' ';
// return 0;
for (int i = 1; i <= t; ++ i)
if (col[x[i]] != col[y[i]])
++ du[col[y[i]]], v[col[x[i]]].emplace_back (col[y[i]]);
for (int i = 1; i <= n; ++ i)
if (! du[i])
q.push (i);
while (! q.empty ()) {
const int x = q.front ();
q.pop ();
maxx = max (maxx, dp[x] += s[x]);
for (const int& i : v[x])
if (dp[i] = max (dp[i], dp[x]), ! -- du[i])
q.push (i);
}
cout << maxx;
I AK IOI;
}
/*
10 20
970 369 910 889 470 106 658 659 916 964
3 2
3 6
3 4
9 5
8 3
5 8
9 1
9 7
9 8
7 5
3 7
7 8
1 7
10 2
1 10
4 8
2 6
3 1
3 5
8 5
*/