Profitable Paths

All submissions for this problem are available.
### Read problems statements in [Hindi](http://www.codechef.com/download/translated/COOK114/hindi/PRT.pdf), [Mandarin Chinese](http://www.codechef.com/download/translated/COOK114/mandarin/PRT.pdf), [Russian](http://www.codechef.com/download/translated/COOK114/russian/PRT.pdf), [Vietnamese](http://www.codechef.com/download/translated/COOK114/vietnamese/PRT.pdf), and [Bengali](http://www.codechef.com/download/translated/COOK114/bengali/PRT.pdf) as well. You are given a tree with $N$ vertices (numbered $1$ through $N$) and a sequence of integers $A_1, A_2, \ldots, A_N$. You may choose an arbitrary permutation $p_1, p_2, \ldots, p_N$ of the integers $1$ through $N$. Then, for each vertex $i$, you should assign the value $A_{p_i}$ to this vertex. The *profit* of a path between two vertices $u$ and $v$ is the sum of the values assigned to the vertices on that path (including $u$ and $v$). Let's consider only (undirected) paths that start at a leaf and end at a different leaf. Calculate the maximum possible value of the sum of profits of all such paths. Since this value could be very large, compute it modulo $10^9 + 7$. ### Input  The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.  The first line of each test case contains a single integer $N$.  The second line contains $N$ spaceseparated integers $A_1, A_2, \ldots, A_N$.  Each of the following $Nā1$ lines contains two spaceseparated integers $u$ and $v$ denoting that vertices $u$ and $v$ are connected by an edge. ### Output For each test case, print a single line containing one integer ā the maximum sum of profits, modulo $10^9 + 7$. ### Constraints  $1 \le T \le 1,000$  $1 \le N \le 300,000$  $1 \le A_i \le 10^9$ for each valid $i$  the sum of $N$ over all test cases does not exceed $5 \cdot 10^5$ ### Example Input ``` 2 4 1 2 3 4 1 2 2 3 2 4 5 1 2 3 4 5 1 2 2 3 3 4 4 5 ``` ### Example Output ``` 24 15 ``` ### Explanation **Example case 1:** $(1, 4, 2, 3)$ is one of the possible permutations that give the optimal answer. Then, the profits of paths between pairs of vertices $(1, 3)$, $(1, 4)$ and $(3, 4)$ are $7$, $8$ and $9$ respectively. **Example case 2:** Here, any permutation could be chosen.Author:  ahmad_salah 
Editorial  https://discuss.codechef.com/problems/PRT 
Tags  ahmad_salah, cook114 
Date Added:  5012020 
Time Limit:  1 sec 
Source Limit:  50000 Bytes 
Languages:  C, CPP14, JAVA, PYTH, PYTH 3.6, PYPY, CS2, PAS fpc, PAS gpc, RUBY, PHP, GO, NODEJS, HASK, rust, SCALA, swift, D, PERL, FORT, WSPC, ADA, CAML, ICK, BF, ASM, CLPS, PRLG, ICON, SCM qobi, PIKE, ST, NICE, LUA, BASH, NEM, LISP sbcl, LISP clisp, SCM guile, JS, ERL, TCL, SQL, kotlin, PERL6, TEXT, SCM chicken, PYP3, CLOJ, R, COB, FS 
Comments
 Please login at the top to post a comment.
SUCCESSFUL SUBMISSIONS
Fetching successful submissions
HELP
If you are still having problems, see a sample solution here. 