fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main (String[] args) throws java.lang.Exception
  11. {
  12. // your code goes here
  13. int[] arr = {5, 10, 5, 10, 1, 1, 8, 2};
  14.  
  15. int n = arr.length;
  16.  
  17. int[] dp = new int[n];
  18. Arrays.fill(dp, Integer.MIN_VALUE);
  19.  
  20. dp[0] = 0;
  21. dp[1] = arr[0] - arr[1];
  22. dp[2] = arr[0] - arr[2];
  23.  
  24. for(int i = 3; i < n; i++){
  25. for(int j = i - 1; j >= 2; j--){
  26. dp[i] = Math.max(dp[i], arr[j] - arr[i] + dp[j - 1]);
  27. }
  28. }
  29. System.out.println(dp[n-1]);
  30.  
  31. }
  32. }
Success #stdin #stdout 0.07s 52568KB
stdin
Standard input is empty
stdout
15