usingnamespacestd; usingnamespace FAST_IO; const ll mod = 1e9 + 7; constint INF = 0x3f3f3f3f; const ll INF_LL = 0x3f3f3f3f3f3f3f3f; constdouble eps = 1e-5; constint maxn = 1e4 + 10; constint maxm = 1e5 + 10; int t, n, m, k; int value1[maxn]; vector<int> g1[maxn], g2[maxn];
int dfn[maxn], low[maxn], dfn_clock = 0; int st[maxn], top = -1; bool vis[maxn]; int scc_id[maxn], value2[maxn], scc_cnt = 0;
int indeg[maxn], dp[maxn]; voidtarjan(int u) { dfn[u] = low[u] = ++dfn_clock; vis[u] = true; st[++top] = u; for (int i = 0; i < g1[u].size(); ++i) { int v = g1[u][i]; if (!dfn[v]) { tarjan(v); low[u] = min(low[v], low[u]); } else { if (vis[v]) low[u] = min(low[u], dfn[v]); } } if (low[u] == dfn[u]) { ++scc_cnt; while (true) { scc_id[st[top]] = scc_cnt; vis[st[top]] = false; value2[scc_cnt] += value1[st[top]]; if (st[top--] == u) break; } } }
voidtopo() { queue<int> q; for (int i = 1; i <= scc_cnt; ++i) { if (!indeg[i]) { q.push(i); dp[i] = value2[i]; } } while (q.size()) { auto u = q.front(); q.pop(); for (int i = 0; i < g2[u].size(); ++i) { int v = g2[u][i]; dp[v] = max(dp[v], dp[u] + value2[v]); --indeg[v]; if (!indeg[v]) q.push(v); } } } intmain() { // #define COMP_DATA #ifndef ONLINE_JUDGE freopen("in.txt", "r", stdin); #endif ios::sync_with_stdio(false); cin.tie(0); read(n, m); for (int i = 1; i <= n; ++i) read(value1[i]); for (int i = 1, u, v; i <= m; ++i) { read(u, v); g1[u].emplace_back(v); } for (int i = 1; i <= n; ++i) { if (!dfn[i]) tarjan(i); } // 建立新图 for (int u = 1; u <= n; ++u) { int uu = scc_id[u]; for (int j = 0; j < g1[u].size(); ++j) { int v = g1[u][j]; int vv = scc_id[v]; if (uu == vv) continue; g2[uu].emplace_back(vv); indeg[vv]++; } } // topo 排序 topo(); int ans = 0; for (int i = 1; i <= scc_cnt; ++i) ans = max(ans, dp[i]); cout << ans << endl; return0; }