C++struct LCA {
int n, tot, dfn[N], id[N], val[N], s[N], fa[N], st[20][N];
Graph* E;
void init(Graph& x) {
E = &x, n = E->n, dfs(1);
for (int i = 1; i <= __lg(n); i ++)
for (int j = 1; j + (1 << i) - 1 <= n; j ++)
st[i][j] = min(st[i - 1][j], st[i - 1][j + (1 << (i - 1))]);
}
void dfs(int u) {
dfn[u] = ++ tot, id[tot] = u, s[u] = s[fa[u]] + val[u], st[0][tot] = dfn[fa[u]];
FEG(u, v, w, (*E), i)
if (v != fa[u]) fa[v] = u, dfs(v);
}
int qry(int u, int v, int flg = 0) {
int lca = ask(u, v);
return (flg ? (s[u] + s[v] - s[lca] - s[fa[lca]]) : (s[u] + s[v] - s[lca] * 2));
}
int ask(int u, int v) {
if (u == v) return u;
if ((u = dfn[u]) > (v = dfn[v])) swap(u, v);
int d = __lg(v - u ++);
return id[min(st[d][u], st[d][v - (1 << d) + 1])];
}
};
C++vector<LL> Dijkstra(const Graph& G, int s, int n = 0) {
vector<LL> dis(max({G.n, n, s}) + 1, 1e18), vis(max({G.n, n, s}) + 1);
priority_queue<pii, vector<pii>, greater<pii>> q;
q.push(pii(0, s)), dis[s] = 0;
while (!q.empty()) {
int u = q.top().se; q.pop();
if (vis[u]) continue; vis[u] = 1;
FEG(u, v, w, G, i)
if (dis[v] > dis[u] + w) dis[v] = dis[u] + w, q.push(pii(dis[v], v));
}
return dis;
}
C++void BlkTree(const Graph& G, Graph& E, int n) {
int cnt = n, tot = 0;
vector<int> dfn(n + 1), low(n + 1), cut(n + 1);
stack<int> stk;
auto Tarjan = [&](auto self, int u) -> void {
dfn[u] = low[u] = ++ tot, stk.push(u);
FEG(u, v, w, G, i) {
if (!dfn[v]) {
self(self, v), low[u] = min(low[u], low[v]);
if (low[v] >= dfn[u]) {
int last = 0;
E.twi(u, ++ cnt), cut[u] = 1;
while (!stk.empty() && last != v)
E.twi(cnt, last = stk.top()), stk.pop();
}
}
low[u] = min(low[u], dfn[v]);
}
};
Tarjan(Tarjan, 1);
}
C++struct maxflow {
int ct, n, rd[N], d[N], hd[N]; maxflow() { ct = 1; }
struct edge { int v, t; LL c; } e[N << 1];
void ins(int u, int v, LL c) { e[++ ct] = {v, hd[u], c}, hd[u] = ct; }
void add(int u, int v, LL c) { ins(u, v, c), ins(v, u, 0), n = max({n, u, v}); }
bool bfs(int s, int t) {
queue<int> q; memset(d, 0, n * 4 +4), d[s] = 1, q.push(s);
while (q.size()) {
int u = q.front(); q.pop(), rd[u] = hd[u];
for (int i = hd[u]; i; i = e[i].t) {
int v = e[i].v;
if (!d[v] && e[i].c) d[v] = d[u] + 1, q.push(v);
}
}
return d[t];
}
LL dfs(int u, LL r, int t) {
if (u == t) return r; LL x = r;
for (int i = rd[u]; i && x; i = e[i].t) {
int v = e[i].v; rd[u] = i;
if (d[v] == d[u] + 1 && e[i].c) {
LL s = dfs(v, min(x, e[i].c), t);
x -= s, e[i].c -= s, e[i ^ 1].c += s;
}
}
return r - x;
}
LL solve(int s, int t) { LL ans = 0; while (bfs(s, t)) ans += dfs(s, 1e18, t); return ans; }
void reset() { REP(i, 1, ct) if (i & 1) e[i ^ 1].c += e[i].c, e[i].c = 0; }
void clear() { ct = 1; REP(i, 1, n) rd[i] = hd[i] = d[i] = 0; }
auto residual() {
vector<tuple<int, int, int>> E;
REP(i, 1, n) for (int j = hd[i]; j; j = e[j].t)
if (!(j & 1)) E.pb(i, e[j].v, e[j].c);
return E;
}
};
C++struct mcmf {
int n, ct, vs[N], hd[N]; LL d[N]; mcmf() { ct = 1; }
struct edge { int v, t; LL c, w; } e[N << 1];
void ins(int u, int v, LL c, LL w) { e[++ ct] = {v, hd[u], c, w}, hd[u] = ct; }
void add(int u, int v, LL c, LL w) { ins(u, v, c, w), ins(v, u, 0, -w), n = max({n, u, v}); }
bool spfa(int s, int t) {
memset(d, 0x3f, n * 8 + 8), memset(vs, 0, n * 4 + 4);
queue<int> q; d[s] = 0, vs[s] = 1, q.push(s);
while (q.size()) {
int u = q.front(); q.pop(), vs[u] = 0;
for (int i = hd[u]; i; i = e[i].t) {
int v = e[i].v;
if (d[v] > d[u] + e[i].w && e[i].c)
d[v] = d[u] + e[i].w, (!vs[v] ? q.push(v), vs[v] = 1 : 0);
}
}
return d[t] < 1e18;
}
LL dfs(int u, LL r, int t, LL& co) {
if (u == t) return r; LL x = r; vs[u] = 1;
for (int i = hd[u]; i && x; i = e[i].t) {
int v = e[i].v;
if (!vs[v] && e[i].c && d[v] == d[u] + e[i].w) {
LL s = dfs(v, min(x, e[i].c), t, co);
x -= s, e[i].c -= s, e[i ^ 1].c += s, co += e[i].w * s;
}
}
return r - x;
}
pii solve(int s, int t) {
LL rs = 0, co = 0, x = 0;
while (spfa(s, t)) {
while (x = dfs(s, 1e9, t, co))
memset(vs, 0, n * 5), rs += x;
}
return pii(rs, co);
}
void reset() { REP(i, 1, ct) if (i & 1) e[i ^ 1].c += e[i].c, e[i].c = 0; }
void clear() { ct = 1; REP(i, 1, n) hd[i] = d[i] = 0; }
auto residual() {
vector<tuple<int, int, int, int>> E;
REP(i, 1, n) for (int j = hd[i]; j; j = e[j].t)
if (!(j & 1)) E.pb(i, e[j].v, e[j].c, e[j].w);
return E;
}
};