https://www.acmicpc.net/problem/11501
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
for (int i = 0; i < t; i++) {
int n = sc.nextInt();
long[] arr = new long[n];
long max = 0;
long answer = 0;
for (int j = 0; j < n; j++) {
arr[j] = sc.nextLong();
}
for (int j = n - 1; j >= 0; j--) {
if (arr[j] > max) {
max = arr[j]; // ๋ง์ง๋ง ๊ฐ์ max๋ก ๋๊ณ , max ๋ณด๋ค ํฐ ๊ฐ์ด ๋ค์ด์ฌ๋ ์ต๋ ์ฃผ๊ฐ๋ก ๋ฐ๊พธ๊ธฐ
} else {
answer += (max - arr[j]); // ์ญ๋ฐฉํฅ์ผ๋ก ํ์ํ๋ฉด์ max ๋ณด๋ค ๊ฐ์ด ์์ผ๋ฉด ํ๋งค
}
}
System.out.println(answer);
}
}
}