/* package whatever; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
class Ideone
{
static final int GOAL = 100;
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, };
Result result = solve(red, blue);
}
static class Result
{
int[] move;
@Override
{
return String.
format("%d %s", distance,
Arrays.
toString(move
)); }
}
public static Result solve(int[] red, int[] blue)
{
Result result = new Result();
solve(0, 0, 0, red, 0, blue, 0, new int[red.length + blue.length + 2], 0, result);
return result;
}
static void solve(int pos, int dir, int distance, int[] red, int r, int[] blue, int b, int[] move, int m, Result result)
{
if (red.length == r && blue.length == b)
{
distance
+= Math.
abs(GOAL
- pos
); if (distance >= result.distance) return;
int g_dir
= Integer.
signum(GOAL
- pos
); if (g_dir != dir) move[m++] = pos;
move[m++] = GOAL;
result.
move = Arrays.
copyOf(move, m
); result.distance = distance;
return;
}
if (red[r] == blue[b])
{
solve(pos, red[r], dir, distance, red, r + 1, blue, b + 1, move, m, result);
return;
}
int r_dir
= Integer.
signum(red
[r
] - pos
); int b_dir
= Integer.
signum(blue
[b
] - pos
); if (r_dir == b_dir)
{
if (Math.
abs(pos
- red
[r
]) < Math.
abs(pos
- blue
[b
])) solve(pos, red[r], dir, distance, red, r + 1, blue, b, move, m, result);
else
solve(pos, blue[b], dir, distance, red, r, blue, b + 1, move, m, result);
return;
}
solve(pos, red[r], dir, distance, red, r + 1, blue, b, move, m, result);
solve(pos, blue[b], dir, distance, red, r, blue, b + 1, move, m, result);
}
static void solve(int pos, int next, int dir, int distance, int[] red, int r, int[] blue, int b, int[] move, int m, Result result)
{
while(r < red.length && next == red[r]) r++;
while(b < blue.length && next == blue[b]) b++;
distance
+= Math.
abs(next
- pos
); if (distance >= result.distance) return;
int n_dir
= Integer.
signum(next
- pos
); if (n_dir != dir) move[m++] = pos;
if (red.length == r) { red = blue; r = b; }
if (blue.length == b) { blue = red; b = r; }
solve(next, n_dir, distance, red, r, blue, b, move, m, result);
}
}
LyogcGFja2FnZSB3aGF0ZXZlcjsgLy8gZG9uJ3QgcGxhY2UgcGFja2FnZSBuYW1lISAqLwoKaW1wb3J0IGphdmEudXRpbC4qOwppbXBvcnQgamF2YS5sYW5nLio7CmltcG9ydCBqYXZhLmlvLio7CgpjbGFzcyBJZGVvbmUKewogICAgc3RhdGljIGZpbmFsIGludCBHT0FMID0gMTAwOwoKICAgIHB1YmxpYyBzdGF0aWMgdm9pZCBtYWluKFN0cmluZ1tdIGFyZ3MpCiAgICB7CiAgICAgICAgaW50W10gcmVkID0geyAxMCwgMjAsIDMwLCA0MCwgNTAsIDYwLCA3MCwgODAsIDkwLCA5OSB9OwogICAgICAgIGludFtdIGJsdWUgPSB7IDkwLCA4MCwgNzAsIDYwLCA1MCwgNDAsIDMwLCAyMCwgMTAsIDEsIH07CgogICAgICAgIFJlc3VsdCByZXN1bHQgPSBzb2x2ZShyZWQsIGJsdWUpOwogICAgICAgIFN5c3RlbS5vdXQucHJpbnRsbihyZXN1bHQpOwogICAgfQoKICAgIHN0YXRpYyBjbGFzcyBSZXN1bHQKICAgIHsKICAgICAgICBpbnQgZGlzdGFuY2UgPSBJbnRlZ2VyLk1BWF9WQUxVRTsKICAgICAgICBpbnRbXSBtb3ZlOwoKICAgICAgICBAT3ZlcnJpZGUKICAgICAgICBwdWJsaWMgU3RyaW5nIHRvU3RyaW5nKCkKICAgICAgICB7CiAgICAgICAgICAgIHJldHVybiBTdHJpbmcuZm9ybWF0KCIlZCAlcyIsIGRpc3RhbmNlLCBBcnJheXMudG9TdHJpbmcobW92ZSkpOwogICAgICAgIH0KICAgIH0KCiAgICBwdWJsaWMgc3RhdGljIFJlc3VsdCBzb2x2ZShpbnRbXSByZWQsIGludFtdIGJsdWUpCiAgICB7CiAgICAgICAgUmVzdWx0IHJlc3VsdCA9IG5ldyBSZXN1bHQoKTsKICAgICAgICBzb2x2ZSgwLCAwLCAwLCByZWQsIDAsIGJsdWUsIDAsIG5ldyBpbnRbcmVkLmxlbmd0aCArIGJsdWUubGVuZ3RoICsgMl0sIDAsIHJlc3VsdCk7CiAgICAgICAgcmV0dXJuIHJlc3VsdDsKICAgIH0KCiAgICBzdGF0aWMgdm9pZCBzb2x2ZShpbnQgcG9zLCBpbnQgZGlyLCBpbnQgZGlzdGFuY2UsIGludFtdIHJlZCwgaW50IHIsIGludFtdIGJsdWUsIGludCBiLCBpbnRbXSBtb3ZlLCBpbnQgbSwgUmVzdWx0IHJlc3VsdCkKICAgIHsKICAgICAgICBpZiAocmVkLmxlbmd0aCA9PSByICYmIGJsdWUubGVuZ3RoID09IGIpCiAgICAgICAgewogICAgICAgICAgICBkaXN0YW5jZSArPSBNYXRoLmFicyhHT0FMIC0gcG9zKTsKICAgICAgICAgICAgaWYgKGRpc3RhbmNlID49IHJlc3VsdC5kaXN0YW5jZSkgcmV0dXJuOwogICAgICAgICAgICBpbnQgZ19kaXIgPSBJbnRlZ2VyLnNpZ251bShHT0FMIC0gcG9zKTsKICAgICAgICAgICAgaWYgKGdfZGlyICE9IGRpcikgbW92ZVttKytdID0gcG9zOwogICAgICAgICAgICBtb3ZlW20rK10gPSBHT0FMOwogICAgICAgICAgICByZXN1bHQubW92ZSA9IEFycmF5cy5jb3B5T2YobW92ZSwgbSk7CiAgICAgICAgICAgIHJlc3VsdC5kaXN0YW5jZSA9IGRpc3RhbmNlOwogICAgICAgICAgICByZXR1cm47CiAgICAgICAgfQoKICAgICAgICBpZiAocmVkW3JdID09IGJsdWVbYl0pCiAgICAgICAgewogICAgICAgICAgICBzb2x2ZShwb3MsIHJlZFtyXSwgZGlyLCBkaXN0YW5jZSwgcmVkLCByICsgMSwgYmx1ZSwgYiArIDEsIG1vdmUsIG0sIHJlc3VsdCk7CiAgICAgICAgICAgIHJldHVybjsKICAgICAgICB9CgogICAgICAgIGludCByX2RpciA9IEludGVnZXIuc2lnbnVtKHJlZFtyXSAtIHBvcyk7CiAgICAgICAgaW50IGJfZGlyID0gS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