program Knapsack_2
    implicit none
    integer(4):: n,wn
    integer(8), parameter:: inf=400000000000_8
    integer(8),allocatable:: w(:), v(:)
    integer(8):: dp(0:100000), ndp(0:100000), ans
    integer(4):: i,j

    read*, n,wn
    allocate(w(n), v(n))
    do i=1,n
        read*, w(i), v(i)
    end do

    dp(:) = inf
    ndp(:) = inf

    dp(0) = 0
    ndp(0) = 0

    do i=1, n
        do j=1, 100000
            if (j < v(i)) then
                ndp(j) = dp(j)
            else
                ndp(j) = min(dp(j), dp(j-v(i))+w(i))
            end if
        end do
        dp(:) = ndp(:)
    end do

    ans = 0
    do i=1, 100000
        if (dp(i) <= wn) then
            ans = max(ans,i)
        end if
    end do
    print*, ans
end program Knapsack_2
; 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"

@_QFEdp = internal unnamed_addr global [100001 x i64] undef
@_QFEndp = internal unnamed_addr global [100001 x i64] undef
@_QFEv.0 = internal unnamed_addr global ptr null
@_QFEw.0 = internal unnamed_addr global ptr null
@_QQcl.52c978c05bf0cabbbe05d2d519c242f0 = linkonce constant [73 x i8] c"/home/ubuntu/Downloads/TheStack_IR/Collated/Fortran/Source_5188/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

define void @_QQmain() local_unnamed_addr {
  %1 = alloca i32, align 4
  %2 = alloca i32, align 4
  %3 = tail call ptr @_FortranAioBeginExternalListInput(i32 -1, ptr nonnull @_QQcl.52c978c05bf0cabbbe05d2d519c242f0, i32 9)
  %4 = call i1 @_FortranAioInputInteger(ptr %3, ptr nonnull %1, i32 4)
  %5 = call i1 @_FortranAioInputInteger(ptr %3, ptr nonnull %2, i32 4)
  %6 = call i32 @_FortranAioEndIoStatement(ptr %3)
  %7 = load i32, ptr %1, align 4, !tbaa !3
  %8 = call i32 @llvm.smax.i32(i32 %7, i32 0)
  %9 = zext i32 %8 to i64
  %10 = shl nuw nsw i64 %9, 3
  %11 = call ptr @malloc(i64 %10)
  store ptr %11, ptr @_QFEw.0, align 8, !tbaa !7
  %12 = call ptr @malloc(i64 %10)
  store ptr %12, ptr @_QFEv.0, align 8, !tbaa !7
  %13 = icmp sgt i32 %7, 0
  br i1 %13, label %.lr.ph.preheader, label %vector.body.preheader

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

vector.body:                                      ; preds = %vector.body, %vector.body.preheader
  %index = phi i64 [ 0, %vector.body.preheader ], [ %index.next.4, %vector.body ]
  %15 = getelementptr i64, ptr @_QFEdp, i64 %index
  store <2 x i64> <i64 400000000000, i64 400000000000>, ptr %15, align 16, !tbaa !3
  %16 = getelementptr i64, ptr %15, i64 2
  store <2 x i64> <i64 400000000000, i64 400000000000>, ptr %16, align 16, !tbaa !3
  %index.next = add nuw nsw i64 %index, 4
  %17 = getelementptr i64, ptr @_QFEdp, i64 %index.next
  store <2 x i64> <i64 400000000000, i64 400000000000>, ptr %17, align 16, !tbaa !3
  %18 = getelementptr i64, ptr %17, i64 2
  store <2 x i64> <i64 400000000000, i64 400000000000>, ptr %18, align 16, !tbaa !3
  %index.next.1 = add nuw nsw i64 %index, 8
  %19 = getelementptr i64, ptr @_QFEdp, i64 %index.next.1
  store <2 x i64> <i64 400000000000, i64 400000000000>, ptr %19, align 16, !tbaa !3
  %20 = getelementptr i64, ptr %19, i64 2
  store <2 x i64> <i64 400000000000, i64 400000000000>, ptr %20, align 16, !tbaa !3
  %index.next.2 = add nuw nsw i64 %index, 12
  %21 = getelementptr i64, ptr @_QFEdp, i64 %index.next.2
  store <2 x i64> <i64 400000000000, i64 400000000000>, ptr %21, align 16, !tbaa !3
  %22 = getelementptr i64, ptr %21, i64 2
  store <2 x i64> <i64 400000000000, i64 400000000000>, ptr %22, align 16, !tbaa !3
  %index.next.3 = add nuw nsw i64 %index, 16
  %23 = getelementptr i64, ptr @_QFEdp, i64 %index.next.3
  store <2 x i64> <i64 400000000000, i64 400000000000>, ptr %23, align 16, !tbaa !3
  %24 = getelementptr i64, ptr %23, i64 2
  store <2 x i64> <i64 400000000000, i64 400000000000>, ptr %24, align 16, !tbaa !3
  %index.next.4 = add nuw nsw i64 %index, 20
  %25 = icmp eq i64 %index.next.4, 100000
  br i1 %25, label %scalar.ph, label %vector.body, !llvm.loop !9

.lr.ph:                                           ; preds = %.lr.ph.preheader, %.lr.ph
  %indvars.iv = phi i64 [ 1, %.lr.ph.preheader ], [ %indvars.iv.next, %.lr.ph ]
  %26 = call ptr @_FortranAioBeginExternalListInput(i32 -1, ptr nonnull @_QQcl.52c978c05bf0cabbbe05d2d519c242f0, i32 12)
  %.unpack213 = load ptr, ptr @_QFEw.0, align 8, !tbaa !7
  %27 = add nsw i64 %indvars.iv, -1
  %28 = getelementptr i64, ptr %.unpack213, i64 %27
  %29 = call i1 @_FortranAioInputInteger(ptr %26, ptr %28, i32 8)
  %.unpack225 = load ptr, ptr @_QFEv.0, align 8, !tbaa !7
  %30 = add nsw i64 %indvars.iv, -1
  %31 = getelementptr i64, ptr %.unpack225, i64 %30
  %32 = call i1 @_FortranAioInputInteger(ptr %26, ptr %31, i32 8)
  %33 = call i32 @_FortranAioEndIoStatement(ptr %26)
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %exitcond.not = icmp eq i64 %indvars.iv, %14
  br i1 %exitcond.not, label %vector.body.preheader, label %.lr.ph

vector.body.preheader:                            ; preds = %.lr.ph, %0
  br label %vector.body

scalar.ph:                                        ; preds = %vector.body
  store i64 400000000000, ptr getelementptr inbounds ([100001 x i64], ptr @_QFEdp, i64 0, i64 100000), align 16, !tbaa !3
  br label %vector.body262

vector.body262:                                   ; preds = %vector.body262, %scalar.ph
  %index263 = phi i64 [ 0, %scalar.ph ], [ %index.next264.4, %vector.body262 ]
  %34 = getelementptr i64, ptr @_QFEndp, i64 %index263
  store <2 x i64> <i64 400000000000, i64 400000000000>, ptr %34, align 16, !tbaa !3
  %35 = getelementptr i64, ptr %34, i64 2
  store <2 x i64> <i64 400000000000, i64 400000000000>, ptr %35, align 16, !tbaa !3
  %index.next264 = add nuw nsw i64 %index263, 4
  %36 = getelementptr i64, ptr @_QFEndp, i64 %index.next264
  store <2 x i64> <i64 400000000000, i64 400000000000>, ptr %36, align 16, !tbaa !3
  %37 = getelementptr i64, ptr %36, i64 2
  store <2 x i64> <i64 400000000000, i64 400000000000>, ptr %37, align 16, !tbaa !3
  %index.next264.1 = add nuw nsw i64 %index263, 8
  %38 = getelementptr i64, ptr @_QFEndp, i64 %index.next264.1
  store <2 x i64> <i64 400000000000, i64 400000000000>, ptr %38, align 16, !tbaa !3
  %39 = getelementptr i64, ptr %38, i64 2
  store <2 x i64> <i64 400000000000, i64 400000000000>, ptr %39, align 16, !tbaa !3
  %index.next264.2 = add nuw nsw i64 %index263, 12
  %40 = getelementptr i64, ptr @_QFEndp, i64 %index.next264.2
  store <2 x i64> <i64 400000000000, i64 400000000000>, ptr %40, align 16, !tbaa !3
  %41 = getelementptr i64, ptr %40, i64 2
  store <2 x i64> <i64 400000000000, i64 400000000000>, ptr %41, align 16, !tbaa !3
  %index.next264.3 = add nuw nsw i64 %index263, 16
  %42 = getelementptr i64, ptr @_QFEndp, i64 %index.next264.3
  store <2 x i64> <i64 400000000000, i64 400000000000>, ptr %42, align 16, !tbaa !3
  %43 = getelementptr i64, ptr %42, i64 2
  store <2 x i64> <i64 400000000000, i64 400000000000>, ptr %43, align 16, !tbaa !3
  %index.next264.4 = add nuw nsw i64 %index263, 20
  %44 = icmp eq i64 %index.next264.4, 100000
  br i1 %44, label %.preheader239, label %vector.body262, !llvm.loop !12

.preheader239:                                    ; preds = %vector.body262
  store i64 400000000000, ptr getelementptr inbounds ([100001 x i64], ptr @_QFEndp, i64 0, i64 100000), align 16, !tbaa !3
  store i64 0, ptr @_QFEdp, align 16, !tbaa !3
  store i64 0, ptr @_QFEndp, align 16, !tbaa !3
  %45 = load i32, ptr %1, align 4, !tbaa !3
  %46 = icmp sgt i32 %45, 0
  br i1 %46, label %.preheader238.lr.ph, label %.preheader

.preheader238.lr.ph:                              ; preds = %.preheader239
  %47 = zext i32 %45 to i64
  %.unpack = load ptr, ptr @_QFEv.0, align 8, !tbaa !7
  %.unpack201 = load ptr, ptr @_QFEw.0, align 8
  br label %.preheader238

.preheader238:                                    ; preds = %.preheader238.lr.ph, %.preheader237.preheader
  %indvars.iv249 = phi i64 [ 1, %.preheader238.lr.ph ], [ %indvars.iv.next250, %.preheader237.preheader ]
  %48 = add nsw i64 %indvars.iv249, -1
  %49 = getelementptr i64, ptr %.unpack, i64 %48
  %50 = load i64, ptr %49, align 8, !tbaa !3
  %51 = add nsw i64 %indvars.iv249, -1
  %52 = getelementptr i64, ptr %.unpack201, i64 %51
  br label %55

.preheader:                                       ; preds = %.preheader237.preheader, %.preheader239
  %53 = load i32, ptr %2, align 4, !tbaa !3
  %54 = sext i32 %53 to i64
  br label %68

55:                                               ; preds = %.preheader238, %66
  %indvars.iv245 = phi i64 [ 1, %.preheader238 ], [ %indvars.iv.next246, %66 ]
  %56 = icmp slt i64 %indvars.iv245, %50
  %57 = getelementptr [100001 x i64], ptr @_QFEdp, i64 0, i64 %indvars.iv245
  %58 = load i64, ptr %57, align 8, !tbaa !3
  br i1 %56, label %66, label %59

59:                                               ; preds = %55
  %60 = sub i64 %indvars.iv245, %50
  %61 = getelementptr [100001 x i64], ptr @_QFEdp, i64 0, i64 %60
  %62 = load i64, ptr %61, align 8, !tbaa !3
  %63 = load i64, ptr %52, align 8, !tbaa !3
  %64 = add i64 %63, %62
  %65 = call i64 @llvm.smin.i64(i64 %58, i64 %64)
  br label %66

66:                                               ; preds = %55, %59
  %.sink = phi i64 [ %65, %59 ], [ %58, %55 ]
  %67 = getelementptr [100001 x i64], ptr @_QFEndp, i64 0, i64 %indvars.iv245
  store i64 %.sink, ptr %67, align 8
  %indvars.iv.next246 = add nuw nsw i64 %indvars.iv245, 1
  %exitcond248.not = icmp eq i64 %indvars.iv.next246, 100001
  br i1 %exitcond248.not, label %.preheader237.preheader, label %55

.preheader237.preheader:                          ; preds = %66
  call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 16 dereferenceable(800008) @_QFEdp, ptr noundef nonnull align 16 dereferenceable(800008) @_QFEndp, i64 800008, i1 false), !tbaa !3
  %indvars.iv.next250 = add nuw nsw i64 %indvars.iv249, 1
  %exitcond252.not = icmp eq i64 %indvars.iv249, %47
  br i1 %exitcond252.not, label %.preheader, label %.preheader238

68:                                               ; preds = %68, %.preheader
  %indvars.iv253 = phi i64 [ 1, %.preheader ], [ %indvars.iv.next254.1, %68 ]
  %.0241 = phi i64 [ 0, %.preheader ], [ %.1.1, %68 ]
  %69 = getelementptr [100001 x i64], ptr @_QFEdp, i64 0, i64 %indvars.iv253
  %70 = load i64, ptr %69, align 8, !tbaa !3
  %.not = icmp sgt i64 %70, %54
  %71 = call i64 @llvm.smax.i64(i64 %.0241, i64 %indvars.iv253)
  %.1 = select i1 %.not, i64 %.0241, i64 %71
  %indvars.iv.next254 = add nuw nsw i64 %indvars.iv253, 1
  %72 = getelementptr [100001 x i64], ptr @_QFEdp, i64 0, i64 %indvars.iv.next254
  %73 = load i64, ptr %72, align 8, !tbaa !3
  %.not.1 = icmp sgt i64 %73, %54
  %74 = call i64 @llvm.smax.i64(i64 %.1, i64 %indvars.iv.next254)
  %.1.1 = select i1 %.not.1, i64 %.1, i64 %74
  %indvars.iv.next254.1 = add nuw nsw i64 %indvars.iv253, 2
  %exitcond256.not.1 = icmp eq i64 %indvars.iv.next254.1, 100001
  br i1 %exitcond256.not.1, label %75, label %68

75:                                               ; preds = %68
  %76 = call ptr @_FortranAioBeginExternalListOutput(i32 -1, ptr nonnull @_QQcl.52c978c05bf0cabbbe05d2d519c242f0, i32 38)
  %77 = call i1 @_FortranAioOutputInteger64(ptr %76, i64 %.1.1)
  %78 = call i32 @_FortranAioEndIoStatement(ptr %76)
  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 ptr @_FortranAioBeginExternalListOutput(i32, ptr, i32) local_unnamed_addr

declare i1 @_FortranAioOutputInteger64(ptr, i64) 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 i64 @llvm.smin.i64(i64, i64) #1

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #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 nounwind willreturn memory(argmem: readwrite) }

!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, !11}
