#include <bits/stdc++.h>
#include <bits/extc++.h>
int n, m, st[10001], low[10001], dfn[10001], stp, tp, scc[10001], siz[10001], tot, in[10001], val[10001], dp[10001];
std::vector<int> g[10001], topo, edge[10001];
std::bitset<10001> vis;
void tarjan(int u) {
low[u] = dfn[u] = ++stp, vis[u] = true, st[++tp] = u;
for (auto v : g[u]) {
if (!dfn[v])tarjan(v), low[u] = std::min(low[u], low[v]);
else if (vis[v])low[u] = std::min(low[u], dfn[v]);
}
if (dfn[u] == low[u]) {
int cur = -1; tot++;
while (cur != u)cur = st[tp--], scc[cur] = tot, siz[tot] += val[cur], vis[cur] = false;
}
}
void toposort() {
std::queue<int> q;
for (int i = 1; i <= tot; i++)if (!in[i])q.push(i);
while (!q.empty()) {
int cur = q.front(); q.pop();
topo.push_back(cur);
for (auto v : edge[cur])if (!(--in[v]))q.push(v);
}
}
void rebuild() {
for (int i = 1; i <= n; i++)
for (auto v : g[i])if (scc[i] != scc[v])edge[scc[i]].push_back(scc[v]), in[scc[v]]++;
}
signed main() {
std::ios::sync_with_stdio(false), std::cin.tie(nullptr), std::cout.tie(nullptr);
std::cin >> n >> m;
for (int i = 1; i <= n; i++)std::cin >> val[i];
for (int i = 1, u, v; i <= m; i++)std::cin >> u >> v, g[u].push_back(v);
for (int i = 1; i <= n; i++)if (!dfn[i])tarjan(i);
rebuild(), toposort();
for (auto i : topo) {
dp[i] = siz[i];
for (auto j : edge[i])dp[i] = std::max(dp[i], dp[j] + siz[i]);
}
return std::cout << *std::max_element(dp + 1, dp + tot + 1), 0;
}
闲话:40pts 代码把有向边加成无向边能拿 60pts