Conquest
Time limit4sMemory limit1024 MB
Starting from island 1, absorb adjacent islands with strictly smaller armies, merging their sizes; maximize the final total army.
- Level
Medium7 of 10
- Topics
- Graph, Greedy, Heap, Union-find
- Solved
- No attempts yet
Problem
Nomads, Kingdoms, and Tribes live on the islands of the great seas. Bridges span between islands, allowing travel between them. It is possible to get from every island to every other island through some sequence of bridges. The islands were at peace until everything changed when the Spanning Nation attacked.
Initially the Spanning Nation occupies island 1. From that point on, the Spanning Nation can attack any island that is directly connected to some island already conquered by the Spanning Nation. Wars are resolved without any fighting. The Spanning Nation only attacks an island if that island's army is strictly smaller than the Spanning Nation's army. The smaller island army simply concedes and joins the Spanning Nation's army.
As the tactical advisor of the Spanning Nation, determine the maximum possible army size the Spanning Nation can have after making a series of attacks.
Input
The first line contains the integer (), the number of islands, and (), the number of bridges.
The next lines describe the bridges. Each of these lines contains two distinct integers and (), indicating that there is a bridge between islands and . There is at most one bridge between any pair of islands.
The next lines describe the islands' army sizes in order. Each of these lines contains a single integer (), which is the army size of this island.
Output
Display the maximum possible army size of the Spanning Nation.