https://www.acmicpc.net/problem/11501
11501๋ฒ: ์ฃผ์
์ ๋ ฅ์ ์ฒซ ์ค์๋ ํ ์คํธ์ผ์ด์ค ์๋ฅผ ๋ํ๋ด๋ ์์ฐ์ T๊ฐ ์ฃผ์ด์ง๋ค. ๊ฐ ํ ์คํธ์ผ์ด์ค ๋ณ๋ก ์ฒซ ์ค์๋ ๋ ์ ์๋ฅผ ๋ํ๋ด๋ ์์ฐ์ N(2 ≤ N ≤ 1,000,000)์ด ์ฃผ์ด์ง๊ณ , ๋์งธ ์ค์๋ ๋ ๋ณ ์ฃผ๊ฐ๋ฅผ ๋ํ
www.acmicpc.net
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);
}
}
}