Under construction

Still being built. Things may change

Split the GSHS 3

Interview

Time limit1sMemory limit1024 MB

Summary
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 NN. The buildings are connected by N−1N-1 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 ii has AiA_i 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 N=7N=7 and A=[1,3,4,0,3,6,2]A=[1, 3, 4, 0, 3, 6, 2].\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 1+4+0=51+4+0=5. The cohesions of the regions containing building 2 and building 6 are 6 and 8. So the dominance in this case is 5×6×8=2405\times 6\times 8=240.\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 NN.\n\nThe second line contains the number of students in each building, A1A_1, A2A_2, cdots\\cdots, ANA_N, separated by spaces.\n\nFrom line 3 to line N+1N+1, line i+2i+2 contains two integers UiU_i and ViV_i separated by a space. This means the ii-th road connects building UiU_i and building ViV_i in both directions.

Output

Print one line containing the maximum dominance Donghyun can get by dividing Gyeonggi Science High School into three regions.

Constraints

  • 3leNle1053 \\le N \\le 10^5\n- 0leAile300 \\le A_i \\le 30 (1leileN)(1 \\le i \\le N)\n- 1leUi<VileN1 \\le U_i < V_i \\le N (1leileN−1)(1 \\le i \\le N-1)\n- The structure of Gyeonggi Science High School is a tree.

Examples2

  1. Example 1

    Input
    7
    1 3 4 0 3 6 2
    1 3
    3 4
    1 5
    2 5
    5 7
    6 7
    
    Expected output
    240
    
  2. Example 2

    Input
    3
    10 20 30
    1 2
    2 3
    
    Expected output
    6000