PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN
#GD0000223. Cậu bé và món đồ chơi (The Child and Toy)
HỌC THEO LỘ TRÌNH
1 lộ trìnhBài tập thuộc lộ trình
Mục lục đề ⌄
Đang đọc
Cậu bé và món đồ chơi (The Child and Toy)
The Child and Toy
Source: Codeforces
Version: Phuoc Hung OJ Extended
Problem Statement
A toy contains parts connected by ropes. Part has energy value . You must remove all parts one at a time.
Removing part costs the sum of energy values of its neighbors that have not yet been removed. If no neighbors remain, the cost is zero. Each rope joins two distinct parts, and there is at most one rope per pair.
Find the minimum total cost over all possible removal orders.
Input
- The first line contains .
- The second line contains numbers .
- The next lines each contain , a rope between parts and .
Output
Print one integer: the minimum total energy needed.
Subtasks
- Subtask 1 (20 points): ; ; ; ; không có cặp dây trùng.
- Subtask 2 (30 points): ; ; ; ; không có cặp dây trùng.
- Subtask 3 (50 points): ; ; ; ; không có cặp dây trùng.
Examples
Input
4 3
10 20 30 40
1 4
1 2
2 3
Output
40
Explanation
Remove parts in order , paying respectively, totaling . Equivalently, the three ropes must cost at least , , and , giving the lower bound .