QOJ.ac

QOJ

حد الوقت: 1 s حد الذاكرة: 256 MB مجموع النقاط: 100

#12590. Interstellar Travel

الإحصائيات

In the year 3000, the Earth Alliance has conquered $N$ planets in the galaxy. Due to budget considerations, the government has established only $N-1$ bidirectional space-time tunnels between the planets, ensuring that any two planets are mutually reachable. For administrative reasons, the governor of the $i$-th planet requires that no citizen may use space-time tunnels to leave that planet more than $H_i$ times in a year (this statistic is based on the number of departures; if you have already used a tunnel to leave the planet $H_i$ times, you cannot use a space-time tunnel to leave that planet again for the rest of the year). Louis Paosen is an interstellar traveler who wishes to use as many space-time tunnels as possible, but he does not want the planet where he eventually settles to have overly harsh conditions. Therefore, he wants to know, for each planet $i$, the maximum number of space-time tunnels he can use during an interstellar journey that starts at planet $0$ and ends at planet $i$.

Input

The first line contains an integer $N$. The next line contains $N$ integers $H_i$, representing the departure limit for each planet. The following $N-1$ lines each contain two integers, representing a space-time tunnel connection between two planets. The planets are numbered starting from $0$, and Louis Paosen starts at planet $0$.

Output

The output contains $N$ lines, each containing an integer representing the maximum number of space-time tunnels that can be used if the journey ends at that planet.

Constraints

  • $40\%$ of the data: $N \le 500$
  • $100\%$ of the data: $N \le 50000$
  • The departure limits for all planets satisfy $1 \le H_i \le 40000$, and for each planet, $H_i$ is greater than or equal to the number of planets directly connected to it (i.e., its degree).

Examples

Input 1

3
2 6 2
0 1
1 2

Output 1

8
7
8

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.