第 228 场 AtCoder Regular Contest++
B
Minimize Topological Order
AtCoder
⌀ 1273
原题
← AC →

Minimize Topological OrderAI (CI翻译)

Problem Statement

You are given a permutation $P=(P_1,P_2,\dots,P_N)$ of $(1,2,\dots,N)$ and a length-$N$ sequence of positive integers $A=(A_1,A_2,\dots,A_N)$.

For a rooted tree $T$ with $N$ vertices numbered $1$ through $N$, define $f(T)$ as follows.

Among the permutations $Q=(Q_1,Q_2,\dots,Q_N)$ of $(1,2,\dots,N)$, call one that satisfies the following condition a good permutation.

  • For every vertex $v$ other than the root, letting $u$ be its parent, $u$ appears before $v$ in $Q$.

Let $f(T)$ be the lexicographically smallest good permutation.

Also, define the cost of a rooted tree $T$ with $N$ vertices as $\sum_{i=1}^{N} A_i c_i^2$, where $c_i$ is the number of children of vertex $i$ in $T$.

Find the minimum cost of a rooted tree $T$ with $N$ vertices satisfying $f(T) = P$.

Constraints

  • $1 \le N \le 2 \times 10^5$
  • $P$ is a permutation of $(1,2,\dots,N)$.
  • $1 \le A_i \le 10^6$
  • All input values are integers.

Input

The input is given from Standard Input in the following format:

N
P_1\ P_2\ \dots\ P_N
A_1\ A_2\ \dots\ A_N

Output

Output the answer.

{"titleZh":"最小化拓扑序","bodyZh":"## 题目描述\n\n给定一个 $(1,2,\\dots,N)$ 的排列 $P=(P_1,P_2,\\dots,P_N)$,以及一个长度为 $N$ 的正整数序列 $A=(A_1,A_2,\\dots,A_N)$。\n\n对于一棵包含 $N$ 个顶点、顶点编号为 $1$ 至 $N$ 的有根树 $T$,定义 $f(T)$ 如下。\n\n> 在 $(1,2,\\dots,N)$ 的所有排列 $Q=(Q_1,Q_2,\\dots,Q_N)$ 中,将满足以下条件的排列称为合法排列。\n> \n> > - 对于除根节点外的每个顶点 $v$,设其父节点为 $u$,则 $u$ 在 $Q$ 中出现在 $v$ 之前。\n> \n> 将 $f(T)$ 定义为字典序最小的合法排列。\n\n此外,定义有根树 $T$ 的代价为 $\sum_{i=1}^{N} A_i c_i^2$,其中 $c_i$ 是树 $T$ 中顶点 $i$ 的子节点数量。\n\n请求出满足 $f(T) = P$ 的有根树 $T$ 的最小代价。\n\n## 约束条件\n\n- $1 \\le N \\le 2 \\times 10^5$\n- $P$ 是 $(1,2,\\dots,N)$ 的排列。\n- $1 \\le A_i \\le 10^6$\n- 所有输入值均为整数。\n\n## 输入格式\n\n输入从标准输入中按以下格式给出:\n\n\nN\nP_1\\ P_2\\ \\dots\\ P_N\nA_1\\ A_2\\ \\dots\\ A_N\n\n\n## 输出格式\n\n输出答案。"}AI (CI翻译)

样例

Input #1
3
1 2 3
1 2 1
Output #1
3
The trees satisfying the condition for $T$ are the following two trees, $T_1$ and $T_2$, both rooted at vertex $1$. The cost of $T_1$ is $1 \times 2^2 + 2 \times 0^2 + 1 \times 0^2 = 4$, and the cost of $T_2$ is $1 \times 1^2 + 2 \times 1^2 + 1 \times 0^2 = 3$, so the minimum cost is $3$. ![image](https://img.atcoder.jp/arc228/b8a50ee288e4eea5b2c122d0b237dea1.png)
Input #2
5
3 1 5 2 4
4 1 1 2 1
Output #2
6
Input #3
10
8 4 7 9 1 3 2 10 6 5
816977 838423 186050 232610 582528 327642 432740 420943 194307 966417
Output #3
3901780
题目信息
题号
arc228_b
时间限制
2 s
内存限制
1024 MB

题面与样例版权归 AtCoder 及出题人所有;中文翻译由 AI 生成(gpt-6.1-sol),如有出入请以原文为准。