-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathArray_and_operations.java
More file actions
53 lines (40 loc) · 1.68 KB
/
Copy pathArray_and_operations.java
File metadata and controls
53 lines (40 loc) · 1.68 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
50
51
52
53
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder output = new StringBuilder();
int t = Integer.parseInt(br.readLine().trim());
while (t-- > 0) {
int n = Integer.parseInt(br.readLine().trim());
long[] a = Arrays.stream(br.readLine().split(" "))
.mapToLong(Long::parseLong)
.toArray();
long[] b = Arrays.stream(br.readLine().split(" "))
.mapToLong(Long::parseLong)
.toArray();
// Pair dishes and sort by courier time
long[][] dishes = new long[n][2];
for (int i = 0; i < n; i++) {
dishes[i][0] = a[i]; // courier time
dishes[i][1] = b[i]; // pickup time
}
Arrays.sort(dishes, Comparator.comparingLong(d -> d[0]));
// Suffix sum of pickup times
long[] suffix = new long[n + 1];
for (int i = n - 1; i >= 0; i--) {
suffix[i] = suffix[i + 1] + dishes[i][1];
}
// Case: pick all yourself
long answer = suffix[0];
// Try delivering first i dishes
for (int i = 0; i < n; i++) {
long courierTime = dishes[i][0];
long pickupTime = suffix[i + 1];
answer = Math.min(answer, Math.max(courierTime, pickupTime));
}
output.append(answer).append('\n');
}
System.out.print(output);
}
}