fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6. import java.util.Scanner;
  7. public class Main {
  8. public static void main (String[] args)
  9. {
  10. Scanner sc = new Scanner(System.in);
  11. int numOfTestCase = sc.nextInt();
  12. for (int i = 0; i < numOfTestCase; i++) {
  13. int n = sc.nextInt();
  14. long profit = 0;
  15. int[] stockPrice = new int[n];
  16. for (int j = 0; j < n; j++)
  17. stockPrice[j] = sc.nextInt();
  18. int currMax = Integer.MIN_VALUE;
  19. for (int j = n - 1; j >= 0; j--) {
  20. if (currMax < stockPrice[j]) {
  21. currMax = stockPrice[j];
  22. }
  23. profit += (currMax - stockPrice[j]);
  24. }
  25. System.out.println(profit);
  26.  
  27. }
  28. }
  29. }
  30.  
Success #stdin #stdout 0.12s 49408KB
stdin
3
3
5 3 2
3
1 2 100
4
1 3 1 2
stdout
0
197
3