|
| 1 | +```java |
| 2 | +import java.io.*; |
| 3 | +import java.util.StringTokenizer; |
| 4 | + |
| 5 | +public class BJ_14719_빗물 { |
| 6 | + |
| 7 | + private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 8 | + private static final BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); |
| 9 | + private static StringTokenizer st; |
| 10 | + |
| 11 | + private static int H, W; |
| 12 | + private static int[] height; |
| 13 | + |
| 14 | + public static void main(String[] args) throws IOException { |
| 15 | + init(); |
| 16 | + sol(); |
| 17 | + } |
| 18 | + |
| 19 | + private static void init() throws IOException { |
| 20 | + st = new StringTokenizer(br.readLine()); |
| 21 | + H = Integer.parseInt(st.nextToken()); |
| 22 | + W = Integer.parseInt(st.nextToken()); |
| 23 | + |
| 24 | + height = new int[W]; |
| 25 | + st = new StringTokenizer(br.readLine()); |
| 26 | + for (int i = 0; i < W; i++) { |
| 27 | + height[i] = Integer.parseInt(st.nextToken()); |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + private static void sol() throws IOException { |
| 32 | + int left = 0, right = W - 1; |
| 33 | + int leftMax = 0, rightMax = 0; |
| 34 | + int result = 0; |
| 35 | + |
| 36 | + while (left < right) { |
| 37 | + if (height[left] < height[right]) { |
| 38 | + if (height[left] >= leftMax) { |
| 39 | + leftMax = height[left]; |
| 40 | + } else { |
| 41 | + result += leftMax - height[left]; |
| 42 | + } |
| 43 | + left++; |
| 44 | + } else { |
| 45 | + if (height[right] >= rightMax) { |
| 46 | + rightMax = height[right]; |
| 47 | + } else { |
| 48 | + result += rightMax - height[right]; |
| 49 | + } |
| 50 | + right--; |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + bw.write(String.valueOf(result)); |
| 55 | + bw.flush(); |
| 56 | + bw.close(); |
| 57 | + br.close(); |
| 58 | + } |
| 59 | + |
| 60 | +} |
| 61 | +``` |
0 commit comments