fork(6) 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. int[] price = {1, 5, 8, 9, 10, 22, 17, 20, 24, 30};
  13. int N = price.length;
  14. int K = 3;
  15. int[][] D = new int[N+1][K+1];
  16.  
  17. for (int n = 1; n <= N; n++)
  18. D[n][0] = price[n-1];
  19.  
  20. for (int k = 1; k <= K; k++)
  21. for (int n = 0; n <= N; n++)
  22. for (int i = 0; i <= n-1; i++)
  23. D[n][k] = Math.max(D[n][k], price[i] + D[n-i-1][k-1]);
  24.  
  25. int best = 0;
  26. for (int k = 0; k <= K; k++)
  27. best = Math.max(best, D[N][k]);
  28.  
  29. System.out.println(best);
  30. }
  31. }
Success #stdin #stdout 0.07s 2841600KB
stdin
Standard input is empty
stdout
32