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[] a = {2, -1, 3, 4};
  14. int n = a.length;
  15. int k = 2;
  16. int[] dp = new int[n];
  17. dp[1] = a[0] * a[1];
  18.  
  19. for(int i = 2; i < n; i++){
  20. dp[i] = a[i] * a[i-1];
  21. int u = 0;
  22. for(int j = Math.max(u, i - k); j <= i-1; j++){
  23. dp[i] = Math.min(dp[i], a[i] * a[j] + dp[j]);
  24. }
  25. }
  26. System.out.print(dp[n-1]);
  27. }
  28. }
Success #stdin #stdout 0.09s 54504KB
stdin
Standard input is empty
stdout
-6