-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
35 lines (34 loc) · 1.03 KB
/
Main.java
File metadata and controls
35 lines (34 loc) · 1.03 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
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
}
int[] left = new int[n];
int[] right = new int[n];
left[0] = arr[0];
right[n - 1] = arr[n - 1];
for (int i = 1; i < n; i++) {
left[i] = Math.max(arr[i], left[i - 1]);
}
for (int i = n - 2; i >= 0; i--) {
right[i] = Math.max(arr[i], right[i + 1]);
}
// for (int i = 0; i < n; i++) {
// System.out.print(left[i] + " ");
// }
// System.out.println();
// for (int i = 0; i < n; i++) {
// System.out.print(right[i] + " ");
// }
// System.out.println();
int water = 0;
for (int i = 0; i < n; i++) {
water += Math.min(left[i], right[i]) - arr[i];
}
System.out.print(water);
}
}