#P9133. [THUPC 2023 初赛] 大富翁

[THUPC 2023 初赛] 大富翁

Background

One day, Xiao W and Xiao H are playing Monopoly.

Problem Description

The rules of this version of Monopoly are quite special. Its map is a rooted tree with nn nodes, where node 11 is the root. Each node on the tree has a price, and the price of node xx is denoted as wxw_x.

For two different nodes x,yx, y on the tree, if xx is an ancestor of yy (that is, xx lies on the simple path from node 11 to node yy), then we say that xx dominates yy.

During the game, Xiao W and Xiao H take turns to buy an unbought node on the tree, until all nn nodes have been bought by either Xiao W or Xiao H. (Before the game starts, all nodes on the tree are unbought.)

For a purchase, suppose the buyer purchases node xx. First, they must pay wxw_x game coins to the system. At this time, suppose xx dominates n1n_1 nodes that have already been bought by the buyer's opponent, and meanwhile xx is dominated by n2n_2 nodes that have already been bought by the opponent. If n1>n2n_1 > n_2, then the opponent must pay n1n2n_1 - n_2 game coins to the buyer; if n1<n2n_1 < n_2, then the buyer must pay n2n1n_2 - n_1 game coins to the opponent.

Both Xiao W and Xiao H are extremely smart. They will both use optimal strategies in the game to earn as many game coins as possible. Now Xiao W wants to test you: if he moves first, how many game coins can he finally earn? (That is, during the whole game, the number of game coins Xiao W receives from Xiao H minus the number of game coins he pays to the system and to Xiao H. You may assume that before the game starts, both Xiao W and Xiao H have enough game coins. Note that the answer may be negative.)

Input Format

The first line contains a positive integer nn.

The second line contains nn non-negative integers, where the ii-th integer is the price wiw_i of node ii.

The third line contains n1n - 1 positive integers, where the ii-th positive integer denotes the parent index of node i+1i + 1.

Output Format

One line contains one integer, which is the answer.

7
0 0 1 0 0 0 0
1 1 2 2 3 3

2
见附件中的 2.in
见附件中的 2.ans

Hint

Sample Explanation 1

One possible game process is:

  • First purchase: Xiao W buys node 11 and pays 00 game coins to the system.
  • Second purchase: Xiao H buys node 22, pays 00 game coins to the system, and pays 11 game coin to Xiao W.
  • Third purchase: Xiao W buys node 33 and pays 11 game coin to the system.
  • Fourth purchase: Xiao H buys node 44, pays 00 game coins to the system, and pays 11 game coin to Xiao W.
  • Fifth purchase: Xiao W buys node 66 and pays 00 game coins to the system.
  • Sixth purchase: Xiao H buys node 55, pays 00 game coins to the system, and pays 11 game coin to Xiao W.
  • Seventh purchase: Xiao W buys node 77 and pays 00 game coins to the system.

Subtasks

For all testdata, 1n2×1051 \leq n \leq 2 \times 10^5, 0wx2×1050 \leq w_x \leq 2 \times 10^5. It is guaranteed that the input graph is a rooted tree with node 11 as the root.

Source

From the preliminary round of the 2023 Tsinghua University Student Programming Contest and Collegiate Invitational Contest (THUPC2023).

Resources such as editorials can be found at https://github.com/THUSAAC/THUPC2023-Pre.

Translated by ChatGPT 5