#CC0000023. Tổng khoảng cách nhỏ nhất giữa các điểm chuyển động (Moving Points)
Bài tập thuộc lộ trình
Mục lục đề ⌄
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 points on a number line with distinct initial integer coordinates. Point moves with constant velocity , so its location at time is . For each unordered pair, find the smallest achievable distance at any real time , and sum these pairwise minima. Different pairs may attain their minima at different times.
Input
The first line contains . The second contains initial integer positions . The third contains integer velocities 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): ; for every ; for every ; all initial coordinates are pairwise distinct.
- Subtask 2 (30 points): ; for every ; for every ; all initial coordinates are pairwise distinct.
- Subtask 3 (50 points): ; for every ; for every ; all initial coordinates are pairwise distinct.
Examples
Input
3
1 3 2
-100 2 3
Output
3
Explanation
Sort by initial position: the point at has velocity , the point at has velocity , and the point at has velocity . The minimum distances for pairs , , and are respectively , , and : the first two pairs move apart, while the left point in the last pair catches the right one. The total is .