-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFestivals.java
executable file
·52 lines (52 loc) · 1.22 KB
/
Festivals.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
import java.io.*;
import java.util.*;
class node {
static String str;
static int val;
node (String str, int val) {
node.str = str;
node.val = val;
}
}
class Festival {
static int t, n, val, sz;
static String str, res;
public static void main (String args[]) throws Exception{
Scanner ss = new Scanner (System.in);
t = ss.nextInt();
while (t > 0) {
HashMap <String, ArrayList<Integer> > mp = new HashMap <> ();
t--;
n = ss.nextInt ();
for (int i = 0; i < n; ++i) {
str = ss.next ();
val = ss.nextInt ();
mp.putIfAbsent(str, new ArrayList<Integer>());
mp.get(str).add(val);
}
long ans = 0, sum;
res = "ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ";
for (Map.Entry entry : mp.entrySet()){
str = (String)entry.getKey();
ArrayList<Integer> arr = mp.get(str);
Collections.sort (arr);
sum = 0;
sz = arr.size();
for(int j = sz - 1; j >= (sz - 3) && j >= 0; --j)
sum += arr.get(j);
if(sum > ans){
ans = sum;
res = str;
}
else if (sum == ans) {
if (str.compareTo(res) < 0) {
ans = sum;
res = str;
}
}
//System.out.println("Result " + ans + " " + res + " "+ arr.size ());
}
System.out.println(res + " " + ans);
}
}
}