PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN
#LCA0000006. Chi phí lớn nhất trên cây (Tree with Maximum Cost)
HỌC THEO LỘ TRÌNH
1 lộ trìnhBài tập thuộc lộ trình
Mục lục đề ⌄
Đang đọc
Chi phí lớn nhất trên cây (Tree with Maximum Cost)
Tree with Maximum Cost
Source: Codeforces
Version: Phuoc Hung OJ Extended
Problem Statement
An undirected tree has vertices. Vertex carries a positive weight , and each edge has unit length.
If a station is placed at vertex , its cost is the sum of over all vertices , where is the number of edges between and .
Choose the station vertex to maximize this cost.
Input
- The first line contains .
- The second line has vertex weights .
- Each of the next lines contains an undirected edge .
Output
Print the maximum possible cost.
Subtasks
- Subtask 1 (20 points): ; ; .
- Subtask 2 (30 points): ; ; .
- Subtask 3 (50 points): ; .
Examples
Input
8
9 4 1 7 10 1 6 5
1 2
2 3
1 4
1 5
5 6
5 7
5 8
Output
121
Explanation
Considering all possible station vertices and summing weighted distances yields a maximum cost of .