QOJ.ac

QOJ

Time Limit: 1 s Memory Limit: 512 MB Total points: 100

#8754. DFS 序

統計

题目描述

给定一棵 $n$ 个点的有根树,$1$ 号点为根。每个点有一个权值 $w_i$。

求一个最优的 DFS 序使得 $\sum\limits_{i=1}^n p_iw_i$ 最大。其中 $p_i$ 表示访问第 $i$ 个点的时刻,即第一次访问节点 $i$ 之前访问过多少个不同的节点(包含节点 $i$ 本身)。

输入格式

从标准输入读入数据。

第一行一个正整数 $n(1\le n \le 2\times 10^5)$。

第二行 $n$ 个正整数,其中第 $i$ 个表示 $w_i(1\le w_i \le 10^8)$。

第三行 $n-1$ 个正整数,其中第 $i$ 个表示 $i+1$ 号节点的父亲,保证取值在 $1\sim i$ 之间。

输出格式

输出到标准输出。

一行一个整数,表示最大的 $\sum\limits_{i=1}^n p_iw_i$。

样例

输入

5
8 5 3 6 4
1 1 3 3

输出

75

解释

按照 $(1,3,5,4,2)$ 的访问顺序可以取得最大值 $1\times 8+2\times 3+3\times 4+4\times 6+5\times 5=75$。

注意 $(1,3,2,4,5)$ 不是一个合法的访问顺序。

About Issues

We understand that our problem archive is not perfect. If you find any issues with the problem, including the statement, scoring configuration, time/memory limits, test cases, etc.

You may use this form to submit an issue regarding the problem. A problem moderator will review your issue and proceed it properly.

STOP! Before you submit an issue, please READ the following guidelines:

  1. This is not a place to publish a discussion, editorial, or requests to debug your code. Your issue will only be visible by you and problem moderators. Other users will not be able to view or reply your issues.
  2. Do not submit duplicated issues. If you have already submitted one, please wait for an moderator to review it. Submitting multiple issues will not speed up the review process and might cause your account to be banned.
  3. Issues must be filed in English or Chinese only.
  4. Be sure your issue is related to this problem. If you need to submit an issue regarding another problem, contest, category, etc., you should submit it to the corresponding page.

Active Issues 0

No issues in this category.

Closed/Resolved Issues 0

No issues in this category.