The Rural Postman
Time limit1sMemory limit128 MB
Start at village 1 and cover every road and village to maximize order-based village payments minus one euro per move.
- Level
Medium5 of 10
- Topics
- Math, Graph, Implementation
- Solved
- No attempts yet
Problem
A rural postman must deliver mail to everyone in the region: the people living in the villages and those living along the roads that connect the villages.
Help him choose a route that drives along every road and visits every village at least once. In every case considered here, such a route is guaranteed to exist. Routes can differ in value, because the post office is paid differently depending on which route is taken, and it is the post office's profit (not the postman's) that matters.
Each village wants the postman to arrive as early as possible, so every village signs the following contract with the post office. Suppose village is the -th distinct village the postman reaches, meaning he had already visited different villages before reaching village for the first time. If , the village pays the post office euros. If , the post office pays the village euros. On top of that, the post office pays the postman one euro for every drive between two consecutive villages on the route.
There are villages, numbered to . The post office is in village , so the route must start in village . Exactly , , or roads meet at each village. Two villages may be connected by several different roads, and a road may also return to the village it starts from.
Compute the maximum total profit, in euros, that the post office can achieve over all valid routes. If every route makes the post office lose money, report the smallest possible loss as a negative number.
Input
The first line contains two integers and separated by a single space: the number of villages () and the number of roads .
Each of the next lines contains one positive integer. The value on line is (), the base amount village would pay the post office (adjusted by the contract described above).
Each of the next lines contains two integers separated by a single space: the numbers of the two villages joined by that road.
Output
Print one integer: the maximum total profit, in euros, that the post office can obtain. The value may be negative.
Hint
