QOJ.ac

QOJ

Limite de temps : 3 s Limite de mémoire : 1024 MB Points totaux : 100 Difficulté: [afficher]

#5092. Forest Game

Statistiques

Little A and Little B are playing a game.

They are presented with a forest of rooted trees, where each node $u$ has a positive integer weight $A_u$.

Little A and Little B take turns, with Little A going first. The current player must choose exactly one tree root to remove, gaining its weight. The subtrees of the removed node become new rooted trees, and its children become the new roots.

The game ends when all nodes have been removed. A player's score is the sum of the weights of the nodes they removed.

Both players aim to maximize their own scores and play optimally. Calculate the final score of Little A.

The initial state consists of exactly one tree with $n$ nodes, labeled from $1$ to $n$, with node $1$ as the root.

Input

The first line contains a positive integer $n$, representing the number of nodes.

The second line contains $n$ positive integers $A_1, A_2, \dots, A_n$, representing the weight of each node.

The next $n-1$ lines describe the initial state, each containing two positive integers $u, v$ representing an edge between $u$ and $v$ in the tree. It is guaranteed that the input forms a tree.

Output

Output a single integer representing the final score of Little A.

Examples

Input 1

5
1 5 3 2 4
1 2
1 3
2 4
2 5

Output 1

7

Input 2

See provided files.

Constraints

For all data, $1 \leq A_i \leq 10^9$.

Subtask ID $n \le $ Special Property Score
$1$ $20$ None $20$
$2$ $1\,000$ $20$
$3$ $2 \times 10^5$ A $20$
$4$ B $20$
$5$ None $20$
  • Special Property A: Let $p_i$ be the parent of $i$. Then for all $2 \leq i \leq n$, $A_i \leq A_{p_i}$.
  • Special Property B: All $A_i$ are chosen uniformly at random from the integers in $[1, 10^9]$.

Editorials

IDTypeStatusTitlePosted ByLast UpdatedActions
EditorialOpen Official EditorialQingyu- Download

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.