program main
  implicit none
  integer :: n, k, i, l(50), res
  read(*, *) n, k
  read(*, *) l(1:n)
  call insertion_sort(l(1:n))
  res = 0
  do i = n - k + 1, n
    res = res + l(i)
  end do
  write(*, "(i0)") res
  contains
  subroutine insertion_sort(s)
    implicit none
    integer, intent(inout) :: s(:)
    integer :: i, j, tmp
    do i = 2, size(s)  ! ループ終了時に [1, i] が昇順
      do j = i, 2, -1  ! i の位置にあった新要素が適切な位置に来るまで隣同士 swap
        if (s(j) >= s(j - 1)) exit
        tmp = s(j)
        s(j) = s(j - 1)
        s(j - 1) = tmp
      end do
    end do
  end subroutine insertion_sort
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"

@_QFEl = internal global [50 x i32] undef
@_QQcl.d7a0df2d57b4e435405c688bab14fdeb = linkonce constant [73 x i8] c"/home/ubuntu/Downloads/TheStack_IR/Collated/Fortran/Source_7113/source.f\00"
@_QQcl.28693029 = linkonce constant [4 x i8] c"(i0)"
@_QQEnvironmentDefaults = local_unnamed_addr constant ptr null

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 = alloca i32, align 4
  %4 = tail call ptr @_FortranAioBeginExternalListInput(i32 -1, ptr nonnull @_QQcl.d7a0df2d57b4e435405c688bab14fdeb, i32 4)
  %5 = call i1 @_FortranAioInputInteger(ptr %4, ptr nonnull %3, i32 4)
  %6 = call i1 @_FortranAioInputInteger(ptr %4, ptr nonnull %2, i32 4)
  %7 = call i32 @_FortranAioEndIoStatement(ptr %4)
  %8 = call ptr @_FortranAioBeginExternalListInput(i32 -1, ptr nonnull @_QQcl.d7a0df2d57b4e435405c688bab14fdeb, i32 5)
  %9 = load i32, ptr %3, align 4, !tbaa !3
  %10 = call i32 @llvm.smax.i32(i32 %9, i32 0)
  %11 = zext i32 %10 to i64
  store ptr @_QFEl, 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 %11, 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
  %12 = call i1 @_FortranAioInputDescriptor(ptr %8, ptr nonnull %1)
  %13 = call i32 @_FortranAioEndIoStatement(ptr %8)
  %14 = load i32, ptr %3, align 4, !tbaa !3
  %15 = call i32 @llvm.smax.i32(i32 %14, i32 0)
  %16 = icmp ugt i32 %15, 1
  br i1 %16, label %.preheader.i, label %_QFPinsertion_sort.exit

.preheader.i:                                     ; preds = %0, %._crit_edge.i
  %.01322.i = phi i32 [ %31, %._crit_edge.i ], [ 2, %0 ]
  %.017.i = add i32 %.01322.i, -1
  %17 = icmp sgt i32 %.017.i, 0
  br i1 %17, label %.lr.ph.i, label %._crit_edge.i

.lr.ph.i:                                         ; preds = %.preheader.i, %29
  %.019.i = phi i32 [ %.0.i, %29 ], [ %.017.i, %.preheader.i ]
  %.01218.i = phi i32 [ %23, %29 ], [ %.01322.i, %.preheader.i ]
  %18 = sext i32 %.01218.i to i64
  %19 = shl nsw i64 %18, 2
  %20 = add nsw i64 %19, -4
  %21 = getelementptr i8, ptr @_QFEl, i64 %20
  %22 = load i32, ptr %21, align 4, !tbaa !3
  %23 = add i32 %.01218.i, -1
  %24 = sext i32 %23 to i64
  %25 = shl nsw i64 %24, 2
  %26 = add nsw i64 %25, -4
  %27 = getelementptr i8, ptr @_QFEl, i64 %26
  %28 = load i32, ptr %27, align 4, !tbaa !3
  %.not.i = icmp slt i32 %22, %28
  br i1 %.not.i, label %29, label %._crit_edge.i

29:                                               ; preds = %.lr.ph.i
  store i32 %28, ptr %21, align 4, !tbaa !3
  store i32 %22, ptr %27, align 4, !tbaa !3
  %.0.i = add i32 %.019.i, -1
  %30 = icmp sgt i32 %.0.i, 0
  br i1 %30, label %.lr.ph.i, label %._crit_edge.i

._crit_edge.i:                                    ; preds = %29, %.lr.ph.i, %.preheader.i
  %31 = add nuw i32 %.01322.i, 1
  %exitcond.not.i = icmp eq i32 %.01322.i, %15
  br i1 %exitcond.not.i, label %_QFPinsertion_sort.exit, label %.preheader.i

_QFPinsertion_sort.exit:                          ; preds = %._crit_edge.i, %0
  %32 = load i32, ptr %2, align 4, !tbaa !3
  %33 = sub i32 %14, %32
  %34 = add i32 %33, 1
  %35 = sext i32 %34 to i64
  %36 = sext i32 %14 to i64
  %37 = add nsw i64 %36, 1
  %38 = sub nsw i64 %37, %35
  %39 = icmp sgt i64 %38, 0
  br i1 %39, label %.lr.ph.preheader, label %._crit_edge

.lr.ph.preheader:                                 ; preds = %_QFPinsertion_sort.exit
  %min.iters.check = icmp ult i64 %38, 8
  br i1 %min.iters.check, label %.lr.ph.preheader31, label %vector.scevcheck

vector.scevcheck:                                 ; preds = %.lr.ph.preheader
  %40 = sub nsw i64 %36, %35
  %41 = trunc i64 %40 to i32
  %42 = add i32 %34, %41
  %43 = icmp slt i32 %42, %34
  %44 = icmp ugt i64 %40, 4294967295
  %45 = or i1 %43, %44
  br i1 %45, label %.lr.ph.preheader31, label %vector.ph

vector.ph:                                        ; preds = %vector.scevcheck
  %n.vec = and i64 %38, -8
  %ind.end = and i64 %38, 7
  %.cast = trunc i64 %n.vec to i32
  %ind.end26 = add i32 %34, %.cast
  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 ], [ %50, %vector.body ]
  %vec.phi28 = phi <4 x i32> [ zeroinitializer, %vector.ph ], [ %51, %vector.body ]
  %.cast29 = trunc i64 %index to i32
  %offset.idx = add i32 %34, %.cast29
  %46 = sext i32 %offset.idx to i64
  %47 = add nsw i64 %46, -1
  %48 = getelementptr [50 x i32], ptr @_QFEl, i64 0, i64 %47
  %wide.load = load <4 x i32>, ptr %48, align 4, !tbaa !3
  %49 = getelementptr i32, ptr %48, i64 4
  %wide.load30 = load <4 x i32>, ptr %49, align 4, !tbaa !3
  %50 = add <4 x i32> %wide.load, %vec.phi
  %51 = add <4 x i32> %wide.load30, %vec.phi28
  %index.next = add nuw i64 %index, 8
  %52 = icmp eq i64 %index.next, %n.vec
  br i1 %52, label %middle.block, label %vector.body, !llvm.loop !9

middle.block:                                     ; preds = %vector.body
  %bin.rdx = add <4 x i32> %51, %50
  %53 = call i32 @llvm.vector.reduce.add.v4i32(<4 x i32> %bin.rdx)
  %cmp.n = icmp eq i64 %38, %n.vec
  br i1 %cmp.n, label %._crit_edge, label %.lr.ph.preheader31

.lr.ph.preheader31:                               ; preds = %vector.scevcheck, %.lr.ph.preheader, %middle.block
  %.ph = phi i64 [ %38, %vector.scevcheck ], [ %38, %.lr.ph.preheader ], [ %ind.end, %middle.block ]
  %.ph32 = phi i32 [ %34, %vector.scevcheck ], [ %34, %.lr.ph.preheader ], [ %ind.end26, %middle.block ]
  %.025.ph = phi i32 [ 0, %vector.scevcheck ], [ 0, %.lr.ph.preheader ], [ %53, %middle.block ]
  br label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph.preheader31, %.lr.ph
  %54 = phi i64 [ %62, %.lr.ph ], [ %.ph, %.lr.ph.preheader31 ]
  %55 = phi i32 [ %61, %.lr.ph ], [ %.ph32, %.lr.ph.preheader31 ]
  %.025 = phi i32 [ %60, %.lr.ph ], [ %.025.ph, %.lr.ph.preheader31 ]
  %56 = sext i32 %55 to i64
  %57 = add nsw i64 %56, -1
  %58 = getelementptr [50 x i32], ptr @_QFEl, i64 0, i64 %57
  %59 = load i32, ptr %58, align 4, !tbaa !3
  %60 = add i32 %59, %.025
  %61 = add i32 %55, 1
  %62 = add nsw i64 %54, -1
  %63 = icmp ugt i64 %54, 1
  br i1 %63, label %.lr.ph, label %._crit_edge, !llvm.loop !12

._crit_edge:                                      ; preds = %.lr.ph, %middle.block, %_QFPinsertion_sort.exit
  %.0.lcssa = phi i32 [ 0, %_QFPinsertion_sort.exit ], [ %53, %middle.block ], [ %60, %.lr.ph ]
  %64 = call ptr @_FortranAioBeginExternalFormattedOutput(ptr nonnull @_QQcl.28693029, i64 4, ptr null, i32 -1, ptr nonnull @_QQcl.d7a0df2d57b4e435405c688bab14fdeb, i32 11)
  %65 = call i1 @_FortranAioOutputInteger32(ptr %64, i32 %.0.lcssa)
  %66 = call i32 @_FortranAioEndIoStatement(ptr %64)
  ret void
}

; Function Attrs: nofree norecurse nosync nounwind memory(readwrite, inaccessiblemem: none)
define void @_QFPinsertion_sort(ptr nocapture readonly %0) local_unnamed_addr #0 {
  %2 = getelementptr { ptr, i64, i32, i8, i8, i8, i8, [1 x [3 x i64]] }, ptr %0, i64 0, i32 7, i64 0, i64 1
  %3 = load i64, ptr %2, align 8, !tbaa !7
  %4 = trunc i64 %3 to i32
  %.01421 = add i32 %4, -1
  %5 = icmp sgt i32 %.01421, 0
  br i1 %5, label %.preheader.lr.ph, label %._crit_edge24

.preheader.lr.ph:                                 ; preds = %1
  %6 = getelementptr { ptr, i64, i32, i8, i8, i8, i8, [1 x [3 x i64]] }, ptr %0, i64 0, i32 7, i64 0, i64 2
  br label %.preheader

.preheader:                                       ; preds = %.preheader.lr.ph, %._crit_edge
  %.01322 = phi i32 [ 2, %.preheader.lr.ph ], [ %24, %._crit_edge ]
  %.017 = add i32 %.01322, -1
  %7 = icmp sgt i32 %.017, 0
  br i1 %7, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %.preheader
  %8 = load ptr, ptr %0, align 8, !tbaa !7
  %9 = load i64, ptr %6, align 8, !tbaa !7
  br label %10

10:                                               ; preds = %.lr.ph, %22
  %.019 = phi i32 [ %.017, %.lr.ph ], [ %.0, %22 ]
  %.01218 = phi i32 [ %.01322, %.lr.ph ], [ %16, %22 ]
  %11 = sext i32 %.01218 to i64
  %12 = add nsw i64 %11, -1
  %13 = mul i64 %9, %12
  %14 = getelementptr i8, ptr %8, i64 %13
  %15 = load i32, ptr %14, align 4, !tbaa !3
  %16 = add i32 %.01218, -1
  %17 = sext i32 %16 to i64
  %18 = add nsw i64 %17, -1
  %19 = mul i64 %9, %18
  %20 = getelementptr i8, ptr %8, i64 %19
  %21 = load i32, ptr %20, align 4, !tbaa !3
  %.not = icmp slt i32 %15, %21
  br i1 %.not, label %22, label %._crit_edge

22:                                               ; preds = %10
  store i32 %21, ptr %14, align 4, !tbaa !3
  store i32 %15, ptr %20, align 4, !tbaa !3
  %.0 = add i32 %.019, -1
  %23 = icmp sgt i32 %.0, 0
  br i1 %23, label %10, label %._crit_edge

._crit_edge:                                      ; preds = %22, %10, %.preheader
  %24 = add nuw i32 %.01322, 1
  %exitcond.not = icmp eq i32 %.01322, %4
  br i1 %exitcond.not, label %._crit_edge24, label %.preheader

._crit_edge24:                                    ; preds = %._crit_edge, %1
  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 i32 @llvm.smax.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 = { nofree norecurse nosync nounwind memory(readwrite, inaccessiblemem: none) }
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, !10}
