private static int countStrokes(int[] f, int l, int r, int h) {
        if (l > r) {
            return 0;
        }

        int minH = f[l];
        for (int i = l + 1; i <= r; i++) {
            minH = Math.min(minH, f[i]);
        }

        int sol = minH - h;

        int i = l;
        while (i <= r) {
            while (i <= r && f[i] - minH == 0) {
                i++;
            }
            int newL = i;

            while (i <= r && f[i] - minH != 0) {
                i++;
            }
            int newR = i - 1;
            sol += countStrokes(f, newL, newR, minH);
        }

        return Math.min(sol, r - l + 1);
    }