#include using namespace std; using ll = long long; vector vst; vector I, loop; void dfs1(vector> &E, int from){ vst[from] = 1; for(auto to : E[from]){ if (vst[to]) continue; dfs1(E, to); } I.push_back(from); } void dfs2(vector> &E, int from){ vst[from] = 1; loop.push_back(from); for(auto to : E[from]){ if (vst[to]) continue; dfs2(E, to); } } vector> scc(vector> &E){ int N = E.size(); vector> res, e(N); vst.resize(N); for (int i=0; i sub; reverse(loop.begin(), loop.end()); for (auto y : loop) sub.push_back(y); res.push_back(sub); } } return res; } template map compress(vector &A){ map comp; int N = A.size(), i=0; for (int i=0; i> N; vector X(N), A(N), v; for (int i=0; i> X[i]; for (int i=0; i> A[i]; for (int i=0; i mp = compress(v), rev; for (auto [x, y] : mp) rev[y] = x; M = mp.size(); vector> E(M), S; for (int i=0; i dp(K, -2e9), root(M); for (int i=0; i