Nations
Time limit1sMemory limit1024 MB
Partition a tree of cities into connected groups, each with total power at most U, minimizing the sum of squared gaps (U minus group power).
- Level
Medium6 of 10
- Topics
- Dynamic programming, Tree
- Solved
- No attempts yet
Combat Power Split
You are playing shake!, a war simulation game popular around the world. The game has cities numbered from 1 to , and each city has a combat power. There are also roads that connect two different cities in both directions, and you can travel between any two cities along the roads.
You plan to found several nations and run a war simulation. Each nation must contain at least one city, and each city must belong to exactly one nation. Any two cities in the same nation must be connected by roads without passing through a city of another nation.
Splitting combat power evenly matters. A nation that is too powerful wins wars too easily, and the game loses its appeal. The combat power of a nation is the sum of the combat powers of all cities in it. You set a combat power cap . No nation may exceed , and if () nations have combat powers , the balanced split is the one that minimizes . Split the combat power of the nations this way.
Input
The first line contains the number of cities . ()
The next lines each contain the combat power of city . ()
The next lines describe the roads. Each line contains the two city numbers that the road connects, separated by a space.
The last line contains the combat power cap . ()
Output
Print the minimum value of .