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 = {8, 1, 9, 2, 7};
  14. int k = 3;
  15.  
  16. int n = A.length;
  17. int[][] dp = new int[n + 1][k+ 1];
  18.  
  19. for(int i = 0; i <= n; i++){
  20. Arrays.fill(dp[i], Integer.MAX_VALUE);
  21. }
  22.  
  23. // for(int i = 0; i <= k; i++){
  24. // dp[0][i] = 0;
  25. // }
  26. dp[0][0] = 0;
  27.  
  28. for(int i = 1; i <= n; i++){
  29. for(int parts = 1; parts <= k; parts++){
  30. int maxSlot = 0;
  31.  
  32. for(int j = i; j >= 1; j--){
  33. maxSlot = Math.max(maxSlot, A[j - 1]);
  34.  
  35. if(dp[j-1][parts-1] != Integer.MAX_VALUE){
  36. dp[i][parts] = Math.min(dp[i][parts], maxSlot + dp[j - 1][parts - 1]);
  37. }
  38. }
  39. }
  40. }
  41.  
  42. System.out.print(dp[n][k]);
  43. }
  44. }
Success #stdin #stdout 0.07s 54536KB
stdin
Standard input is empty
stdout
18