integer N
integer,allocatable,dimension(:)::A,B
integer(16) ans
read*,N
allocate(A(N+1),B(N))
read*,A
read*,B
ans=0
do i=1,N
  if(A(i)>=B(i))then
    ans=ans+B(i)
  else
    ans=ans+A(i)
    B(i)=B(i)-A(i)
    if(A(i+1)>=B(i))then
      ans=ans+B(I)
      A(i+1)=A(i+1)-B(i)
    else
      ans=ans+A(i+1)
      A(i+1)=0
    endif
  endif
end do

print"(I0)",ans
end
; 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"

@_QFEa.0 = internal unnamed_addr global ptr null
@_QFEa.8 = internal unnamed_addr global i64 0
@_QFEb.0 = internal unnamed_addr global ptr null
@_QFEb.8 = internal unnamed_addr global i64 0
@_QQcl.e717dbca34acdd7cf40584bdf58129f2 = linkonce constant [73 x i8] c"/home/ubuntu/Downloads/TheStack_IR/Collated/Fortran/Source_3657/source.f\00"
@_QQcl.28493029 = linkonce constant [4 x i8] c"(I0)"
@_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

define void @_QQmain() local_unnamed_addr {
  %1 = alloca { ptr, i64, i32, i8, i8, i8, i8, [1 x [3 x i64]] }, align 8
  %2 = alloca { ptr, i64, i32, i8, i8, i8, i8, [1 x [3 x i64]] }, align 8
  %3 = alloca i32, align 4
  %4 = tail call ptr @_FortranAioBeginExternalListInput(i32 -1, ptr nonnull @_QQcl.e717dbca34acdd7cf40584bdf58129f2, i32 4)
  %5 = call i1 @_FortranAioInputInteger(ptr %4, ptr nonnull %3, i32 4)
  %6 = call i32 @_FortranAioEndIoStatement(ptr %4)
  %7 = load i32, ptr %3, align 4, !tbaa !3
  %8 = add i32 %7, 1
  %9 = call i32 @llvm.smax.i32(i32 %8, i32 0)
  %10 = zext i32 %9 to i64
  %11 = shl nuw nsw i64 %10, 2
  %12 = call ptr @malloc(i64 %11)
  store ptr %12, ptr @_QFEa.0, align 8, !tbaa !7
  store i64 %10, ptr @_QFEa.8, align 8, !tbaa !7
  %13 = call i32 @llvm.smax.i32(i32 %7, i32 0)
  %14 = zext i32 %13 to i64
  %15 = shl nuw nsw i64 %14, 2
  %16 = call ptr @malloc(i64 %15)
  store ptr %16, ptr @_QFEb.0, align 8, !tbaa !7
  store i64 %14, ptr @_QFEb.8, align 8, !tbaa !7
  %17 = call ptr @_FortranAioBeginExternalListInput(i32 -1, ptr nonnull @_QQcl.e717dbca34acdd7cf40584bdf58129f2, i32 6)
  %.unpack = load ptr, ptr @_QFEa.0, align 8, !tbaa !7
  %.unpack563.unpack.unpack565 = load i64, ptr @_QFEa.8, align 8, !tbaa !7
  store ptr %.unpack, ptr %2, align 8, !tbaa !7
  %.fca.1.gep = getelementptr inbounds { ptr, i64, i32, i8, i8, i8, i8, [1 x [3 x i64]] }, ptr %2, 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 %2, 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 %2, 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 %2, 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 %2, 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 %2, 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 %2, 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 %2, i64 0, i32 7, i64 0, i64 1
  store i64 %.unpack563.unpack.unpack565, 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 %2, i64 0, i32 7, i64 0, i64 2
  store i64 4, ptr %.fca.7.0.2.gep, align 8, !tbaa !7
  %18 = call i1 @_FortranAioInputDescriptor(ptr %17, ptr nonnull %2)
  %19 = call i32 @_FortranAioEndIoStatement(ptr %17)
  %20 = call ptr @_FortranAioBeginExternalListInput(i32 -1, ptr nonnull @_QQcl.e717dbca34acdd7cf40584bdf58129f2, i32 7)
  %.unpack568 = load ptr, ptr @_QFEb.0, align 8, !tbaa !7
  %.unpack575.unpack.unpack577 = load i64, ptr @_QFEb.8, align 8, !tbaa !7
  store ptr %.unpack568, ptr %1, align 8, !tbaa !7
  %.fca.1.gep125 = 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.gep125, align 8, !tbaa !7
  %.fca.2.gep127 = 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.gep127, align 8, !tbaa !7
  %.fca.3.gep129 = 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.gep129, align 4, !tbaa !7
  %.fca.4.gep131 = 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.gep131, align 1, !tbaa !7
  %.fca.5.gep133 = 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.gep133, align 2, !tbaa !7
  %.fca.6.gep135 = 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.gep135, align 1, !tbaa !7
  %.fca.7.0.0.gep137 = 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.gep137, align 8, !tbaa !7
  %.fca.7.0.1.gep139 = 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 %.unpack575.unpack.unpack577, ptr %.fca.7.0.1.gep139, align 8, !tbaa !7
  %.fca.7.0.2.gep141 = 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.gep141, align 8, !tbaa !7
  %21 = call i1 @_FortranAioInputDescriptor(ptr %20, ptr nonnull %1)
  %22 = call i32 @_FortranAioEndIoStatement(ptr %20)
  %23 = load i32, ptr %3, align 4, !tbaa !3
  %24 = icmp sgt i32 %23, 0
  br i1 %24, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %0
  %25 = zext i32 %23 to i64
  %.unpack580 = load ptr, ptr @_QFEa.0, align 8, !tbaa !7
  %.unpack592 = load ptr, ptr @_QFEb.0, align 8, !tbaa !7
  %26 = add nuw nsw i64 %25, 1
  br label %27

27:                                               ; preds = %.lr.ph, %53
  %indvars.iv = phi i64 [ 1, %.lr.ph ], [ %indvars.iv.next.pre-phi, %53 ]
  %.0629 = phi i128 [ 0, %.lr.ph ], [ %.1, %53 ]
  %28 = add i64 %indvars.iv, -1
  %29 = getelementptr i32, ptr %.unpack580, i64 %28
  %30 = load i32, ptr %29, align 4, !tbaa !3
  %31 = add i64 %indvars.iv, -1
  %32 = getelementptr i32, ptr %.unpack592, i64 %31
  %33 = load i32, ptr %32, align 4, !tbaa !3
  %.not = icmp sgt i32 %33, %30
  br i1 %.not, label %37, label %34

34:                                               ; preds = %27
  %35 = sext i32 %33 to i128
  %36 = add i128 %.0629, %35
  %.pre = add nuw nsw i64 %indvars.iv, 1
  br label %53

37:                                               ; preds = %27
  %38 = sext i32 %30 to i128
  %39 = add i128 %.0629, %38
  %40 = sub i32 %33, %30
  store i32 %40, ptr %32, align 4, !tbaa !3
  %41 = add nuw nsw i64 %indvars.iv, 1
  %sext = shl i64 %41, 32
  %42 = ashr exact i64 %sext, 32
  %43 = add nsw i64 %42, -1
  %44 = getelementptr i32, ptr %.unpack580, i64 %43
  %45 = load i32, ptr %44, align 4, !tbaa !3
  %.not628 = icmp slt i32 %45, %40
  br i1 %.not628, label %50, label %46

46:                                               ; preds = %37
  %47 = sext i32 %40 to i128
  %48 = add i128 %39, %47
  %49 = sub i32 %45, %40
  store i32 %49, ptr %44, align 4, !tbaa !3
  br label %53

50:                                               ; preds = %37
  %51 = sext i32 %45 to i128
  %52 = add i128 %39, %51
  store i32 0, ptr %44, align 4, !tbaa !3
  br label %53

53:                                               ; preds = %34, %46, %50
  %indvars.iv.next.pre-phi = phi i64 [ %.pre, %34 ], [ %41, %46 ], [ %41, %50 ]
  %.1 = phi i128 [ %36, %34 ], [ %48, %46 ], [ %52, %50 ]
  %exitcond.not = icmp eq i64 %indvars.iv.next.pre-phi, %26
  br i1 %exitcond.not, label %._crit_edge, label %27

._crit_edge:                                      ; preds = %53, %0
  %.0.lcssa = phi i128 [ 0, %0 ], [ %.1, %53 ]
  %54 = call ptr @_FortranAioBeginExternalFormattedOutput(ptr nonnull @_QQcl.28493029, i64 4, ptr null, i32 -1, ptr nonnull @_QQcl.e717dbca34acdd7cf40584bdf58129f2, i32 25)
  %55 = call i1 @_FortranAioOutputInteger128(ptr %54, i128 %.0.lcssa)
  %56 = call i32 @_FortranAioEndIoStatement(ptr %54)
  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 @_FortranAioBeginExternalFormattedOutput(ptr, i64, ptr, i32, ptr, i32) local_unnamed_addr

declare i1 @_FortranAioOutputInteger128(ptr, i128) local_unnamed_addr

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

attributes #0 = { mustprogress nofree nounwind willreturn allockind("alloc,uninitialized") allocsize(0) memory(inaccessiblemem: readwrite) "alloc-family"="malloc" }
attributes #1 = { 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}
