-
Notifications
You must be signed in to change notification settings - Fork 0
/
perfect.java
57 lines (53 loc) · 1.9 KB
/
perfect.java
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import java.util.*;
public class perfect {
static class Pair{
int mins;
String skills;
Pair(int m, String s){
mins = m;
skills = s;
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
for(int i=1; i<=t; i++){
int n = sc.nextInt();
PriorityQueue<Pair> minh = new PriorityQueue<>((a,b) -> {
return a.mins - b.mins;
});
for(int j=1; j<=n; j++){
int m = sc.nextInt();
String s = sc.next() + sc.nextLine();
minh.add(new Pair(m, s));
}
int count1 = Integer.MAX_VALUE, count2 = Integer.MAX_VALUE, count3 = Integer.MAX_VALUE;
while(!minh.isEmpty()){
Pair item = minh.poll();
if(item.skills.equals("11")){
count1 = Math.min(count1, item.mins);
}
else if(item.skills.equals("01")){
count2 = Math.min(count2, item.mins);
}
else if(item.skills.equals("10")){
count3 = Math.min(count3, item.mins);
}
}
int minTime = 0;
if(count1 != Integer.MAX_VALUE && count2 != Integer.MAX_VALUE && count3 != Integer.MAX_VALUE){
minTime = Math.min(count1, count2 + count3);
}
else if(count1 == Integer.MAX_VALUE && count2 != Integer.MAX_VALUE && count3 != Integer.MAX_VALUE){
minTime = count2 + count3;
}
else if(count1 != Integer.MAX_VALUE && (count2 == Integer.MAX_VALUE || count3 == Integer.MAX_VALUE)){
minTime = count1;
}
else{
minTime = -1;
}
System.out.println(minTime);
}
}
}