Split the GSHS 3
InterviewTime limit1sMemory limit1024 MB
Given a tree of buildings with student counts, cut two edges to split it into three parts and maximize the product of their student totals.
- Level
Medium6 of 10
- Topics
- Tree, Dynamic programming, DFS
- Solved
- No attempts yet
Problem
Gyeonggi Science High School, a well-known landmark in Gyeonggi-do, has buildings numbered 1 through . The buildings are connected by two-way roads, and every building can reach every other building, so the campus is a tree. The villain Donghyun plans to split Gyeonggi Science High School into several pieces and then rule it.\n\nAfter his first plan failed, Donghyun decided to use a second plan. In this plan, he destroys 2 of the roads to divide the school into 3 regions. Two buildings belong to the same region if one can reach the other using only roads that were not destroyed.\n\nThrough a spy he hid at the school, Donghyun knows that building has students. After the school is divided into three regions, the cohesion of each region is the number of students in that region. The dominance is the product of the cohesions of the three regions.\n\nFor example, take and .\n\n
\n\nDonghyun destroys the road between buildings 1 and 5 and the road between buildings 5 and 7. The school is then divided into three regions as shown below.\n\n
\n\nThe cohesion of the region containing building 1 is . The cohesions of the regions containing building 2 and building 6 are 6 and 8. So the dominance in this case is .\n\nWrite a program that finds the maximum dominance Donghyun can get by dividing Gyeonggi Science High School into three regions.
Input
The first line contains the number of buildings .\n\nThe second line contains the number of students in each building, , , , , separated by spaces.\n\nFrom line 3 to line , line contains two integers and separated by a space. This means the -th road connects building and building in both directions.
Output
Print one line containing the maximum dominance Donghyun can get by dividing Gyeonggi Science High School into three regions.
Constraints
- \n- \n- \n- The structure of Gyeonggi Science High School is a tree.