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 N = 5;
  14. int[] A = {0, 3, 5, 2, 1, 9};
  15. int[] B = {0, 1, 1, 10, 5, 3};
  16.  
  17. int[][] dp = new int[N + 1][3];
  18.  
  19. dp[1][0] = A[1]; //for a
  20. dp[1][2] = Integer.MAX_VALUE; //for b2
  21. dp[1][1] = B[1]; //for b1
  22.  
  23. for(int i = 2; i <= N; i++){
  24. int v1 = A[i] + Math.min(dp[i-1][2], dp[i-1][0]);
  25. int v2 = B[i] + B[i-1] + Math.min(dp[i - 2][1], dp[i-2][0]);
  26. int v3 = B[i] + Math.min(dp[i-1][1], dp[i-1][0]);
  27.  
  28. dp[i][0] = v1;
  29. dp[i][2] = v2;
  30. dp[i][1] = v3;
  31.  
  32. }
  33.  
  34. System.out.println(Math.min(dp[N][0], dp[N][2]));
  35. }
  36. }
Success #stdin #stdout 0.07s 52484KB
stdin
Standard input is empty
stdout
12