-
Notifications
You must be signed in to change notification settings - Fork 75
Expand file tree
/
Copy pathCodeforces_0977C_Less_or_Equal.java
More file actions
40 lines (38 loc) · 1.13 KB
/
Codeforces_0977C_Less_or_Equal.java
File metadata and controls
40 lines (38 loc) · 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// AC: 888 ms
// Memory: 5500 KB
// TreeMap || sort.
// T:O(n), S:O(n)
//
import java.util.Scanner;
import java.util.TreeMap;
public class Codeforces_0977C_Less_or_Equal {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt(), k = sc.nextInt(), curRank = 0;
TreeMap<Integer, Integer> record = new TreeMap<>();
for (int i = 0; i < n; i++) {
record.merge(sc.nextInt(), 1, Integer::sum);
}
for (int key : record.keySet()) {
if (k == 0) {
if (key <= 1) {
System.out.println("-1");
return;
} else {
System.out.println(key - 1);
return;
}
} else {
curRank += record.get(key);
if (curRank == k) {
System.out.println(key);
return;
} else if (curRank > k) {
System.out.println("-1");
return;
}
}
}
System.out.println("-1");
}
}