File tree Expand file tree Collapse file tree 1 file changed +41
-0
lines changed
Expand file tree Collapse file tree 1 file changed +41
-0
lines changed Original file line number Diff line number Diff line change 1+ ```java
2+ import java.io.*;
3+ import java.util.*;
4+
5+ public class Main {
6+ public static void main(String[] args) throws IOException {
7+ BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
8+ StringTokenizer st = new StringTokenizer(br.readLine());
9+
10+ int H = Integer.parseInt(st.nextToken());
11+ int W = Integer.parseInt(st.nextToken());
12+
13+ int[] blocks = new int[W];
14+ st = new StringTokenizer(br.readLine());
15+ for (int i = 0; i < W; i++) {
16+ blocks[i] = Integer.parseInt(st.nextToken());
17+ }
18+
19+ int totalWater = 0;
20+
21+ for (int i = 1; i < W - 1; i++) {
22+ int leftMax = 0;
23+ for (int j = 0; j < i; j++) {
24+ leftMax = Math.max(leftMax, blocks[j]);
25+ }
26+
27+ int rightMax = 0;
28+ for (int j = i + 1; j < W; j++) {
29+ rightMax = Math.max(rightMax, blocks[j]);
30+ }
31+
32+ int waterLevel = Math.min(leftMax, rightMax);
33+ if (waterLevel > blocks[i]) {
34+ totalWater += waterLevel - blocks[i];
35+ }
36+ }
37+
38+ System.out.println(totalWater);
39+ }
40+ }
41+ ```
You can’t perform that action at this time.
0 commit comments