Godzilla
Time limit1sMemory limit128 MB
Each day Godzilla starts at junction 1, walks a path, eats a building and destroys buildings on the way; each night one person leaves every standing building. Maximize the total eaten.
- Level
Hard8 of 10
- Topics
- Graph, Greedy, Implementation, Math
- Solved
- No attempts yet
Problem
The dreaded monster Godzilla has decided to visit Byteburg again. Every day the monster crawls out of the ocean, reaches one of the city's skyscrapers, and eats it together with everyone living inside. Eating one skyscraper takes Godzilla an entire day; afterwards it slips back into the sea. While moving across the city, Godzilla's tail accidentally destroys every skyscraper it passes along the way.
The citizens of Byteburg can hardly stand this. That is why, every night, one citizen flees to the countryside from each skyscraper that is still standing.
Every junction in Byteburg holds exactly one skyscraper, and the junctions are joined by two-way streets. One junction sits right next to the ocean; this is where Godzilla begins its journey each day. Godzilla always travels along streets.
Godzilla must hurry, choosing both the skyscrapers to eat and the streets to travel with great care. It cannot eat a skyscraper that has already been eaten or destroyed along the way. What is the maximum number of people Godzilla can eat before the city becomes completely deserted?
Input
The first line contains two integers and (, ), the number of junctions and the number of streets. The junctions are numbered through , and junction is the one next to the ocean. The second line contains integers (), where is the number of people living in the skyscraper at junction . Each of the next lines contains two integers and (, ), meaning there is a street connecting junctions and . Every junction is reachable from junction .
Output
Print, on a single line, the number of people Godzilla eats when it chooses which skyscrapers to eat and how to move optimally.