AT_arc228_b.Minimize Topological Order
普及+/提高
通过率:0%
时间限制:2.00s
内存限制:1024MB
AC君温馨提醒
该题目为【atcoder】题库的题目,您提交的代码将被提交至atcoder进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
You are given a permutation P=(P1,P2,…,PN) of (1,2,…,N) and a length-N sequence of positive integers A=(A1,A2,…,AN).
For a rooted tree T with N vertices numbered 1 through N, define f(T) as follows.
Among the permutations Q=(Q1,Q2,…,QN) of (1,2,…,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 ∑i=1NAici2, where ci 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.
给你一个 (1,2,…,N) 的排列 P=(P1,P2,…,PN) 和一个长度为 N 的正整数序列 A=(A1,A2,…,AN)。
对于一棵以 1 至 N 编号的 N 个顶点构成的有根树 T,定义 f(T) 如下:
在所有 (1,2,…,N) 的排列 Q=(Q1,Q2,…,QN) 中,称满足如下条件的排列为好排列:
- 对于除根以外的每个顶点 v,设其父节点为 u,则 u 在 Q 中出现在 v 之前。
定义 f(T) 为字典序最小的好排列。
此外,定义一棵含 N 个顶点的有根树 T 的代价为 ∑i=1NAici2,其中 ci 表示顶点 i 在 T 中的子节点数目。
求满足 f(T)=P 的、含 N 个顶点的有根树 T 的最小代价。
输入格式
The input is given from Standard Input in the following format:
N
P1 P2 … PN
A1 A2 … AN
输入从标准输入中按以下格式给出:
N
P1 P2 … PN
A1 A2 … AN
输出格式
Output the answer.
输出答案。
输入输出样例
输入#1
3 1 2 3 1 2 1
输出#1
3
输入#2
5 3 1 5 2 4 4 1 1 2 1
输出#2
6
输入#3
10 8 4 7 9 1 3 2 10 6 5 816977 838423 186050 232610 582528 327642 432740 420943 194307 966417
输出#3
3901780
说明/提示
Sample 1 Explanation:
The trees satisfying the condition for T are the following two trees, T1 and T2, both rooted at vertex 1.
The cost of T1 is 1×22+2×02+1×02=4, and the cost of T2 is 1×12+2×12+1×02=3, so the minimum cost is 3.

Constraints
- 1≤N≤2×105
- P is a permutation of (1,2,…,N).
- 1≤Ai≤106
- All input values are integers.
样例 1 解释:
满足条件的树 T 有以下两棵,即以顶点 1 为根的树 T1 和 T2。
T1 的代价为 1×22+2×02+1×02=4,
T2 的代价为 1×12+2×12+1×02=3,
因此最小代价为 3。

约束条件
- 1≤N≤2×105
- P 是 (1,2,…,N) 的一个排列。
- 1≤Ai≤106
- 所有输入值均为整数。
输入解题思路,AI测评打分。不知道怎么写?