Untitled
unknown
java
a year ago
866 B
14
Indexable
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int T = sc.nextInt();
while (T-- > 0) {
int N = sc.nextInt();
int[] A = new int[N];
for (int i = 0; i < N; i++) A[i] = sc.nextInt();
int l = 0, r = N - 1, leftMax = 0, rightMax = 0;
long ans = 0;
while (l < r) {
if (A[l] < A[r]) {
if (A[l] >= leftMax) leftMax = A[l];
else ans += leftMax - A[l];
l++;
} else {
if (A[r] >= rightMax) rightMax = A[r];
else ans += rightMax - A[r];
r--;
}
}
System.out.println(ans);
}
}
}
Editor is loading...
Leave a Comment