import std.conv, std.functional, std.range, std.stdio, std.string; import std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons; import core.bitop; class EOFException : Throwable { this() { super("EOF"); } } string[] tokens; string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; } int readInt() { return readToken.to!int; } long readLong() { return readToken.to!long; } real readReal() { return readToken.to!real; } bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } } bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } } int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; } int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); } int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); } enum INF = 10L^^18; enum BIG = 10L^^14; void main() { try { for (; ; ) { const M = readInt(); const N = readInt(); auto A = new long[M]; foreach (i; 0 .. M) { A[i] = readLong(); } auto B = new long[N]; foreach (j; 0 .. N) { B[j] = readLong(); } auto xs = (A ~ B).sort.uniq.array; const xsLen = cast(int)(xs.length); debug { writeln("A = ", A); writeln("B = ", B); writeln("xsLen = ", xsLen); } auto as = new int[M]; foreach (i; 0 .. M) { as[i] = xs.lowerBound(A[i]); } auto bs = new int[N]; foreach (j; 0 .. N) { bs[j] = xs.lowerBound(B[j]); } foreach (k; 1 .. M + 1) { debug { writefln("-------- k = %s --------", k); } auto ss = new long[xsLen], ts = new long[xsLen]; foreach (i; 0 .. M) { ss[as[i]] += 1; } foreach (j; 0 .. N) { ts[bs[j]] += k; } alias Entry = Tuple!(long, "slope", long, "len"); DList!Entry queL, queR; long ofsL, ofsR; long lenR; long val; queL.insertBack(Entry(-BIG, INF)); foreach (e; 0 .. xsLen) { if (ss[e] > 0) { debug { writefln("shift +%s", ss[e]); } for (long need = ss[e]; need > 0; ) { const tmp = min(need, queL.back.len); queR.insertFront(Entry(queL.back.slope + ofsL - ofsR, tmp)); need -= tmp; if ((queL.back.len -= tmp) == 0) { queL.removeBack; } lenR += tmp; } } if (ts[e] > 0) { debug { writefln("stretch min -%s", ts[e]); } queR.insertBack(Entry(0 - ofsR, ts[e])); for (long need = ts[e]; need > 0; ) { const tmp = min(need, queR.front.len); queL.insertBack(Entry(queR.front.slope + ofsR - ofsL, tmp)); need -= tmp; if ((queR.front.len -= tmp) == 0) { queR.removeFront; } } } debug { write(" L:"); foreach (ref entry; queL) writef(" (%s, %s)", entry.slope + ofsL, entry.len); writeln; write(" R:"); foreach (ref entry; queR) writef(" (%s, %s)", entry.slope + ofsR, entry.len); writeln; writefln(" lenR = %s, val = %s", lenR, val); } if (e + 1 < xsLen) { const x = xs[e + 1] - xs[e]; debug { writefln("add %s |t|", x); } ofsL -= x; ofsR += x; val += x * lenR; for (; !queR.empty && queR.back.slope + ofsR > 0; ) { lenR -= queR.back.len; val -= (queR.back.slope + ofsR) * queR.back.len; queR.removeBack; } } debug { write(" L:"); foreach (ref entry; queL) writef(" (%s, %s)", entry.slope + ofsL, entry.len); writeln; write(" R:"); foreach (ref entry; queR) writef(" (%s, %s)", entry.slope + ofsR, entry.len); writeln; writefln(" lenR = %s, val = %s", lenR, val); } } long ans = val; foreach_reverse (ref entry; queR) { ans -= (entry.slope + ofsR) * entry.len; } writeln(ans); } } } catch (EOFException e) { } }