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)
  11. {
  12. int[] red = { 10, 20, 30, 40, 50, 60, 70, 80, 90, 99 };
  13. int[] blue = { 90, 80, 70, 60, 50, 40, 30, 20, 10, 1 };
  14. System.out.println(solve(0, 0, 0, red, 0, blue));
  15. }
  16.  
  17. static int solve(int pos, int distance, int r, int[] red, int b, int[] blue)
  18. {
  19. if (r == red.length && b == blue.length) return distance + Math.abs(100 - pos);
  20. return Math.min(
  21. r == red.length ? Integer.MAX_VALUE : solve(red[r], distance + Math.abs(red[r] - pos), r + 1, red, b, blue),
  22. b == blue.length ? Integer.MAX_VALUE : solve(blue[b], distance + Math.abs(blue[b] - pos), r, red, b + 1, blue)
  23. );
  24. }
  25. }
Success #stdin #stdout 0.08s 380224KB
stdin
Standard input is empty
stdout
278