F.A.Q
Hand In Hand
Online Acmers
Problem Archive
Realtime Judge Status
Authors Ranklist
 
     C/C++/Java Exams     
ACM Steps
Go to Job
Contest LiveCast
ICPC@China
Best Coder beta
VIP | STD Contests
    DIY | Web-DIY beta
Author ID 
Password 
 Register new ID

Darnassus

Time 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
2 5 4 3 5 1 2 10 4 7 3 8 6 1 9 10 5 2
 

Sample Output
7 24
 

Source
 

Statistic | Submit | Discuss | Note
Hangzhou Dianzi University Online Judge 3.0
Copyright © 2005-2024 HDU ACM Team. All Rights Reserved.
Designer & Developer : Wang Rongtao LinLe GaoJie GanLu
Total 0.000000(s) query 1, Server time : 2024-05-10 08:33:11, Gzip enabled