-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaxFinalValue.java
More file actions
28 lines (22 loc) · 819 Bytes
/
MaxFinalValue.java
File metadata and controls
28 lines (22 loc) · 819 Bytes
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
import java.util.PriorityQueue;
import java.util.Scanner;
public class MaxFinalValue {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int t = scanner.nextInt(); // number of test cases
for (int i = 0; i < t; i++) {
int n = scanner.nextInt(); // length of the array
PriorityQueue<Integer> pq = new PriorityQueue<>();
for (int j = 0; j < n; j++) {
pq.add(scanner.nextInt());
}
while (pq.size() > 1) {
int x = pq.poll();
int y = pq.poll();
pq.add((x + y) / 2);
}
System.out.println(pq.poll()); // the final element
}
scanner.close();
}
}