PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#CC0000023. Tổng khoảng cách nhỏ nhất giữa các điểm chuyển động (Moving Points)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

2 lộ trình

Tổng khoảng cách nhỏ nhất giữa các điểm chuyển động (Moving Points)

Moving Points

Source: Codeforces

Version: Phuoc Hung OJ Extended

Problem Statement

There are nn points on a number line with distinct initial integer coordinates. Point ii moves with constant velocity viv_i, so its location at time t≥0t\ge0 is xi+tvix_i+t v_i. For each unordered pair, find the smallest achievable distance at any real time t≥0t\ge0, and sum these pairwise minima. Different pairs may attain their minima at different times.

Input

The first line contains nn. The second contains initial integer positions x1,…,xnx_1,\ldots,x_n. The third contains integer velocities v1,…,vnv_1,\ldots,v_n of the corresponding points.

Output

Print one 64-bit integer: the sum of the minimum distances over all unordered pairs of points.

Subtasks

  • Subtask 1 (20 points): 2≤n≤102\le n\le 10; 1≤xi≤1081\le x_i\le 10^8 for every ii; −108≤vi≤108-10^8\le v_i\le 10^8 for every ii; all initial coordinates xix_i are pairwise distinct.
  • Subtask 2 (30 points): 2≤n≤9002\le n\le 900; 1≤xi≤1081\le x_i\le 10^8 for every ii; −108≤vi≤108-10^8\le v_i\le 10^8 for every ii; all initial coordinates xix_i are pairwise distinct.
  • Subtask 3 (50 points): 2≤n≤2000002\le n\le 200000; 1≤xi≤1081\le x_i\le 10^8 for every ii; −108≤vi≤108-10^8\le v_i\le 10^8 for every ii; all initial coordinates xix_i are pairwise distinct.

Examples

Input

3
1 3 2
-100 2 3

Output

3

Explanation

Sort by initial position: the point at 11 has velocity −100-100, the point at 22 has velocity 33, and the point at 33 has velocity 22. The minimum distances for pairs (1,2)(1,2), (1,3)(1,3), and (2,3)(2,3) are respectively 11, 22, and 00: the first two pairs move apart, while the left point in the last pair catches the right one. The total is 1+2+0=31+2+0=3.