We need to find an integer point (x,y) with 0 ≤ x,y ≤ 1e9 that lies in exactly k of the given squares. Squares are axis-aligned, with opposite corners at (0,0) and (ai, ai). So each square is the set of points (x,y) such that 0 ≤ x ≤ ai and 0 ≤ y ≤ ai (including boundaries). Since (0,0) and (ai,ai) are opposite corners, the square is essentially the region from 0 to ai in both axes. All squares share the corner at origin. So they are nested? Actually not necessarily nested because ai can be any positive integers. But since they all share the origin, they are all "aligned" with lower left corner at (0,0) and extending to (ai,ai). So each square is basically the region [0,ai] x [0,ai]. So they are nested in the sense that if ai <= aj, then square i is contained within square j (since its side is smaller). So the squares are not distinct: they are all squares anchored at origin, varying side lengths. So the set of squares forms a nested family sorted by side length.
We need a point (x,y) integer that belongs to exactly k squares. A point belongs to square i if 0 ≤ x ≤ ai and 0 ≤ y ≤ ai. So essentially, point (x,y) is inside a square if both coordinates are ≤ ai. So the number of squares containing (x,y) is the number of i such that ai ≥ max(x,y). Because both coordinates must be ≤ ai, i.e., ai ≥ max(x,y). So condition: square i contains (x,y) iff ai >= M, where M = max(x,y). So the number of squares containing (x,y) is count of ai >= M. Let's denote sorted ai in non-increasing order? Actually easier: For a given M = max(x,y), the count = number of squares with ai >= M. So the count depends only on M, not on the specific values of x,y as long as max(x,y)=M, and also x,y must be within bounds 0..1e9, and also x,y must satisfy 0 ≤ x,y ≤ M? Actually we must have x ≤ M and y ≤ M because max = M. That's automatically satisfied if max(x,y)=M. So any point (x,y) with max(x,y)=M yields the same count: count of ai >= M. So we need to find integer M (0? M could be 0) such that count = k, and then we can choose any point with max(x,y)=M. Also note: M could be larger than 1e9? No, x,y ≤ 1e9, so M ≤ 1e9. Also ai up to 1e9, so it's fine.
But careful: the point must be integer, and coordinates between 0 and 1e9 inclusive. Also M can be 0: then (0,0) yields max=0. For M=0, condition is ai >= 0, which all squares satisfy? Since ai >= 1, yes all squares contain (0,0) because 0 ≤ 0 ≤ ai. So count = n. So (0,0) belongs to all squares. So if k = n, we can output (0,0). That's fine.
Also note: If we set M = some value, we need to ensure that we can pick (x,y) integer with max(x,y)=M, and also 0 ≤ x,y ≤ 1e9. Since M ≤ 1e9, we can set (M,0) for example, as long as M ≤ 1e9? But (M,0) has max=M. But is (M,0) within the square boundaries? The point itself doesn't have to be inside all squares, we only care about count. (M,0) is a valid integer point. However, we must ensure that (M,0) is within the global coordinate constraints: 0 ≤ x,y ≤ 1e9. So M ≤ 1e9, so ok. But careful: (M,0) will satisfy x=M, y=0. For square i with ai >= M, we have 0 ≤ 0 ≤ ai and M ≤ ai? Actually we need x ≤ ai and y ≤ ai. y=0 ≤ ai always true (since ai>0). x=M ≤ ai if ai >= M. So indeed count of squares with ai >= M. So works.
But there is a nuance: If M is 0, then (0,0) works.
Thus the problem reduces to: find an integer M (0 ≤ M ≤ 1e9) such that the number of ai >= M equals k. If such M exists, output any point (x,y) with max(x,y)=M, say (M,0) if M <= 1e9, or (M,M) etc. However, we must also consider that the point (M,0) must be such that 0 ≤ x,y ≤ 1e9, M can be up to 1e9, so fine.
But is there any scenario where no M yields count exactly k? Since the count function f(M) = number of ai >= M. As M increases from 0 to max(ai)+1, f(M) is a non-increasing step function that takes values from n (at M=0) down to 0 (for M > max ai). So it takes only certain values: the number of squares with ai >= some threshold. So if k is between 1 and n-1, we need to see if there is an M such that f(M)=k. Since the function can skip some numbers if there are gaps. For example, if a = [5,5] then f(M) for M in [0,5] is 2, for M>5 it's 0. So only k=2 or 0. But k is at least 1 and ≤ 50, so if we have squares with same ai, then there might be gaps: For distinct squares, but squares are distinct means the ai are distinct? The problem says: "It is guaranteed that all given squares are distinct." Since squares are defined by ai, distinct squares means all ai are distinct. Because if two have same ai, squares would be identical. So all ai are distinct positive integers. So we have n distinct numbers a_i. Then f(M) counts how many of these numbers are >= M. As M increases, f(M) will decrease by 1 each time M passes a distinct a_i. Precisely, if we sort a_i descending: a_(1) > a_(2) > ... > a_(n). Then for M in [0, a_n]? Actually careful: Suppose sorted ascending: a1 < a2 < ... < an (distinct). Then for M <= a1, f = n? Wait: if M <= a1 (the smallest), then all ai >= M, so f = n. For M in (a1, a2], then a1 is excluded, so f = n-1. For M in (a2, a3], f = n-2, ... For M in (a_{n-1}, a_n], f = 1. For M > a_n, f = 0. So f takes all values from n down to 1 exactly, plus 0. Since there are n distinct thresholds, the count can be any integer between 1 and n inclusive, depending on M. So with distinct ai, we can achieve any k in [1,n] exactly by choosing M appropriately: if k = n, choose M = 0? Actually for M = 0, f(0) = n (since all ai >= 0). But is M=0 allowed? M = max(x,y) can be 0 only if point (0,0). That's fine. However, note: M must be integer and point must be integer; M=0 yields (0,0). So if k=n, we can output (0,0). Also we could choose M = min(ai)? Actually if M = min(ai) we need to check: if M = a_min, then a_min is >= M? Since condition is ai >= M, square with ai = a_min satisfies ai >= M? If M = a_min, then that square's ai = a_min, so ai >= M true, so includes that square. So f = number of ai >= a_min = n, because a_min is the smallest, so all ai >= a_min. So f = n again. So to get f=n we can use any M ≤ a_min. So there is flexibility.
If k is between 1 and n-1, we can set M = a_{k+1}? Let's define sorted ascending: a1 < a2 < ... < an. For M in (a_{i}, a_{i+1}] (with a0 maybe?), we get f = n - i? Let's be precise: Let a sorted ascending: b1 < b2 < ... < bn. For a given M, f = number of bi >= M. If M is in (b_{i}, b_{i+1}] (with b0 = 0, b_{n+1}=∞), then the squares with bi <= b_{i} are excluded? Actually if M > b_i, then b_i < M, so b_i not >= M; all b_j for j <= i are < M. So f = n - i. So to get f = k, we need n - i = k => i = n - k. So M must be > b_{n-k} and ≤ b_{n-k+1} (provided we define appropriately). Let's denote indices: if we want f = k, we need exactly k squares with ai >= M, i.e., the k largest ai. So M should be greater than the (k+1)-th largest ai? Alternatively, let sorted descending: c1 > c2 > ... > cn. The k largest are c1,...,ck. For f = k, we need M such that ck >= M but c_{k+1} < M (if k < n). So M must satisfy c_{k+1} < M ≤ ck. Since ai are distinct, c_{k+1} < ck. So there exist integer M in that interval, e.g., M = c_{k+1}+1? But careful: M must be an integer, and we need to pick a point with max(x,y)=M. Since c_{k+1} and ck are integers, we can choose M = c_{k+1}+1, as long as ≤ ck. But need to ensure M ≤ 1e9, which is fine. Also M could be ck itself, but if M = ck, then squares with ai = ck satisfy ai >= M (since equality), so that square is included. Then f = number of ai >= ck = number of squares with ai ≥ ck. Since ck is the k-th largest, all larger ones (c1..c_{k-1}) are >= ck, and ck is equal, so total f = k? Actually if we set M = ck, then condition ai >= ck: all squares with ai >= ck, which includes c1..ck, so exactly k (since c_{k+1} < ck). So M = ck works. Also M could be any integer between c_{k+1}+1 and ck inclusive. So we can choose M = ck for simplicity.
Now if k = 0? Is k allowed? Input says 1 ≤ k ≤ 50, so k is at least 1. So we don't need to consider 0.
Thus with distinct ai, answer always exists for any k between 1 and n inclusive? But k can be up to 50, n also up to 50, but k can be larger than n? Possibly k ≤ 50, n ≤ 50, but k can be > n. The problem doesn't restrict k ≤ n? It says 1 ≤ n, k ≤ 50. It does not say k ≤ n. But logically, if k > n, then no point can belong to more than n squares, because there are only n squares. So for k > n, answer is -1. The examples show one with n=4, k=50 -> -1. So we need to check if k > n: then output -1.
Also what if k <= n? Since ai are distinct, we can always find M as described. But wait: there might be edge case when M=0? Actually for k = n, we need f = n. That's achievable by any M ≤ min(ai). For example, M = 0. So (0,0) works. But also we could use M = 1 if min(ai) >= 1, which is always true. So fine.
But check constraints: x,y must be ≤ 1e9. For k = n, M could be 0, that's fine. For other k, M = ck (the k-th largest). But ck could be up to 1e9, so M ≤ 1e9. So point (M,0) would be valid as long as M ≤ 1e9. Yes.
Now what if ai are distinct? Guaranteed. So answer exists iff k ≤ n.
But wait, there's a nuance: The point (x,y) must be integer and also must be "belongs to exactly k squares". However, we argued that count depends only on M = max(x,y). But is it always exactly count of ai >= M? Yes. However, we need to ensure that the point does not belong to any squares with ai < M. That's automatically because if ai < M, then either x or y may be > ai? Actually if ai < M, then max(x,y) = M > ai, so at least one coordinate > ai, so point is not inside that square. So count is exactly those with ai >= M. So it's exactly the count of squares with ai >= M.
Thus for given M, any point with max(x,y)=M yields the same count. However, we must also make sure that the point is within the squares? Not needed; we just want it to belong to exactly k squares. So indeed.
Thus the solution is trivial: if k > n, output -1. Else, sort a_i. Let the k-th largest value be needed? Actually we need M such that exactly k squares have ai >= M. So we can take M = a_sorted[n-k] (if using 0-index). Let's sort ascending: arr sorted ascending. Then the number of ai >= M equals count of arr[i] >= M. For k from 1 to n, we need to pick M such that exactly k ai are >= M. That can be done by taking M = arr[n-k] (the smallest among the top k) or M = arr[n-k] + something? Let's test: Suppose n=4, a = [1,3,4,5] sorted ascending. For k=3: we want exactly 3 squares containing point. That means M must be > a_{1}? Actually smallest? Let's compute f(M) for various M:
M=0: f=4
M=1: includes squares with ai>=1 => all 4? Since 1>=1? For square with a=1, ai=1, condition ai>=1 true, so included. So f(1)=4. Actually if M=1, we need ai >=1: all squares have ai >=1? yes, all. So f=4.
M=2: ai >=2: squares with a=3,4,5 => 3. So f=3. M=3: ai >=3: squares with a=3,4,5 => 3 (since 3>=3). So f=3. M=4: ai >=4: squares with 4,5 => 2. So to get f=3, we need M in (1,4]? Actually from M>1 up to M<=3 yields 3? Let's check: M in (1,3] -> f=3 because any M>1 excludes the a=1 square? For M=1.5 (not integer) but integer M only. M=2,3 yields f=3. So M can be 2 or 3. The k-th largest (3rd largest) is 3? Actually the largest:5, second:4, third:3. So the k-th largest is 3. So M = k-th largest works (3). Also M = (k+1)-th largest +1? For k=3, (k+1)-th largest = 4th? Actually careful: indices: if we list descending: 5,4,3,1. The 3rd largest is 3. So M = 3 yields f = count of ai >=3 = 3. M = 2 yields f = count of ai >=2 = 3? But 2 is not the k-th largest. However, 2 is not equal to any ai. But M=2 is valid integer. However, is it always possible to choose M=some integer that yields exactly k? Yes, we can always choose M = (the (k+1)-th largest) + 1 if k < n, but we need to ensure that's ≤ the k-th largest. Since the numbers are distinct, (k+1)-th largest < k-th largest, so (k+1)-th largest +1 ≤ k-th largest. So that is also a valid integer M. For k=n, we need M = 0 or any <= min(ai). Simpler: for k=n, we can output (0,0). For k < n, we can output M = a_{n-k} (if sorted ascending) or M = a_{n-k+1}? Let's define indices properly.
Let's sort a in ascending order: a[0] < a[1] < ... < a[n-1]. For M = a[n-k], how many ai >= a[n-k]? Since a[n-k] is the element at index n-k. There are n - (n-k) = k elements from index n-k to n-1 inclusive. So f(a[n-k]) = count of ai >= a[n-k] = k (since all those are >=, and those below are <). But careful: Are there elements equal? Since it's distinct, those below are strictly less. So f = exactly k. Good. For k=n, n-k =0, a[0] is min. f(a[0]) = count of ai >= a[0] = n. So that also works? For k=n, M = a[0] yields f = n. So we could use M = a[0] for k=n. But we must ensure that point (M,0) is within bounds: M = a[0] ≤ 1e9, fine. But is there any issue if M = a[0]? The point (a[0],0) belongs to exactly n squares? Check: For square with a = a[0], ai = a[0], condition: x = a[0] ≤ a[0] true, y=0 ≤ a[0] true, so that square contains point. So yes. So we could use M = a_{n-k} for any k from 1 to n. For k=n, that is a[0] (min). That yields f=n. But what about the possibility that M = a_{n-k} might be 0? Since ai >=1, it's >=1, so okay. But for k=n, M = a[0] >0, so M >0. So point (a[0],0) works.
But wait: Are there any constraints that require the point to be inside the squares? No, it's fine if it's on the boundary. So (a[0],0) is on the boundary of the smallest square? Actually the smallest square has side a[0]. The point (a[0],0) is on the bottom right corner? Actually (a[0],0) is on the bottom side, since y=0, x=a[0] is on the boundary. So it belongs to that square, and to all larger squares? For larger square with a > a[0], we have x = a[0] ≤ a, y=0 ≤ a, so yes. So it belongs to all squares. So f = n. So it's consistent.
Thus we can unify: if k > n, output -1. Else, sort a ascending. Let M = a[n-k] (0-indexed). Then output point (M, 0). But we need to ensure (M,0) satisfies 0 ≤ x,y ≤ 1e9. Since a_i ≤ 1e9, M ≤ 1e9, ok. Also y=0 is within [0,1e9]. So that works.
But is there any case where M = 0 needed? For k=n, we could output (0,0) as well, but M = a[0] is >0, so also works. So we don't need M=0.
But what about the condition that the point must belong to exactly k squares? Let's double-check with an example: n=4, a = [5,1,3,4] (example1). Sorted ascending: [1,3,4,5]. k=3, so n-k=1, M = a[1] = 3. Output (3,0)? But example output is (2,1). Our answer (3,0) would also have exactly 3 squares? Let's test: squares with a=1,3,4,5. For point (3,0): x=3,y=0. Squares: a=1: 3≤1? false -> not in. a=3: 3≤3 true,0≤3 true -> in. a=4: 3≤4,0≤4 -> in. a=5: in. So count=3. So (3,0) also satisfies k=3. The problem accepts any answer. So our method works.
But note: The example 1 output (2,1). That's also valid: max(2,1)=2, so count of ai≥2 = squares with a≥2: 3,4,5 ->3. So both fine.
Thus answer exists for any k ≤ n. But wait: Are there any constraints like point must be integer and 0 ≤ x,y ≤ 10^9, we satisfied that.
But one more nuance: The problem statement says: "Vasya wants to find such integer point (with integer coordinates) of the plane, that belongs to exactly k drawn squares." The squares are drawn, and we assume they are "drawn" as described, i.e., with opposite corners at (0,0) and (ai, ai). The squares are axis-aligned, and they include boundaries. So our reasoning holds.
But is there any scenario where the point (M,0) would be outside the allowed coordinate range? Upper bound 1e9, M can be up to 1e9, so okay. If M = 1e9 exactly, then (1e9,0) is within bounds. So fine.
Edge case: n=1, k=1. Then a = [a1]. Sorted ascending: [a1]. n-k = 0, M = a[0] = a1. Point (a1,0). Check: square a1 includes point (a1,0)? Since a1 >= a1? Yes, because x=a1 <= a1, y=0 <= a1, so belongs to the square. So count=1. Good.
What about n=1, k=1, a1 maybe 1e9, point (1e9,0) valid.
Now, what about the case where k=0? Not possible per constraints, but if it were, we could set M = max(ai)+1, but that may exceed 1e9? Possibly max(ai) could be 1e9, so M = 1e9+1 which exceeds limit. But since k≥1, not needed.
Thus algorithm: read n,k and array a of length n. If k > n: print -1. Else: sort a in non-decreasing order. Let M = a[n - k] (0-indexed). Print M and 0.
But is there any case where this fails? Let's test with sample2: n=3, k=1, a=[2,4,1] (sample: 3 1; 2 4 1). Sorted ascending: [1,2,4]. n-k=2, M = a[2] = 4. Output (4,0). Check: squares: a=1,2,4. Point (4,0): x=4,y=0. For a=1: 4≤1 false. a=2:4≤2 false. a=4:4≤4 true,0≤4 true -> count=1. So exactly 1. Sample output is (4,0) as well? Actually sample2 output: "4 0". So matches.
Sample3: n=4, k=50 -> k > n, output -1. matches.
Thus solution appears trivial. But wait: The problem says "It is guaranteed that all given squares are distinct." That means all ai are distinct. So our reasoning stands.
Thus answer is straightforward.
But I need to double-check: Are there any other conditions that could make the point invalid? For instance, the point must be integer and 0 ≤ x,y ≤ 10^9. Our point (M,0) meets that. For k=n, M = a[0] is positive (since ai >=1). So M >0, so (M,0) is within range. So fine.
But could there be a scenario where k=n and we output (a[0],0) but that point might belong to more than n squares? No, only n squares exist. So it's fine.
One more nuance: In the case where k=n, we could also output (0,0) which is maybe simpler, but our method works.
So overall solution: if k > n: -1; else sort a, take x = a[n-k], y = 0.
But wait: In 0-indexed, n-k could be 0 when k=n, so x = a[0], y=0. Works.
But we might also need to consider if there is any off-by-one: For k = n, a[n-k] = a[0] = min. That yields exactly n squares? Let's test with n=2, a=[1,2], k=2. M = a[0] = 1. Point (1,0): check squares: a=1: 1≤1,0≤1 -> in. a=2: 1≤2,0≤2 -> in. count=2. Good.
For k=1 with n=2, a=[1,2], sorted ascending [1,2], n-k=1, M=a[1]=2. (2,0): a=1: 2≤1 false; a=2: 2≤2 true,0≤2 true -> count=1. Good.
So logic is sound.
Thus problem is simple.
But wait, there is a catch: The squares are defined with points (0,0) and (ai, ai) as opposite corners. But does that necessarily mean the squares are axis-aligned and cover exactly the region [0,ai]x[0,ai]? Typically opposite corners means they are the lower left and upper right corners. But the problem statement says "points with coordinates (0, 0) and (ai, ai) are the opposite corners of the i-th square." That indicates these are the opposite corners, but it doesn't specify which ones are left/right, top/bottom. However, given that (0,0) is a common point, and (ai,ai) is another, the square is aligned with axes, but it could be that the other corners are (0,ai) and (ai,0) (i.e., axis-aligned). Usually "opposite corners" means diagonal corners, so the square's sides are parallel to the axes if the points are (0,0) and (ai,ai). Actually if the square is axis-aligned, those would be opposite corners. Could the square be rotated? For a square, opposite corners are two vertices that are not adjacent. Given two points, there are two possible squares that have them as opposite vertices: one axis-aligned (if sides are horizontal/vertical) and one rotated by 45 degrees. But in coordinate geometry problems, when they say "points with coordinates (0,0) and (ai, ai) are the opposite corners of the i-th square", usually they mean the square's sides are parallel to the axes? Not necessarily, but given that both coordinates of the second point are equal (ai,ai), it is common to assume it's axis-aligned (the square with vertices (0,0), (ai,0), (0,ai), (ai,ai)). Many problems use that phrasing. Let's read carefully: "points with coordinates (0, 0) and (ai, ai) are the opposite corners of the i-th square." In typical Codeforces problems, squares are often axis-aligned. But there is potential ambiguity: If they didn't specify orientation, it could be any square having those two points as opposite vertices. However, the phrase "drawn in this coordinate system" likely indicates that squares are aligned with axes? But we need to be sure.
Let's consider the possibility that squares could be rotated. If orientation is not specified, there are infinite squares with given diagonal endpoints, but typically in such problems they assume axis-aligned squares. Additionally, the sample input and output: Example1: n=4, k=3, a=[5,1,3,4]. Our method yields (3,0) as a solution, but the sample output is (2,1). But both are valid under axis-aligned assumption. However, what if squares are rotated? Let's test whether (2,1) belongs to exactly 3 rotated squares? That would be more complex. The problem would be much harder if squares are not axis-aligned. So it's safe to assume axis-aligned.
But let's double-check: Could the squares be oriented such that they are not axis-aligned? If they are rotated, then the set of points inside a square with opposite vertices (0,0) and (a_i, a_i) would be different. Typically the square's sides would be at 45 degrees if the vertices are (0,0) and (a_i, a_i) and the other vertices are (0, a_i) and (a_i,0) for axis-aligned. But if rotated, the square could have vertices ( (a_i/2, -a_i/2), (a_i/2, a_i/2), ... ) Actually the line connecting (0,0) and (a_i, a_i) is diagonal of the square. The center is (a_i/2, a_i/2). The other two vertices are (a_i/2 - (a_i/2)*sin45? Let's derive: For a square, the diagonal length is sqrt(2)*side. The distance between (0,0) and (a_i,a_i) is sqrt(2)a_i. So the side length would be a_i. That's interesting: distance between these two points is sqrt(2)a_i, so side length = a_i. So the square's side length is a_i. The vector from center to a vertex is (a_i/2, a_i/2) for the given vertices. The other two vertices are obtained by rotating that vector by 90 degrees: ( -a_i/2, a_i/2 ) and ( a_i/2, -a_i/2 ). That gives vertices: center + those: (0, a_i) and (a_i, 0). So indeed the square is axis-aligned! Because if the diagonal endpoints are (0,0) and (a_i, a_i), then the other two vertices are (0, a_i) and (a_i, 0) if sides are parallel to axes, OR they could be something else if the square is rotated? Let's examine: In a square, the diagonal is perpendicular to the other diagonal? Actually the diagonals are perpendicular only in a square? In a square, diagonals are perpendicular only if it's a rhombus? Actually in a square, the diagonals are perpendicular? Wait: In a square, diagonals are perpendicular only if it's a square? In a square, diagonals are perpendicular? Let's think: For a square with sides horizontal and vertical, the diagonal from (0,0) to (a,a) has slope 1, the other diagonal from (0,a) to (a,0) has slope -1, they are perpendicular. So yes. In a rotated square, the diagonal endpoints might also be (0,0) and (a,a) if the square is rotated by 45 degrees? Let's consider a square rotated 45 degrees. Its diagonal length is sidesqrt(2) as well. But if the diagonal endpoints are (0,0) and (a,a), then the center is (a/2,a/2). The other diagonal would be perpendicular to this one, so its direction vector would be (1,-1) (since the first diagonal direction is (1,1)). Its length is also sidesqrt(2)=a? Actually if side = s, diagonal = s√2. So s = a/√2. So the half-diagonal length is a/2? Actually the full diagonal length between (0,0) and (a,a) is a√2. So s = a (since a√2 = s√2 => s = a). Wait: a√2 = s√2 => s = a. So side length = a. So if we have a square with diagonal length a√2, the side length is a. But if the square is rotated, the coordinates of the vertices relative to the center are ±(s/√2, s/√2) along one diagonal, and ±(s/√2, -s/√2) along the other. For s = a, half-diagonal length = s/√2 = a/√2. So the vector from center to (0,0) is (-a/2? Actually center is at (a/2, a/2). To get to (0,0) we subtract (a/2, a/2) so the vertex vector from center is (-a/2, -a/2). Similarly the opposite vertex is (a/2, a/2). That's consistent with diagonal direction (1,1). For the other diagonal, the vertices are at center ± (a/2, -a/2) giving points (a/2 ± a/2, a/2 ∓ a/2) = (0, a) and (a, 0). So indeed the vertices are (0,a) and (a,0). That is the axis-aligned square. What about a rotated square? If we rotate the square about the center, the diagonal endpoints would still be (0,0) and (a,a) if we rotate? But if we rotate the square, the diagonal endpoints would not be (0,0) and (a,a) unless the rotation is multiple of 90 degrees? Actually consider a square with diagonal endpoints (0,0) and (a,a). That diagonal is fixed. The square is uniquely determined by the two diagonal endpoints because there is only one square (up to reflection) that has those as opposite vertices? Actually there are two squares that have a given line segment as diagonal: one with sides parallel to the coordinate axes (if the segment is at 45 degrees) and one with sides at 45 degrees to the axes? But is that true? Let's check: For a given diagonal, the square is determined by the fact that the other two vertices are located such that they are equidistant from the midpoint and the lines connecting them to the endpoints are perpendicular? Actually in a square, the diagonals are perpendicular and bisect each other. So given one diagonal (endpoints A and C), the other diagonal must be a line segment through the midpoint perpendicular to AC, with length equal to AC (since diagonals of a square are equal). So there are two choices for the direction (positive or negative orientation) but they both lie on the same line (the perpendicular bisector). So the other two vertices are uniquely determined (up to exchange). So given A and C, there is exactly one square (actually two squares that are reflections across the diagonal, but they are congruent? Actually if we consider the square as a set of points, there are two possible squares: one with vertices A, B, C, D in order; and the other with A, D', C, B'? Let's examine: In a square, the diagonals are perpendicular. Given diagonal AC, the other diagonal BD must be perpendicular to AC and have the same midpoint. So there are two possible directions (B and D can be swapped). But they produce the same set of vertices: B and D are just the two points on the perpendicular line at distance half the diagonal from the midpoint. So the set of vertices is uniquely determined: B and D are the two points on the perpendicular line at distance AC/2 from the midpoint. So the square is uniquely determined (up to naming of vertices). So there is exactly one square with given diagonal endpoints. So orientation is fixed by geometry. And we computed that the other vertices are (0,a) and (a,0) assuming AC is from (0,0) to (a,a). Let's verify that this is the only possibility: Diagonal AC has endpoints (0,0) and (a,a). Midpoint M = (a/2, a/2). The vector from M to A is (-a/2, -a/2). The other diagonal must be perpendicular to AC and have length equal to AC. A vector perpendicular to (1,1) is (1,-1) or (-1,1). The half-length of the diagonal is distance from M to B = half the diagonal length = AC/2 = (a√2)/2 = a/√2. So B = M + (a/2√? Actually we need a vector of length a/√2 in direction (1,-1) normalized. The unit vector in direction (1,-1) is (1/√2, -1/√2). Multiply by half-diagonal length a/√2 gives (a/2, -a/2). So B = (a/2 + a/2, a/2 - a/2) = (a, 0). D = M - that vector = (a/2 - a/2, a/2 + a/2) = (0, a). So indeed the vertices are (0,0), (a,0), (0,a), (a,a). So the square is axis-aligned. So the interpretation is unique: the square with opposite corners (0,0) and (a_i, a_i) is the axis-aligned square. So our assumption is correct.
Thus solution stands.
But is there any other subtlety? The problem says "0 ≤ x, y ≤ 109". So we cannot output negative coordinates, which we are not.
Edge cases: When n=50, k=50, all ai distinct. Then we output a[0] (the smallest). That is ≤ 1e9. That's fine.
Now, we must also consider the possibility that the point we output (M, 0) might belong to exactly k squares but also need to ensure that x and y are within 0..1e9. M could be 0? For k=n, M could be 0 but we output a[0] which is >=1. But what about k < n? M = a[n-k] can be 0? Since ai >= 1, a[n-k] >= 1, so not zero. So x is positive.
But what about the case where M = a[n-k] but maybe M = 0? Only if some ai=0, but ai≥1, so no.
Thus algorithm is simple.
Let's implement: read n,k; list a; if k > n: print(-1); else sort a; x = a[n-k]; print(x, 0).
Wait: There is one more nuance: The point (x,0) with x = a[n-k] will have max(x,y) = x. For k=n, x = a[0], max = a[0] which is min. Count of ai >= a[0] = n. For k less than n, as argued, count = number of ai >= a[n-k]. Since a[n-k] is the smallest among the k largest, all k largest are >= that value, and the rest are < that value because distinct. So count = k. Perfect.
But need to be careful: if there are duplicates? But problem guarantees distinct squares, so ai distinct. So okay.
Now let's test with more cases: n=5, a = [1,2,3,4,5], k=2. n-k=3, a[3]=4, output (4,0). Count of ai >=4: 4,5 -> 2. Good.
What about k=0? Not allowed.
What about k > n? -1.
Seems solved.
But wait: Could there be a scenario where we need to output -1 even if k <= n? Possibly if the point coordinates must be integer and within [0,10^9], but what if a[n-k] is 0? Not possible. Or if we need to output a point that belongs to exactly k squares, but maybe using M = a[n-k] yields that but there is an edge when M is exactly equal to some a_i that is the (k+1)-th largest? But we need exactly k squares. Since a_i are distinct, if M = a[n-k] (which is the k-th largest), then all squares with ai >= M are exactly k (since those with ai > M are also >= M, but there are k-1 larger ones? Let's analyze: sorted ascending: indices 0..n-1. Let index p = n-k. Then a[p] is the smallest among the k largest. Since all a_i are distinct, any a_i > a[p] is among the larger ones, and there are exactly k-1? Wait, we need to verify: The number of elements greater than a[p] is (n-1) - p = (n-1) - (n-k) = k-1. So elements strictly greater than a[p] are k-1. Including a[p] itself, we have k elements that are >= a[p] (since all greater and equal). So indeed count of ai >= a[p] = (number > a[p]) + 1 = (k-1)+1 = k. So correct.
Thus solution works.
But there is an alternative: Could there be a point (x,y) with max(x,y) = M such that M is not integer? No, M must be integer because we need integer coordinates. So M integer.
Now, is there any possibility that there exists a point with max(x,y) = M that yields count exactly k, but M is not within the range of a values? For example, if we set M = 2.5, not integer. So we consider integer M. Our method uses M = a[n-k] which is integer. So fine.
But is there any possibility that the answer requires both coordinates to be ≤ 10^9 and we must output x and y both within that range? M could be up to 10^9, so fine. (M,0) works.
Now let's also consider if the point (M,0) is allowed when M = 0? Could be if we choose M=0 for k=n, but we didn't.
Alright.
But wait: The problem statement says "If there are multiple answers, you are allowed to print any of them." So our answer (M,0) is acceptable.
One more check: In sample1, they output (2,1) which has max=2. Our output would be (3,0) which is different but also correct. So it's fine.
Thus the solution is straightforward.
But I see a potential pitfall: The count of squares containing (x,y) = number of i with ai >= max(x,y). However, we must also consider that the square includes boundaries, so x can equal ai and y can equal ai, as we did. That's fine.
Now, what if k = 0? Not allowed, ignore.
Thus code is trivial.
We'll write solution: read n,k, list a. If k>n: print -1; else sort a; x = a[n-k]; print x,0.
But we need to be careful about 0-indexing. In Python, after sorting a, we can do x = a[n-k].
Edge: if k = n, n-k = 0, a[0] is the smallest, x = smallest, y=0.
Now, double-check with n=1,k=1: a[0] = a1, output a1,0 works.
Now, we should also consider the scenario where k < n but maybe the point (M,0) yields exactly k squares? Yes.
Thus answer.
Wait: But is there a scenario where M = a[n-k] leads to count = k, but the point (M,0) might have x coordinate > 10^9? Since a_i ≤ 1e9, so M ≤ 1e9. So safe.
Now, the problem also says "0 ≤ x, y ≤ 10^9". Our y=0 is within range.
Thus done.
But let's test with a random case: n=4, a=[1,10,100,1000], k=2. Sorted ascending: [1,10,100,1000]; n-k=2; M=a[2]=100; point (100,0). Count of ai>=100: 100 and 1000 =>2. Works.
Now test boundary: n=50, all a_i large up to 1e9. Works.
Thus answer is as above.
But I want to be thorough: Could there be a scenario where M = a[n-k] works but maybe (M,0) is on the boundary of some squares, but that's fine. So all good.
Therefore final solution: if k > n, print -1; else sort a, output a[n-k] and 0.
But we must also consider that we must print two integers. So that's it.
Now, some might argue that there could be alternative answers but we have found one.
Now let's confirm with sample2: n=3,k=1, a=[2,4,1], sorted [1,2,4]; n-k=2 => a[2]=4; output 4 0 matches sample.
Sample3: n=4,k=50 => 50>4 => -1.
Thus correct.
Now, this seems too easy for a Codeforces problem? Let's see the problem origin: Possibly from Codeforces Round #... but it's indeed simple. Actually many participants might have overthought it. But it's correct.
Let's double-check the problem statement: "Vasya has found a piece of paper with a coordinate system written on it. There are n distinct squares drawn in this coordinate system. Let's number the squares with integers from 1 to n. It turned out that points with coordinates (0, 0) and (ai, ai) are the opposite corners of the i-th square." So squares are drawn. There is no rotation, they are axis-aligned as typical.
One more nuance: The squares might be drawn overlapping? But that doesn't affect counting.
Now, could there be a scenario where the point (M,0) is not inside some squares because of something like squares are oriented differently? Already considered.
Thus final solution is as described.
We'll code in C++ for speed.
Implementation steps:
- Read n, k.
- Read n integers into vector a.
- If k > n: cout << -1 << endl; return 0;
- sort(a.begin(), a.end());
- int x = a[n - k];
- cout << x << " " << 0 << endl;
But careful: n and k up to 50, but a_i up to 1e9, so x fits in int? 1e9 fits in 32-bit signed int? 1e9 < 2^31-1 (2,147,483,647), so int works. But in C++ int is usually 32-bit, safe. But to be safe use long long or int64? However constraints within int. But we can use long long to be safe.
Now, could there be a case where n-k is negative? If k>n we already handled. So n-k >=0.
Now, output 0 as y.
But consider if we output (0,0) for k=n? That would also work and might be simpler. But our method works.
Edge case: If k=0 (not allowed) we would need M such that count of ai >= M = 0. That would require M > max(ai). But max(ai) could be 1e9, then M = 1e9+1 > 1e9, not allowed because x must be <=1e9. So k=0 might be impossible if max(ai)=1e9? Actually if max(ai) < 1e9, we could set M = max(ai)+1 <= 1e9? Possibly if max(ai) < 1e9, then M = max(ai)+1 ≤ 1e9, and point (M,0) works. But k=0 not needed.
So final answer is as above.
Now, before finalizing, I'll think about alternative possibilities: Could there be points where max(x,y) is not the only factor? Actually we reasoned that point belongs to square i iff x <= ai and y <= ai. So indeed count = number of ai >= max(x,y). So correct.
Thus solution is O(n log n) for sorting, trivial.
Now, the problem may have been intended for a more complex scenario? Let's see example: In example1, they output (2,1). Their answer uses coordinates not just (M,0). So they allowed any.
One more nuance: The point (M,0) may have x = M and y = 0. However, is it possible that this point lies exactly on the boundary of some square but we don't want to consider that as belonging? No, it belongs.
Thus done.
Now, we must produce final answer.
But let's also test a case where a values are not distinct? The problem says distinct squares, so they are distinct, but it does not explicitly say that the ai are distinct? If squares are distinct, then the side lengths are distinct, because if two squares have same ai, they would be identical (since both corners (0,0) and (ai,ai) define the same square). So yes, ai are distinct.
But what if they are not? The guarantee says distinct squares, so they are distinct, implying ai distinct.
Thus we rely on distinctness to guarantee that for M = a[n-k], the count equals exactly k. If there were duplicates, then M = a[n-k] might give count > k if there are multiple squares with same side length. Example: a = [2,2] duplicates, n=2, k=1. Sorted: [2,2]. n-k=1, a[1]=2. Count of ai >=2 = both, so count=2, not 1. So duplicates would cause trouble. But guarantee ensures distinct.
Thus solution works.
Now, let's produce final code.
But wait: The problem statement says "0 ≤ x, y ≤ 10^9". So we cannot output negative numbers. Good.
Now, we can also handle k=n by outputting (0,0) which is simpler: (0,0) works because max=0, count = n (since all ai >=0). That is also valid. Actually check: (0,0) always belongs to all squares because x=0<=ai, y=0<=ai. So count = n. So for k=n, we could output (0,0). But our method using a[0] also works. However, we need to ensure that (0,0) is within allowed range, yes.
But note: If we output (0,0) for k=n, it's fine. But if we output (a[0],0) also fine. We'll stick with general formula for simplicity.
Now, one more edge: For k=0? Not needed.
Thus solution.
Now, I'll implement in C++.
Pseudo:
#include
#include
#include
using namespace std;
int main() {
int n, k;
cin >> n >> k;
vector a(n);
for (int i = 0; i < n; ++i) cin >> a[i];
if (k > n) {
cout << -1 << endl;
return 0;
}
sort(a.begin(), a.end());
long long x = a[n - k];
cout << x << " " << 0 << endl;
return 0;
}
But we need to ensure that x is within [0,1e9]. Since a[i] <= 1e9, it's fine.
Now, what if n - k = 0? Then x = a[0] = smallest, works.
Now, test with sample1: n=4, k=3, a=[5,1,3,4] => sorted [1,3,4,5]; n-k=1; x=a[1]=3; output "3 0". That is accepted, but sample output is "2 1". Both are acceptable.
Now, confirm that for n=4, k=3, (3,0) indeed yields count=3. Yes.
Thus we can produce.
But the problem statement might have something like "If there are multiple answers, you are allowed to print any of them." So fine.
Thus final answer.
But I wonder if there is any catch: The squares are drawn, and points inside squares count. For a point (x,y) with x= a[n-k] and y=0, is it possible that this point lies exactly on the boundary of a square with a_i = a[n-k]? Yes. That square contains it. But are there any squares with a_i exactly equal to that value? Yes, one. So that's fine.
Now, also we must consider if (M,0) is valid when M=0? In k=n case, we could output (0,0). But our method uses a[0] >0, so M>0. That's also fine. But if a[0] = 10^9, M = 10^9, point (10^9, 0) is valid. So fine.
Thus answer.
Now, as a final check, I'll think about potential misinterpretation: Could the squares be defined with opposite corners (0,0) and (ai, ai) but maybe the square could be rotated 45 degrees? As we reasoned, the square is uniquely defined by two opposite vertices; the other two vertices are determined. Given (0,0) and (a_i, a_i), the square will have vertices (0,0), (a_i,0), (0,a_i), (a_i,a_i) if the sides are axis-aligned. But is that the only square? Actually, given two opposite vertices of a square, there are infinitely many squares in 3D? But in the plane, there are two possible squares: one with sides parallel to axes and one rotated by 90 degrees? Wait, given a line segment as diagonal, the square can have that segment as diagonal. The center is the midpoint. The other diagonal is perpendicular and of same length. There are two possible orientations for the other diagonal: one at +90°, one at -90°. But both result in the same set of vertices? Actually they result in the same two points? Let's compute: The perpendicular direction vectors are (1,-1) and (-1,1) which are opposite directions, but they define the same line: the line through the midpoint with direction (1,-1). The two possible positions for the other two vertices are symmetric: one is B = M + v, D = M - v; the other orientation would be B' = M - v, D' = M + v, which is just swapping labels. So the set of vertices is the same: {A, B, C, D} where B and D are those two points. So the square is uniquely determined. Thus it's axis-aligned? Actually we got B = (a,0) and D = (0,a). That's axis-aligned. So the square's sides are horizontal and vertical? Let's check: Points: (0,0), (a,0), (a,a), (0,a) form a square with sides horizontal and vertical. So indeed it's axis-aligned. So no ambiguity.
Thus solution stands.
Therefore final answer is as described.
Now, I'll produce a detailed explanation for solution: Since each square i includes all points (x,y) with 0 ≤ x ≤ ai and 0 ≤ y ≤ ai, a point (x,y) belongs to square i iff max(x,y) ≤ ai. Thus the number of squares containing (x,y) equals the count of ai ≥ max(x,y). Therefore we just need to find an integer M such that exactly k ai's are ≥ M. Since all ai are distinct, such M exists iff k ≤ n. Choose M = the k-th largest ai (or equivalently the (n-k)-th smallest). Then point (M,0) works. If k > n, output -1.
Implementation details: sort array a ascending, set x = a[n-k] (if 0-indexed), output x and 0.
But note: For k = n, n-k = 0, so x = a[0] is the smallest ai. That yields max = a[0]; count of ai ≥ a[0] = n, so works. Alternatively, we could output (0,0) but not needed.
Now produce final answer.
Also consider that coordinates must be ≤ 1e9; a_i ≤ 1e9, so M ≤ 1e9, fine.
Thus final solution.
Now, in the code, careful with integer types: use long long if using C++ to avoid overflow, but a_i up to 1e9 fits in int, but safe to use long long.
We'll write.
Now, confirm with some random test: n=1, k=1, a=[1000000000]. Output (1000000000, 0). Check: max=1000000000, count of ai >= 1e9 = 1, ok. Coordinates within bound.
n=1, k=0 not allowed.
n=2, k=2, a=[1,2]. Output (1,0). Check: max=1, count of ai >=1 = 2? 1>=1, 2>=1 => 2, correct.
n=2, k=1, a=[1,2]. Output (2,0). Count: max=2, ai >=2: only 2 =>1.
Thus fine.
Now, any case where we cannot use (M,0) because M=0? Not needed because for k=n, we use a[0]>0. But what about a[0]=0? But ai ≥ 1, so no.
Now, the problem might require "x and y (0 ≤ x, y ≤ 10^9)". Our y=0 is allowed.
Thus final solution.
Time complexity: O(n log n) due to sorting, trivial.
Memory: O(n).
Now, answer.
But is there any possibility that the answer exists but our method fails? For instance, if k = n, we output (a[0],0) where a[0] is smallest. But consider if a[0] = 1, (1,0) works. If a[0] is huge, it's fine.
Now, maybe there is a scenario where the point (M,0) lies on the boundary of the square with ai = M, and the problem might consider boundary as belonging, so fine.
Thus final.
I think that's the solution.
Now, I'll produce the final answer accordingly. ```cpp
#include
#include
#include
using namespace std;
int main() {
int n, k;
cin >> n >> k;
vector a(n);
for (int i = 0; i < n; ++i)
cin >> a[i];
// No point can belong to more squares than exist
if (k > n) {
cout << -1 << endl;
return 0;
}
sort(a.begin(), a.end());
// a[0] < a[1] < ... < a[n-1] (distinct)
int x = a[n - k];
cout << x << " " << 0 << endl;
return 0;
}