-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbaekjoon_14719.java
More file actions
64 lines (41 loc) · 1.5 KB
/
baekjoon_14719.java
File metadata and controls
64 lines (41 loc) · 1.5 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
54
55
56
57
58
59
60
61
62
63
64
import java.util.*;
import java.io.*;
public class baekjoon_14719 {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int H = Integer.parseInt(st.nextToken());
int W = Integer.parseInt(st.nextToken());
int[] arr = new int[W];
st = new StringTokenizer(br.readLine());
for(int i=0;i<W;i++) {
arr[i] = Integer.parseInt(st.nextToken());
}
int left = 0, right = 0;
int result = 0;
for(int i=0;i<W;i++) {
int leftMax=0, rightMax=0;
for(int l =0;l < i;l++) {
leftMax = Math.max(arr[l], leftMax);
}
for(int r = i + 1; r < W ; r++) {
rightMax = Math.max(arr[r], rightMax);
}
int min = Math.min(leftMax, rightMax);
if(min > arr[i]) result += min - arr[i];
}
result += calcWater(left, right, arr);
System.out.println(result);
}
public static int calcWater(int left, int right, int[] arr) {
if(left == right || right - left < 2 ) {
return 0;
}
int height = Math.min(arr[left], arr[right]);
int result = 0;
for(int i=left + 1;i<right;i++) {
result += height - arr[i];
}
return result;
}
}