/* package whatever; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
public static void main
(String[] args
) {
int[] red = { 10, 20, 30, 40, 50, 60, 70, 80, 90, 99 };
int[] blue = { 90, 80, 70, 60, 50, 40, 30, 20, 10, 1 };
System.
out.
println(solve
(0,
0,
0, red,
0, blue
)); }
static int solve(int pos, int distance, int r, int[] red, int b, int[] blue)
{
if (r
== red.
length && b
== blue.
length) return distance
+ Math.
abs(100 - pos
); r
== red.
length ? Integer.
MAX_VALUE : solve
(red
[r
], distance
+ Math.
abs(red
[r
] - pos
), r
+ 1, red, b, blue
),
b
== blue.
length ? Integer.
MAX_VALUE : solve
(blue
[b
], distance
+ Math.
abs(blue
[b
] - pos
), r, red, b
+ 1, blue
) );
}
}
LyogcGFja2FnZSB3aGF0ZXZlcjsgLy8gZG9uJ3QgcGxhY2UgcGFja2FnZSBuYW1lISAqLwoKaW1wb3J0IGphdmEudXRpbC4qOwppbXBvcnQgamF2YS5sYW5nLio7CmltcG9ydCBqYXZhLmlvLio7CgovKiBOYW1lIG9mIHRoZSBjbGFzcyBoYXMgdG8gYmUgIk1haW4iIG9ubHkgaWYgdGhlIGNsYXNzIGlzIHB1YmxpYy4gKi8KY2xhc3MgSWRlb25lCnsKICAgIHB1YmxpYyBzdGF0aWMgdm9pZCBtYWluKFN0cmluZ1tdIGFyZ3MpCiAgICB7CiAgICAgICAgaW50W10gcmVkID0geyAxMCwgMjAsIDMwLCA0MCwgNTAsIDYwLCA3MCwgODAsIDkwLCA5OSB9OwogICAgICAgIGludFtdIGJsdWUgPSB7IDkwLCA4MCwgNzAsIDYwLCA1MCwgNDAsIDMwLCAyMCwgMTAsIDEgfTsKICAgICAgICBTeXN0ZW0ub3V0LnByaW50bG4oc29sdmUoMCwgMCwgMCwgcmVkLCAwLCBibHVlKSk7CiAgICB9CgogICAgc3RhdGljIGludCBzb2x2ZShpbnQgcG9zLCBpbnQgZGlzdGFuY2U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