program main
implicit none
integer :: n, i, res, r
integer , allocatable :: w(:)

read(*,*) n
allocate( w(n) )
read(*,*) w(1:n)

res = sum(w)
do i = 1, n-1
r = min( res, abs( sum(w(1:i))-sum(w(i+1:n)) ) )
res = r
end do
write(*,'(i0)') res

end program main

; 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.e301969ca500516dce7d745a333a78a7 = linkonce constant [73 x i8] c"/home/ubuntu/Downloads/TheStack_IR/Collated/Fortran/Source_4087/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

define void @_QQmain() local_unnamed_addr {
  %1 = alloca { ptr, i64, i32, i8, i8, i8, i8, [1 x [3 x i64]] }, align 8
  %2 = alloca i32, align 4
  %3 = tail call ptr @_FortranAioBeginExternalListInput(i32 -1, ptr nonnull @_QQcl.e301969ca500516dce7d745a333a78a7, i32 6)
  %4 = call i1 @_FortranAioInputInteger(ptr %3, ptr nonnull %2, i32 4)
  %5 = call i32 @_FortranAioEndIoStatement(ptr %3)
  %6 = load i32, ptr %2, align 4, !tbaa !3
  %7 = call i32 @llvm.smax.i32(i32 %6, i32 0)
  %8 = zext i32 %7 to i64
  %9 = shl nuw nsw i64 %8, 2
  %10 = call ptr @malloc(i64 %9)
  store ptr %10, ptr @_QFEw.0, align 8, !tbaa !7
  store i64 %8, ptr @_QFEw.8, align 8, !tbaa !7
  %11 = call ptr @_FortranAioBeginExternalListInput(i32 -1, ptr nonnull @_QQcl.e301969ca500516dce7d745a333a78a7, i32 8)
  %.unpack = load ptr, ptr @_QFEw.0, align 8, !tbaa !7
  %12 = load i32, ptr %2, align 4, !tbaa !3
  %13 = call i32 @llvm.smax.i32(i32 %12, i32 0)
  %14 = zext i32 %13 to i64
  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 %14, 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 %11, ptr nonnull %1)
  %16 = call i32 @_FortranAioEndIoStatement(ptr %11)
  %.unpack214 = load ptr, ptr @_QFEw.0, align 8, !tbaa !7
  %.unpack221.unpack.unpack223 = load i64, ptr @_QFEw.8, align 8, !tbaa !7
  %17 = icmp sgt i64 %.unpack221.unpack.unpack223, 0
  br i1 %17, label %.lr.ph.i.preheader, label %_FortranASumInteger4x1_contract_simplified.exit

.lr.ph.i.preheader:                               ; preds = %0
  %min.iters.check = icmp ult i64 %.unpack221.unpack.unpack223, 8
  br i1 %min.iters.check, label %.lr.ph.i.preheader341, label %vector.ph

vector.ph:                                        ; preds = %.lr.ph.i.preheader
  %n.vec = and i64 %.unpack221.unpack.unpack223, -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 ], [ %21, %vector.body ]
  %vec.phi292 = phi <4 x i32> [ zeroinitializer, %vector.ph ], [ %22, %vector.body ]
  %18 = shl i64 %index, 2
  %19 = getelementptr i8, ptr %.unpack214, i64 %18
  %wide.load = load <4 x i32>, ptr %19, align 4, !tbaa !3
  %20 = getelementptr i32, ptr %19, i64 4
  %wide.load293 = load <4 x i32>, ptr %20, align 4, !tbaa !3
  %21 = add <4 x i32> %wide.load, %vec.phi
  %22 = add <4 x i32> %wide.load293, %vec.phi292
  %index.next = add nuw i64 %index, 8
  %23 = icmp eq i64 %index.next, %n.vec
  br i1 %23, label %middle.block, label %vector.body, !llvm.loop !9

middle.block:                                     ; preds = %vector.body
  %bin.rdx = add <4 x i32> %22, %21
  %24 = call i32 @llvm.vector.reduce.add.v4i32(<4 x i32> %bin.rdx)
  %cmp.n = icmp eq i64 %.unpack221.unpack.unpack223, %n.vec
  br i1 %cmp.n, label %_FortranASumInteger4x1_contract_simplified.exit, label %.lr.ph.i.preheader341

.lr.ph.i.preheader341:                            ; preds = %.lr.ph.i.preheader, %middle.block
  %.ph342 = phi i32 [ 0, %.lr.ph.i.preheader ], [ %24, %middle.block ]
  %.ph343 = phi i64 [ 0, %.lr.ph.i.preheader ], [ %n.vec, %middle.block ]
  br label %.lr.ph.i

.lr.ph.i:                                         ; preds = %.lr.ph.i.preheader341, %.lr.ph.i
  %25 = phi i32 [ %30, %.lr.ph.i ], [ %.ph342, %.lr.ph.i.preheader341 ]
  %26 = phi i64 [ %31, %.lr.ph.i ], [ %.ph343, %.lr.ph.i.preheader341 ]
  %27 = shl i64 %26, 2
  %28 = getelementptr i8, ptr %.unpack214, i64 %27
  %29 = load i32, ptr %28, align 4, !tbaa !3
  %30 = add i32 %29, %25
  %31 = add nuw nsw i64 %26, 1
  %exitcond.not.i = icmp eq i64 %31, %.unpack221.unpack.unpack223
  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, %0
  %.lcssa.i = phi i32 [ 0, %0 ], [ %24, %middle.block ], [ %30, %.lr.ph.i ]
  %32 = load i32, ptr %2, align 4, !tbaa !3
  %33 = add i32 %32, -1
  %34 = icmp sgt i32 %33, 0
  br i1 %34, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %_FortranASumInteger4x1_contract_simplified.exit
  %35 = zext i32 %33 to i64
  %36 = sext i32 %32 to i64
  br label %.lr.ph.i250.preheader

.lr.ph.i250.preheader:                            ; preds = %_FortranASumInteger4x1_contract_simplified.exit257, %.lr.ph
  %indvar = phi i32 [ %indvar.next, %_FortranASumInteger4x1_contract_simplified.exit257 ], [ 0, %.lr.ph ]
  %indvars.iv = phi i64 [ %indvars.iv.next, %_FortranASumInteger4x1_contract_simplified.exit257 ], [ 1, %.lr.ph ]
  %.0281 = phi i32 [ %81, %_FortranASumInteger4x1_contract_simplified.exit257 ], [ %.lcssa.i, %.lr.ph ]
  %37 = add i32 %indvar, 1
  %smax311 = call i32 @llvm.smax.i32(i32 %37, i32 0)
  %38 = zext i32 %smax311 to i64
  %39 = add i32 %indvar, 2
  %40 = sext i32 %39 to i64
  %41 = sub nsw i64 %36, %40
  %smax = call i64 @llvm.smax.i64(i64 %41, i64 -1)
  %42 = add nsw i64 %smax, 1
  %43 = trunc i64 %indvars.iv to i32
  %44 = call i32 @llvm.smax.i32(i32 %43, i32 0)
  %45 = zext i32 %44 to i64
  %min.iters.check314 = icmp ult i32 %smax311, 8
  br i1 %min.iters.check314, label %.lr.ph.i250.preheader331, label %vector.ph315

vector.ph315:                                     ; preds = %.lr.ph.i250.preheader
  %n.vec317 = and i64 %38, 2147483640
  br label %vector.body320

vector.body320:                                   ; preds = %vector.body320, %vector.ph315
  %index321 = phi i64 [ 0, %vector.ph315 ], [ %index.next326, %vector.body320 ]
  %vec.phi322 = phi <4 x i32> [ zeroinitializer, %vector.ph315 ], [ %49, %vector.body320 ]
  %vec.phi323 = phi <4 x i32> [ zeroinitializer, %vector.ph315 ], [ %50, %vector.body320 ]
  %46 = shl i64 %index321, 2
  %47 = getelementptr i8, ptr %.unpack214, i64 %46
  %wide.load324 = load <4 x i32>, ptr %47, align 4, !tbaa !3
  %48 = getelementptr i32, ptr %47, i64 4
  %wide.load325 = load <4 x i32>, ptr %48, align 4, !tbaa !3
  %49 = add <4 x i32> %wide.load324, %vec.phi322
  %50 = add <4 x i32> %wide.load325, %vec.phi323
  %index.next326 = add nuw i64 %index321, 8
  %51 = icmp eq i64 %index.next326, %n.vec317
  br i1 %51, label %middle.block312, label %vector.body320, !llvm.loop !13

middle.block312:                                  ; preds = %vector.body320
  %bin.rdx327 = add <4 x i32> %50, %49
  %52 = call i32 @llvm.vector.reduce.add.v4i32(<4 x i32> %bin.rdx327)
  %cmp.n319 = icmp eq i64 %n.vec317, %38
  br i1 %cmp.n319, label %_FortranASumInteger4x1_contract_simplified.exit253, label %.lr.ph.i250.preheader331

.lr.ph.i250.preheader331:                         ; preds = %.lr.ph.i250.preheader, %middle.block312
  %.ph332 = phi i32 [ 0, %.lr.ph.i250.preheader ], [ %52, %middle.block312 ]
  %.ph333 = phi i64 [ 0, %.lr.ph.i250.preheader ], [ %n.vec317, %middle.block312 ]
  br label %.lr.ph.i250

.lr.ph.i250:                                      ; preds = %.lr.ph.i250.preheader331, %.lr.ph.i250
  %53 = phi i32 [ %58, %.lr.ph.i250 ], [ %.ph332, %.lr.ph.i250.preheader331 ]
  %54 = phi i64 [ %59, %.lr.ph.i250 ], [ %.ph333, %.lr.ph.i250.preheader331 ]
  %55 = shl i64 %54, 2
  %56 = getelementptr i8, ptr %.unpack214, i64 %55
  %57 = load i32, ptr %56, align 4, !tbaa !3
  %58 = add i32 %57, %53
  %59 = add nuw nsw i64 %54, 1
  %exitcond.not.i251 = icmp eq i64 %59, %45
  br i1 %exitcond.not.i251, label %_FortranASumInteger4x1_contract_simplified.exit253, label %.lr.ph.i250, !llvm.loop !14

_FortranASumInteger4x1_contract_simplified.exit253: ; preds = %.lr.ph.i250, %middle.block312
  %.lcssa = phi i32 [ %52, %middle.block312 ], [ %58, %.lr.ph.i250 ]
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %sext = shl i64 %indvars.iv.next, 32
  %60 = ashr exact i64 %sext, 32
  %61 = add nsw i64 %60, -1
  %62 = sub nsw i64 %36, %60
  %63 = call i64 @llvm.smax.i64(i64 %62, i64 -1)
  %64 = getelementptr i32, ptr %.unpack214, i64 %61
  %.not278 = icmp slt i64 %62, 0
  br i1 %.not278, label %_FortranASumInteger4x1_contract_simplified.exit257, label %.lr.ph.i254.preheader

.lr.ph.i254.preheader:                            ; preds = %_FortranASumInteger4x1_contract_simplified.exit253
  %min.iters.check296 = icmp ult i64 %42, 8
  br i1 %min.iters.check296, label %.lr.ph.i254.preheader329, label %vector.ph297

vector.ph297:                                     ; preds = %.lr.ph.i254.preheader
  %n.vec299 = and i64 %42, -8
  br label %vector.body302

vector.body302:                                   ; preds = %vector.body302, %vector.ph297
  %index303 = phi i64 [ 0, %vector.ph297 ], [ %index.next308, %vector.body302 ]
  %vec.phi304 = phi <4 x i32> [ zeroinitializer, %vector.ph297 ], [ %68, %vector.body302 ]
  %vec.phi305 = phi <4 x i32> [ zeroinitializer, %vector.ph297 ], [ %69, %vector.body302 ]
  %65 = shl i64 %index303, 2
  %66 = getelementptr i8, ptr %64, i64 %65
  %wide.load306 = load <4 x i32>, ptr %66, align 4, !tbaa !3
  %67 = getelementptr i32, ptr %66, i64 4
  %wide.load307 = load <4 x i32>, ptr %67, align 4, !tbaa !3
  %68 = add <4 x i32> %wide.load306, %vec.phi304
  %69 = add <4 x i32> %wide.load307, %vec.phi305
  %index.next308 = add nuw i64 %index303, 8
  %70 = icmp eq i64 %index.next308, %n.vec299
  br i1 %70, label %middle.block294, label %vector.body302, !llvm.loop !15

middle.block294:                                  ; preds = %vector.body302
  %bin.rdx309 = add <4 x i32> %69, %68
  %71 = call i32 @llvm.vector.reduce.add.v4i32(<4 x i32> %bin.rdx309)
  %cmp.n301 = icmp eq i64 %42, %n.vec299
  br i1 %cmp.n301, label %_FortranASumInteger4x1_contract_simplified.exit257, label %.lr.ph.i254.preheader329

.lr.ph.i254.preheader329:                         ; preds = %.lr.ph.i254.preheader, %middle.block294
  %.ph = phi i32 [ 0, %.lr.ph.i254.preheader ], [ %71, %middle.block294 ]
  %.ph330 = phi i64 [ 0, %.lr.ph.i254.preheader ], [ %n.vec299, %middle.block294 ]
  br label %.lr.ph.i254

.lr.ph.i254:                                      ; preds = %.lr.ph.i254.preheader329, %.lr.ph.i254
  %72 = phi i32 [ %77, %.lr.ph.i254 ], [ %.ph, %.lr.ph.i254.preheader329 ]
  %73 = phi i64 [ %78, %.lr.ph.i254 ], [ %.ph330, %.lr.ph.i254.preheader329 ]
  %74 = shl i64 %73, 2
  %75 = getelementptr i8, ptr %64, i64 %74
  %76 = load i32, ptr %75, align 4, !tbaa !3
  %77 = add i32 %76, %72
  %78 = add nuw nsw i64 %73, 1
  %exitcond.not.i255 = icmp eq i64 %73, %63
  br i1 %exitcond.not.i255, label %_FortranASumInteger4x1_contract_simplified.exit257, label %.lr.ph.i254, !llvm.loop !16

_FortranASumInteger4x1_contract_simplified.exit257: ; preds = %.lr.ph.i254, %middle.block294, %_FortranASumInteger4x1_contract_simplified.exit253
  %.lcssa.i256 = phi i32 [ 0, %_FortranASumInteger4x1_contract_simplified.exit253 ], [ %71, %middle.block294 ], [ %77, %.lr.ph.i254 ]
  %79 = sub i32 %.lcssa, %.lcssa.i256
  %80 = call i32 @llvm.abs.i32(i32 %79, i1 false)
  %81 = call i32 @llvm.smin.i32(i32 %.0281, i32 %80)
  %exitcond.not = icmp eq i64 %indvars.iv, %35
  %indvar.next = add i32 %indvar, 1
  br i1 %exitcond.not, label %._crit_edge, label %.lr.ph.i250.preheader

._crit_edge:                                      ; preds = %_FortranASumInteger4x1_contract_simplified.exit257, %_FortranASumInteger4x1_contract_simplified.exit
  %.0.lcssa = phi i32 [ %.lcssa.i, %_FortranASumInteger4x1_contract_simplified.exit ], [ %81, %_FortranASumInteger4x1_contract_simplified.exit257 ]
  %82 = call ptr @_FortranAioBeginExternalFormattedOutput(ptr nonnull @_QQcl.28693029, i64 4, ptr null, i32 -1, ptr nonnull @_QQcl.e301969ca500516dce7d745a333a78a7, i32 15)
  %83 = call i1 @_FortranAioOutputInteger32(ptr %82, i32 %.0.lcssa)
  %84 = call i32 @_FortranAioEndIoStatement(ptr %82)
  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 @_FortranAioOutputInteger32(ptr, i32) local_unnamed_addr

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

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

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

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

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

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) }
attributes #2 = { 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}
!13 = distinct !{!13, !10, !11}
!14 = distinct !{!14, !11, !10}
!15 = distinct !{!15, !10, !11}
!16 = distinct !{!16, !11, !10}
