F.A.Q
Hand In Hand
Online Acmers
Problem Archive
Realtime Judge Status
Authors Ranklist
 
     C/C++/Java Exams     
ACM Steps
Go to Job
Contest LiveCast
ICPC@China
Best Coder beta
VIP | STD Contests
    DIY | Web-DIY beta
Author ID 
Password 
 Register new ID

Level Up

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 710    Accepted Submission(s): 200


Problem Description
The Arithmetic Coconut Median company has n employees. Every person has a direct supervisor except the boss. Boss's number is 1. Everyone has a Ability Value $A_{i}$. The wage of person i is the Median of Ability Value of person i and his (direct and indirect) subordinates. The ACM company decided to send a person to study in the center of the universe, then the Ability Value of this person will become 100000.

Please calculate the maximum sum of wages of all people.

Define the Median of t integers: the $\left \lceil \frac{t}{2} \right \rceil $ th smallest number of these t integers.
 

Input
The input contains multiple test cases. There are about 30 test cases.
Each test case begin with an integer n.
next line contains n integers $A_{1},A_{2}\cdots A_{n}$.
next line contains n-1 integers $P_{2},P_{3}\cdots P_{n}. P_{i}$ indicates the direct supervisor of i.

$1 \leq n \leq 100000$
$1 \leq A_{i} \leq 100000$
$1 \leq P_{i} < i$
 

Output
For each test case output a line contains an integer.
 

Sample Input
1 1 2 2 1 1 2 1 2 1
 

Sample Output
100000 100002 100001
 

Author
ZSTU
 

Source
 

Statistic | Submit | Discuss | Note
Hangzhou Dianzi University Online Judge 3.0
Copyright © 2005-2024 HDU ACM Team. All Rights Reserved.
Designer & Developer : Wang Rongtao LinLe GaoJie GanLu
Total 0.000000(s) query 1, Server time : 2024-06-13 14:15:50, Gzip enabled