Language: C++

import java.util.*;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int[] h = new int[n];
		for(int i = 0; i < n; i++) {
			h[i] = sc.nextInt();
		}
		Garden garden = new Garden(n,h);
		while(garden.gardenFlag()) {
			garden.watering();
		}

		System.out.println(garden.getWaterCount());
		sc.close();
	}
}
class Garden{

	int n;
	int[] f;
	int[] h;
	int waterCount;

	Garden(int n, int[] h){
		this.n = n;
		this.h = h;
		f = new int[n];
		waterCount = 0;
	}
	//waterCount取得
	int getWaterCount() {
		return waterCount;
	}

	//判別
	boolean gardenFlag() {
		boolean flag = false;
		for(int i = 0; i < n; i++) {
			if(f[i] != h[i]) {
				flag = true;//異なっていたら続行
				break;
			}
		}
		return flag;
	}

	//開始地点
	int startPoint() {
		int sp = 0;
		for(int i = 0; i < n; i++) {
			if(f[i] < h[i]) {
				sp = i;
				break;
			}
		}
		System.out.print(sp + " ");
		return sp;
	}
	//終了地点
	int[] endPoint() {
		int[] point = new int[2];
		point[0] = startPoint();
		point[1] = point[0] + 1;

		for(int i = point[0] + 1; i < n; i++) {
			if(f[i] < h[i]) {
				point[1]++;
			}
			if(f[i] == h[i]) {
				break;
			}
		}
		System.out.println(point[1]);
		return point;
	}
	//水やり
	void watering() {
		int[] point = endPoint();
		for(int i = point[0]; i < point[1]; i++) {
			f[i]++;
		}
		waterCount++;

		for(int i = 0; i < n; i++) {
			System.out.print(f[i] + " ");
		}
		System.out.println();
	}
}


Compiler output:
a.cc:1:1: error: 'import' does not name a type
    1 | import java.util.*;
      | ^~~~~~
a.cc:1:1: note: C++20 'import' only available with '-fmodules-ts'
a.cc:3:1: error: expected unqualified-id before 'public'
    3 | public class Main {
      | ^~~~~~
a.cc:23:12: error: expected unqualified-id before '[' token
   23 |         int[] f;
      |            ^
a.cc:24:12: error: expected unqualified-id before '[' token
   24 |         int[] h;
      |            ^
a.cc:27:29: error: expected ',' or '...' before 'h'
   27 |         Garden(int n, int[] h){
      |                             ^
a.cc:39:9: error: 'boolean' does not name a type; did you mean 'bool'?
   39 |         boolean gardenFlag() {
      |         ^~~~~~~
      |         bool
a.cc:63:12: error: expected unqualified-id before '[' token
   63 |         int[] endPoint() {
      |            ^
a.cc:92:2: error: expected ';' after class definition
   92 | }
      |  ^
      |  ;
a.cc: In constructor 'Garden::Garden(int, int*)':
a.cc:28:22: error: request for member 'n' in '(Garden*)this', which is of pointer type 'Garden*' (maybe you meant to use '->' ?)
   28 |                 this.n = n;
      |                      ^
a.cc:29:22: error: request for member 'h' in '(Garden*)this', which is of pointer type 'Garden*' (maybe you meant to use '->' ?)
   29 |                 this.h = h;
      |                      ^
a.cc:29:26: error: 'h' was not declared in this scope
   29 |                 this.h = h;
      |                          ^
a.cc:30:17: error: 'f' was not declared in this scope
   30 |                 f = new int[n];
      |                 ^
a.cc: In member function 'int Garden::startPoint()':
a.cc:54:28: error: 'f' was not declared in this scope
   54 |                         if(f[i] < h[i]) {
      |                            ^
a.cc:54:35: error: 'h' was not declared in this scope
   54 |                         if(f[i] < h[i]) {
      |                                   ^
a.cc:59:17: error: 'System' was not declared in this scope
   59 |                 System.out.print(sp + " ");
      |                 ^~~~~~
a.cc: In member function 'void Garden::watering()':
a.cc:81:20: error: structured binding declaration cannot have type 'int'
   81 |                 int[] point = endPoint();
      |                    ^~
a.cc:81:20: note: type must be cv-qualified 'auto' or reference to cv-qualified 'auto'
a.cc:81:20: error: empty structured binding declaration
a.cc:81:23: error: expected initializer before 'point'
   81 |                 int[] point = endPoint();
      |                       ^~~~~
a.cc:82:29: error: 'point' was not declared in this scope; did you mean 'int'?
   82 |                 for(int i = point[0]; i < point[1]; i++) {
      |                             ^~~~~
      |                             int
a.cc:83:25: error: 'f' was not declared in this scope
   83 |                         f[i]++;
      |                         ^
a.cc:88:25: error: 'System' was not declared in this scope
   88 |                         System.out.print(f[i] + " ");
      |                         ^~~~~~
a.cc:88:42: error: 'f' was not declared in this scope
   88 |                         System.out.print(f[i] + " ");
      |                                          ^
a.cc:90:17: error: 'System' was not declared in this scope
   90 |                 System.out.println();
      |                 ^~~~~~