|
||||||||||
DarnassusTime Limit: 6000/3000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Others)Total Submission(s): 4177 Accepted Submission(s): 841 Problem Description $\space \space$*Even the World Tree must bow to the cycle of life. Everything born will die.* $\space \space$*Archimonde has hurt it once, Sylvanas burnt it again.* $\space \space$*Now the World Tree is slowly recovering.* The World Tree is burnt apart into $n$ parts. Now it tries to rebuild itself. Each part of the World Tree has an attribute $p_i$, and all $p_i\ (1\leq i\leq n)$ forms a permutation of $1,2,3...n$. For all $1 \leq i < j \leq n$, if the World Tree wants to grow an edge connecting part $i$ and part $j$ directly, it needs to spend $|i-j| * |p_i-p_j|$ energy. $|x|$ means the absolute value of $x$. The World Tree is very smart, so it will grow some edges such that all its $n$ parts become connected (in other words, you can go from any part to any other part using only the edges that have been grown), spending the minimum energy. Please calculate the minimum energy the World Tree needs to spend. Input The input consists of multiple test cases. The first line contains an integer $T\ (1\leq T \leq 5)$ denoting the number of test cases. For each test case, the first line contains a single integer $n(1 \leq n \leq 50000)$. The second line contains $n$ integers $p_i\ (1 \leq p_i \leq n)$, it's guaranteed that all $p_i$ forms a permutation. Output For each test case, output one line containing one integer indicating the answer. Sample Input
Sample Output
Source | ||||||||||
|