program prob5
    implicit none
    integer :: n, ans
    integer, allocatable :: h(:)
    read(*,*) n
    allocate(h(n))
    read(*,*) h

    ans = 0
    call solve(1, n)
    write(*,*) ans

    deallocate(h)

    stop
contains
recursive subroutine solve(left, right)
    implicit none
    integer :: i, mi
    integer :: left, right
    if(left == right) then
        ans = ans + h(left)
        return
    end if

    mi = minval(h(left:right))
    ans = ans + mi
    do i = left, right
        h(i) = h(i) - mi
    end do


    do i = left, right
        if(h(i) == 0) then
            if(i /= left) then
                call solve(left, i-1)
            end if
            if(i /= right) then
                call solve(i+1, right)
            end if
            return
        end if
    end do

    
    return
end subroutine solve

end program prob5
; ModuleID = 'FIRModule'
source_filename = "FIRModule"
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-pc-linux-gnu"

@_QFEh.0 = internal unnamed_addr global ptr null
@_QFEh.8 = internal unnamed_addr global i64 0
@_QQcl.580ed18956a9cdbbc164682a08c61566 = linkonce constant [73 x i8] c"/home/ubuntu/Downloads/TheStack_IR/Collated/Fortran/Source_5087/source.f\00"
@_QQEnvironmentDefaults = local_unnamed_addr constant ptr null

; Function Attrs: mustprogress nofree nounwind willreturn allockind("alloc,uninitialized") allocsize(0) memory(inaccessiblemem: readwrite)
declare noalias noundef ptr @malloc(i64 noundef) local_unnamed_addr #0

; Function Attrs: mustprogress nounwind willreturn allockind("free") memory(argmem: readwrite, inaccessiblemem: readwrite)
declare void @free(ptr allocptr nocapture noundef) local_unnamed_addr #1

; Function Attrs: noreturn
define void @_QQmain() local_unnamed_addr #2 {
  %1 = alloca { ptr, i64, i32, i8, i8, i8, i8, [1 x [3 x i64]] }, align 8
  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca { ptr }, align 8
  store ptr %3, ptr %5, align 8, !tbaa !3
  %6 = call ptr @_FortranAioBeginExternalListInput(i32 -1, ptr nonnull @_QQcl.580ed18956a9cdbbc164682a08c61566, i32 5)
  %7 = call i1 @_FortranAioInputInteger(ptr %6, ptr nonnull %4, i32 4)
  %8 = call i32 @_FortranAioEndIoStatement(ptr %6)
  %9 = load i32, ptr %4, align 4, !tbaa !3
  %10 = call i32 @llvm.smax.i32(i32 %9, i32 0)
  %11 = zext i32 %10 to i64
  %12 = shl nuw nsw i64 %11, 2
  %13 = call ptr @malloc(i64 %12)
  store ptr %13, ptr @_QFEh.0, align 8, !tbaa !7
  store i64 %11, ptr @_QFEh.8, align 8, !tbaa !7
  %14 = call ptr @_FortranAioBeginExternalListInput(i32 -1, ptr nonnull @_QQcl.580ed18956a9cdbbc164682a08c61566, i32 7)
  %.unpack = load ptr, ptr @_QFEh.0, align 8, !tbaa !7
  %.unpack122.unpack.unpack124 = load i64, ptr @_QFEh.8, align 8, !tbaa !7
  store ptr %.unpack, ptr %1, align 8, !tbaa !7
  %.fca.1.gep = getelementptr inbounds { ptr, i64, i32, i8, i8, i8, i8, [1 x [3 x i64]] }, ptr %1, i64 0, i32 1
  store i64 4, ptr %.fca.1.gep, align 8, !tbaa !7
  %.fca.2.gep = getelementptr inbounds { ptr, i64, i32, i8, i8, i8, i8, [1 x [3 x i64]] }, ptr %1, i64 0, i32 2
  store i32 20180515, ptr %.fca.2.gep, align 8, !tbaa !7
  %.fca.3.gep = getelementptr inbounds { ptr, i64, i32, i8, i8, i8, i8, [1 x [3 x i64]] }, ptr %1, i64 0, i32 3
  store i8 1, ptr %.fca.3.gep, align 4, !tbaa !7
  %.fca.4.gep = getelementptr inbounds { ptr, i64, i32, i8, i8, i8, i8, [1 x [3 x i64]] }, ptr %1, i64 0, i32 4
  store i8 9, ptr %.fca.4.gep, align 1, !tbaa !7
  %.fca.5.gep = getelementptr inbounds { ptr, i64, i32, i8, i8, i8, i8, [1 x [3 x i64]] }, ptr %1, i64 0, i32 5
  store i8 0, ptr %.fca.5.gep, align 2, !tbaa !7
  %.fca.6.gep = getelementptr inbounds { ptr, i64, i32, i8, i8, i8, i8, [1 x [3 x i64]] }, ptr %1, i64 0, i32 6
  store i8 0, ptr %.fca.6.gep, align 1, !tbaa !7
  %.fca.7.0.0.gep = getelementptr inbounds { ptr, i64, i32, i8, i8, i8, i8, [1 x [3 x i64]] }, ptr %1, i64 0, i32 7, i64 0, i64 0
  store i64 1, ptr %.fca.7.0.0.gep, align 8, !tbaa !7
  %.fca.7.0.1.gep = getelementptr inbounds { ptr, i64, i32, i8, i8, i8, i8, [1 x [3 x i64]] }, ptr %1, i64 0, i32 7, i64 0, i64 1
  store i64 %.unpack122.unpack.unpack124, ptr %.fca.7.0.1.gep, align 8, !tbaa !7
  %.fca.7.0.2.gep = getelementptr inbounds { ptr, i64, i32, i8, i8, i8, i8, [1 x [3 x i64]] }, ptr %1, i64 0, i32 7, i64 0, i64 2
  store i64 4, ptr %.fca.7.0.2.gep, align 8, !tbaa !7
  %15 = call i1 @_FortranAioInputDescriptor(ptr %14, ptr nonnull %1)
  %16 = call i32 @_FortranAioEndIoStatement(ptr %14)
  store i32 0, ptr %3, align 4, !tbaa !3
  store i32 1, ptr %2, align 4, !tbaa !3
  call void @_QFPsolve(ptr nonnull %2, ptr nonnull %4, ptr nonnull %5)
  %17 = call ptr @_FortranAioBeginExternalListOutput(i32 -1, ptr nonnull @_QQcl.580ed18956a9cdbbc164682a08c61566, i32 11)
  %18 = load i32, ptr %3, align 4, !tbaa !3
  %19 = call i1 @_FortranAioOutputInteger32(ptr %17, i32 %18)
  %20 = call i32 @_FortranAioEndIoStatement(ptr %17)
  %.unpack127 = load ptr, ptr @_QFEh.0, align 8, !tbaa !7
  call void @free(ptr %.unpack127)
  store ptr null, ptr @_QFEh.0, align 8, !tbaa !7
  store i64 0, ptr @_QFEh.8, align 8, !tbaa !7
  %21 = call {} @_FortranAStopStatement(i32 0, i1 false, i1 false)
  unreachable
}

define void @_QFPsolve(ptr nocapture readonly %0, ptr nocapture readonly %1, ptr nest %2) local_unnamed_addr {
  %4 = alloca { ptr, i64, i32, i8, i8, i8, i8, [1 x [3 x i64]] }, align 8
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = load ptr, ptr %2, align 8, !tbaa !3
  %8 = load i32, ptr %0, align 4, !tbaa !3
  %9 = load i32, ptr %1, align 4, !tbaa !3
  %10 = icmp eq i32 %8, %9
  br i1 %10, label %11, label %18

11:                                               ; preds = %3
  %12 = load i32, ptr %7, align 4, !tbaa !3
  %.unpack152 = load ptr, ptr @_QFEh.0, align 8, !tbaa !7
  %13 = sext i32 %8 to i64
  %14 = add nsw i64 %13, -1
  %15 = getelementptr i32, ptr %.unpack152, i64 %14
  %16 = load i32, ptr %15, align 4, !tbaa !3
  %17 = add i32 %16, %12
  store i32 %17, ptr %7, align 4, !tbaa !3
  br label %.loopexit

18:                                               ; preds = %3
  %.unpack = load ptr, ptr @_QFEh.0, align 8, !tbaa !7
  %19 = sext i32 %8 to i64
  %20 = sext i32 %9 to i64
  %21 = add nsw i64 %19, -1
  %22 = sub nsw i64 %20, %19
  %23 = tail call i64 @llvm.smax.i64(i64 %22, i64 -1)
  %24 = add nsw i64 %23, 1
  %25 = getelementptr i32, ptr %.unpack, i64 %21
  store ptr %25, ptr %4, align 8, !tbaa !7
  %.fca.1.gep = getelementptr inbounds { ptr, i64, i32, i8, i8, i8, i8, [1 x [3 x i64]] }, ptr %4, i64 0, i32 1
  store i64 4, ptr %.fca.1.gep, align 8, !tbaa !7
  %.fca.2.gep = getelementptr inbounds { ptr, i64, i32, i8, i8, i8, i8, [1 x [3 x i64]] }, ptr %4, i64 0, i32 2
  store i32 20180515, ptr %.fca.2.gep, align 8, !tbaa !7
  %.fca.3.gep = getelementptr inbounds { ptr, i64, i32, i8, i8, i8, i8, [1 x [3 x i64]] }, ptr %4, i64 0, i32 3
  store i8 1, ptr %.fca.3.gep, align 4, !tbaa !7
  %.fca.4.gep = getelementptr inbounds { ptr, i64, i32, i8, i8, i8, i8, [1 x [3 x i64]] }, ptr %4, i64 0, i32 4
  store i8 9, ptr %.fca.4.gep, align 1, !tbaa !7
  %.fca.5.gep = getelementptr inbounds { ptr, i64, i32, i8, i8, i8, i8, [1 x [3 x i64]] }, ptr %4, i64 0, i32 5
  store i8 0, ptr %.fca.5.gep, align 2, !tbaa !7
  %.fca.6.gep = getelementptr inbounds { ptr, i64, i32, i8, i8, i8, i8, [1 x [3 x i64]] }, ptr %4, i64 0, i32 6
  store i8 0, ptr %.fca.6.gep, align 1, !tbaa !7
  %.fca.7.0.0.gep = getelementptr inbounds { ptr, i64, i32, i8, i8, i8, i8, [1 x [3 x i64]] }, ptr %4, i64 0, i32 7, i64 0, i64 0
  store i64 1, ptr %.fca.7.0.0.gep, align 8, !tbaa !7
  %.fca.7.0.1.gep = getelementptr inbounds { ptr, i64, i32, i8, i8, i8, i8, [1 x [3 x i64]] }, ptr %4, i64 0, i32 7, i64 0, i64 1
  store i64 %24, ptr %.fca.7.0.1.gep, align 8, !tbaa !7
  %.fca.7.0.2.gep = getelementptr inbounds { ptr, i64, i32, i8, i8, i8, i8, [1 x [3 x i64]] }, ptr %4, i64 0, i32 7, i64 0, i64 2
  store i64 4, ptr %.fca.7.0.2.gep, align 8, !tbaa !7
  %26 = call i32 @_FortranAMinvalInteger4(ptr nonnull %4, ptr nonnull @_QQcl.580ed18956a9cdbbc164682a08c61566, i32 26, i32 0, ptr null)
  %27 = load i32, ptr %7, align 4, !tbaa !3
  %28 = add i32 %27, %26
  store i32 %28, ptr %7, align 4, !tbaa !3
  %29 = load i32, ptr %0, align 4, !tbaa !3
  %30 = sext i32 %29 to i64
  %31 = load i32, ptr %1, align 4, !tbaa !3
  %32 = sext i32 %31 to i64
  %33 = sub nsw i64 1, %30
  %34 = add nsw i64 %33, %32
  %35 = icmp sgt i64 %34, 0
  br i1 %35, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %18
  %.unpack140 = load ptr, ptr @_QFEh.0, align 8, !tbaa !7
  %min.iters.check = icmp ult i64 %34, 8
  br i1 %min.iters.check, label %scalar.ph.preheader, label %vector.scevcheck

vector.scevcheck:                                 ; preds = %.lr.ph
  %36 = sub nsw i64 %32, %30
  %37 = trunc i64 %36 to i32
  %38 = add i32 %29, %37
  %39 = icmp slt i32 %38, %29
  %40 = icmp ugt i64 %36, 4294967295
  %41 = or i1 %39, %40
  br i1 %41, label %scalar.ph.preheader, label %vector.ph

vector.ph:                                        ; preds = %vector.scevcheck
  %n.vec = and i64 %34, -8
  %ind.end = and i64 %34, 7
  %.cast = trunc i64 %n.vec to i32
  %ind.end175 = add i32 %29, %.cast
  %broadcast.splatinsert = insertelement <4 x i32> poison, i32 %26, i64 0
  %broadcast.splat = shufflevector <4 x i32> %broadcast.splatinsert, <4 x i32> poison, <4 x i32> zeroinitializer
  %broadcast.splatinsert179 = insertelement <4 x i32> poison, i32 %26, i64 0
  %broadcast.splat180 = shufflevector <4 x i32> %broadcast.splatinsert179, <4 x i32> poison, <4 x i32> zeroinitializer
  br label %vector.body

vector.body:                                      ; preds = %vector.body, %vector.ph
  %index = phi i64 [ 0, %vector.ph ], [ %index.next, %vector.body ]
  %.cast177 = trunc i64 %index to i32
  %offset.idx = add i32 %29, %.cast177
  %42 = sext i32 %offset.idx to i64
  %43 = add nsw i64 %42, -1
  %44 = getelementptr i32, ptr %.unpack140, i64 %43
  %wide.load = load <4 x i32>, ptr %44, align 4, !tbaa !3
  %45 = getelementptr i32, ptr %44, i64 4
  %wide.load178 = load <4 x i32>, ptr %45, align 4, !tbaa !3
  %46 = sub <4 x i32> %wide.load, %broadcast.splat
  %47 = sub <4 x i32> %wide.load178, %broadcast.splat180
  store <4 x i32> %46, ptr %44, align 4, !tbaa !3
  store <4 x i32> %47, ptr %45, align 4, !tbaa !3
  %index.next = add nuw i64 %index, 8
  %48 = icmp eq i64 %index.next, %n.vec
  br i1 %48, label %middle.block, label %vector.body, !llvm.loop !9

middle.block:                                     ; preds = %vector.body
  %cmp.n = icmp eq i64 %34, %n.vec
  br i1 %cmp.n, label %._crit_edge.loopexit, label %scalar.ph.preheader

scalar.ph.preheader:                              ; preds = %vector.scevcheck, %.lr.ph, %middle.block
  %.ph = phi i64 [ %34, %vector.scevcheck ], [ %34, %.lr.ph ], [ %ind.end, %middle.block ]
  %.ph182 = phi i32 [ %29, %vector.scevcheck ], [ %29, %.lr.ph ], [ %ind.end175, %middle.block ]
  br label %scalar.ph

scalar.ph:                                        ; preds = %scalar.ph.preheader, %scalar.ph
  %49 = phi i64 [ %57, %scalar.ph ], [ %.ph, %scalar.ph.preheader ]
  %50 = phi i32 [ %56, %scalar.ph ], [ %.ph182, %scalar.ph.preheader ]
  %51 = sext i32 %50 to i64
  %52 = add nsw i64 %51, -1
  %53 = getelementptr i32, ptr %.unpack140, i64 %52
  %54 = load i32, ptr %53, align 4, !tbaa !3
  %55 = sub i32 %54, %26
  store i32 %55, ptr %53, align 4, !tbaa !3
  %56 = add i32 %50, 1
  %57 = add nsw i64 %49, -1
  %58 = icmp ugt i64 %49, 1
  br i1 %58, label %scalar.ph, label %._crit_edge.loopexit, !llvm.loop !12

._crit_edge.loopexit:                             ; preds = %scalar.ph, %middle.block
  %.pre = load i32, ptr %0, align 4, !tbaa !3
  %.pre171 = load i32, ptr %1, align 4, !tbaa !3
  br label %._crit_edge

._crit_edge:                                      ; preds = %._crit_edge.loopexit, %18
  %59 = phi i32 [ %.pre171, %._crit_edge.loopexit ], [ %31, %18 ]
  %60 = phi i32 [ %.pre, %._crit_edge.loopexit ], [ %29, %18 ]
  %61 = sub i32 1, %60
  %62 = add i32 %61, %59
  %63 = icmp sgt i32 %62, 0
  br i1 %63, label %.lr.ph169, label %.loopexit

.lr.ph169:                                        ; preds = %._crit_edge
  %.unpack127 = load ptr, ptr @_QFEh.0, align 8, !tbaa !7
  br label %64

64:                                               ; preds = %.lr.ph169, %77
  %.0167 = phi i32 [ %62, %.lr.ph169 ], [ %78, %77 ]
  %.0112166 = phi i32 [ %60, %.lr.ph169 ], [ %79, %77 ]
  %65 = sext i32 %.0112166 to i64
  %66 = add nsw i64 %65, -1
  %67 = getelementptr i32, ptr %.unpack127, i64 %66
  %68 = load i32, ptr %67, align 4, !tbaa !3
  %69 = icmp eq i32 %68, 0
  br i1 %69, label %70, label %77

70:                                               ; preds = %64
  %.not = icmp eq i32 %.0112166, %60
  br i1 %.not, label %73, label %71

71:                                               ; preds = %70
  %72 = add i32 %.0112166, -1
  store i32 %72, ptr %6, align 4, !tbaa !3
  call void @_QFPsolve(ptr nonnull %0, ptr nonnull %6, ptr nonnull %2)
  %.pre172 = load i32, ptr %1, align 4, !tbaa !3
  br label %73

73:                                               ; preds = %71, %70
  %74 = phi i32 [ %.pre172, %71 ], [ %59, %70 ]
  %.not139 = icmp eq i32 %.0112166, %74
  br i1 %.not139, label %.loopexit, label %75

75:                                               ; preds = %73
  %76 = add i32 %.0112166, 1
  store i32 %76, ptr %5, align 4, !tbaa !3
  call void @_QFPsolve(ptr nonnull %5, ptr nonnull %1, ptr nonnull %2)
  br label %.loopexit

77:                                               ; preds = %64
  %78 = add nsw i32 %.0167, -1
  %79 = add i32 %.0112166, 1
  %80 = icmp sgt i32 %.0167, 1
  br i1 %80, label %64, label %.loopexit

.loopexit:                                        ; preds = %77, %._crit_edge, %11, %75, %73
  ret void
}

declare ptr @_FortranAioBeginExternalListInput(i32, ptr, i32) local_unnamed_addr

declare i1 @_FortranAioInputInteger(ptr, ptr, i32) local_unnamed_addr

declare i32 @_FortranAioEndIoStatement(ptr) local_unnamed_addr

declare i1 @_FortranAioInputDescriptor(ptr, ptr) local_unnamed_addr

declare ptr @_FortranAioBeginExternalListOutput(i32, ptr, i32) local_unnamed_addr

declare i1 @_FortranAioOutputInteger32(ptr, i32) local_unnamed_addr

declare {} @_FortranAStopStatement(i32, i1, i1) local_unnamed_addr

declare i32 @_FortranAMinvalInteger4(ptr, ptr, i32, i32, ptr) local_unnamed_addr

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i64 @llvm.smax.i64(i64, i64) #3

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.smax.i32(i32, i32) #3

attributes #0 = { mustprogress nofree nounwind willreturn allockind("alloc,uninitialized") allocsize(0) memory(inaccessiblemem: readwrite) "alloc-family"="malloc" }
attributes #1 = { mustprogress nounwind willreturn allockind("free") memory(argmem: readwrite, inaccessiblemem: readwrite) "alloc-family"="malloc" }
attributes #2 = { noreturn }
attributes #3 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }

!llvm.module.flags = !{!0, !1, !2}

!0 = !{i32 2, !"Debug Info Version", i32 3}
!1 = !{i32 8, !"PIC Level", i32 2}
!2 = !{i32 7, !"PIE Level", i32 2}
!3 = !{!4, !4, i64 0}
!4 = !{!"any data access", !5, i64 0}
!5 = !{!"any access", !6, i64 0}
!6 = !{!"Flang Type TBAA Root"}
!7 = !{!8, !8, i64 0}
!8 = !{!"descriptor member", !5, i64 0}
!9 = distinct !{!9, !10, !11}
!10 = !{!"llvm.loop.isvectorized", i32 1}
!11 = !{!"llvm.loop.unroll.runtime.disable"}
!12 = distinct !{!12, !10}
