classTu { public: structed { int to; int nex; }; voidinsert(int p1, int p2){ ++newp; e[newp].to = p2; e[newp].nex = head[p1]; head[p1] = newp; }
int& operator[] (int &p) { return w[p]; }
voidtarjan(int p){ dfn[p] = low[p] = ++tim; s.push(p); v[p] = 1; for (int i = head[p]; i; i = e[i].nex) { int y = e[i].to; if (!dfn[y]) { tarjan(y); low[p] = min(low[p], low[y]); } elseif (v[y]) { low[p] = min(low[p], dfn[y]); } } if (dfn[p] == low[p]) { v[p] = 0; color[p] = p; while (s.top() != p) { int y = s.top(); s.pop(); color[y] = p; } s.pop(); } }
inttopsort(int *cl){ queue<int> q; for (int i = 1; i <= n; ++i) { if (cl[i] == i && !in[i]) { q.push(i); f[i] = w[i]; } } while (!q.empty()) { int u = q.front(); q.pop(); for (int i = head[u]; i; i = e[i].nex) { int y = e[i].to; f[y] = max(f[y], f[u] + w[y]); if (--in[y] == 0) { q.push(y); } } } int ans = 0; for (int i = 1; i <= n; ++i) { ans = max(f[i], ans); } return ans; }
ed e [MAXN]; int head[MAXN]; int newp, cnt; int w[MAXN]; int color[MAXN]; int dfn[MAXN], low[MAXN], tim; int out[MAXN], f[MAXN], in[MAXN]; bool v[MAXN]; stack<int> s; } a, b;
intmain(void){ scanf("%d%d", &n, &m); for (int i = 1; i <= n; ++i) { scanf("%d", &a[i]); } for (int i = 1; i <= m; ++i) { int x, y; scanf("%d%d", &x, &y); a.insert(x, y); } for (int i = 1; i <= n; ++i) { if (!a.color[i]) a.color[i] = i; if (!a.dfn[i]) { a.tarjan(i); } }
for (int i = 1; i <= n; ++i) { int u = a.color[i]; for (int j = a.head[i]; j; j = a.e[j].nex) { int y = a.color[a.e[j].to]; if (u == y) continue; b.insert(u, y); ++b.in[y]; } } for (int i = 1; i <= n; ++i) { b.w[a.color[i]] += a.w[i]; } printf("%d\n", b.topsort(a.color)); return0; }