-
Notifications
You must be signed in to change notification settings - Fork 0
/
kattissquest.java
55 lines (48 loc) · 2.2 KB
/
kattissquest.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
import java.io.*;
import java.util.*;
public class Kattissquest {
public static void main(String[] args) throws IOException {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
PrintWriter writer = new PrintWriter(System.out);
TreeMap<Integer, PriorityQueue<Integer>> map = new TreeMap<>();
StringBuilder str = new StringBuilder();
long totalGold = 0;
int N = Integer.parseInt(bf.readLine());
for (int i = 0; i < N; i++) {
String[] separateInput = bf.readLine().split(" ");
String command = separateInput[0];
if(command.equals("add")) {
int energy = Integer.parseInt(separateInput[1]);
int gold = Integer.parseInt(separateInput[2]);
if(map.containsKey(energy)) {
map.get(energy).add(gold);
} else {
//priorityque should be in descending order
PriorityQueue<Integer> goldQueue = new PriorityQueue<>(Collections.reverseOrder());
goldQueue.add(gold);
map.put(energy, goldQueue);
}
} else if(command.equals("query")) {
int energySes = Integer.parseInt(separateInput[1]);
//floorEntery gets key-value mapping associated with the least key strictly greater than the given key
// or null if there is no such key.
Map.Entry<Integer, PriorityQueue<Integer>> entry = map.floorEntry(energySes);
while(entry != null) {
totalGold += entry.getValue().poll();
if(entry.getValue().isEmpty()) {
map.remove(entry.getKey());
}
energySes -= entry.getKey();
//do this untill the key are all > energySes -> (entry == NULL)
entry = map.floorEntry(energySes);
}
str.append(totalGold + "\n");
totalGold = 0;
}
}
writer.print(str);
bf.close();
writer.flush();
writer.close();
}
}