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];
  18.  
  19. dp[0] = 0;
  20.  
  21. for(int i = 1; i <= N; i++){
  22. int ans = 1000;
  23. int sum = B[i];
  24. ans = Math.min(ans, A[i] + dp[i-1]);
  25. for(int j = i - 1; j >= 1; j--){
  26. sum += B[j];
  27. ans = Math.min(ans, sum + dp[j - 1]);
  28. }
  29. dp[i] = ans;
  30. }
  31. System.out.println(dp[N]);
  32. }
  33. }
Success #stdin #stdout 0.08s 52628KB
stdin
Standard input is empty
stdout
12