K Closest Points to Origin

MediumHeap
Next
HeapGeometrySorting

Given points on a plane, find the k points closest to the origin (0, 0), using Euclidean distance (compare squared distances to avoid square roots). Print them ordered by distance; ties are ordered by smaller x, then smaller y. All points are distinct.

Input

Line 1: n and k. Next n lines: the integer coordinates x and y of each point.

Output

Print k lines, each containing the x and y of a point, nearest first.

Constraints

1 <= k <= n <= 10^4 -10^4 <= x, y <= 10^4

Example 1
Input
3 2
1 3
-2 2
2 -2
Output
-2 2
2 -2

Squared distances are 10, 8 and 8. The two with distance 8 are nearest; x breaks the tie.

Hints

K Closest Points to Origin - Medium Heap Problem | Skill Shiksha