import java.util.Scanner;
import java.io.FileInputStream;
class Solution
{
public static void main(String args[]) throws Exception
{
Scanner sc = new Scanner(System.in);
int T;
T=sc.nextInt();
for(int test_case = 1; test_case <= T; test_case++)
{
int N = sc.nextInt();
int[] arr = new int[N];
for(int i =0; i<N; i++){
arr[i] = sc.nextInt();
}
long answer = 0;
long sum = 0;
int count = 0;
int max = 0;
boolean flag = false;
for(int i = 0; i<N; i++){
if(!flag){
for(int j = i+1; j<N; j++){
max = Math.max(max,arr[j]);
}
}
if(arr[i]>=max){
answer += arr[i]*count - sum;
count = 0;
max = 0;
sum = 0;
flag = false;
}
else{
sum += arr[i];
count++;
flag = true;
}
}
System.out.println("#" +test_case+" "+answer);
}
}
}
단순한 구현문제인데 뭔가 생각해내기 어려웠다.
그리고 출력값이 크기때문에 long타입을 사용해야 한다.