AT_arc228_b.Minimize Topological Order

普及+/提高

通过率:0%

时间限制:2.00s

内存限制:1024MB

AC君温馨提醒

该题目为【atcoder】题库的题目,您提交的代码将被提交至atcoder进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

You are given a permutation P=(P1,P2,…,PN)P=(P_1,P_2,\dots,P_N) of (1,2,…,N)(1,2,\dots,N) and a length-NN sequence of positive integers A=(A1,A2,…,AN)A=(A_1,A_2,\dots,A_N).

For a rooted tree TT with NN vertices numbered 11 through NN, define f(T)f(T) as follows.

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

  • For every vertex vv other than the root, letting uu be its parent, uu appears before vv in QQ.

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

Also, define the cost of a rooted tree TT with NN vertices as ∑i=1NAici2\sum_{i=1}^{N} A_i c_i^2, where cic_i is the number of children of vertex ii in TT.

Find the minimum cost of a rooted tree TT with NN vertices satisfying f(T)=Pf(T) = P.

给你一个 (1,2,…,N)(1,2,\dots,N) 的排列 P=(P1,P2,…,PN)P=(P_1,P_2,\dots,P_N) 和一个长度为 NN 的正整数序列 A=(A1,A2,…,AN)A=(A_1,A_2,\dots,A_N)。

对于一棵以 11 至 NN 编号的 NN 个顶点构成的有根树 TT,定义 f(T)f(T) 如下:

在所有 (1,2,…,N)(1,2,\dots,N) 的排列 Q=(Q1,Q2,…,QN)Q=(Q_1,Q_2,\dots,Q_N) 中,称满足如下条件的排列为好排列:

  • 对于除根以外的每个顶点 vv,设其父节点为 uu,则 uu 在 QQ 中出现在 vv 之前。

定义 f(T)f(T) 为字典序最小的好排列。

此外,定义一棵含 NN 个顶点的有根树 TT 的代价为 ∑i=1NAici2\sum_{i=1}^{N} A_i c_i^2,其中 cic_i 表示顶点 ii 在 TT 中的子节点数目。

求满足 f(T)=Pf(T) = P 的、含 NN 个顶点的有根树 TT 的最小代价。

输入格式

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

NN
P1 P2 … PNP_1\ P_2\ \dots\ P_N
A1 A2 … ANA_1\ A_2\ \dots\ A_N

输入从标准输入中按以下格式给出:

NN
P1 P2 … PNP_1\ P_2\ \dots\ P_N
A1 A2 … ANA_1\ A_2\ \dots\ A_N

输出格式

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 TT are the following two trees, T1T_1 and T2T_2, both rooted at vertex 11.

The cost of T1T_1 is 1×22+2×02+1×02=41 \times 2^2 + 2 \times 0^2 + 1 \times 0^2 = 4, and the cost of T2T_2 is 1×12+2×12+1×02=31 \times 1^2 + 2 \times 1^2 + 1 \times 0^2 = 3, so the minimum cost is 33.

Constraints

  • 1≤N≤2×1051 \le N \le 2 \times 10^5
  • PP is a permutation of (1,2,…,N)(1,2,\dots,N).
  • 1≤Ai≤1061 \le A_i \le 10^6
  • All input values are integers.

样例 1 解释:
满足条件的树 TT 有以下两棵,即以顶点 11 为根的树 T1T_1 和 T2T_2。

T1T_1 的代价为 1×22+2×02+1×02=41 \times 2^2 + 2 \times 0^2 + 1 \times 0^2 = 4,
T2T_2 的代价为 1×12+2×12+1×02=31 \times 1^2 + 2 \times 1^2 + 1 \times 0^2 = 3,
因此最小代价为 33。

约束条件

  • 1≤N≤2×1051 \le N \le 2 \times 10^5
  • PP 是 (1,2,…,N)(1,2,\dots,N) 的一个排列。
  • 1≤Ai≤1061 \le A_i \le 10^6
  • 所有输入值均为整数。

输入解题思路,AI测评打分。不知道怎么写?

首页