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)); } void bAdd(T)(T[] bit, int pos, T val) in { assert(0 <= pos && pos < bit.length, "bAdd: 0 <= pos < |bit| must hold"); } do { for (int x = pos; x < bit.length; x |= x + 1) { bit[x] += val; } } // sum of [0, pos) T bSum(T)(T[] bit, int pos) in { assert(0 <= pos && pos <= bit.length, "bSum: 0 <= pos <= |bit| must hold"); } do { T ret = 0; for (int x = pos - 1; x >= 0; x = (x & (x + 1)) - 1) { ret += bit[x]; } return ret; } void main() { try { for (; ; ) { const N = readInt; auto X = new long[N]; auto Y = new long[N]; foreach (i; 0 .. N) { X[i] = readLong; Y[i] = readLong; } auto ys = Y.dup; ys = ys.sort.uniq.array; const ysLen = cast(int)(ys.length); debug { writeln("ys = ", ys); } alias Entry = Tuple!(long, "x", int, "i"); auto es = new Entry[N]; foreach (i; 0 .. N) { es[i] = Entry(X[i], i); } es.sort; // 0: 1, 1: X, 2: Y, 3: XY long[4] totL, totR; long[][4] bitL, bitR; static foreach (h; 0 .. 4) { bitL[h] = new long[ysLen]; bitR[h] = new long[ysLen]; } foreach (i; 0 .. N) { const pos = ys.lowerBound(Y[i]); totR[0] += 1; totR[1] += X[i]; totR[2] += Y[i]; totR[3] += X[i] * Y[i]; bitR[0].bAdd(pos, 1); bitR[1].bAdd(pos, X[i]); bitR[2].bAdd(pos, Y[i]); bitR[3].bAdd(pos, X[i] * Y[i]); } long ans = long.max; foreach (e; es) { { const i = e.i; const pos = ys.lowerBound(Y[i]); totL[0] += 1; totL[1] += X[i]; totL[2] += Y[i]; totL[3] += X[i] * Y[i]; bitL[0].bAdd(pos, 1); bitL[1].bAdd(pos, X[i]); bitL[2].bAdd(pos, Y[i]); bitL[3].bAdd(pos, X[i] * Y[i]); totR[0] -= 1; totR[1] -= X[i]; totR[2] -= Y[i]; totR[3] -= X[i] * Y[i]; bitR[0].bAdd(pos, -1); bitR[1].bAdd(pos, -X[i]); bitR[2].bAdd(pos, -Y[i]); bitR[3].bAdd(pos, -X[i] * Y[i]); } /* // min pos s.t. pred(sum of [0, pos)) // assume pred(sum of [0, pos)) is non-decreasing int bBinarySearch(alias pred, T)(T[] bit) { import core.bitop : bsr; import std.functional : unaryFun; alias predFun = unaryFun!pred; if (predFun(0)) return 0; int pos = 0; T sum = 0; foreach_reverse (e; 0 .. bsr(bit.length) + 1) { const x = (pos | 1 << e) - 1; if (x < bit.length && !predFun(sum + bit[x])) { pos |= 1 << e; sum += bit[x]; } } return pos + 1; } */ { long tot; tot += (totL[0] * e.x - totL[1]); tot -= (totR[0] * e.x - totR[1]); int pos; long sum; foreach_reverse (f; 0 .. bsr(ysLen) + 1) { const pp = (pos | 1 << f) - 1; if (pp < ysLen) { long ss = sum; ss += (bitL[0][pp] * e.x - bitL[1][pp]); ss -= (bitR[0][pp] * e.x - bitR[1][pp]); if (2 * ss <= tot) { pos |= 1 << f; sum = ss; } } } assert(0 <= pos && pos <= ysLen); long[4] resL, resR; static foreach (h; 0 .. 4) { resL[h] = bitL[h].bSum(pos); resR[h] = bitR[h].bSum(pos); } const x = X[e.i]; const y = ys[min(pos, ysLen - 1)]; long cost; cost += (resL[0] * x * y - resL[1] * y - resL[2] * x + resL[3]); cost -= ((totL[0] - resL[0]) * x * y - (totL[1] - resL[1]) * y - (totL[2] - resL[2]) * x + (totL[3] - resL[3])); cost -= (resR[0] * x * y - resR[1] * y - resR[2] * x + resR[3]); cost += ((totR[0] - resR[0]) * x * y - (totR[1] - resR[1]) * y - (totR[2] - resR[2]) * x + (totR[3] - resR[3])); debug { writeln(e, " ", pos, " ", totL, " ", resL, " ", totR, " ", resR, " ", cost); } chmin(ans, cost); } } writeln(ans); } } catch (EOFException e) { } }