implicit none
integer :: N
integer, allocatable :: W(:)
integer :: i, ds, dsmin, s1, s2, T

read *,N
allocate(W(N))
read *, (W(i),i=1,N)

s2 = sum(W)

dsmin = s2
s1 = 0
do T = 1, N-1
   s1 = s1 + W(T)
   s2 = S2 - W(T)
   ds = abs(s1-s2)
   if (ds < dsmin) then
      dsmin = ds
   else
      exit  ! Once weight starts to increase, no longer consider.
   endif
enddo

write(6,'(i0)') dsmin
stop
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"

@_QFEw.0 = internal unnamed_addr global ptr null
@_QFEw.8 = internal unnamed_addr global i64 0
@_QQcl.445e9e76d6727753b9df26379c629376 = linkonce constant [73 x i8] c"/home/ubuntu/Downloads/TheStack_IR/Collated/Fortran/Source_4095/source.f\00"
@_QQcl.28693029 = 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

; Function Attrs: noreturn
define void @_QQmain() local_unnamed_addr #1 {
  %1 = alloca i32, align 4
  %2 = tail call ptr @_FortranAioBeginExternalListInput(i32 -1, ptr nonnull @_QQcl.445e9e76d6727753b9df26379c629376, i32 6)
  %3 = call i1 @_FortranAioInputInteger(ptr %2, ptr nonnull %1, i32 4)
  %4 = call i32 @_FortranAioEndIoStatement(ptr %2)
  %5 = load i32, ptr %1, align 4, !tbaa !3
  %6 = call i32 @llvm.smax.i32(i32 %5, i32 0)
  %7 = zext i32 %6 to i64
  %8 = shl nuw nsw i64 %7, 2
  %9 = call ptr @malloc(i64 %8)
  store ptr %9, ptr @_QFEw.0, align 8, !tbaa !7
  store i64 %7, ptr @_QFEw.8, align 8, !tbaa !7
  %10 = call ptr @_FortranAioBeginExternalListInput(i32 -1, ptr nonnull @_QQcl.445e9e76d6727753b9df26379c629376, i32 8)
  %11 = load i32, ptr %1, align 4, !tbaa !3
  %12 = icmp sgt i32 %11, 0
  br i1 %12, label %.lr.ph.preheader, label %._crit_edge

.lr.ph.preheader:                                 ; preds = %0
  %13 = zext i32 %11 to i64
  br label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph.preheader, %.lr.ph
  %14 = phi i64 [ %19, %.lr.ph ], [ 1, %.lr.ph.preheader ]
  %.unpack180 = load ptr, ptr @_QFEw.0, align 8, !tbaa !7
  %sext = shl i64 %14, 32
  %15 = ashr exact i64 %sext, 32
  %16 = add nsw i64 %15, -1
  %17 = getelementptr i32, ptr %.unpack180, i64 %16
  %18 = call i1 @_FortranAioInputInteger(ptr %10, ptr %17, i32 4)
  %19 = add nuw nsw i64 %14, 1
  %exitcond.not = icmp eq i64 %14, %13
  br i1 %exitcond.not, label %._crit_edge, label %.lr.ph

._crit_edge:                                      ; preds = %.lr.ph, %0
  %20 = call i32 @_FortranAioEndIoStatement(ptr %10)
  %.unpack = load ptr, ptr @_QFEw.0, align 8, !tbaa !7
  %.unpack163.unpack.unpack165 = load i64, ptr @_QFEw.8, align 8, !tbaa !7
  %21 = icmp sgt i64 %.unpack163.unpack.unpack165, 0
  br i1 %21, label %.lr.ph.i.preheader, label %_FortranASumInteger4x1_contract_simplified.exit

.lr.ph.i.preheader:                               ; preds = %._crit_edge
  %min.iters.check = icmp ult i64 %.unpack163.unpack.unpack165, 8
  br i1 %min.iters.check, label %.lr.ph.i.preheader205, label %vector.ph

vector.ph:                                        ; preds = %.lr.ph.i.preheader
  %n.vec = and i64 %.unpack163.unpack.unpack165, -8
  br label %vector.body

vector.body:                                      ; preds = %vector.body, %vector.ph
  %index = phi i64 [ 0, %vector.ph ], [ %index.next, %vector.body ]
  %vec.phi = phi <4 x i32> [ zeroinitializer, %vector.ph ], [ %25, %vector.body ]
  %vec.phi203 = phi <4 x i32> [ zeroinitializer, %vector.ph ], [ %26, %vector.body ]
  %22 = shl i64 %index, 2
  %23 = getelementptr i8, ptr %.unpack, i64 %22
  %wide.load = load <4 x i32>, ptr %23, align 4, !tbaa !3
  %24 = getelementptr i32, ptr %23, i64 4
  %wide.load204 = load <4 x i32>, ptr %24, align 4, !tbaa !3
  %25 = add <4 x i32> %wide.load, %vec.phi
  %26 = add <4 x i32> %wide.load204, %vec.phi203
  %index.next = add nuw i64 %index, 8
  %27 = icmp eq i64 %index.next, %n.vec
  br i1 %27, label %middle.block, label %vector.body, !llvm.loop !9

middle.block:                                     ; preds = %vector.body
  %bin.rdx = add <4 x i32> %26, %25
  %28 = call i32 @llvm.vector.reduce.add.v4i32(<4 x i32> %bin.rdx)
  %cmp.n = icmp eq i64 %.unpack163.unpack.unpack165, %n.vec
  br i1 %cmp.n, label %_FortranASumInteger4x1_contract_simplified.exit, label %.lr.ph.i.preheader205

.lr.ph.i.preheader205:                            ; preds = %.lr.ph.i.preheader, %middle.block
  %.ph = phi i32 [ 0, %.lr.ph.i.preheader ], [ %28, %middle.block ]
  %.ph206 = phi i64 [ 0, %.lr.ph.i.preheader ], [ %n.vec, %middle.block ]
  br label %.lr.ph.i

.lr.ph.i:                                         ; preds = %.lr.ph.i.preheader205, %.lr.ph.i
  %29 = phi i32 [ %34, %.lr.ph.i ], [ %.ph, %.lr.ph.i.preheader205 ]
  %30 = phi i64 [ %35, %.lr.ph.i ], [ %.ph206, %.lr.ph.i.preheader205 ]
  %31 = shl i64 %30, 2
  %32 = getelementptr i8, ptr %.unpack, i64 %31
  %33 = load i32, ptr %32, align 4, !tbaa !3
  %34 = add i32 %33, %29
  %35 = add nuw nsw i64 %30, 1
  %exitcond.not.i = icmp eq i64 %35, %.unpack163.unpack.unpack165
  br i1 %exitcond.not.i, label %_FortranASumInteger4x1_contract_simplified.exit, label %.lr.ph.i, !llvm.loop !12

_FortranASumInteger4x1_contract_simplified.exit:  ; preds = %.lr.ph.i, %middle.block, %._crit_edge
  %.lcssa.i = phi i32 [ 0, %._crit_edge ], [ %28, %middle.block ], [ %34, %.lr.ph.i ]
  %36 = load i32, ptr %1, align 4, !tbaa !3
  %.0145192 = add i32 %36, -1
  %37 = icmp sgt i32 %.0145192, 0
  br i1 %37, label %.lr.ph198, label %._crit_edge199

38:                                               ; preds = %.lr.ph198
  %39 = add nuw i32 %.0196, 1
  %exitcond202.not = icmp eq i32 %39, %36
  br i1 %exitcond202.not, label %._crit_edge199, label %.lr.ph198

.lr.ph198:                                        ; preds = %_FortranASumInteger4x1_contract_simplified.exit, %38
  %.0196 = phi i32 [ %39, %38 ], [ 1, %_FortranASumInteger4x1_contract_simplified.exit ]
  %.0142195 = phi i32 [ %45, %38 ], [ %.lcssa.i, %_FortranASumInteger4x1_contract_simplified.exit ]
  %.0143194 = phi i32 [ %44, %38 ], [ 0, %_FortranASumInteger4x1_contract_simplified.exit ]
  %.0144193 = phi i32 [ %47, %38 ], [ %.lcssa.i, %_FortranASumInteger4x1_contract_simplified.exit ]
  %40 = sext i32 %.0196 to i64
  %41 = add nsw i64 %40, -1
  %42 = getelementptr i32, ptr %.unpack, i64 %41
  %43 = load i32, ptr %42, align 4, !tbaa !3
  %44 = add i32 %43, %.0143194
  %45 = sub i32 %.0142195, %43
  %46 = sub i32 %44, %45
  %47 = call i32 @llvm.abs.i32(i32 %46, i1 false)
  %48 = icmp slt i32 %47, %.0144193
  br i1 %48, label %38, label %._crit_edge199

._crit_edge199:                                   ; preds = %38, %.lr.ph198, %_FortranASumInteger4x1_contract_simplified.exit
  %.0144.lcssa = phi i32 [ %.lcssa.i, %_FortranASumInteger4x1_contract_simplified.exit ], [ %.0144193, %.lr.ph198 ], [ %47, %38 ]
  %49 = call ptr @_FortranAioBeginExternalFormattedOutput(ptr nonnull @_QQcl.28693029, i64 4, ptr null, i32 6, ptr nonnull @_QQcl.445e9e76d6727753b9df26379c629376, i32 25)
  %50 = call i1 @_FortranAioOutputInteger32(ptr %49, i32 %.0144.lcssa)
  %51 = call i32 @_FortranAioEndIoStatement(ptr %49)
  %52 = call {} @_FortranAStopStatement(i32 0, i1 false, i1 false)
  unreachable
}

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 ptr @_FortranAioBeginExternalFormattedOutput(ptr, i64, ptr, i32, ptr, i32) local_unnamed_addr

declare i1 @_FortranAioOutputInteger32(ptr, i32) local_unnamed_addr

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

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

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.abs.i32(i32, i1 immarg) #2

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(none)
declare i32 @llvm.vector.reduce.add.v4i32(<4 x i32>) #3

attributes #0 = { mustprogress nofree nounwind willreturn allockind("alloc,uninitialized") allocsize(0) memory(inaccessiblemem: readwrite) "alloc-family"="malloc" }
attributes #1 = { noreturn }
attributes #2 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #3 = { nocallback nofree nosync nounwind 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, !11, !10}
