-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.java
More file actions
49 lines (46 loc) · 1.55 KB
/
Copy pathSolution.java
File metadata and controls
49 lines (46 loc) · 1.55 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
41
42
43
44
45
46
47
48
49
import java.util.*;
import java.util.stream.*;
public class Solution {
public static long solve(List<List<Integer>> events) {
Map<String, Long> costs = new HashMap<>();
long totalCost = 0;
for (List<Integer> event : events) {
int type = event.get(0);
if (type == 1) {
int from = event.get(1);
int to = event.get(2);
int fee = event.get(3);
String key = from + "-" + to;
costs.put(key, costs.getOrDefault(key, 0L) + fee);
} else if (type == 2) {
int from = event.get(1);
int to = event.get(2);
String key = from + "-" + to;
totalCost += costs.getOrDefault(key, 0L);
}
}
return totalCost;
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int q = scan.nextInt();
List<List<Integer>> a = new ArrayList<>();
for (int i = 0; i < q; i++) {
List<Integer> row = new ArrayList<>();
int type = scan.nextInt();
row.add(type);
if (type == 1) {
row.add(scan.nextInt());
row.add(scan.nextInt());
row.add(scan.nextInt());
} else if (type == 2) {
row.add(scan.nextInt());
row.add(scan.nextInt());
}
a.add(row);
}
long result = solve(a);
System.out.println(result);
scan.close();
}
}