program main
  implicit none
  integer :: h, n, i, j
  integer, allocatable :: m(:), a(:), b(:)
  read (*, *) h, n
  allocate (m(0:h), a(n), b(n))
  do i = 1, n
    read *, a(i), b(i)
  end do

  ! m(d) は d ダメージ与えるために必要な最小 MP
  m(:) = 10 ** 9
  m(0) = 0

  ! もらう DP
  do j = 1, h
    do i = 1, n
      m(j) = min(m(j), b(i) + m(max(0, j - a(i))))
    end do
  end do

  print "(i0)", m(h)
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"

@_QFEa.0 = internal unnamed_addr global ptr null
@_QFEb.0 = internal unnamed_addr global ptr null
@_QFEm.0 = internal unnamed_addr global ptr null
@_QFEm.7 = internal unnamed_addr global i64 1
@_QFEm.8 = internal unnamed_addr global i64 0
@_QQcl.90ed8688fc668e40ee68b44985af646f = linkonce constant [73 x i8] c"/home/ubuntu/Downloads/TheStack_IR/Collated/Fortran/Source_2534/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 i32, align 4
  %2 = alloca i32, align 4
  %3 = tail call ptr @_FortranAioBeginExternalListInput(i32 -1, ptr nonnull @_QQcl.90ed8688fc668e40ee68b44985af646f, i32 5)
  %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 -1)
  %9 = sext i32 %8 to i64
  %10 = add nsw i64 %9, 1
  %11 = shl nuw nsw i64 %10, 2
  %12 = call ptr @malloc(i64 %11)
  %13 = icmp eq i64 %10, 0
  %14 = zext i1 %13 to i64
  store ptr %12, ptr @_QFEm.0, align 8, !tbaa !7
  store i64 %14, ptr @_QFEm.7, align 8, !tbaa !7
  store i64 %10, ptr @_QFEm.8, align 8, !tbaa !7
  %15 = load i32, ptr %2, align 4, !tbaa !3
  %16 = call i32 @llvm.smax.i32(i32 %15, i32 0)
  %17 = zext i32 %16 to i64
  %18 = shl nuw nsw i64 %17, 2
  %19 = call ptr @malloc(i64 %18)
  store ptr %19, ptr @_QFEa.0, align 8, !tbaa !7
  %20 = call ptr @malloc(i64 %18)
  store ptr %20, ptr @_QFEb.0, align 8, !tbaa !7
  %21 = icmp sgt i32 %15, 0
  br i1 %21, label %.lr.ph.preheader, label %._crit_edge

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

.lr.ph:                                           ; preds = %.lr.ph.preheader, %.lr.ph
  %indvars.iv = phi i64 [ 1, %.lr.ph.preheader ], [ %indvars.iv.next, %.lr.ph ]
  %23 = call ptr @_FortranAioBeginExternalListInput(i32 -1, ptr nonnull @_QQcl.90ed8688fc668e40ee68b44985af646f, i32 8)
  %.unpack355 = load ptr, ptr @_QFEa.0, align 8, !tbaa !7
  %24 = add nsw i64 %indvars.iv, -1
  %25 = getelementptr i32, ptr %.unpack355, i64 %24
  %26 = call i1 @_FortranAioInputInteger(ptr %23, ptr %25, i32 4)
  %.unpack367 = load ptr, ptr @_QFEb.0, align 8, !tbaa !7
  %27 = add nsw i64 %indvars.iv, -1
  %28 = getelementptr i32, ptr %.unpack367, i64 %27
  %29 = call i1 @_FortranAioInputInteger(ptr %23, ptr %28, i32 4)
  %30 = call i32 @_FortranAioEndIoStatement(ptr %23)
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %exitcond.not = icmp eq i64 %indvars.iv, %22
  br i1 %exitcond.not, label %._crit_edge.loopexit, label %.lr.ph

._crit_edge.loopexit:                             ; preds = %.lr.ph
  %.unpack.pre = load ptr, ptr @_QFEm.0, align 8, !tbaa !7
  %.unpack290.unpack.unpack292.pre = load i64, ptr @_QFEm.8, align 8, !tbaa !7
  br label %._crit_edge

._crit_edge:                                      ; preds = %._crit_edge.loopexit, %0
  %.unpack290.unpack.unpack292 = phi i64 [ %.unpack290.unpack.unpack292.pre, %._crit_edge.loopexit ], [ %10, %0 ]
  %.unpack = phi ptr [ %.unpack.pre, %._crit_edge.loopexit ], [ %12, %0 ]
  %31 = call i64 @llvm.smax.i64(i64 %.unpack290.unpack.unpack292, i64 0)
  %.not = icmp slt i64 %.unpack290.unpack.unpack292, 1
  br i1 %.not, label %._crit_edge381, label %.lr.ph380.preheader

.lr.ph380.preheader:                              ; preds = %._crit_edge
  %min.iters.check = icmp ult i64 %31, 8
  br i1 %min.iters.check, label %.lr.ph380.preheader400, label %vector.ph

vector.ph:                                        ; preds = %.lr.ph380.preheader
  %n.vec = and i64 %31, 9223372036854775800
  br label %vector.body

vector.body:                                      ; preds = %vector.body, %vector.ph
  %index = phi i64 [ 0, %vector.ph ], [ %index.next, %vector.body ]
  %32 = getelementptr i32, ptr %.unpack, i64 %index
  store <4 x i32> <i32 1000000000, i32 1000000000, i32 1000000000, i32 1000000000>, ptr %32, align 4, !tbaa !3
  %33 = getelementptr i32, ptr %32, i64 4
  store <4 x i32> <i32 1000000000, i32 1000000000, i32 1000000000, i32 1000000000>, ptr %33, align 4, !tbaa !3
  %index.next = add nuw i64 %index, 8
  %34 = icmp eq i64 %index.next, %n.vec
  br i1 %34, label %middle.block, label %vector.body, !llvm.loop !9

middle.block:                                     ; preds = %vector.body
  %cmp.n = icmp eq i64 %31, %n.vec
  br i1 %cmp.n, label %._crit_edge381, label %.lr.ph380.preheader400

.lr.ph380.preheader400:                           ; preds = %.lr.ph380.preheader, %middle.block
  %.ph = phi i64 [ 0, %.lr.ph380.preheader ], [ %n.vec, %middle.block ]
  br label %.lr.ph380

.lr.ph380:                                        ; preds = %.lr.ph380.preheader400, %.lr.ph380
  %35 = phi i64 [ %37, %.lr.ph380 ], [ %.ph, %.lr.ph380.preheader400 ]
  %36 = getelementptr i32, ptr %.unpack, i64 %35
  store i32 1000000000, ptr %36, align 4, !tbaa !3
  %37 = add nuw nsw i64 %35, 1
  %exitcond389.not = icmp eq i64 %37, %31
  br i1 %exitcond389.not, label %._crit_edge381, label %.lr.ph380, !llvm.loop !12

._crit_edge381:                                   ; preds = %.lr.ph380, %middle.block, %._crit_edge
  %.unpack302.unpack.unpack = load i64, ptr @_QFEm.7, align 8, !tbaa !7
  %38 = sub i64 0, %.unpack302.unpack.unpack
  %39 = getelementptr i32, ptr %.unpack, i64 %38
  store i32 0, ptr %39, align 4, !tbaa !3
  %40 = load i32, ptr %1, align 4, !tbaa !3
  %41 = icmp sgt i32 %40, 0
  br i1 %41, label %.lr.ph386, label %._crit_edge387

.lr.ph386:                                        ; preds = %._crit_edge381
  %42 = zext i32 %40 to i64
  %.unpack331 = load ptr, ptr @_QFEb.0, align 8
  %.unpack343 = load ptr, ptr @_QFEa.0, align 8
  br label %43

43:                                               ; preds = %.lr.ph386, %._crit_edge384
  %indvars.iv394 = phi i64 [ 1, %.lr.ph386 ], [ %indvars.iv.next395, %._crit_edge384 ]
  %44 = load i32, ptr %2, align 4, !tbaa !3
  %45 = icmp sgt i32 %44, 0
  br i1 %45, label %.lr.ph383, label %._crit_edge384

.lr.ph383:                                        ; preds = %43
  %46 = zext i32 %44 to i64
  %47 = sub i64 %indvars.iv394, %.unpack302.unpack.unpack
  %48 = getelementptr i32, ptr %.unpack, i64 %47
  %.promoted = load i32, ptr %48, align 4, !tbaa !3
  %49 = trunc i64 %indvars.iv394 to i32
  br label %50

50:                                               ; preds = %.lr.ph383, %50
  %indvars.iv390 = phi i64 [ 1, %.lr.ph383 ], [ %indvars.iv.next391, %50 ]
  %51 = phi i32 [ %.promoted, %.lr.ph383 ], [ %65, %50 ]
  %52 = add nsw i64 %indvars.iv390, -1
  %53 = getelementptr i32, ptr %.unpack331, i64 %52
  %54 = load i32, ptr %53, align 4, !tbaa !3
  %55 = add nsw i64 %indvars.iv390, -1
  %56 = getelementptr i32, ptr %.unpack343, i64 %55
  %57 = load i32, ptr %56, align 4, !tbaa !3
  %58 = sub i32 %49, %57
  %59 = call i32 @llvm.smax.i32(i32 %58, i32 0)
  %60 = zext i32 %59 to i64
  %61 = sub i64 %60, %.unpack302.unpack.unpack
  %62 = getelementptr i32, ptr %.unpack, i64 %61
  %63 = load i32, ptr %62, align 4, !tbaa !3
  %64 = add i32 %63, %54
  %65 = call i32 @llvm.smin.i32(i32 %51, i32 %64)
  store i32 %65, ptr %48, align 4, !tbaa !3
  %indvars.iv.next391 = add nuw nsw i64 %indvars.iv390, 1
  %exitcond393.not = icmp eq i64 %indvars.iv390, %46
  br i1 %exitcond393.not, label %._crit_edge384, label %50

._crit_edge384:                                   ; preds = %50, %43
  %indvars.iv.next395 = add nuw nsw i64 %indvars.iv394, 1
  %exitcond397.not = icmp eq i64 %indvars.iv394, %42
  br i1 %exitcond397.not, label %._crit_edge387, label %43

._crit_edge387:                                   ; preds = %._crit_edge384, %._crit_edge381
  %66 = call ptr @_FortranAioBeginExternalFormattedOutput(ptr nonnull @_QQcl.28693029, i64 4, ptr null, i32 -1, ptr nonnull @_QQcl.90ed8688fc668e40ee68b44985af646f, i32 22)
  %.unpack307 = load ptr, ptr @_QFEm.0, align 8, !tbaa !7
  %.unpack314.unpack.unpack = load i64, ptr @_QFEm.7, align 8, !tbaa !7
  %67 = load i32, ptr %1, align 4, !tbaa !3
  %68 = sext i32 %67 to i64
  %69 = sub i64 %68, %.unpack314.unpack.unpack
  %70 = getelementptr i32, ptr %.unpack307, i64 %69
  %71 = load i32, ptr %70, align 4, !tbaa !3
  %72 = call i1 @_FortranAioOutputInteger32(ptr %66, i32 %71)
  %73 = call i32 @_FortranAioEndIoStatement(ptr %66)
  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 @_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.smin.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}
!9 = distinct !{!9, !10, !11}
!10 = !{!"llvm.loop.isvectorized", i32 1}
!11 = !{!"llvm.loop.unroll.runtime.disable"}
!12 = distinct !{!12, !11, !10}
